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 (115 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():
    _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()
by (212 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 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 webmaster@godotengine.org with your username.