Recompute normals after displacement map been applied

Hey folks,

Just wondering how to re-compute normals after a displacement textures been applied?

This is PG:

I couldn’t figure out how to get the new vertices position after displacement distortion happened.

These are my references:

Many thanks!
Tawibox

After the displacement map has been applied, the mesh.getVerticesData("position") and mesh.getVerticesData("normal") will retrieve the updated positions and normals in local space.

In your PG, you apply a rotation to the mesh, so if you want that showNormals shows the correct normals, you should transform the positions and normals by the mesh transformation matrix. Or, easier, just make the source mesh parent of the LineMesh so that the latter inherits the mesh transformation.

2 Likes

Hey @Evgeni_Popov thank you very much for the reply.

Can you please provide a playground example of recomputing normals? I have been tried in the past few days and couldn’t get a correct result.

Many thanks
Tawibox

Sure:

1 Like

@Evgeni_Popov Thanks for the response!

In your example the normals still not look right. I printed out meshPositions, and found no matter you apply a displacement texture or not (toggle line 24) the meshPosition prints out the exact same values. So my understanding is mySphere.getVerticesData(BABYLON.VertexBuffer.PositionKind); didn’t get the updated vertex positions after displacement.

Also, the showNormals function seems using the same approach to get the vert positions. So this may explain why the normal visualizer is not showing the updated normal either.

Please see my PG:

Am I understand it correctly? Is there a way to get updated mesh info after displacement?

Many thanks!
Tawibox

I think the issue comes from the fact “applyDisplacementMap” is asynchronous - it needs to load the texture-, so you need to wait for it to complete before doing the RecomputeNormals step, which is why there’s a “callback” parameter.

3 Likes

Its a timing thing. Wait till after the matrix of the mesh has been updated after the displacement and then you should be good, or use the callback to compute the normals after the displacement has fully been applied.

^^^ hahah dude beat me to it… nice @SvenFrankson

1 Like

@SvenFrankson @Pryme8 aha!! gotcha. Thanks a lot folks! This all makes sense now. Really need to spend time learning javascript foundation better now :grinning:

Many thanks again.
Tawibox