Three-bvh-csg: A library for performing fast CSG operations!

Hello! I want to share the three-bvh-csg project - a library designed to support fast, arbitrary boolean construction-solid-geometry operations between arbitrary geometries! The project uses three-mesh-bvh to quickly perform triangle intersections between the geometries in addition to other acceleration structures to keep the operations running faster than BSP-based CSG operations.

The project is still in-progress but I would love to see some contributors if there are others who are interested in this kind of work! With some more work I think this could be an incredibly robust, fast library for hierarchical mesh operations - perfect for CAD work, game development, level design, 3d print modeling, and more!

Interactive CSG demo

Complex geometry operations

Multimaterial demo

10 Likes

Freaking awesome, as usual! :slight_smile:

If anyone is interested in this kind of computational geometry work, @Antonio_Gonzalez_Viegas and the devs at IFC.js have been kind enough to sponsor 3 bounties for some big contributions to help move three-bvh-csg forward! The sponsored features are aimed at improving the robustness and testability of the triangle intersection logic as well as the structure of the final boolean result.

See them all under the bounties label! Of course I’m happy to help provide guidance and advice on moving any of the forward and I’m looking forward to seeing more of what can be built with CSG in the browser.

Thanks again to IFC.js for the support!

6 Likes

wow, awesome. I am one of just few contributors to https://openjscad.xyz/ that uses csg.js and am always on the lookout for what is new in this csg space.

I have lined up few different engines on my whislist, and am thinking that jscad should be open to different engines.

I am excited to see things moving in this space :slight_smile:

Looking over Three-bvh-csg with keen interest and appreciation. Two questions:
(1) Is it robust against just-touch situations?
(2) Does it require water-tight and oriented (outward normal) objects?

(1) Is it robust against just-touch situations?

It attempts to be but there are still cases where this fails which need to be fixed.

(2) Does it require water-tight and oriented (outward normal) objects?

Yes - proper CSG will always require this.

3 Likes

Thanks for the answers.

I have played around with the Complex Geometry example and browsed the source code.

Based on my understanding, I am investigating whether this software can be used in the following way:

 while (true) {
{
       prepareObject(A);

       prepareObject(B);

      C = intersectObjects(A,B)

      answer = interrogateGeometrically(C)

      if (answer == true) break;

      displaceGeometrically(A)

      displaceGeometrically(B)

}

I hope this pseudo-code is sufficiently clear.

prepareObject() involves starting with a group of threejs meshes, combining them, checking/fixing water-tight requirements, orientation requirements, converting to a half-edge structure and whatever else is needed for a boolean operation. I think it could get quite expensive, particularly for meshes composed of a large number of triangles (much larger than the Stanford bunny).

displaceGeometrically() is just a rigid body movement. No changes to the object geometry itself.

The while loop is expected to execute many, many times!!

My questions:
(a) Are objects A, B changed/modified/destroyed after the boolean operation?
(b) How much of prepareObject() can be shifted to before the while loop?
(c) Is C oriented and water-tight?

Thanks

As you only displace A and B and do not rotate or modify their vertices, the prepareObjects calls can be left completely outside of the loop. For this, you can use a custom function to check for intersects that relies on three-mesh-bvh directly instead of three-mesh-csg. That way, you can subtract the A and B displacements to the rays origins, and hence the BVH structure doesn’t need to be recomputed for A and B.

You didn’t specify if you only need to detect A and B intersection, or you need to analize C. The custom intersection function would only detect it (so you can compute C after detection)

I can give you the code for the function if you’re interested.

1 Like

Getting back to this after a while.

I am trying to run this entire package locally after downloading the distribution from GitHub.
I make use of the Servez infrastructure - which is a very simple web server for local web development.
Unfortunately, I am running into all sorts of “unresolved symbol” type errors - even after trying to address the obvious problems.

Has anyone in this group been able to run the Three-bvh-csg library examples locally? If so, I request your guidance.

Thanks much

OLDMAN

every npm library normally requires bundling, a bundler. they already bring dev servers. there is nothing you need to set up, config or worry about, it just works. use vite for instance:

npm create vite
cd yourProjectFolder
npm install
npm install three three-bvh-csg
npm run dev

servez looks like a lot of unwanted trouble if you ask me, and i still see nothing that would allow you to resolve three-bvh-csg the name of the npm package defined in package.json. in any case a HTTP server simply isn’t enough and will make dealing with webdev the hardest it can possibly be.

1 Like

I’m thinking of conducting the following test on this most promising package.

(1) Use the model indicated in the following picture

(2) Each of the 3 objects is currently a collection of 3JS meshes. I need to compose each into a single 3JS mesh that is (a) closed water tight and (b) oriented with outward facing face normals.

(3) Do the following operation:

(PINK PLATE) intersect ( union (BLUE PIN, GREEN PIN))

(4) See how it handles this “just touch” situation

Do send me your feedback and comments before I commence.

Best,

OLDMAN

Thanks for the tip. Still struggling since I’m new to this node and npm world. Anyway, after some reading and downloading, I followed your steps. It installed a whole bunch of node modules, including those from three. However, when I do “npm run dev”, it says there is no such script. I also notice that the “examples” folder is missing. I’m sure there are some basics I am missing in my understand of this whole process. Please forgive and assist…Best…OLDMAN

I need to add…to make it worse, I am new to the world of github as well :frowning: Perhaps my mistake is right there. All I did was copy the package.json file to my local directory and then run “npm install”. Is that the right way?

no worries, it seems overwhelming at first,

there’s normally nothing you need to copy by hand. it sets up everything, including package.json. cd yourProjectFolder is the name of the project you have given vite, whatever it was. npm run dev is only valid inside that folder. im guessing you tried calling it somewhere else.

Greetings drcmda,

I’m still struggling!

Let me tell you what I’ve done.

(1) I have ignored npm create vite, because I’m not able to understand what it is. Documentation says yet-another “make things even easier” layer. I believe I should be able to get things working without this. Perhaps I am wrong here itself.

(2) Anyway, I copied over the zip file from three-bvh-csg github and unzipped it in my local folder.

(3) npm install – seems to work

(4) npm install three – seems to work

(5) npm install three-bvh-csg – seems to work

(6) npm run dev – does not work as the script does not exist in the json file

(7) npm run start – seems to be doing something, but does not terminate. I had to kill it.

(8) Once everything is setup, what is the command to run each of the csg boolean examples locally?

Thanks much again…

Best,

OLDMAN

I think it would best to have discussions around node, npm, and other tooling questions in a dedicated thread so this one can remain focused on three-bvh-csg features.

2 Likes

Talking again about just-touch conditions…I created two axis aligned cubes, one inside the other and, step-by-step, moved the inner cube outside the larger one. Along the way, I got objects like this:

Looks leaky, not water-tight.
Is this to be expected with the way the software exists currently?

Best,

OLDMAN

As I’ve mentioned previously the library is not yet robust to all cases… That aside your image does not look problematic. The topology just includes some small triangles. If you think there’s an issue feel free to open an issue with a clear explanation and reproduction case of the issue.

2 Likes

I’ve just released a new 0.0.3 revision of the project! This version includes significant improvements to the robustness of just-touch, overlap, and coplanar triangle cases, includes some bug fixes, and adds a utility function for computing geometry volume. There are still some issues but the behavior should be significantly improved.

I’m hoping to see some game level layout builders from this at some point!

Release notes here

Hierarchical Operations Demo

Demo

Demo showing multiple operations performed on the geometry to build a new model.

Coplanar / Overlap Operations

Showing proper CSG behavior using two overlapping cylinders with DIFFERENCE and INTERSECTION operations.

7 Likes

well here you go :smiley: live link: room_design2

I’m adjusting the boolean operations 20 seconds in.

And I’ve just upgraded to 0.0.3 and can see that coplanar operations resolve perfectly well. Really pleasant to use, that will avoid having to offset objects slightly before those operations.

And in addition to three-bvh-csg, the FPS control in that video is setup to use bvh to detect collisions.

5 Likes