UVs not updating on Mesh despite all vertexData kinds being set to updatable?

UVs not updating on Mesh despite all vertexData kinds being set to updatable?
https://playground.babylonjs.com/#ZNGJG1

var createScene = function () {
var scene = new BABYLON.Scene(engine);
var camera = new BABYLON.ArcRotateCamera("Camera", 3 * Math.PI / 2, Math.PI / 4, 8, BABYLON.Vector3.Zero(), scene);
camera.attachControl(canvas, true);
var light = new BABYLON.HemisphericLight("hemi", new BABYLON.Vector3(1, 1, 0), scene);
light.intesity = 0.5;

var positions = [
    -4, 0, 4,
    4, 0, 4,
    4, 0, -4,
    -4, 0, -4,
    -3, 0, 1,
    -1, 0, 1,
    -1, 0, -1,
    -3, 0, -1,
    -2.5, 1, 0.5,
    -1.5, 1, 0.5,
    -1.5, 1, -0.5,
    -2.5, 1, -0.5,
    1, 0, 1,
    3, 0, 1,
    3, 0, -1,
    1, 0, -1,
    2, 1.5, 0,
    -3, 0, 4,
    -1, 0, 4,
    1, 0, 4,
    3, 0, 4,
    3, 0, -4,
    1, 0, -4,
    -1, 0, -4,
    -3, 0, -4
];

var indices = [
    9, 8, 10,
    8, 11, 10,
    8, 4, 11,
    11, 4, 7,
    8, 5, 4,
    8, 9, 5,
    9, 10, 5,
    10, 6, 5,
    10, 7, 6,
    10, 11, 7,
    16, 12, 15,
    16, 13, 12,
    16, 14, 13,
    16, 15, 14,
    4, 18, 17,
    4, 5, 18,
    5, 19, 18,
    5, 12, 19,
    5, 6, 15,
    5, 15, 12,
    12, 20, 19, 
    12, 13, 20,
    13, 1, 20,
    13, 2, 1,
    13, 14, 2,
    14, 21, 2,
    14, 22, 21,
    14, 15, 22,
    15, 23, 22,
    15, 6, 23, 
    6, 24, 23,
    7, 24, 6,
    7, 3, 24, 
    7, 0, 3,
    7, 4, 0,
    4, 17, 0
];

//take uv value relative to bottom left corner of roof (-4, -4) noting length and width of roof is 8
// base uv value on the x, z coordinates only
var uvs = [];
for(var p = 0; p < positions.length / 3; p++) {
    uvs.push((positions[3 * p] - (-4)) / 8, (positions[3 * p + 2] - (-4)) / 8);
}

var customMesh = new BABYLON.Mesh("custom", scene);
customMesh.markVerticesDataAsUpdatable(BABYLON.VertexBuffer.PositionKind, true);
customMesh.markVerticesDataAsUpdatable(BABYLON.VertexBuffer.UVKind, true);
customMesh.markVerticesDataAsUpdatable(BABYLON.VertexBuffer.UV2Kind, true);
customMesh.markVerticesDataAsUpdatable(BABYLON.VertexBuffer.UV3Kind, true);
customMesh.markVerticesDataAsUpdatable(BABYLON.VertexBuffer.UV4Kind, true);
customMesh.markVerticesDataAsUpdatable(BABYLON.VertexBuffer.UV5Kind, true);
customMesh.markVerticesDataAsUpdatable(BABYLON.VertexBuffer.UV6Kind, true);
customMesh.markVerticesDataAsUpdatable(BABYLON.VertexBuffer.ColorKind, true);
customMesh.markVerticesDataAsUpdatable(BABYLON.VertexBuffer.MatricesIndicesKind, true);
customMesh.markVerticesDataAsUpdatable(BABYLON.VertexBuffer.MatricesIndicesExtraKind, true);
customMesh.markVerticesDataAsUpdatable(BABYLON.VertexBuffer.MatricesWeightsKind, true);
customMesh.markVerticesDataAsUpdatable(BABYLON.VertexBuffer.MatricesWeightsExtraKind, true);

var normals = [];  //Empty array to contain calculated values or normals added
BABYLON.VertexData.ComputeNormals(positions, indices, normals);  //Calculations of normals added

var vertexData = new BABYLON.VertexData();
vertexData.positions = positions;
vertexData.indices = indices;
vertexData.normals = normals; //Assignment of normal to vertexData added
vertexData.uvs = uvs;
vertexData.applyToMesh(customMesh);

var mat = new BABYLON.StandardMaterial("", scene);
mat.diffuseTexture = new BABYLON.Texture("textures/co.png")
customMesh.material = mat;

setTimeout(()=>{
    for(var p = 0; p < positions.length / 3; p++) {
        uvs.push(Math.abs(Math.random()), Math.abs(Math.random()));
    }
    //vertexData.uvs = uvs;
    //vertexData.applyToMesh(customMesh);
    customMesh.updateVerticesData(BABYLON.VertexBuffer.UVKind, uvs);
    customMesh.setVerticesData(BABYLON.VertexBuffer.UVKind, uvs);
}, 3000);

return scene;

}

It seems to me you are not resetting the uvs array so you are simply adding at the end of it instead of restarting at the beginning

1 Like

Also once you change the data did you see this in the docs .

Note: When creating your own custom mesh to make it updatable you need to add a second parameter with value true when applying the mesh to the vertex data.

1 Like

Fix: UVs Updating Not Working | Babylon.js Playground

3 Likes