javascriptthree.jscsg

How to remain the original color after csg operations (using ThreeCSG.js)


In html, I use JSONLoader to load my obj(.js file exporting from Blender 2.66). After loading my obj, I use ThreeCSG to subtract a cube. The effect I want to achieve is like cross-section. Now csg operations work fine. The problem is that I can't remain the original color after csg operations.

For example, after A subtract B, I hope that the mesh remain the materials of A, somewhat similar to the image(a.subtract(b)) csg.js website shows. The difference is I hope it all red (A's materials). However, it may be more complicated because my obj has several colors and more complex geometry.

Here is a part of my code.

loader.load( "obj/blenderscene/FinFET2.4.js", createScene1 );

function createScene1( geometry, materials ) {

  mesh = new THREE.Mesh( geometry, new THREE.MeshFaceMaterial( materials ) );

  var mesh_bsp = new ThreeBSP(mesh);
  var cube_geometry = new THREE.CubeGeometry( 2.5,7 , 7 );
  cube_geometry = new THREE.Mesh( cube_geometry ,new THREE.MeshLambertMaterial( { color: 0xff11ff, opacity: 0.9, shading: THREE.FlatShading, wireframe: true, wireframeLinewidth: 2, transparent: true } ) );

  var cube_bsp = new ThreeBSP( cube_geometry );
  var subtract_bsp = mesh_bsp.subtract( cube_bsp );

  var result = subtract_bsp.toMesh( new THREE.MeshFaceMaterial(materials) );
  result.geometry.computeVertexNormals();
  scene.add( result );
 }

The version of three.js is R55.

Sorry for my poor English.I really need some help.Hope you can understand what I mean.

Thank you all.


Solution

  • set the material indexes correctly.

    How to:

    Add the cube material to the materials

    loop over result.geometry.faces as searchFace

    search for a face with same vertices as searchFace in geometry.faces

    if found then add the materialindex of the face to searchFace

    if not found, search for a face with 2 of the same vertices as searchFace in geometry.faces

    if found then add the materialindex of the face to searchFace

    if not found, search for a face with 1 of the same vertices as searchFace in geometry.faces

    if found then add the materialindex of the face to searchFace

    if not found add the material index of the cube material