question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug in default shader with normal map

See original GitHub issue
Description of the problem

There are rending issues due to the default shader (checked with MeshStandardMaterial and MeshPhongMaterial) when using the normal map if all the texture coordinates for the face are exactly the same.

I had exactly the same issue with other shaders and this was related to the dFdx(uv) and dFdy(uv) shader functions returning 0 and leading to a division by 0.

Live example:

Three.js version
  • Dev
  • r108
  • r104
Browser
  • All of them
  • Chrome
  • Firefox
  • Internet Explorer
OS
  • All of them
  • Windows
  • macOS
  • Linux
  • Android
  • iOS
Hardware Requirements (graphics card, VR Device, …)
OpenGL vendor: NVIDIA Corporation (0x10de)
OpenGL renderer: GeForce RTX 2060/PCIe/SSE2 (0x1f08)
OpenGL version: 4.6.0 NVIDIA 430.40

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:32 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
WestLangleycommented, Sep 24, 2019

This is what I believe is happening…

Even if the uv’s at each vertex are identical, interpolation of uv’s across the face of the primitive can result in different values due to numerical roundoff.

Consequently,

vec2 st0 = dFdx( vUv.st );

may not be the zero vector we would hope it would be.

The next line

float scale = sign( st1.t * st0.s - st0.t * st1.s );

will return - 1, 0, or 1.

And the following line is normalizing something that is very close to zero

vec3 S = normalize( ( q0 * st1.t - q1 * st0.t ) * scale );

which just amplifies the numerical errors since it returns a unit-length vector.

2reactions
WestLangleycommented, Sep 24, 2019

Just wanna say that additional if statements in shader code can degrade performance.

@Mugen87 The OP is only trying to help locate the issue at this point.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Bug] Specular and Normal maps not working with shaders
Have a shader enabled (I used Sildurs enhanced default V1.10); Ensure normal maps and specular maps are on in the shaders menu ...
Read more >
Trying to set a normal map on a simple shader - Unity Forum
Hi. I am unable to create a shader with a normal map in the Shader Graph (with HDRP). I had been trying it...
Read more >
Normal map inverted UVWs shader bug
Your normal map is messed up. Try this normal map, it works fine. ... There are three things that last forever: Abort, Retry,...
Read more >
Normal map (Bump mapping) - Unity - Manual
Normal maps are a type of Bump Map. They are a special kind of texture that allow you to add surface detail such...
Read more >
Strange behaviours in Node Material normals - Bugs
Normal default color (127, 127, 255) does not align with empty normal. This seems to be a general normal map problem that also...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found