CSG2 submeshes not defined

This looks like maybe a tree shaking related. I was wondering if anybody has come across this, before I dig through the babylon code. Hopefully it’s just an import and then this will serve as something searchable to help the next person.

Here is a playground that does work:
CSG2 example | Babylon.js Playground

My calling code looks like this:

import { CSG2, InitializeCSG2Async } from '@babylonjs/core/Meshes/csg2'

// I have already awaited InitializeCSG2Async here:
const csg = CSG2.FromMesh(node)
const sphere = MeshBuilder.CreateSphere(
  'circle',
  {
    diameter: 3,
  },
  scene
)
sphere.position.x -= 2
const res = csg.subtract(CSG2.FromMesh(sphere))

// in CSG used to pass in keepSubMeshes as true
let mesh = res.toMesh('base', scene, {
  // materialToUse: csgMeshMaterial
})

This is my stack trace:

Uncaught TypeError: this.subMeshes is undefined
    bakeTransformIntoVertices webpack-internal:///../../node_modules/@babylonjs/core/Meshes/mesh.js:2353
    bakeCurrentTransformIntoVertices webpack-internal:///../../node_modules/@babylonjs/core/Meshes/mesh.js:2401
    toMesh webpack-internal:///../../node_modules/@babylonjs/core/Meshes/csg2.js:157

Hopefully I’m just missing a side-effect import. I usually check here:
https://doc.babylonjs.com/setup/frameworkPackages/es6Support#faq

I’m just updating react-babylonjs docs from CSG to CSG2. :smile:

cc @Deltakosh

Question more for our build guru @RaananW. Would you see why CSG2 would not import all the needed mesh code?

I would love to see this project (or a simple reproduction), if possible.

MeshBuilder is loading everything you need to build a mesh, there should not be any class missing. I don’t see you import it, but I can only assume the MeshBuilder itself is available and working. I can’t think of any side-effects that enable subMeshes. If the sphere is rendering, it has to be there. But there must be something missing, right? :slight_smile:

I tried actually to make a repro before in code sandbox. I didn’t see any options like for earcut to inject a dependency. I get this on codesandbox (not locally though):

TypeError
Failed to resolve module specifier 'module'
    at https://unpkg.com/manifold-3d
3.0.0/manifold.js:9:458

If it helps - this is the stacktrace:

bakeTransformIntoVertices(transform) {
        // Position
        if (!this.isVerticesDataPresent(VertexBuffer.PositionKind)) {
            return this;
        }
        const submeshes = this.subMeshes.splice(0);
        this._resetPointsArrayCache();
        let data = this.getVerticesData(VertexBuffer.PositionKind);
       ....
}

I don’t know why I specified “keepSubmeshes” in CSG - there’s no option in CSG2. I thought it would be something like that. If submeshes really are optional why not:

const submeshes = this.subMeshes?.splice(0) ?? [];

The project is here, but it’s a big ask to expect anybody to dig through. I was hoping for a quick answer like. “Hey Brian - RTFM just add this import, duh!!”. I don’t mind digging through the code another time.

brianzinn/react-babylonjs at csg2-example

yarn install
yarn build
yarn start-docs

Open docs website. Navigate to Examples → Basic → Extrude (+CSG2)