BabylonJS/Babylon.js · error

Error while creating the CSG:

Error message

Error while creating the CSG: 

What it means

CSG2._ProcessData wraps the construction of the underlying manifold-3d Manifold object in a try/catch. If `new CSG2(new Manifold(manifoldMesh), ...)` throws (e.g. the manifold library rejects the merged mesh data), the original error is re-thrown prefixed with "Error while creating the CSG: ". It indicates the input geometry could not be converted into a valid manifold representation.

Source

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

            for (let idx = 0; idx < structure.length; idx++) {
                const component = structure[idx];

                for (let strideIndex = 0; strideIndex < component.stride; strideIndex++) {
                    vertProperties[i * numProp + offset + strideIndex] = component.data![i * component.stride + strideIndex];
                }
                offset += component.stride;
            }
        }

        // eslint-disable-next-line @typescript-eslint/naming-convention
        const manifoldMesh = new ManifoldMesh({ numProp: numProp, vertProperties, triVerts, runIndex, runOriginalID });
        manifoldMesh.merge();

        let returnValue: CSG2;
        try {
            returnValue = new CSG2(new Manifold(manifoldMesh), numProp, structure);
        } catch (e) {
            throw new Error("Error while creating the CSG: " + e.message, { cause: e });
        }

        return returnValue;
    }

    private static _Construct(data: IVertexDataLike, worldMatrix: Nullable<Matrix>, runIndex?: Uint32Array, runOriginalID?: Uint32Array, invertWinding = false) {
        // Create the MeshGL for I/O with Manifold library.
        const triVerts = new Uint32Array(data.indices!.length);

        if (invertWinding) {
            // The mesh winding is reflected relative to Babylon's default winding for the
            // current handedness (e.g. a glTF mesh in a right-handed scene). Keep the
            // original order so Manifold always receives consistently outward-wound input.
            triVerts.set(data.indices!);
        } else {
            // Revert order to match Manifold's expected winding for Babylon-default meshes
            for (let i = 0; i < data.indices!.length; i += 3) {
                triVerts[i] = data.indices![i + 2];

View on GitHub (pinned to 0592b347b8)

Solutions

  1. Inspect `e.cause` (the original error) to see the underlying manifold failure message.
  2. Sanitize the input mesh: remove degenerate triangles, NaN/Infinity positions, and unreferenced vertices before calling FromMesh/FromVertexData.
  3. Verify the manifold-3d dependency/WASM is correctly loaded and version-compatible with Babylon.js.
  4. Try simplifyying geometry (e.g. merge duplicate vertices, Babylon's mesh.cleanParentedInformation/VertexData utils) and retry.

Example fix

// before
const csg = CSG2.FromMesh(rawImportedMesh);
// after
rawImportedMesh.geometry?.removeUnreferencedVertices?.();
if (rawImportedMesh.getVerticesData(VertexBuffer.PositionKind).some((v) => !isFinite(v))) {
  throw new Error("Mesh has non-finite positions; fix before CSG");
}
try {
  const csg = CSG2.FromMesh(rawImportedMesh);
} catch (e) {
  console.error("CSG failed, cause:", e.cause);
}
Defensive patterns

Strategy: try-catch

Validate before calling

const positions = mesh.getVerticesData(VertexBuffer.PositionKind);
function isCsgInputValid(pos: Nullable<Float32Array>): boolean {
  return !!pos && pos.length >= 9 && pos.every((v) => Number.isFinite(v));
}
if (!isCsgInputValid(positions)) throw new Error("Invalid CSG input geometry");

Type guard

function hasFinitePositions(vd: VertexData): vd is VertexData & { positions: Float32Array; indices: IndicesArray } {
  return !!vd.positions && !!vd.indices && vd.positions.every((v) => Number.isFinite(v));
}

Try / catch

try {
  const result = CSG2.FromMesh(mesh);
} catch (e) {
  console.error("CSG construction failed:", (e as Error).cause ?? e);
  // fall back to non-CSG path or sanitized-geometry retry
}

Prevention

When it happens

Trigger: Calling CSG2.FromMesh/FromVertexData -> _Construct -> _ProcessData with geometry that the Manifold constructor rejects: degenerate triangles, NaN/Infinity positions, non-manifold topology the library cannot repair, or manifold-3d throwing on merge() output.

Common situations: Meshes with zero-area triangles or duplicate vertices from modeling exports; vertex data containing NaNs after bad matrix math; broken/incompatible manifold-3d WASM build; meshes imported from formats with corrupt index buffers.

Related errors


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