function drawHull(scene, convHull) {
for (let face of convHull.faces) {
if (face.normal.z > 0) continue;
const shouldHighlight = Math.random() < 0.02;
const highlightColor = new THREE.Color(0.2, 0.2, 0.8).lerp(
new THREE.Color(1, 1, 1),
0.5
);
const col = 0.5 + Math.random() / 2;
const material = new THREE.MeshBasicMaterial({
color: shouldHighlight
? highlightColor
: new THREE.Color(
0.5 + Math.random() / 2,
0.5 + Math.random() / 2,
0.5 + Math.random() / 2
),
side: THREE.DoubleSide
});
const geometry = new THREE.BufferGeometry();
const p1 = face.getEdge(0).vertex.point;
const p2 = face.getEdge(1).vertex.point;
const p3 = face.getEdge(2).vertex.point;
const vertices = new Float32Array([
p1.x,
p1.z,
p1.y,
p2.x,
p2.z,
p2.y,
p3.x,
p3.z,
p3.y
]);
geometry.setAttribute("position", new THREE.BufferAttribute(vertices, 3));
const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
const materialClone = new THREE.MeshBasicMaterial({
color: "black",
wireframe: true,
side: THREE.DoubleSide
});
const geometryClone = new THREE.BufferGeometry();
const verticesClone = new Float32Array([
p1.x,
p1.z,
p1.y,
p2.x,
p2.z,
p2.y,
p3.x,
p3.z,
p3.y
]);
geometryClone.setAttribute(
"position",
new THREE.BufferAttribute(verticesClone, 3)
);
const meshClone = new THREE.Mesh(geometryClone, materialClone);
scene.add(meshClone);
const materialPlane = new THREE.MeshBasicMaterial({
color: shouldHighlight ? highlightColor : "black",
wireframe: true
});
const geometryPlane = new THREE.BufferGeometry();
const verticesPlane = new Float32Array([
p1.x,
0,
p1.y,
p2.x,
0,
p2.y,
p3.x,
0,
p3.y
]);
geometryPlane.setAttribute(
"position",
new THREE.BufferAttribute(verticesPlane, 3)
);
const meshPlane = new THREE.Mesh(geometryPlane, materialPlane);
scene.add(meshPlane);
const materialPlaneClone = new THREE.MeshBasicMaterial({
color: shouldHighlight ? highlightColor : "white",
wireframe: false
});
const geometryPlaneClone = new THREE.BufferGeometry();
const verticesPlaneClone = new Float32Array([
p1.x,
0,
p1.y,
p2.x,
0,
p2.y,
p3.x,
0,
p3.y
]);
geometryPlaneClone.setAttribute(
"position",
new THREE.BufferAttribute(verticesPlaneClone, 3)
);
const meshPlaneClone = new THREE.Mesh(
geometryPlaneClone,
materialPlaneClone
);
scene.add(meshPlaneClone);
// highlight some faces
if (shouldHighlight) {
const lineMaterial = new THREE.LineBasicMaterial({
color: highlightColor
});
const lineGeometry1 = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(p1.x, p1.z, p1.y),
new THREE.Vector3(p1.x, 0, p1.y)
]);
const line1 = new THREE.Line(lineGeometry1, lineMaterial);
scene.add(line1);
const lineGeometry2 = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(p2.x, p2.z, p2.y),
new THREE.Vector3(p2.x, 0, p2.y)
]);
const line2 = new THREE.Line(lineGeometry2, lineMaterial);
scene.add(line2);
const lineGeometry3 = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(p3.x, p3.z, p3.y),
new THREE.Vector3(p3.x, 0, p3.y)
]);
const line3 = new THREE.Line(lineGeometry3, lineMaterial);
scene.add(line3);
}
}
}