Skip to content

Commit

Permalink
feat(ExtrudeGeometry): add anchor point offset (#452)
Browse files Browse the repository at this point in the history
  • Loading branch information
Codeboy-cn authored Oct 22, 2024
1 parent bf14c61 commit fc2303d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 15 deletions.
69 changes: 60 additions & 9 deletions packages/geometry/ExtrudeGeometry/ExtrudeGeometry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GeometryBase, Vector2, VertexAttributeName } from "@orillusion/core";
import { GeometryBase, Vector2, Vector3, VertexAttributeName } from "@orillusion/core";
import { Shape2D } from "./Shape2D";
import { ShapeUtils } from "./ShapeUtils";

Expand All @@ -11,6 +11,7 @@ export type ExtrudeGeometryArgs = {
bevelSize?: number;
bevelOffset?: number;
bevelSegments?: number;
anchorPoint?: Vector3;
}

export class ExtrudeGeometry extends GeometryBase {
Expand All @@ -28,12 +29,47 @@ export class ExtrudeGeometry extends GeometryBase {
}
}

protected getExtractPointsAndBoundingSize(shapes: Shape2D[], options: ExtrudeGeometryArgs): { BoundingSize: { min: Vector3; max: Vector3; }, ShapePoints: { shape: Vector2[]; holes: Vector2[][]; }[] } {
const depth: number = options.depth !== undefined ? options.depth : 1;
const curveSegments: number = options.curveSegments !== undefined ? options.curveSegments : 12;
let minPoint: Vector3 = new Vector3(Infinity, Infinity, depth > 0 ? 0 : depth);
let maxPoint: Vector3 = new Vector3(-Infinity, -Infinity, depth < 0 ? 0 : depth);
let shapePointsArray = [];
for (let shape of this.shapes) {
const shapePoints = shape.extractPoints(curveSegments);
shapePointsArray.push(shapePoints);
let vertices = shapePoints.shape;
for (let i = 0; i < vertices.length; i++) {
const p = vertices[i];
if (p.x < minPoint.x)
minPoint.x = p.x;
if (p.y < minPoint.y)
minPoint.y = p.y;

if (p.x > maxPoint.x)
maxPoint.x = p.x;
if (p.y > maxPoint.y)
maxPoint.y = p.y;
}
}

return {
ShapePoints: shapePointsArray,
BoundingSize: { min: minPoint, max: maxPoint },
}
}

protected buildGeometry(options: ExtrudeGeometryArgs) {
this.verticesArray = [];
this.uvArray = [];

let anchorPoint: Vector3 = options.anchorPoint !== undefined ? options.anchorPoint : new Vector3(0, 0, 0.5);
const result = this.getExtractPointsAndBoundingSize(this.shapes, options);
const offsetSize = result.BoundingSize.min.subtract(result.BoundingSize.max);
offsetSize.multiply(anchorPoint, offsetSize);

for (let shape of this.shapes) {
this.addShape(shape, options);
this.addShape(shape, options, offsetSize);
}

const indices = new Uint32Array(this.verticesArray.length / 3);
Expand Down Expand Up @@ -61,7 +97,7 @@ export class ExtrudeGeometry extends GeometryBase {
});
}

protected addShape(shape: Shape2D, options: ExtrudeGeometryArgs) {
protected addShape(shape: Shape2D, options: ExtrudeGeometryArgs, offsetSize: Vector3) {
const verticesArray = this.verticesArray;
const uvArray = this.uvArray;
const self = this;
Expand Down Expand Up @@ -89,6 +125,21 @@ export class ExtrudeGeometry extends GeometryBase {
const holes = shapePoints.holes;
const reverse = !ShapeUtils.isClockWise(vertices);

for (let i = 0; i < vertices.length; i++) {
const p = vertices[i];
p.x += offsetSize.x;
p.y += offsetSize.y;
}
for (let i = 0; i < holes.length; i++) {
const holeV = holes[i];
for (let j = 0; j < holeV.length; j++) {
const p = holeV[j];
p.x += offsetSize.x;
p.y += offsetSize.y;
}
}


if (reverse) {
vertices = vertices.reverse();
for (let i = 0; i < holes.length; i++) {
Expand Down Expand Up @@ -137,29 +188,29 @@ export class ExtrudeGeometry extends GeometryBase {

for (let i = 0; i < contour.length; i++) {
const vert = this.scalePoint2(contour[i], contourMovements[i], bs);
v(vert.x, vert.y, - z);
v(vert.x, vert.y, - z + offsetSize.z);
}

for (let h = 0, hl = holes.length; h < hl; h++) {
const ahole = holes[h];
oneHoleMovements = holesMovements[h];
for (let i = 0; i < ahole.length; i++) {
const vert = this.scalePoint2(ahole[i], oneHoleMovements[i], bs);
v(vert.x, vert.y, - z);
v(vert.x, vert.y, - z + offsetSize.z);
}
}
}

const bs = bevelSize + bevelOffset;
for (let i = 0; i < vlen; i++) {
const vert = bevelEnabled ? this.scalePoint2(vertices[i], verticesMovements[i], bs) : vertices[i];
v(vert.x, vert.y, 0);
v(vert.x, vert.y, 0 + offsetSize.z);
}

for (let s = 1; s <= steps; s++) {
for (let i = 0; i < vlen; i++) {
const vert = bevelEnabled ? this.scalePoint2(vertices[i], verticesMovements[i], bs) : vertices[i];
v(vert.x, vert.y, depth / steps * s);
v(vert.x, vert.y, depth / steps * s + offsetSize.z);
}
}

Expand All @@ -170,15 +221,15 @@ export class ExtrudeGeometry extends GeometryBase {

for (let i = 0, il = contour.length; i < il; i++) {
const vert = this.scalePoint2(contour[i], contourMovements[i], bs);
v(vert.x, vert.y, depth + z);
v(vert.x, vert.y, depth + z + offsetSize.z);
}

for (let h = 0, hl = holes.length; h < hl; h++) {
const ahole = holes[h];
oneHoleMovements = holesMovements[h];
for (let i = 0, il = ahole.length; i < il; i++) {
const vert = this.scalePoint2(ahole[i], oneHoleMovements[i], bs);
v(vert.x, vert.y, depth + z);
v(vert.x, vert.y, depth + z + offsetSize.z);
}
}
}
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.4",
"version": "0.2.5",
"author": "Orillusion",
"description": "Orillusion Geometry Plugin",
"main": "./dist/geometry.umd.js",
Expand Down
9 changes: 4 additions & 5 deletions samples/geometry/Sample_TextGeometry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Engine3D, View3D, Scene3D, CameraUtil, AtmosphericComponent, webGPUContext, HoverCameraController, Object3D, DirectLight, KelvinUtil, LitMaterial, MeshRenderer } from "@orillusion/core";
import { Engine3D, View3D, Scene3D, CameraUtil, AtmosphericComponent, webGPUContext, HoverCameraController, Object3D, DirectLight, KelvinUtil, LitMaterial, MeshRenderer, Vector3 } from "@orillusion/core";
import { TextGeometry, FontParser } from "@orillusion/geometry";
import { Graphic3D } from "@orillusion/graphic";

Expand Down Expand Up @@ -32,9 +32,10 @@ class Sample_TextGeometry {
mr.geometry = new TextGeometry("Hello, Orillusion!", {
font: font, // required
fontSize: 16, // required
depth: 2.5,
depth: 5,
steps: 1,
bevelEnabled: false
bevelEnabled: false,
anchorPoint: new Vector3(0.5, 0.5, 0.5),
});

let mats = [];
Expand All @@ -44,8 +45,6 @@ class Sample_TextGeometry {
}
mr.materials = mats;

obj.x = mr.geometry.bounds.size.x * -0.5;

scene.addChild(obj);
}

Expand Down

0 comments on commit fc2303d

Please sign in to comment.