What is wrong here CSG subtract result look like opposite mesh

Here is my PG
I want to subtract mesh using CSG but it return/subtract opposite mesh.
Can any on explain this what is wrong here?

I asked GPT.

To fix this issue, you need to swap the order of the operands when performing the subtraction operation. Instead of subtracting sp1 from sp2, you should subtract sp2 from sp1. Here’s the updated code snippet:

const newMesh = sp1.subtract(sp2).toMesh('newmesh', null, scene);

By subtracting sp2 from sp1, you will achieve the desired subtraction result.


CSG Experiment | Babylon.js Playground (babylonjs.com)


Thanks @CodingCrusader for reply.

If I swap the order of the operands the desire subtraction not happening.

For the confirmation I create another polygon and subtract it, The result is as expected. Please check my PG. Also I need to remove the bottom part of new Mesh.


Why don’t you use Blender? The program is optimized to handle such operations. You can export the finished result afterwards via a babylon.plugin and don’t have to do the calculations programmatically in babylon, which can also be time-consuming.

Here all data are dynamic and unlimited subtraction can be happen . Blender is not a good solution.

and after that this is an issue or I miss something else, that need to be fixed.


CSG is a tricky operation to perform, especially in a shape like this terrain which has a lot of surface area. What exactly is your application for it? If you explain what you’re trying to do more we might be more capable of helping.

Thanks @carolhmj for reply…
My application is a live location tracker inside any golf course.
I struggling for create new mesh from a mesh inside a path.
The scenario is like this PG.
Now I’m trying to create mesh using CSG subtraction. But some of the CSG operation return opposite as expected.
More specifically I’m trying to do this for avoiding dynamic texture because of this issue:

That’s a nice idea with the location tracker :slight_smile:

If you are still having trouble with the built-in CSG operators of bb, it might be helpful to switch to external libraries such as https://evanw.github.io/csg.js/

Hmmm and why you need to create new meshes in this case? Are you trying to display only a specific part of the course?

Need to show Golf course object such as perimeter, tee-boxes, bunkers, green, Creek, road, Water body, Lake, pond, Bridge, tree every thing for a single hole or for all holes as per user demand.
Here I have elevated data only for course’s boundary not for individual object. That’s the reason I need to create new meshes from that elevated mesh like this

In this case, Cedric’s suggestions in your dynamic texture post will be much better for your app. Creating meshes with CSG is a very heavy operation and prone to issues with geometry, and you should avoid doing this online as much as possible. You could look into displaying the boundaries using shaders too, it’s also a much better solution.

1 Like

Ok, Thanks for your suggestion @carolhmj . I will try to displaying the boundaries using shaders. But I have no Idea about shader. Can you please give me an PG example for how to show the boundaries using shader on mesh it will be more help full.

When getting into shaders, I highly recommend taking a bit of time to read up on how they work first before trying to look at examples, or it will become confusing fast. The Book of Shaders is an AMAZING introductory resource. We also have a short introduction in our docs: Introduction to Shaders | Babylon.js Documentation (babylonjs.com). You can also use Node Material Editor (NME) to create shaders: (2) Node Material: Ink Shader Tutorial - YouTube

I would avoid using CSG for this, the first problem you will run into is the thickness of the terrain. Its hard for a CSG operation to know what the terrain or for all intensive purposes the planes thickness is (seeing how it has no thickness) so there is really no “body” for the other objects to subtract from. Also its a very intensive operation for what you are trying to do.

A better solution for what you are attempting to do in my opinion would be to have a collection of “stamps” that would be the silhouette shapes of the objects you are wishing to place on the terrain. Then you would have some sort of metadata associated with each stamp, that would tell a secondary system how much to “indent” or change the height of the terrain. Basically it would be way easier to modify the terrain meshes vertices other then anything else. If you wanted to get really fancy, you could do dynamic subdivisions as well to make sure in areas that you place more of your elements would have higher vertices counts but that is a different conversation.

This silhouette process would also work well for paths, as you would do the same idea but with some sort of line system to create the silhouette on the fly for the paths.