CSG operations on bufferGeometry

1.2k Views Asked by At

I am currently using the three.js geometry class for creating a shape and then performing multiple CSG operations on that shape. Thus continuously redrawing the shape.

This process of performing multiple csg operations is slow, as I am using ray-casting to get the shape on click and perform CSG of the selected shape and a pre-defined shape (any shape or geometry).

So my questions are :

  • Will using buffer geometry speed up my CSG, but that said is there any library to perform CSG operations on THREE.BufferGeometry instances?

  • Is there a way I can speed up the process by using any other methods ?

This is my code-flow :

var objects = [];

init();
render();

function init(){
 //scene and camera setup ... etc
   var sphere =  new THREE.SphereGeometry(200, 32, 32);
   objects.push(sphere);
 // raycasting config
 // bind mouse click and move event
 }
  function onMouseDown() {

   var intersects = raycaster.intersectObjects(objects);
   .....
   // get intersected shape ..
   // perfrom csg with predifend shape .
   // Also contains steps to convert 
      geometry to *CSG libaray geometry* 
      and back to Three.js geometry)..
   // replace the shape with existing
   .....
    render();
 }

I am using this library for CSG operations and overall flow is similar to this example in three.js examples.

2

There are 2 best solutions below

0
On BEST ANSWER

I don't have element for performance comparison, but you can find a buffergeometry library in develop branch of ThreeCSG ThreeCSG develop from Wilt

It support buffergeometry (from examples):

var nonIndexedBoxMesh = new THREE.Mesh( nonIndexedBufferGeometry, material );
var bsp1 = new ThreeBSP( nonIndexedBoxMesh );
var indexedBoxMesh = new THREE.Mesh( indexedBufferGeometry, material );
var bsp2 = new ThreeBSP( indexedBoxMesh );
var geometry = bsp1.subtract( bsp2 ).toBufferGeometry();
var mesh = new THREE.Mesh( geometry, material );

It works with r75

0
On

For CSG performance improvements I highly recommend taking a look at this OctreeCSG repo! It uses a newer method known as Octree-Embedded BSPs for a nearly 1000X performance increase (details here)!

I don't think whether you're using standard Geometry or BufferGeometry will make a big difference (if any). The main performance catalyst is the underlying CSG algorithm.