this.uiManager.uploadedBrushes = this.uiManager.uploadedBrushes.map((brush, index) => {
if (!doesIntersect(brush)) return brush;
const originalState = originalStates[index];
const result = this.evaluator.evaluate(brush, cuttingBrush, SUBTRACTION);
result.userData = { ...originalState.userData };
result.geometry = mergeVertices(result.geometry, 1e-4)
// result.geometry.computeVertexNormals();
const normals = result.geometry.attributes.normal;
const faceCount = result.geometry.index ?
result.geometry.index.count / 3 :
result.geometry.attributes.position.count / 3;
const faceKeys = new Array(faceCount);
const materialsMap = new Map();
const finalMaterials = [];
for (let faceIndex = 0; faceIndex < faceCount; faceIndex++) {
const faceNormal = new THREE.Vector3();
for (let i = 0; i < 3; i++) {
const vertexIndex = result.geometry.index ?
result.geometry.index.array[faceIndex * 3 + i] :
faceIndex * 3 + i;
faceNormal.add(new THREE.Vector3(
normals.array[vertexIndex * 3],
normals.array[vertexIndex * 3 + 1],
normals.array[vertexIndex * 3 + 2]
));
}
faceNormal.normalize();
let keyFound = null;
for (const [normalKey, materialInfo] of originalState.faceNormals) {
const originalNormal = new THREE.Vector3().fromArray(normalKey.split(',').map(Number));
originalNormal.normalize();
if (faceNormal.dot(originalNormal) > 0.9999) {
keyFound = normalKey;
break;
}
}
if (!keyFound) {
keyFound = faceNormal.toArray().join(',');
}
faceKeys[faceIndex] = keyFound;
if (!materialsMap.has(keyFound)) {
let materialInfo = originalState.faceNormals.get(keyFound);
if (!materialInfo) {
materialInfo = {
materialIndex: 0,
material: Array.isArray(brush.material) ?
brush.material[0].clone() :
brush.material.clone()
};
}
const newIndex = finalMaterials.length;
materialsMap.set(keyFound, newIndex);
finalMaterials.push(materialInfo.material);
}
}
result.geometry.clearGroups();
let groupStartFace = 0;
let currentKey = faceKeys[0];
for (let faceIndex = 1; faceIndex < faceCount; faceIndex++) {
if (faceKeys[faceIndex] !== currentKey) {
const materialIndex = materialsMap.get(currentKey);
result.geometry.addGroup(groupStartFace * 3, (faceIndex - groupStartFace) * 3, materialIndex);
groupStartFace = faceIndex;
currentKey = faceKeys[faceIndex];
}
}
{
const materialIndex = materialsMap.get(currentKey);
result.geometry.addGroup(groupStartFace * 3, (faceCount - groupStartFace) * 3, materialIndex);
}
result.material = finalMaterials;
result.material.forEach(mat => {
mat.side = THREE.DoubleSide;
mat.needsUpdate = true;
});
this.scene.remove(brush);
this.scene.add(result);
return result;
});
there are small gaps between faces of the mesh if you load the model in blender or 3d viewing software.
please help me solve this