Skip to content

Commit

Permalink
fix(extrudeGeometry): fix shape in CCW order
Browse files Browse the repository at this point in the history
  • Loading branch information
lslzl3000 committed Nov 10, 2024
1 parent 8e0354a commit a7c8d01
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 24 deletions.
63 changes: 41 additions & 22 deletions packages/geometry/ExtrudeGeometry/ExtrudeGeometry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ export class ExtrudeGeometry extends GeometryBase {
}

const indices = new Uint32Array(this.verticesArray.length / 3);
for (let i = 0; i < indices.length; i++) {
for (let i = 0; i < indices.length; i+=3) {
indices[i] = i;
indices[i + 1] = i + 2;
indices[i + 2] = i + 1;
}

this.setIndices(indices);
Expand Down Expand Up @@ -123,7 +125,6 @@ export class ExtrudeGeometry extends GeometryBase {
const shapePoints = shape.extractPoints(curveSegments);
let vertices = shapePoints.shape;
const holes = shapePoints.holes;
const reverse = !ShapeUtils.isClockWise(vertices);

for (let i = 0; i < vertices.length; i++) {
const p = vertices[i];
Expand All @@ -139,14 +140,16 @@ export class ExtrudeGeometry extends GeometryBase {
}
}


// make sure shape is CW
const reverse = !ShapeUtils.isClockWise(vertices);
if (reverse) {
vertices = vertices.reverse();
for (let i = 0; i < holes.length; i++) {
const hole = holes[i];
if (ShapeUtils.isClockWise(hole)) {
holes[i] = hole.reverse();
}
}
// make sure hole is CCW
for (let i = 0; i < holes.length; i++) {
const hole = holes[i];
if (ShapeUtils.isClockWise(hole)) {
holes[i] = hole.reverse();
}
}

Expand Down Expand Up @@ -288,7 +291,7 @@ export class ExtrudeGeometry extends GeometryBase {
self.addGroup(start, verticesArray.length / 3 - start, 1);
}

function sidewalls(contour: Vector2[], layeroffset: number) {
function sidewalls(contour: Vector2[], layeroffset: number, isClockWise = false) {
let i = contour.length;
while (--i >= 0) {
const j = i;
Expand All @@ -303,7 +306,7 @@ export class ExtrudeGeometry extends GeometryBase {
c = layeroffset + k + slen2,
d = layeroffset + j + slen2;

f4(a, b, c, d);
f4(a, b, c, d, isClockWise);
}
}
}
Expand All @@ -314,10 +317,16 @@ export class ExtrudeGeometry extends GeometryBase {
placeholder.push(z);
}

function f3(a: number, b: number, c: number) {
addVertex(a);
addVertex(b);
addVertex(c);
function f3(a: number, b: number, c: number, isClockWise = false) {
if(isClockWise){
addVertex(a);
addVertex(b);
addVertex(c);
}else{
addVertex(a);
addVertex(c);
addVertex(b);
}

const nextIndex = verticesArray.length / 3;
const uvs = WorldUVGenerator.generateTopUV(verticesArray, nextIndex - 3, nextIndex - 2, nextIndex - 1);
Expand All @@ -327,14 +336,24 @@ export class ExtrudeGeometry extends GeometryBase {
addUV(uvs[2]);
}

function f4(a: number, b: number, c: number, d: number) {
addVertex(a);
addVertex(b);
addVertex(d);

addVertex(b);
addVertex(c);
addVertex(d);
function f4(a: number, b: number, c: number, d: number, isClockWise = false) {
if(isClockWise){
addVertex(a);
addVertex(b);
addVertex(d);

addVertex(b);
addVertex(c);
addVertex(d);
}else{
addVertex(a);
addVertex(d);
addVertex(b);

addVertex(b);
addVertex(d);
addVertex(c);
}

const nextIndex = verticesArray.length / 3;
const uvs = WorldUVGenerator.generateSideWallUV(verticesArray, nextIndex - 6, nextIndex - 3, nextIndex - 2, nextIndex - 1);
Expand Down
2 changes: 1 addition & 1 deletion packages/geometry/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@orillusion/geometry",
"version": "0.2.6",
"version": "0.2.7",
"author": "Orillusion",
"description": "Orillusion Geometry Plugin",
"main": "./dist/geometry.umd.js",
Expand Down
1 change: 0 additions & 1 deletion packages/geometry/text/TextGeometry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ export class TextGeometry extends ExtrudeGeometry {
break;
}
}

if (shape2D) {
this.shapes.push(shape2D);
}
Expand Down

0 comments on commit a7c8d01

Please sign in to comment.