Can anyone help?
Using vector colors as normal map for 3D object
xyz Here's how it looks in Godot 4.0 with vertex color output to albedo and no light.
Here's how it look inside Blender with the custom normals from another model painted on.
I believe my issue is with correctly mapping the colors to normal in Godot 4.0. Below is a gif of two models in rendered viewport. The model on the left has custom normals and the model on the right has painted vertex colors. As you can see both react identical to rotation of light source.
Here's how the shader nodes look like in Blender. I believe most of the magic takes place in the normal map node.
I had a look at the source code for the node and tried to replicate it in godot but my limited knowledge with shaders isn't helping me. Here's the source code for the normal map or so I think. https://github.com/blender/blender/blob/main/source/blender/gpu/shaders/material/gpu_shader_material_normal_map.glsl
xyz That unfortunately didn't work out.
Here's the code I used for swapping normal components.
shader_type spatial;
void vertex () {
vec3 normal = COLOR.rgb;
float y = normal.y;
normal.y = normal.z;
normal.z = y;
NORMAL = normal;
}
Here's how it looks in Godot.
As you can see even without any additional changes, the normal map is off. The model in the right is one with custom normals only and how its is supposed to react to directional lighting. The model on the left is the one with vertex colors.
NepNep978 Now it looks like you need to flip the z component too.
Btw you can swizzle vector components in the shading language:
NORMAL = COLOR.xzy;
NORMAL.z = -NORMAL.z;
If this doesn't work exactly, try to play around with different component signs and/or component swapping, and see how it affects the lighting.
xyz Okay, Thank you for your help. This didn't exactly work out but I'll see if I can mess with the values. BTW, you mentioned which space they were baked in earlier and I checkd in Blender they are in the Model/Object space.
xyz Would it be possible for us to connect over discord or any private messaging service of your choosing? I could share the .blend files over there. Its just that I have been researching for the past three weeks starting from modelling and custom normals until I hit a roadblock where applying shapekeys in Blender would break the normals. Then I figured out after some looking around that baking the edited custom normals to the model would allow me to change shapekeys without breaking normals and it did work in Blender but I can't get it working in Godot. I looked at the source code for the normal map node in Blender linked above and tried to replicate in Godot but it didn't work probably because the code wasn't right.
xyz I couldn't bake the normals to an image texture but that was probably because the model I was trying to bake it from only had the vertex normals edited. I've attached the godot project but removed the shadercache to reduce the size. Also attached is the .blend file.
- Edited
- Best Answerset by NepNep978
NepNep978 Your baked normals are ok. They appear to be the same in Godot and in Blender. The problem (so to speak) seems to be with the first model (named Cube). It looks like there's a difference in up axis convention between baked and actual normals. So if you only plan to use baked normals, you're fine. Otherwise try to disable y+up option in Blender's glTF export options when exporting the non-baked model. You'll have to rotate the model in Godot, but the normals should be the same as in baked model.
Btw, normal textures could not be baked because there are no proper UVs on the model.
- Edited
The forum does support private conversations. Just navigate to the users profile and you should be able to start a private discussion with them there. Drop-down menu towards top-right(for me as an admin anyways). May just be a button with an icon of a sort of folding 3 panels for regular users.
- Edited
Hm, what I wrote in previous post should ensure the normals on both models have matching orientation, however their orientation will be rotated 90 degrees and both will look off in respect to actual light direction in Godot.
So best way to ensure the correct orientation on both models without re-exporting anything would be to rotate the color-encoded normals by 90 degrees around x axis in the vertex shader:
const float a = PI/2.0;
const mat3 mx = mat3(
vec3(1.0, 0.0, 0.0),
vec3(0.0, cos(a), -sin(a)),
vec3(0.0, sin(a), cos(a))
);
NORMAL = mx * (COLOR.xyz * 2.0 - 1.0);
Now both models will have correct normal orientation in respect to light direction in Godot:
However there still seems to be some slight discrepancy. This was probably caused by your transfer/bake process in Blender. So double-check that.
xyz This actually worked! I cannot believe all of my woes were for one export setting in Blender.
Regarding the UV Map, I have a seperate blend file with the UVs created but baking the normals would just create a purple image with no deformations.
Anyways, thank you so much for your help!
xyz Okay, I'll look into it.
You've given me a lot of stuff to look into and once again thank you. You've helped me immensely.
Just one last thing. Do you know where I can do some reading up on the things you mentioned such as vectors and math in 3D space. I've been looking into the spatial shaders documentation for Godot for the last couple of days but that didn't help me figure out a solution on my own.
NepNep978 The key areas of math used in 3d graphics are trigonometry and linear algebra. Linear algebra deals with vectors, matrices, n-dimensional linear spaces etc. It's a very broad field, but only its 2d/3d subset is typically used in practical graphics/game programming.
You can surely find a lot of resources by googling "linear algebra for 3d graphics"
3Blue1Brown has a good series on linear algebra. It may be a bit involved, depending on your affinities and current level of knowledge.
Also there's a decent introductory series at Khan academy