2

I'm trying to take a cross section of a heart model that I loaded using three.js's STLLoader function. I'm currently trying to use the ThreeCSG wrapper for the csg.js library, same as in this stack overflow post.

Here's my code for the csg subtraction

function modelLoadedCallBack( geometry ) {

    material = new THREE.MeshPhongMaterial( { color: model.color } );
    mesh = new THREE.Mesh( geometry, material );
    mesh.rotation.set( model.rotationX, model.rotationY, model.rotationZ );
    mesh.scale.set( model.scale, model.scale, model.scale );
    var originalBSP = new ThreeBSP( mesh );

    var xSectionBSP = new ThreeBSP( xSection );
    var subtractedBSP = originalBSP.subtract( xSectionBSP );

    var result = subtractedBSP.toMesh( material );
    result.geometry.computeVertexNormals();

    scene.add( result );

};

I load the stl model, then in the loader's callback function, I try to subtract the meshes. The error I'm getting is on line 34 of the ThreeCSG wrapper file, saying

ThreeCSG.js:34 Uncaught TypeError: Cannot read property 'length' of undefined

I'm guessing this is because (a) I'm not using ThreeCSG proprerly, (b) I need to do the subtraction somewhere else in the code, or (c) STL format models are not supported.

In any case, I'm completely stumped and would appreciate the advice of someone more experienced in using three.js.

Community
  • 1
  • 1
  • I have the same error in my script. I suppose here is the unswer to our question https://stackoverflow.com/questions/20602045/csg-operation-with-stlloader/20724436#20724436 – user2075609 Mar 01 '16 at 15:29
  • will probably use obj loader in my situation – user2075609 Mar 01 '16 at 15:30
  • Since I only needed cross-sections at specific planes, I wrote my own shader program. In the fragment shader, I simply check if the given vertex is beyond the cutting plane and if it is, I discard that vertex. if ( view == 1 && vPosition.z > 0.0 ) discard; - view describes the selected cutting plane. [Here](http://gamedevelopment.tutsplus.com/tutorials/a-beginners-guide-to-coding-graphics-shaders--cms-23313) is a tutorial on shaders. – Rahat Dhande Mar 04 '16 at 19:54

1 Answers1

0

It looks correct. This error can happen if either the STL geometry is empty or if you forgot to initialize xSection.

kintel
  • 425
  • 3
  • 4
  • I stepped through the debugger and found that the STL mesh's geometry doesn't have a faces field. In threeCSG, 'geometry.faces.length' is called, and that's what throws the error. Is there another way to take cross sections or is there something I can do to the STL geometry to fix this? – Rahat Dhande Jan 21 '16 at 17:33
  • Update: I've tried converting the bufferGeometry to a regular geometry but this significantly slows down the load time and also crashes the web page when I try to convert the mesh to a ThreeBSP object. – Rahat Dhande Jan 21 '16 at 18:00
  • ThreeCSG doesn't support BufferGeometry. If it crashes on a Geometry, that sounds like a bug in ThreeCSG or csg.js. Note: Those are not considered stable libraries. – kintel Jan 21 '16 at 18:31
  • Another note: BufferGeometry is only created for binary STL files. If you load an ASCII STL file it should work. – kintel Jan 21 '16 at 18:32
  • My files are too big to be processed using a BSP tree, so I'm going to try to use a custom vertex and fragment shader to take cross sections. I don't need arbitrary cuts - only cuts along specified planes - so using CSG was a bit of overkill anyways. – Rahat Dhande Jan 27 '16 at 18:37