setVerticesData and getVerticesData

hello,
i used setVerticesData() to update mesh vertex and then i used getVerticesData() to get mesh vertex positions,but it cause a error like that:


this is my code:

 var newNormals: BABYLON.Nullable<BABYLON.FloatArray> = [];
 BABYLON.VertexData.ComputeNormals(positions, indices, newNormals);

 mesh.setVerticesData(BABYLON.VertexBuffer.PositionKind, positions);
 mesh.setVerticesData(BABYLON.VertexBuffer.NormalKind, newNormals);
 mesh.setIndices(indices, indices.length);

 let positions2 = mesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);

 console.log(positions2);

i want to know how to fix it…can you help me ? thanks very much!!

yeah,i found the reason and solved it ~~~~~~

i used positions to handle data, it should use new array to handle data like this

let positions = mesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);
    let indices = mesh.getIndices();
    if (!positions || positions.length === 0 || !indices || indices.length === 0) {
      return null;
    }
    console.log(moveVector);
  
    let vertexPosition: BABYLON.Vector3[] = [];
    for (let i = 0; i < positions.length; i = i + 3) {
        let pos = new BABYLON.Vector3(positions[i], positions[i + 1], positions[i + 2]);
        vertexPosition.push(pos);
    }

    // use new array to handle data
    let pos = [];
    for (let i = 0;i<positions.length;i++){
        pos.push(positions[i]);
    }
    
    let lineIndice = dxmInfoNode.metadata.lineIndice;
    for (let indices of lineIndice){
        let vertexPos1 = vertexPosition[indices[0]];
        let vertexPos2 = vertexPosition[indices[1]];
        for (let i = 0; i < pos.length; i += 3) {
            let v1 = BABYLON.Vector3.FromArray(pos!, i);
            if (v1.x === vertexPos1.x && v1.y === vertexPos1.y && v1.z === vertexPos1.z){
                console.log(":"+ i);
                pos[i] = vertexPos1.x + moveVector.x;
                pos[i+1] = vertexPos1.y + moveVector.y;
                pos[i+2] = vertexPos1.z + moveVector.z;
            }else if (v1.x === vertexPos2.x && v1.y === vertexPos2.y && v1.z === vertexPos2.z){
                console.log(":"+ i);
                pos[i] = vertexPos2.x + moveVector.x;
                pos[i+1] = vertexPos2.y + moveVector.y;
                pos[i+2] = vertexPos2.z + moveVector.z;
            }   
        }

    }
1 Like

Would be cool to share so that it might help others having the same issue ?