Incorrect results with normal map node when strength is not set to 1 #109679

Closed
opened 2023-07-04 04:10:23 +02:00 by Alex-R-Heskett · 13 comments

System Information
Operating system: Windows 10
Graphics card: NVIDIA GeForce RTX 3070 Laptop GPU

Blender Version
Broken: as long as I've used blender (2.79+)
Worked: has not worked correctly in any version I've used

Short description of error
The normal map node creates unexpected and mathematically incorrect results when the strength input is set above or below one.
When the strength is set above one on a flat plane, the z axis of the normals go below 0 on steep sections, which should not occur (z should approach zero but never pass it). Left: default result, Right: expected result
image

When the strength is set below one, the steeper sections of the normal map are not correctly preserved, resulting in an unexpectedly flat result. Left: default result, Right: expected result
image

This incorrect behavior also results in extreme artifacts in recent versions:
image

Exact steps for others to reproduce the error

  1. Add a plane to your scene with a new material.
  2. In the plane's material, add an image texture node and import any normal map texture, with the node set to non-color
  3. Plug the image texture node into a normal map node and increase the normal map node's strength above 1

I have created a fixed normal map nodegroup which can be seen in the "expected" sections of each image above. The math is still fairly simple, with only a few deviations from the original implementation. I've attached a demo file containing this nodegroup. image

**System Information** Operating system: Windows 10 Graphics card: NVIDIA GeForce RTX 3070 Laptop GPU **Blender Version** Broken: as long as I've used blender (2.79+) Worked: has not worked correctly in any version I've used **Short description of error** The normal map node creates unexpected and mathematically incorrect results when the strength input is set above or below one. When the strength is set above one on a flat plane, the z axis of the normals go below 0 on steep sections, which should not occur (z should approach zero but never pass it). Left: default result, Right: expected result ![image](/attachments/4e077883-0ef2-42fd-9796-c5f283229696) When the strength is set below one, the steeper sections of the normal map are not correctly preserved, resulting in an unexpectedly flat result. Left: default result, Right: expected result ![image](/attachments/d6d3488a-48bb-4bc1-b04d-de3347a764ac) This incorrect behavior also results in extreme artifacts in recent versions: ![image](/attachments/c8d9d83d-1699-49bc-a33d-8f105b310a98) **Exact steps for others to reproduce the error** 1. Add a plane to your scene with a new material. 2. In the plane's material, add an image texture node and import any normal map texture, with the node set to non-color 3. Plug the image texture node into a normal map node and increase the normal map node's strength above 1 I have created a fixed normal map nodegroup which can be seen in the "expected" sections of each image above. The math is still fairly simple, with only a few deviations from the original implementation. I've attached a demo file containing this nodegroup. ![image](/attachments/07e7a7c6-14c7-44ad-a97e-0c3cbe1211d6)
Alex-R-Heskett added the
Type
Report
Status
Needs Triage
Priority
Normal
labels 2023-07-04 04:10:24 +02:00
Alex-R-Heskett changed title from Incorrect results with normal map node when strength is not set to one to Incorrect results with normal map node when strength is not set to 1 2023-07-04 04:17:33 +02:00
Member

While I believe the negative Z when strength over 1 isn't correct, I think thwn strength is less than 1 it should look flatter.

In your image, you probably meant to adjust the strength in such a way it applies a gamma over the tilted angle, so the "heavily tilted region" more or less remains the same tilt. I think this can be discussed, maybe a new mode can be added to the shader.

I guess what you meant is:

图片

While I believe the negative Z when strength over 1 isn't correct, I think thwn strength is less than 1 it should look flatter. In your image, you probably meant to adjust the strength in such a way it applies a gamma over the tilted angle, so the "heavily tilted region" more or less remains the same tilt. I think this can be discussed, maybe a new mode can be added to the shader. I guess what you meant is: ![图片](/attachments/9b0c19b2-d26c-4865-811b-ed3da8409d12)
Author

I'm not totally sure what you mean, your diagram doesn't really reflect what my setup is doing. In fact, what you've labled as "current" is what my nodegroup is doing. The normal map node isn't applying the strength correctly.

I'm not totally sure what you mean, your diagram doesn't really reflect what my setup is doing. In fact, what you've labled as "current" is what my nodegroup is doing. The normal map node isn't applying the strength correctly.
Author

I'm not sure what further information is needed about the issue though, I feel that it still needs triage.

I'm not sure what further information is needed about the issue though, I feel that it still needs triage.
Author

This would be a more accurate comparison:
(left = current implementation, right = my implementation)
image

This would be a more accurate comparison: (left = current implementation, right = my implementation) ![image](/attachments/8a590b3e-3b8d-4221-b8bf-5921d116f241)
8.3 KiB

i replicate this bug

i replicate this bug
Author

When comparing the normal map node with actual displaced geometry, it is clear that my implementation is the correct of the two.
Left: custom nodegroup, middle: displaced geometry, right: normal map node
image
image
image
image

When comparing the normal map node with actual displaced geometry, it is clear that my implementation is the correct of the two. Left: custom nodegroup, middle: displaced geometry, right: normal map node ![image](/attachments/f9752f87-a92e-4c09-9d63-cf27bac9ecd6) ![image](/attachments/af951fc5-bfff-499b-a79c-488730422cd4) ![image](/attachments/387f91b4-b8c1-42b8-b2ee-80b246a57908) ![image](/attachments/3792c34f-cb74-4d43-9812-3b6045bf189c)
Contributor

Heya, I got curious as to why this might be and dug around a bit!

Here are some relevant links to start with:
https://projects.blender.org/blender/blender/src/branch/main/source/blender/gpu/shaders/material/gpu_shader_material_normal_map.glsl
https://projects.blender.org/blender/blender/src/branch/main/source/blender/nodes/shader/nodes/node_shader_normal_map.cc

So it seems like the node_normal_map_mix might be the source of error, if strength > 1.0 then the mix node linearly extrapolates the normal map, i.e. multiplying it which might result in the aforementioned behaviour. The max also explains the negative values.

So it should be pretty easy to implement it directly and submitting a PR, unsure whether there's some other discussion which should be had regarding the maths.

Some links for reference:
https://computergraphics.stackexchange.com/questions/5411/correct-way-to-set-normal-strength
https://blog.selfshadow.com/publications/blending-in-detail/

BR

Heya, I got curious as to why this might be and dug around a bit! Here are some relevant links to start with: https://projects.blender.org/blender/blender/src/branch/main/source/blender/gpu/shaders/material/gpu_shader_material_normal_map.glsl https://projects.blender.org/blender/blender/src/branch/main/source/blender/nodes/shader/nodes/node_shader_normal_map.cc So it seems like the `node_normal_map_mix ` might be the source of error, if strength > 1.0 then the mix node linearly extrapolates the normal map, i.e. multiplying it which might result in the aforementioned behaviour. The max also explains the negative values. So it should be pretty easy to implement it directly and submitting a PR, unsure whether there's some other discussion which should be had regarding the maths. Some links for reference: https://computergraphics.stackexchange.com/questions/5411/correct-way-to-set-normal-strength https://blog.selfshadow.com/publications/blending-in-detail/ BR
Author

Thanks for the extra info! I assumed as much, ie the issues being caused by the strength input simply linearly interpolating instead of applying the correct math. Shouldn't require much of a change to fix the issue.

Thanks for the extra info! I assumed as much, ie the issues being caused by the strength input simply linearly interpolating instead of applying the correct math. Shouldn't require much of a change to fix the issue.
Member

So it seems like the node_normal_map_mix might be the source of error, if strength > 1.0 then the mix node linearly extrapolates the normal map, i.e. multiplying it which might result in the aforementioned behaviour. The max also explains the negative values.

Yeah this seems to be very wrong... We need to compress/scale the Z direction and then normalize I guess.

> So it seems like the node_normal_map_mix might be the source of error, if strength > 1.0 then the mix node linearly extrapolates the normal map, i.e. multiplying it which might result in the aforementioned behaviour. The max also explains the negative values. Yeah this seems to be very wrong... We need to compress/scale the Z direction and then normalize I guess.
Iliya Katushenock added
Status
Confirmed
and removed
Status
Needs Info from Developers
labels 2023-07-05 15:35:27 +02:00
Contributor

I've experimented with the shader code and come up with the following snippet

void node_normal_map_mix(float strength, vec3 newnormal, out vec3 outnormal)
{
  outnormal = normalize(vec3(g_data.N.xy + newnormal.xy * strength, mix(g_data.N.z, newnormal.z, saturate(strength))));
}

This seems to replicate the node group you created exactly, see attached images.
Will create a PR but I'm happy to discuss the math more in depth if I've missed anything

I've experimented with the shader code and come up with the following snippet ```glsl void node_normal_map_mix(float strength, vec3 newnormal, out vec3 outnormal) { outnormal = normalize(vec3(g_data.N.xy + newnormal.xy * strength, mix(g_data.N.z, newnormal.z, saturate(strength)))); } ``` This seems to replicate the node group you created exactly, see attached images. Will create a PR but I'm happy to discuss the math more in depth if I've missed anything
Author

Looks perfect! Thanks for the quick response!

Looks perfect! Thanks for the quick response!
Author

Just to verify, does this work on all geometry, not just a z facing plane?

Just to verify, does this work on all geometry, not just a z facing plane?

When replicating the nodegroup as seen above, SOME of the UV seams become visible, even when leaving the strength at 1. This is not an issue with the standard NormalMap Node.

The seam comes from just the z component of the output. The seams ONLY appeared on one side of my model, so I assume it is related to how the UV islands are unwrapped/whether or not they've been "flipped"

This seems to be an issue, only with the group setup, as it lacks tangent.w, which is needed to know the left/right-handedness of the tangent.

image
image

image
image

When replicating the nodegroup as seen above, SOME of the UV seams become visible, even when leaving the strength at 1. This is not an issue with the standard NormalMap Node. The seam comes from just the z component of the output. The seams ONLY appeared on one side of my model, so I assume it is related to how the UV islands are unwrapped/whether or not they've been "flipped" This seems to be an issue, only with the group setup, as it lacks tangent.w, which is needed to know the left/right-handedness of the tangent. ![image](/attachments/82a40e9d-90a1-493a-9909-b6d376ec990a) ![image](/attachments/6a7762f7-04f1-4f01-8d1d-f8f4a0b15350) ![image](/attachments/a827dd4c-3e01-454a-9836-ed3c8ca8c1e1) ![image](/attachments/4b3648ee-6b7c-4e1e-b6d0-86a0211fc826)
Blender Bot added
Status
Resolved
and removed
Status
Confirmed
labels 2023-07-17 20:06:53 +02:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
5 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#109679
No description provided.