Why does FastNoiseLite give me different results for get_noise_2d() and get_image()?

Godot Version

v4.1.2.stable.official [399c9dc39]

Question

Firstly, what exactly does the “normalize” parameter do in FNL’s get_image() function? I expected it to adjust the result range from [-1.0 : 1.0] to [0.0 : 1.0] based on the documentation, but when I tested it the normalized version looked darker. I don’t understand why.

Secondly, when I used FNL’s get_noise_2d() function the result looked very dark. I assumed that it was giving me results from -1.0 to 1.0, so I “normalized” it manually. The result looked better, but when I compared it to the get_image() function results, my normalized version matched the non-normalized result from get_image().

Here is my code:

func test_get_noise_2d(normalize: bool):
	var noise = FastNoiseLite.new()
	noise.noise_type = FastNoiseLite.TYPE_PERLIN
	
	var img = Image.new()
	img = Image.create(256, 256, true, Image.FORMAT_RGB8)
	
	var noise_value
	for x in range(256):
		for y in range(256):
			if normalize:
				noise_value = (noise.get_noise_2d(x, y) + 1.0) / 2.0
			else:
				noise_value = noise.get_noise_2d(x, y)
			var col = lerp(Color.BLACK, Color.WHITE, noise_value)
			img.set_pixel(x, y, col)
		
	$Sprite2D.material.set_shader_parameter("texture_param", ImageTexture.create_from_image(img))
	
	
func test_get_image(normalize: bool):
	var noise = FastNoiseLite.new()
	noise.noise_type = FastNoiseLite.TYPE_PERLIN
		
	var img = noise.get_image(256, 256, false, false, normalize)
	$Sprite2D.material.set_shader_parameter("texture_param", ImageTexture.create_from_image(img))
	

@export var inspector_noise: Texture2D
func test_inspector_noise():
	$Sprite2D.material.set_shader_parameter("texture_param", inspector_noise)

And here are the results:

So what exactly is the normalize parameter doing? And how do I get the get_noise_2d() (top left) results to match the “normalized” get_Image() (bottom right) results?

1 Like

I imagine that there’s different standards for value ranges that images can have. The inspector does a courtesy of converting to 0-1 range, while the original image is probably -1 1 range. Not sure tho.

Your “not normalized” get_noise_2d result absolutely must be that dark. Because there you are trying to lerp (linearly interpolate) the raw values – which are in [-1; 1] – between Colors.Black (which is ) and Colors.White and the resulting numbers often lerping even beyond Colors.Black, but in practice, it just produces black color too.

So before this, you need to InverseLerp() your get_noise_2d() value between [0; 1] to make it normalized. And so you did. So your bottom left image actually should be it.

Why does the “normalized” get_image() picture look darker tho – it bothers me too, and I have no clue why it does. But actually, the bottom left one is how things should be, because black = 1.0 – one of the extremes, appearances of which is not that common; and the 0.8, for example, is already 1/5 of the white color, so it’s not that dark

1 Like