Creating a noisey-looking sprite at runtime

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Ertain
:warning: Old Version Published before Godot 3 was released.

I’m trying to generate a sprite filled with random data created from a noise function. I have a script that I found here which can make some noise values (such as simplex noise), and so I’ve been trying to turn that into a sprite of some kind. What I do is preload that other script, create an Image object, assign that to an ImageTexture object, set that to a texture, create the data, set the color of the pixels in the Image object, and finally set that data to the texture. It’s suppose to take the array data and map the colors to the sprite based on the values of each cell in the array. So if some cell (which corresponds to a pixel in the Image object) has a value between 0 and 0.3, it’s suppose to make the pixel a bluish color.

Unfortunately, all this does is create a window that has different colored tiles in it.

The perpetrator

I can’t figure out where the problem is. Is it in the Image object? Or the ImageTexture object? Does it lie somewhere else? I don’t know.

My scene is set up like this:
some_tilemap (a tilemap node) → some_sprite (obviously, a sprite node)

Btw, here’s the code that I’m using:

extends TileMap

onready var preScript = preload("res://scripts/softnoise.gd")
onready var softNoise

func _ready():
    randomize()
    # Create a vector to set the size of the image.
    var imageSize = Vector2(256,256)
    # Create an image
    var noisey_image = Image(imageSize.x, imageSize.y, false, Image.FORMAT_RGBA)
    # Create an ImageTexture
    var le_texture = ImageTexture.new()
    le_texture.create(imageSize.x, imageSize.y, Image.FORMAT_RGBA, 0)
    le_texture.set_data(noisey_image)
    # Get the node of the sprite
    var le_sprite = get_node("some_sprite")
    # Set the texure in the sprite
    le_sprite.set_texture(le_texture)
    # Make some random number
    var num = randi()%1000
    # Hold the noise data here.
    var noise_data = {}
    # Some other variables
    var b = 0
	    var thing = 0
    var color = {}
    # Random
    softNoise = preScript.SoftNoise.new(num)
    for i in range(0, imageSize.x):
	    for j in range(0, imageSize.y):
		    thing = softNoise.openSimplex2D(i,j)
		    # For each pixel, if the resulting noise lies within a certain
		    # range, assign a certain color to that pixel.
		    if -0.5 <= thing <= 0:
			    # Brown
			    color = Color(250, 58, 20)
		    elif 0 <= thing <= 0.3:
			    # Blue
			    color = Color(37, 134, 187)
		    elif 0.3 <= thing <= 0.6:
			    # Green
			    color = Color(54, 194, 85)
		    elif 0.6 <= thing <= 1.0:
			    # Pale color
			    color = Color(227, 207, 168)
		    noisey_image.put_pixel(i,j, color)
    le_texture.set_data(noisey_image)
:bust_in_silhouette: Reply From: mollusca

Those comparisons probably aren’t doing what you’re wanting them to do. Try changing them to this form:

if -0.5 <= thing and thing <= 0:

Thank you for the reply. I’ll try that.
Tries that
Well, I’ve made some progress. Now the squares look a different color.
Looks a little different now
I guess this one will take a while to crack.

Ertain | 2017-09-11 16:54

I tried the code myself, on a Sprite. The colors are a bit weird but this should work:

extends Sprite

var softnoise = preload("softnoise.gd").SoftNoise.new()

func _ready():
    var img = Image(256, 256, false, Image.FORMAT_RGBA)
    var texture = ImageTexture.new()
    texture.create(img.get_width(), img.get_height(), Image.FORMAT_RGBA, 0)
    for x in range(img.get_width()):
        for y in range(img.get_height()):
            var noise_val = softnoise.openSimplex2D(x / 32.0, y / 32.0)
            var col = Color(1.0, 1.0, 1.0)
            if -0.5 <= noise_val and noise_val <= 0.0:
                col = Color(250 / 256.0, 58 / 256.0, 20 / 256.0)
            elif 0.0 <= noise_val and noise_val <= 0.3:
                col = Color(37 / 256.0, 134 / 256.0, 187 / 256.0)
            elif 0.3 <= noise_val and noise_val <= 0.6:
                col = Color(54 / 256.0, 194 / 256.0, 85 / 256.0)
            elif 0.6 <= noise_val and noise_val <= 1.0:
                col = Color(227 / 256.0, 207 / 256.0, 168 / 256.0)
            img.put_pixel(x, y, col)
    texture.set_data(img)
    set_texture(texture)

I changed the colors to float values and added a scaling factor to the noise input coordinates.

mollusca | 2017-09-11 18:49

Okay, I’m getting something that looks like what I want. Thank you for your help, mollusca.

Ertain | 2017-09-12 04:10