Can I generate a multicolored noise texture?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By kitfox

At the moment, if I want to generate an OpenSimplexNoise texture, I’m restricted to either black and while noise or a normal texture. I want to send a shader I’ve written two different noise textures representing a displacement on the x and y axes. The normal noise textures will send data I do not want and sending two independent black and white textures seem to me to waste channels. Is there a way to create a single multicolored noise texture with noise in all four channels?

:bust_in_silhouette: Reply From: tuon

This might take some more work, but I was able to map colors from noise images into the rgba components of a color and create another image with it. Assume you have a scene with a Control node for the root and a TextureRect child. I also added a Button called RegenerateButton in my test to see it change. This also seems kinda slow, but as long as your not doing this all the time it might do.

extends Control

onready var texture_rect = $TextureRect

func _color_to_float(c: Color) -> float:
	var t = c.to_rgba32()
	return float(c.to_rgba32()) / 4294967295

func _get_noise_image():
	var n = OpenSimplexNoise.new()
	n.seed = randi()
	return n.get_image(500, 500)

func _ready():
	_generate()
	
func _generate():
	randomize()
	
	var tr1_image = _get_noise_image()
	var tr2_image = _get_noise_image()
	var tr3_image = _get_noise_image()
	var tr4_image = _get_noise_image()
	
	var image:Image = Image.new()
	image.create(500, 500, false, Image.FORMAT_RG8)
	image.lock()
	tr1_image.lock()
	tr2_image.lock()
	tr3_image.lock()
	tr4_image.lock()
	for y in range(image.get_height()):
		for x in range(image.get_width()):
			var r =  _color_to_float(tr1_image.get_pixel(x, y))
			var g =  _color_to_float(tr2_image.get_pixel(x, y))
			var b =  _color_to_float(tr3_image.get_pixel(x, y))
			var a =  _color_to_float(tr4_image.get_pixel(x, y))
			var c = Color(r,g,b,a)
			image.set_pixel(x, y, c)
	image.unlock()
	tr1_image.unlock()
	tr2_image.unlock()
	tr3_image.unlock()
	tr4_image.unlock()
	
	var image_texture : ImageTexture = ImageTexture.new()
	image_texture.create_from_image(image)
	texture_rect.texture = image_texture


func _on_RegenerateButton_pressed():
	_generate()