# CSG2 submeshes not defined

**URL:** <https://forum.babylonjs.com/t/csg2-submeshes-not-defined/55332>\
**Category:** Questions\
**Created:** [December 12, 2024, 9:28pm UTC](https://forum.babylonjs.com/t/csg2-submeshes-not-defined/55332 "2024-12-12T21:28:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![brianzinn](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/brianzinn/32/28013_2.png) [@brianzinn](https://forum.babylonjs.com/u/brianzinn)\
**Post date:** [December 12, 2024, 9:28pm UTC](https://forum.babylonjs.com/t/csg2-submeshes-not-defined/55332/1 "2024-12-12T21:28:28Z")

</div>

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](https://playground.babylonjs.com/#NIBI64)

My calling code looks like this:

```jsx
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:

```auto
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](https://doc.babylonjs.com/setup/frameworkPackages/es6Support#faq)

I’m just updating `react-babylonjs` docs from CSG to CSG2. 😄

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [December 13, 2024, 1:01pm UTC](https://forum.babylonjs.com/t/csg2-submeshes-not-defined/55332/2 "2024-12-13T13:01:27Z")

</div>

cc @Deltakosh

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [December 13, 2024, 5:26pm UTC](https://forum.babylonjs.com/t/csg2-submeshes-not-defined/55332/3 "2024-12-13T17:26:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RaananW](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/raananw/32/28955_2.png) [@RaananW](https://forum.babylonjs.com/u/RaananW)\
**Post date:** [December 13, 2024, 6:09pm UTC](https://forum.babylonjs.com/t/csg2-submeshes-not-defined/55332/4 "2024-12-13T18:09:57Z")

</div>

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? 🙂

---

<div class="post-metadata">

**Author:** ![brianzinn](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/brianzinn/32/28013_2.png) [@brianzinn](https://forum.babylonjs.com/u/brianzinn)\
**Post date:** [December 13, 2024, 6:27pm UTC](https://forum.babylonjs.com/t/csg2-submeshes-not-defined/55332/5 "2024-12-13T18:27:15Z")

</div>

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):

```auto
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:

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/0/6/068cf109ffb0401cddb76dde3baf9ed499818e55.png)

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

```

---

<div class="post-metadata">

**Author:** ![brianzinn](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/brianzinn/32/28013_2.png) [@brianzinn](https://forum.babylonjs.com/u/brianzinn)\
**Post date:** [December 13, 2024, 6:32pm UTC](https://forum.babylonjs.com/t/csg2-submeshes-not-defined/55332/6 "2024-12-13T18:32:45Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![brianzinn](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/brianzinn/32/28013_2.png) [@brianzinn](https://forum.babylonjs.com/u/brianzinn)\
**Post date:** [December 13, 2024, 6:41pm UTC](https://forum.babylonjs.com/t/csg2-submeshes-not-defined/55332/7 "2024-12-13T18:41:31Z")

</div>

> [@RaananW](#):
>
> I would love to see this project (or a simple reproduction), if possible.

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](https://github.com/brianzinn/react-babylonjs/tree/csg2-example)

```auto
yarn install
yarn build
yarn start-docs

```

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