API

Updating Vertices

How To Update Vertices

Vertex Data

The data contained in each of a mesh's vertices can be obtained from the vertex buffer. This data includes the position and normal at the vertex, as well as color and/or uv values. You can also obtain the indices for each vertex.

import { VertexBuffer } from "@babylonjs/core/Buffers/buffer";
var positions = mesh.getVerticesData(VertexBuffer.PositionKind);
var normals = mesh.getVerticesData(VertexBuffer.NormalKind);
var colors = mesh.getVerticesData(VertexBuffer.ColorKind);
var uvs = mesh.getVerticesData(VertexBuffer.UVKind);
var indices = mesh.getIndices();

Each set of data is an array of numbers as detailed in Creating Custom Meshes.

For example positions will be an array such as [-5, 2, -3, -7, -2, -3, -3, -2, -3, 5, 2, 3, 7, -2, 3, 3, -2, 3] and the
indices array such as [0, 1, 2, 3, 4, 5] giving the following table.

indexposition
0
(-5, 2, -3)
1
(-7, -2, -3)
2
(-3, -2, -3)
3
(5, 2, 3)
4
(7, -2, 3)
5
(3, -2, 3)

Updating the Data

Make sure that the mesh has been set as updatable on creation. Then it is simply a matter of altering any of the data in the positions, normals, colors, and uvs arrays to suit the project, followed by updating the vertex data.

import { VertexBuffer } from "@babylonjs/core/Buffers/buffer";
mesh.updateVerticesData(VertexBuffer.PositionKind, positions);
mesh.updateVerticesData(VertexBuffer.NormalKind, normals);
mesh.updateVerticesData(VertexBuffer.ColorKind, colors);
mesh.updateVerticesData(VertexBuffer.UVKind, uvs);

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

When a mesh is created, the normal for each face is created smoothly to match the intended shape, such as a sphere. To flatten the normals and make the mesh flat-shaded, convertToFlatShadedMesh can be used.

sphere.convertToFlatShadedMesh()
Updating Vertex Data
vertexData.applyToMesh(customMesh, true);

Adding to the Data

What happens if you want to add to vertexData after creating a mesh? For example, many of the set and parametric meshes are created without the ColorKind array, so it is not possible to use

import { VertexBuffer } from "@babylonjs/core/Buffers/buffer";
mesh.updateVerticesData(VertexBuffer.ColorKind, colors);

since mesh.getVerticesData(BABYLON.VertexBuffer.ColorKind) will return null.

In this case, after creating a color for each vertex in a color array, use

import { VertexBuffer } from "@babylonjs/core/Buffers/buffer";
mesh.setVerticesData(VertexBuffer.ColorKind, colors);

for example:

import { VertexBuffer } from "@babylonjs/core/Buffers/buffer";
var colors = mesh.getVerticesData(VertexBuffer.ColorKind);
if (!colors) {
colors = [];
var positions = mesh.getVerticesData(VertexBuffer.PositionKind);
for (let p = 0; p < positions.length / 3; p++) {
colors.push(Math.random(), Math.random(), Math.random(), 1);
}
}
mesh.setVerticesData(VertexBuffer.ColorKind, colors);

Examples

Scaling Positions: Scaling Positions Playing Around with Positions: Playing With Positions Adding Color to Vertices: Adding Colors To Vertices