Hi everyone,
I’m working on a project involving CSG (Constructive Solid Geometry) operations in three.js. I have a function that takes a mesh and a series of machining operations, then returns the resulting mesh after performing the CSG operations. The machining operations data is stored in JSON format like this:
{
"id": "76",
"type": "StepDrilling",
"steps": [
{ "diameter": 5, "depth": 100 },
{ "diameter": 20, "depth": 50 }
],
"sortNumber": 4,
"position": {
"id": "77",
"type": "PositionMatrix",
"vxx": 1,
"vxy": 0,
"vxz": 0,
"vyx": 0,
"vyy": 0,
"vyz": -1,
"vzx": 0,
"vzy": 1,
"vzz": 0,
"mx": 30,
"my": 50,
"mz": 35
}
}
Below is my function to perform the operation and return the resulting mesh:
applyMachiningOperations(part, machining) {
let resultMesh = part.clone();
if (resultMesh instanceof THREE.Mesh && resultMesh.geometry instanceof THREE.BufferGeometry) {
machining.forEach(operation => {
const matrix = new THREE.Matrix4().set(
operation.position.vxx, operation.position.vyx, operation.position.vzx, operation.position.mx,
operation.position.vxy, operation.position.vyy, operation.position.vzy, operation.position.my,
operation.position.vxz, operation.position.vyz, operation.position.vzz, operation.position.mz,
0, 0, 0, 1
);
let geometry, operatedMesh;
switch (operation.type) {
case "StepDrilling":
const stepGeometries = [];
operation.steps.forEach(step => {
geometry = new THREE.CylinderGeometry(step.diameter / 2, step.diameter / 2, step.depth, 32);
operatedMesh = new THREE.Mesh(geometry);
operatedMesh.applyMatrix4(matrix);
resultMesh = CSG.subtract(resultMesh, operatedMesh);
});
break;
default:
console.error("Unsupported operation type:", operation.type);
return;
}
});
return resultMesh;
}
}
Output:
However, when I try to use BufferGeometryUtils.mergeBufferGeometries
to combine the geometries before performing the CSG operation, it doesn’t seem to have any effect on my main mesh. Here’s the modified version of the function:
applyMachiningOperations(part, machining) {
let resultMesh = part.clone();
if (resultMesh instanceof THREE.Mesh && resultMesh.geometry instanceof THREE.BufferGeometry) {
machining.forEach(operation => {
const matrix = new THREE.Matrix4().set(
operation.position.vxx, operation.position.vyx, operation.position.vzx, operation.position.mx,
operation.position.vxy, operation.position.vyy, operation.position.vzy, operation.position.my,
operation.position.vxz, operation.position.vyz, operation.position.vzz, operation.position.mz,
0, 0, 0, 1
);
let geometry, operatedMesh;
switch (operation.type) {
case "StepDrilling":
const stepGeometries = [];
operation.steps.forEach(step => {
const geometry1 = new THREE.CylinderGeometry(step.diameter / 2, step.diameter / 2, step.depth, 32);
stepGeometries.push(geometry1);
});
> geometry = BufferGeometryUtils.mergeBufferGeometries(stepGeometries);
operatedMesh = new THREE.Mesh(geometry);
operatedMesh.applyMatrix4(matrix);
this.scene().add(operatedMesh);
this.scene().add(resultMesh);
resultMesh = CSG.subtract(resultMesh, operatedMesh);
break;
default:
console.error("Unsupported operation type:", operation.type);
return;
}
});
return resultMesh;
}
}
Despite merging the geometries and attempting to perform the subtraction, the result doesn’t change as expected. Can anyone help me understand why merging the geometries with BufferGeometryUtils.mergeBufferGeometries
isn’t affecting the main mesh and how I might resolve this issue?
Thanks in advance!