Baking imported meshes vertices to use with CSG

Hello everybody.

First off, thanks for this incredible technology and helpful community.
I’ve always been helped by lurking the forum posts and BJS tutorials, but now I’m being stuck with several problems.

But let’s try to keep it simple : I want to highlight zones of imported (with SceneLoader.Append) meshes that intersects. Like in this PG : https://playground.babylonjs.com/#15S2V0#2
PG will come soon, but let me explain first :

First solution https://www.babylonjs-playground.com/#Q9K825#11

Let’s

  • select at least 2 meshes
  • convert them to a CSG
  • get the intersectCSG
  • create a new mesh from this intersection
        const firstMeshCsg = CSG.FromMesh(firstMesh);
        const otherMeshCsg = CSG.FromMesh(otherMesh);

        const intersectionCst = firstMeshCsg
          .intersect(otherMeshCsg)
          .inverse();

        const intersectionMesh = intersectionCst.toMesh('qsfqsf', mat2, this.scene)
        intersectionMesh.renderingGroupId = 1;
        intersectionMesh.isPickable = false ;

With this code, everything works fine,
BUT !
my intersectionMesh position (and sometimes rotation) is f****d.

:thinking:

Second solution https://www.babylonjs-playground.com/#Q9K825#12

Okay, lets screw firstMeshCsg and otherMeshCsg for good !

        firstMesh.bakeCurrentTransformIntoVertices() // <--
        otherMesh.bakeCurrentTransformIntoVertices() // <--

        const firstMeshCsg = CSG.FromMesh(firstMesh);
        const otherMeshCsg = CSG.FromMesh(otherMesh);

        const intersectionCst = firstMeshCsg
          .intersect(otherMeshCsg)
          .inverse();

        const intersectionMesh = intersectionCst.toMesh('qsfqsf', mat2, this.scene)
        intersectionMesh.renderingGroupId = 1;
        intersectionMesh.isPickable = false ;

Yay ! my shape is at the right place !

but i cannot interact with my scene properly : some meshes disappear when I try to click on them, and there’s a lot of WebGL: INVALID_OPERATION: drawElements: no buffer is bound to enabled attribute in the console.

SO
My questions are :

  • do my bakeCurrentTransformIntoVertices() misses something
  • can I achieve what I want in an other way ?

@Evgeni_Popov has been looking into CSG recently and might have an idea else @JohnK might also be awesome with it :slight_smile:

On my side I feel ashame to be clueless on this one.

1 Like

Also would be amazing if you could repro in the playground :slight_smile:

1 Like

Thanks for the reply, and yes, I’m on it :stuck_out_tongue:

But it seems to be a quite specific problem. I need to upload my own models : it will take some time

1 Like

As the linked PG does work, we will definitely need a repro.

I don’t you need to bake the transformations, it should work without that (translating all the objects in the linked PG still works).

Woohoo ! Here comes the PG !

Without baking : https://www.babylonjs-playground.com/#Q9K825#11
With baking :upside_down_face: : https://www.babylonjs-playground.com/#Q9K825#12

On both you can point at a mesh to see it’s material change … and see that it’s broken

1 Like

The csg mesh is not correctly positioned because it is created outside of the hierarchy of the nodes and so does not inherit the special transformations created when a gltf/glb file is loaded. So, setting its parent with the same parent than one of the mesh corrects the problem:

https://www.babylonjs-playground.com/#Q9K825#15

Also, it seems to me that mesh picking is working(?)

Regarding the inversion you need to apply, I guess it’s because the gltf/glb data are right handed and the CSG code does not handle it properly, but I’m not familiar with this code so I don’t really know…

2 Likes

YES ! This is it !

Without a parent, the created mesh was lost :cry:

Also, it seems to me that mesh picking is working(?)
In the version without baking, yes, but not with.

Thanks @Evgeni_Popov !