Creating a polygon (wall) in 3D space using Three.JS has been quite challenging for me.
I need to add thickness to my wall, which is why I want to utilize ExtrudeGeometry
. Additionally, the number of points can vary from three to infinite, ruling out a simple BoxGeometry
.
Take a look at this example: https://i.sstatic.net/QB4sj.png
Assuming the given points are always on the same plane and moving clockwise from bottom left, here are the coordinates of the green points:
[
[1, 0, -1],
[1, 3, -1],
[5, 3, -4],
[5, 0, -4],
]
The challenge arises because Shape
works with 2D points. In a 2D plane, it looks like this:
const shape = new THREE.Shape();
shape.lineTo(0, 0);
shape.lineTo(0, 5);
shape.lineTo(5, 5);
shape.lineTo(5, 0);
const geometry = new THREE.ExtrudeGeometry(shape, { steps: 1, depth: 1 });
const material = new THREE.MeshStandardMaterial();
const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
The current solution involves rotating the points to a 2D plane, creating the shape, and then rotating the geometry back. This method feels overly complicated, especially if rotation about multiple axes is required.
Is there a simpler way to position a polygon in 3D space based on a set of three or more points?