I guess it is an easy task, but I have flaws in my code or thoughts.
I want to create a polygon from some points that are all in the same plane and they are ordered (around the polygon) in a points array. But, depending on the points, the polygon does not look like it should.
I have created a minimal repo with just the code that builds the scene with the poly and an orthographic camera to view it.
npm i
npm run dev:test
I guess the important part is the way I build the polygon. Here is the code
function createPoly() {
const points: readonly Vector3[] = [
new Vector3(0, 0, 0),
new Vector3(4, 0, 0),
// new Vector3(3, 0, 2), // <- works
new Vector3(4, 0, 4),
// new Vector3(2, 0, 3), // does not work
new Vector3(0, 0, 4)
];
let polyShape = new Shape(
points.map((v3) => {
const v2 = new Vector2(v3.x, v3.z);
return v2;
})
);
const polyGeometry = new ShapeGeometry(polyShape);
polyGeometry.setAttribute(
"position",
new Float32BufferAttribute(
points.map(v3 => [v3.x,v3.y,v3.z]).flat(),3)
)
const polyMesh = new Mesh(
polyGeometry,
new MeshBasicMaterial({
color: 0x999999,
side: DoubleSide })
)
return polyMesh;
}
The 4 points of the square (x/z plane) work. But adding (2/0/3) is one example that behaves differently than I think.
(3/0/2)
works, as long as I don't also add (2/0/3)
.
Please give me a hint to my thought/code flaw.