BabylonJS/Babylon.js · error

The vertexData must at least have positions and indices

Error message

The vertexData must at least have positions and indices

What it means

CSG2.FromVertexData requires the supplied VertexData to contain both `positions` and `indices`; a CSG solid cannot be built from bare normals/uvs or non-indexed primitives in this API path. The guard throws before any geometry processing when either array is missing or empty.

Source

Thrown at packages/dev/core/src/Meshes/csg2.ts:413

        if (sourceColors) {
            numProp += 4;
            structure.push({ stride: 4, kind: VertexBuffer.ColorKind, data: sourceColors });
        }

        return this._ProcessData(data.positions!.length / 3, triVerts, structure, numProp, runIndex, runOriginalID);
    }

    /**
     * Create a new Constructive Solid Geometry from a vertexData
     * @param vertexData defines the vertexData to use to create the CSG
     * @returns a new CSG2 class
     */
    public static FromVertexData(vertexData: VertexData): CSG2 {
        const sourceVertices = vertexData.positions;
        const sourceIndices = vertexData.indices;

        if (!sourceVertices || !sourceIndices) {
            throw new Error("The vertexData must at least have positions and indices");
        }

        return this._Construct(vertexData, null);
    }

    /**
     * Create a new Constructive Solid Geometry from a mesh
     * @param mesh defines the mesh to use to create the CSG
     * @param ignoreWorldMatrix defines if the world matrix should be ignored
     * @returns a new CSG2 class
     */
    public static FromMesh(mesh: Mesh, ignoreWorldMatrix = false): CSG2 {
        const sourceVertices = mesh.getVerticesData(VertexBuffer.PositionKind);
        const sourceIndices = mesh.getIndices();
        const worldMatrix = mesh.computeWorldMatrix(true);

        if (!sourceVertices || !sourceIndices) {
            throw new Error("The mesh must at least have positions and indices");

View on GitHub (pinned to 0592b347b8)

Solutions

  1. Ensure both `vertexData.positions` (Float32Array of xyz triples) and `vertexData.indices` (Uint32Array/number[]) are populated before the call.
  2. If starting from a mesh, prefer `CSG2.FromMesh(mesh)` which fetches positions/indices for you.
  3. For non-indexed geometry, generate a trivial index array [0,1,2,3,4,5,...] matching the position count.

Example fix

// before
const vd = new VertexData();
vd.positions = positions; // indices never set
const csg = CSG2.FromVertexData(vd);
// after
const vd = new VertexData();
vd.positions = positions;
vd.indices = positions.map((_, i) => i); // or real triangle indices
const csg = CSG2.FromVertexData(vd);
Defensive patterns

Strategy: validation

Validate before calling

function canBuildCsgFromVertexData(vd: VertexData): boolean {
  return !!vd.positions && vd.positions.length > 0 && !!vd.indices && vd.indices.length > 0;
}
if (!canBuildCsgFromVertexData(vertexData)) throw new Error("positions and indices required for CSG");

Type guard

function hasPositionsAndIndices(vd: VertexData): vd is VertexData & { positions: ArrayLike<number>; indices: ArrayLike<number> } {
  return vd.positions != null && vd.indices != null;
}

Prevention

When it happens

Trigger: Calling `CSG2.FromVertexData(vertexData)` where `vertexData.positions` or `vertexData.indices` is undefined/null — e.g. a VertexData built with only normals/uvs, or positions set but applyToMesh/indices never generated.

Common situations: Hand-constructing VertexData and forgetting setIndex/indices; converting a mesh whose geometry was created non-indexed; passing VertexData extracted from a mesh that had its geometry disposed.

Related errors


AI-assisted analysis of BabylonJS/Babylon.js@0592b347b8 (2026-08-30). Data as JSON: /api/errors/34ca2411ebe25094. Report an issue: GitHub.