Hello, I’m new to Three.js, and I am using React Three Fiber for my project. My main task is to implement a plane intersection with a mesh algorithm and compute the convex hull of the intersection points for circumference measurement on the human body. I am working on it but got stuck. Can you help me figure out what I am doing wrong? I want to only get points that intersect the plane in a convex shape.
const setPointOfIntersection=(line, plane, pointsOfIntersection)=> {
const pointOfIntersection = plane?.intersectLine(line, new Vector3());
if (pointOfIntersection) {
pointsOfIntersection.push(pointOfIntersection);
};
}
function calculateIntersections(plane, mesh) {
var a = new Vector3(),
b = new Vector3(),
c = new Vector3();
var planePointA = new Vector3(),
planePointB = new Vector3(),
planePointC = new Vector3();
var lineAB = new Line3(),
lineBC = new Line3(),
lineCA = new Line3();
// var pointOfIntersection = new Vector3();
var mathPlane = new Plane();
const positionAttribute = plane.geometry.getAttribute('position');
const localVertex = new Vector3();
localVertex.fromBufferAttribute(positionAttribute, 0);
plane.localToWorld(planePointA?.copy(localVertex));
localVertex.fromBufferAttribute(positionAttribute, 1);
plane.localToWorld(planePointB?.copy(localVertex));
localVertex.fromBufferAttribute(positionAttribute, 2);
plane.localToWorld(planePointC?.copy(localVertex));
mathPlane.setFromCoplanarPoints(planePointA, planePointB, planePointC);
var pointsOfIntersection = [];
const dodecaPositionAttribute = mesh.geometry.getAttribute('position');
for (let vertexIndex = 0; vertexIndex < dodecaPositionAttribute.count; vertexIndex += 3) {
localVertex.fromBufferAttribute(dodecaPositionAttribute, vertexIndex);
mesh.localToWorld(a?.copy(localVertex));
localVertex.fromBufferAttribute(dodecaPositionAttribute, vertexIndex + 1);
mesh.localToWorld(b?.copy(localVertex));
localVertex.fromBufferAttribute(dodecaPositionAttribute, vertexIndex) + 2
mesh.localToWorld(c?.copy(localVertex));
lineAB = new Line3(a, b);
lineBC = new Line3(b, c);
lineCA = new Line3(c, a);
setPointOfIntersection(lineAB, mathPlane, pointsOfIntersection);
setPointOfIntersection(lineBC, mathPlane, pointsOfIntersection);
setPointOfIntersection(lineCA, mathPlane, pointsOfIntersection);
}
return pointsOfIntersection;
// return positions;
// pointsOfIntersection.setAttribute(
// 'position',
// new BufferAttribute(new Float32Array(positions), 3));
// var pointsMaterial = new PointsMaterial({
// size: 0.01,
// color: "red"
// });
// var points = new Points(pointsOfIntersection, pointsMaterial);
// mesh.add(points);
}
const addPlaneToMesh = () => {
if (!obj) return;
const mesh = obj.children[0];
if (mesh.isMesh) {
//const randomIndex = 1327
const plane = new Mesh(new PlaneGeometry(1, 1), new MeshBasicMaterial({ color: "lightgray", transparent: true, opacity: 0.425, side: DoubleSide }));
plane.rotateX(-Math.PI / 2);
mesh.add(plane);
const intersectionPoints = calculateIntersections(plane, mesh);
// console.log("intersectionPoints",intersectionPoints);
const convexHullPoints = computeConvexHull(intersectionPoints);
const pointsGeometry = new BufferGeometry().setFromPoints(intersectionPoints);
let pointsMaterial = new PointsMaterial({
size: .1,
color: "red"
});
let points = new Points(pointsGeometry, pointsMaterial);
mesh.add(points);
}
}
function computeConvexHull(points) {
const geometry = new ConvexGeometry(points);
const positions = geometry.attributes.position.array;
const vertices = [];
for (let i = 0; i < geometry.attributes.position.count; i += 3) {
vertices.push(new Vector3(positions[i], positions[i + 1], positions[i + 2]));
}
return vertices;
}