0 votes

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?

in Engine by (147 points)

1 Answer

0 votes

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():

func _generate():

    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)
    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)

    var image_texture : ImageTexture = ImageTexture.new()
    texture_rect.texture = image_texture

func _on_RegenerateButton_pressed():
by (244 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.