Godot Version
4.4.1-stable
Question
How do I use a NoiseTexture2D with 'as_normal_map' set, to set my normals in the fragment shader?
I’m currently using a noise texture 2d as a height map for some terrain I’m generating. It looks great.
I wanted to tackle normals for my terrain - I found this video online https://www.youtube.com/watch?v=-dMbacvrDrs that outlined the approach of duplicating your noise texture - with the duplicated one having the use as normal option enabled. I did this and followed through the video. I’m getting some convincing results, but it doesn’t quite feel right, and I have loads of questions:
Here’s the relevant code bits:
uniform sampler2D terrain_noise_normal_map_texture;
vec3 unpack_normalmap(vec4 rgba) {
vec3 n = rgba.xyz * 2.0 - vec3(1.0);
n.y *= -1.0;
return n;
}
void fragment() {
vec3 terrain_normal = normalize(unpack_normalmap(texture(terrain_noise_normal_map_texture, UV)));
NORMAL = vec4(terrain_normal, 0.0).xyz; // This used to get multiplied by VIEW_MATRIX
}
Questions:
- Why the need to unpack the normal map? why isn’t it in a godot native format?
- How can I figure out the angle of the normal along the y-axis (positive-y being “height” for me) ? I had thought sampling the normal’s .y value would give me a 0->1.0 range of how far up the axis the normal is pointing, but this doesn’t look right
- The original video I linked above contained a multiplication like so:
NORMAL = (VIEW_MATRIX * vec4(terrain_normal, 0.0)).xyz;
, however this seemed to cause the normals to change as my camera tilted up/down. Removing that multiplication removed this effect