Rendering tangent space normal maps in Cycles

Version: Blender 2.82a

Issue:
Create a composite node setup to render a tangent space normal map.

Current setup:

enter image description here

Node setup I have right now after lots of googling(still doesn’t work correctly):

enter image description here

What it produces:

enter image description here

Anything in the scene that is on the Z axis seems to produce invalid colors for a tangent space map. While X and Y are producing good results. As you can see from the example with a cylinder on a plane. Although even the base color seems a bit off. Not sure.

Here’s an example of the the type of result I’m looking for, just for a different object:

enter image description here

Any ideas of how to go about this would be greatly appreciated, even if it’s a different approach than I am using now. Thanks!

yup, we need some new vector transformations in the compositor!

You can still do this with a second render, and with a material override.
You might want to read this thread about how the material should look.

Thank you, that thread was actually really helpful in gaining more understanding about the issue, but still couldn’t solve it. A few problems I see with using material approach, and please correct me if I’m wrong about these, is that I can’t record data such as roughness, displacement etc. I want those qualities to be reflected in the normal map. Can I still achieve this with the material approach, rather than composite?

That thread is only about Normal maps… and Normal maps don’t have information about roughness or displacement.
For roughness, you need to render another image, with the roughness value as output.
For displacement, it depends from other factors… You’d need to post an example scene, for a more precise answer.

But say I apply a displacement map to a plane mesh in a scene. Now it visually looks different, some parts of the mesh are higher, some lower. It’s not plane anymore. If I were to handpaint a normal map from looking at that - the displacement would affect how I paint the normal map. Do you understand what I mean? Not sure about the roughness though. You might be correct.

Dealing with displacements and normals is not a simple task. It depends on what kind of objects are you ‘baking’, what will be the use of the final textures, etc.
Normally, displacement is calculated first, and then the normal map is applied to the surface (in tangent space). This poses problems in different systems… the uv space gets distorted, the normals are reoriented to the displaced mesh, etc.

It’s easier to work from a specific problem than to debate all kinds of situations, and that’s why i mentioned the example scene.

Then let me provide some specifics:

I am rendering 3D models into 2D spritesheets for a game engine.
Within the engine I make use of the normals maps to create lighting effects on the otherwise 2D illustrations.

So let’s say I make a plane mesh in blender, apply a brick texture and then use displacement to make some bricks higher, some lower. I would need this information to be reflected in the normal map so light interacts differently with those bricks.

In that situation, just the render from the camera, with the displacement applied, is enough.
Remember that you’re not really baking, and whatever you see in the camera is what you’ll get in the texture. If there’s a displacement, or if the mesh is sculpted, is the same. :wink:

Yes, what I see in the camera is exactly what I want to get. But I want to render it as a normal map. The normal texture comes out fine. It’s the normal map that I can’t get.

I’m a bit blind here, to know what do you see, how is your scene setup, what materials are you using…
Can you please post an example file? I don’t need the objects, they might well be replaced with a cube, but i need to see what’s going on.

I just realized what you were saying before was correct. I can just use that normal material from the other thread you linked and apply displacement and it shows up correctly!

I’m really close to getting the result I want, the only issue I’m having is how to setup a camera facing a different direction and still get the same color scheme as in the example thread you linked at.

Here’s an example file. It’s the same as the thread you linked, but I changed the camera angle and position. How could I achieve same color scheme from that angle? Camera normal material.blend (1.4 MB)

1 Like

Cause from the angle I need, I get this color scheme:

As you can see the plane is now greenish. Could I somehow set this as the default camera perspective and have the original normals colorscheme appear?

Okay, I finally got the result I needed!

So using a material was the answer after all, thank you @Secrop. Marked your first reply as the answer!

EDIT 1: removed some misleading and incorrect information. Correct answer speaks for itself.

That is quite the expected when the angle between the normal and the view plane is huge (bigger than 45º). But the values are correct. :slight_smile:

The uncommon greenish color is just because normal maps don’t tend to have such high ‘inclinations’; as in raytracers, such normals tend to abruptly terminate the rays, because they would ‘reflect’ the real surface (the reflection would be in the backface direction, one might better call it refraction).

For composition, that’s not really the case, as you are only interested in the normal vector, with no further geometry intersection algorithm. :wink:

Now, your scaling on the other hand, seems odd. The plane is pointing somewhere in the Y direction (in the view plane), but its color tells that is pointing to the viewer… :confused:

Yes, you’re right. I thought I had it, but the values are not completely correct.

Okay so yes, again you were right, the plane being greenish when tilted was completely normal facepalm. I’m pretty new to normal maps, so I’ve been running into a lot of misunderstandings. Thank you for steering me in the right direction.

So to summarize the answer is just use the normals material from the post marked as “solution”. That’s all you need to do.