engine icon indicating copy to clipboard operation
engine copied to clipboard

Add example to show and test normals and tangents

Open erikdubbelboer opened this issue 1 year ago • 6 comments

This shows that normals and tangents are not used correctly in both WebGL2 and WebGPU.

See #5735 See also https://github.com/KhronosGroup/glTF-Sample-Models/tree/master/2.0/NormalTangentTest

Front with WebGL2 is correct: Screenshot 2023-12-29 at 15 51 17

Back with WebGL2 has the wrong reflection on the right, it should be upside down. Screenshot 2023-12-29 at 15 51 29

WebGPU normals are wrong, but out of scope to fix in this pull request.

I confirm I have read the contributing guidelines and signed the Contributor License Agreement.

erikdubbelboer avatar Dec 29 '23 14:12 erikdubbelboer

I tried to find a way to fix these issues but I'm not sure where to start. Should this be fixed in the GLB parser or in somewhere else? If anyone can point me in the right direction I can try and fix the issues in this pull request as well.

erikdubbelboer avatar Dec 29 '23 14:12 erikdubbelboer

Oh that's interesting. I never realised our lighting basis is wrong on the back faces. (We were aware of WebGPU issues though). Thanks for pointing this out!

slimbuck avatar Jan 03 '24 09:01 slimbuck

@slimbuck if you point me in the right direction, I can try and fix it. Are the normals and tangents calculated at a specific place? I couldn't easily find it as I'm not familiar enough with the source yet.

erikdubbelboer avatar Jan 03 '24 12:01 erikdubbelboer

I would have expected backfaces to be handled by this bit of code here: https://github.com/playcanvas/engine/blob/main/src/scene/shader-lib/programs/lit-shader.js#L989

The actual tangents and binormals are calculated in this shader chunk: https://github.com/playcanvas/engine/blob/main/src/scene/shader-lib/chunks/lit/frag/TBNderivative.js

slimbuck avatar Jan 03 '24 12:01 slimbuck

I have fixed the calculations for both WebGL and WebGPU. I have also fixed the WebGPU version looking so weird, this was caused by setting mipmaps: true on the envAtlas.

erikdubbelboer avatar Jan 07 '24 06:01 erikdubbelboer

I'd be definitely great to have this example as a test for normals / tangents.

mvaligursky avatar Jan 22 '24 10:01 mvaligursky

I have rebased the example on main with the new examples refactor. What needs to happen to get this merged now?

erikdubbelboer avatar Feb 24 '24 04:02 erikdubbelboer

Thanks for doing that, @erikdubbelboer - we'll take another look ASAP! 😄

willeastcott avatar Feb 24 '24 07:02 willeastcott

It seems the description only shows 'before' state, but not 'after' state - could you please update it.

mvaligursky avatar Feb 26 '24 09:02 mvaligursky

Hi @erikdubbelboer,

Looking at the changes, I think it makes sense to handle twoSidedLighting in just one place - after dTBN has been calculated.

This is probably best done in a new shader chunk. So I went ahead and implemented this approach in https://github.com/playcanvas/engine/compare/main...slimbuck:erik-normals-and-tangents.

Could you take a look and let me know what you think? This approach also means we probably fix the case where TBNObjectSpace.js is used.

If you're happy with this I can push directly to your branch?

Thanks!

slimbuck avatar Mar 09 '24 21:03 slimbuck

@slimbuck great, I have merged your changes.

@mvaligursky I think this pull is ready now.

erikdubbelboer avatar Apr 02 '24 08:04 erikdubbelboer