I am trying to draw a polygon from the intersection points of a dodecahedron and a plane. I found this algorithm, but it draws not quite correctly. It works with some figures, but not with dodecahedron or cube.
var tri = new THREE.Triangle(
pointsOfIntersection[2],
pointsOfIntersection[1],
pointsOfIntersection[0],
);
var normal = new THREE.Vector3();
tri.getNormal(normal);
var baseNormal = new THREE.Vector3(0, 0, 1);
var quaternion = new THREE.Quaternion().setFromUnitVectors(
normal,
baseNormal
);
var tempPoints = [];
pointsOfIntersection.forEach((p) => {
tempPoints.push(p.clone().applyQuaternion(quaternion));
});
var shape = new THREE.Shape(tempPoints);
var shapeGeom = new THREE.ShapeBufferGeometry(shape);
const mesh = new THREE.Mesh(
shapeGeom,
new THREE.MeshBasicMaterial({
color: "red",
side: THREE.DoubleSide,
wireframe: false,
})
);
var box = new THREE.Box3().setFromObject(mesh);
var size = new THREE.Vector3();
box.getSize(size);
var vec3 = new THREE.Vector3(); // temp vector
var attPos = mesh.geometry.attributes.position;
var attUv = mesh.geometry.attributes.uv;
for (let i = 0; i < attPos.count; i++) {
vec3.fromBufferAttribute(attPos, i);
attUv.setXY(
i,
(vec3.x - box.min.x) / size.x,
(vec3.y - box.min.y) / size.y
);
}
// turn vectors' values to a typed array
var bufferPoints = [];
pointsOfIntersection.slice().forEach((p) => {
bufferPoints.push(p.x, p.y, p.z);
});
var F32A = new Float32Array(bufferPoints);
attPos.set(F32A, 0);
scene.add(mesh);