I lose the materials of object after apply subtract from CSG

Hi All,

I have an object and a box,I want to subtract the object from the box, when I apply one material I get the fit shape, but when I apply materials I get the fit shape but I can not set value for the material for every face of the box,

my code is :slight_smile:

const materials = [
    new MeshStandardMaterial({ color: 0xff11ff }), // Right
    new MeshStandardMaterial({ color: 0xff00ff }), // Left
    new MeshStandardMaterial({ color: 0x55ffff }), // Top
    new MeshStandardMaterial({ color: 0xffff11 }), // Bottom
    new MeshStandardMaterial({ color: 0x00ffff }), // Front
    new MeshStandardMaterial({ color: 0xff00ff }), // Back
  ];
  var plane = new BoxGeometry( Length,Height , 0.2 ).toNonIndexed();
  var planeMesh = new Mesh( plane,  materials);

  // emptyingBox is a boxGeometry
  let mesh = emptyingBox.clone();
  mesh.scale.x = object.userData.W - 0.05;
  mesh.scale.z = (object.userData.H - 0.05) / 4;
  mesh.scale.y = 1;
  

  let bspFront = CSG.fromMesh(mesh);
  bspResult = bspResult.subtract(bspFront);

  planeMesh = CSG.toMesh(
    bspResult,
    planeMesh.matrix,
    planeMesh.material
  );
  scene.add( planeMesh );


Thank you.

A box accepts multiple materials because its faces are defined as groups. Maybe CSG operations destroy the groups? If this is what happens, you might have to manually recreate the groups, or use vertex colors, if the materials are just plain colors.

Mesh-bvh-csg handles multi material quite well ootb but may not include multi material per object three-bvh-csg - Multi Material three-bvh-csg

Yes, the operation destroyed the groups. I tried to recreate them, but I got different shapes and did not know how to do it.

I tried it, but the result is not good. I always get one object, and no operations are applied.

const evaluator = new Evaluator();
let  brush1 = new Brush( doorGeometry,  new MeshStandardMaterial({ color: 0xff0000 }) );
let  brush2 = new Brush( planeMesh.geometry, materials );

let result = evaluator.evaluate( brush1, brush2,  SUBTRACTION);

result.castShadow = true;
result.receiveShadow = true;

scene.add( result );

Do you have a reference image or simple live example that illustrates the two geometries you are tying to evaluate with subtraction?