Unable to update Normals with mesh.updateVerticesData

Hey there,

I am importing a glb file that contains no normal data. I want to calculate the normals in babylon and assign it to the mesh. I do that with the following lines:

let normals = [];
BABYLON.VertexData.ComputeNormals(positions, mesh.getIndices(), normals, {useRightHandedSystem: true});
mesh.updateVerticesData(BABYLON.VertexBuffer.NormalKind, normals);
mesh.markVerticesDataAsUpdatable(BABYLON.VertexBuffer.NormalKind, false);

When I now log mesh.getVerticesData(VertexBuffer.NormalKind) I get null as output.

I also tried assigning new normals to a mesh from a glb file that contains normal data. When I log the normals after I assigned them I still get the old normals that were in the glb file.
Updaing positional VerticesData works fine tho.

I am not sure if there is a problem with the normals that I want to assign or the updateVerticesData function not used correctly.

Here are some of the normals that get calculated by babylon, that I want to assign to the mesh:

[4.598461638259964e-8, -0.7071067827039836, 0.7071067796691101, -1, 0, 0, 4.598461638259965e-8, -0.7071067827039836, 0.7071067796691101, 0.9999999999999906, 0, -1.3734263967059612e-7, -2.791149088641237e-16, -1, -4.291957489706169e-9, -1, 0, 0, -6.503207674669478e-8, 0, -0.9999999999999979, -2.791148907127026e-16, -1, -4.29195748970615e-9, 0.9999999999999906, 0, -1.3734263967059612e-7, -6.503207674669478e-8, 0, -0.9999999999999979, -0.6666666533519996, 0.6666666750293564, 0.3333333432372876, 0.4082483522344218, 0.4082482991359628, 0.8164965457063936, -0.4082483358807086, 0.4082482827822415, -0.8164965620601127, 0.6666666355483176, 0.6666666572256821, -0.33333341445198894, 0.4658385042362787, 0.7045829703247973, -0.5353104948524661, 0.40627880861137067, 0.035627481733082576, -0.9130543314713995, 0.9133802539951988, 0.0760896417581252, -0.3999335920234584, 0.723294597665292, 0.6904839016060469, 0.008769641446170878, 0.053586574217419886, 0.6812205246593184, -0.7301144265432123, -0.483543019468095, 0.6553851676893931, -0.5802210184890095, -0.7811584367611728, 0.6220898721199501, -0.05287426295112553, -0.771131441152216, 0.07102471669275595, -0.6327019757241322, -0.46524072192735405, 0.04074604246381047, -0.8842459107533672, -0.053588173468265166, 0.6387104142149294, -0.7675788652885961, 0.9967044283638481, 0.07887743312790357, -0.018937608694023833, -0.9945753404905061, 0.05237297133617486, -0.08987193088842672, 0.06897289481220718, 0.9857895685164555, -0.15317201567311362, -0.06951776485470834, 0.9857133384975062, -0.15341608350399988, 0.07581344412774046, 0.07615872803738986, -0.9942093189229403, -0.06513717945492956, 0.06554271052769509, -0.995721497683634, -0.0000028633006379925027, 0.9844924626213138, -0.17542688230035486, 0, 0.9881413108455315, -0.15354722335644816, 0, 0.07974854530083796, -0.9968150126891149, 0, …]

Thank you for your help!

I think the problem is that updateVerticesData returns early without doing anything if the buffer doesn’t exist already or if it isn’t updatable, but if you use setVerticesData it will create a new buffer for you and set it regardless. Try doing it like below. :slight_smile:

let normals = [];
BABYLON.VertexData.ComputeNormals(positions, mesh.getIndices(), normals, {useRightHandedSystem: true});
mesh.setVerticesData(BABYLON.VertexBuffer.NormalKind, normals);

Awesome that solves my issue!

I still wonder why I couldnt update the normals when I used a glb file that contained normal data…

Probably the existing buffer is set as not updatable so updateVerticesData can’t update it and so setVerticesData is needed to create and set a new buffer. :slight_smile:

EDIT: and if you pass true as the third argument to setVerticesData then you should be able to later call updateVerticesData to update it (this 3rd parameter, updatable, defaults to false).

Nice, good to know. Thank you very much