{"record":{"id":"835f5f528174957e","repo":"BabylonJS/Babylon.js","slug":"error-while-creating-the-csg","errorCode":null,"errorMessage":"Error while creating the CSG: ","messagePattern":"Error while creating the CSG: ","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/dev/core/src/Meshes/csg2.ts","lineNumber":320,"sourceCode":"            for (let idx = 0; idx < structure.length; idx++) {\r\n                const component = structure[idx];\r\n\r\n                for (let strideIndex = 0; strideIndex < component.stride; strideIndex++) {\r\n                    vertProperties[i * numProp + offset + strideIndex] = component.data![i * component.stride + strideIndex];\r\n                }\r\n                offset += component.stride;\r\n            }\r\n        }\r\n\r\n        // eslint-disable-next-line @typescript-eslint/naming-convention\r\n        const manifoldMesh = new ManifoldMesh({ numProp: numProp, vertProperties, triVerts, runIndex, runOriginalID });\r\n        manifoldMesh.merge();\r\n\r\n        let returnValue: CSG2;\r\n        try {\r\n            returnValue = new CSG2(new Manifold(manifoldMesh), numProp, structure);\r\n        } catch (e) {\r\n            throw new Error(\"Error while creating the CSG: \" + e.message, { cause: e });\r\n        }\r\n\r\n        return returnValue;\r\n    }\r\n\r\n    private static _Construct(data: IVertexDataLike, worldMatrix: Nullable<Matrix>, runIndex?: Uint32Array, runOriginalID?: Uint32Array, invertWinding = false) {\r\n        // Create the MeshGL for I/O with Manifold library.\r\n        const triVerts = new Uint32Array(data.indices!.length);\r\n\r\n        if (invertWinding) {\r\n            // The mesh winding is reflected relative to Babylon's default winding for the\r\n            // current handedness (e.g. a glTF mesh in a right-handed scene). Keep the\r\n            // original order so Manifold always receives consistently outward-wound input.\r\n            triVerts.set(data.indices!);\r\n        } else {\r\n            // Revert order to match Manifold's expected winding for Babylon-default meshes\r\n            for (let i = 0; i < data.indices!.length; i += 3) {\r\n                triVerts[i] = data.indices![i + 2];\r","sourceCodeStart":302,"sourceCodeEnd":338,"githubUrl":"https://github.com/BabylonJS/Babylon.js/blob/0592b347b8a4ee0236089ea86a749cacfdb266d8/packages/dev/core/src/Meshes/csg2.ts#L302-L338","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Inspect `e.cause` (the original error) to see the underlying manifold failure message.","Sanitize the input mesh: remove degenerate triangles, NaN/Infinity positions, and unreferenced vertices before calling FromMesh/FromVertexData.","Verify the manifold-3d dependency/WASM is correctly loaded and version-compatible with Babylon.js.","Try simplifyying geometry (e.g. merge duplicate vertices, Babylon's mesh.cleanParentedInformation/VertexData utils) and retry."],"exampleFix":"// before\nconst csg = CSG2.FromMesh(rawImportedMesh);\n// after\nrawImportedMesh.geometry?.removeUnreferencedVertices?.();\nif (rawImportedMesh.getVerticesData(VertexBuffer.PositionKind).some((v) => !isFinite(v))) {\n  throw new Error(\"Mesh has non-finite positions; fix before CSG\");\n}\ntry {\n  const csg = CSG2.FromMesh(rawImportedMesh);\n} catch (e) {\n  console.error(\"CSG failed, cause:\", e.cause);\n}","handlingStrategy":"try-catch","validationCode":"const positions = mesh.getVerticesData(VertexBuffer.PositionKind);\nfunction isCsgInputValid(pos: Nullable<Float32Array>): boolean {\n  return !!pos && pos.length >= 9 && pos.every((v) => Number.isFinite(v));\n}\nif (!isCsgInputValid(positions)) throw new Error(\"Invalid CSG input geometry\");","typeGuard":"function hasFinitePositions(vd: VertexData): vd is VertexData & { positions: Float32Array; indices: IndicesArray } {\n  return !!vd.positions && !!vd.indices && vd.positions.every((v) => Number.isFinite(v));\n}","tryCatchPattern":"try {\n  const result = CSG2.FromMesh(mesh);\n} catch (e) {\n  console.error(\"CSG construction failed:\", (e as Error).cause ?? e);\n  // fall back to non-CSG path or sanitized-geometry retry\n}","preventionTips":["Always read `e.cause` for the root manifold error.","Pre-sanitize meshes: remove degenerate/duplicate faces and non-finite floats.","Pin a compatible manifold-3d version and smoke-test CSG in CI."],"tags":["csg","geometry","manifold"],"backgroundTag":"csg-manifold-construction-failed","analyzedSha":"0592b347b8a4ee0236089ea86a749cacfdb266d8","analyzedAt":"2026-08-30T15:11:20.442Z","schemaVersion":2},"datasetVersion":"2026-08-30T18:17:15.746Z"}