diff --git a/packages/geometry/ExtrudeGeometry/ExtrudeGeometry.ts b/packages/geometry/ExtrudeGeometry/ExtrudeGeometry.ts index 308799fc..b65de838 100644 --- a/packages/geometry/ExtrudeGeometry/ExtrudeGeometry.ts +++ b/packages/geometry/ExtrudeGeometry/ExtrudeGeometry.ts @@ -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"; @@ -11,6 +11,7 @@ export type ExtrudeGeometryArgs = { bevelSize?: number; bevelOffset?: number; bevelSegments?: number; + anchorPoint?: Vector3; } export class ExtrudeGeometry extends GeometryBase { @@ -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); @@ -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; @@ -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++) { @@ -137,7 +188,7 @@ 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++) { @@ -145,7 +196,7 @@ export class ExtrudeGeometry extends GeometryBase { 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); } } } @@ -153,13 +204,13 @@ export class ExtrudeGeometry extends GeometryBase { 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); } } @@ -170,7 +221,7 @@ 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++) { @@ -178,7 +229,7 @@ export class ExtrudeGeometry extends GeometryBase { 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); } } } diff --git a/packages/geometry/package.json b/packages/geometry/package.json index 05218faf..df1a74a7 100644 --- a/packages/geometry/package.json +++ b/packages/geometry/package.json @@ -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", diff --git a/samples/geometry/Sample_TextGeometry.ts b/samples/geometry/Sample_TextGeometry.ts index ac7014c4..cc9bbc03 100644 --- a/samples/geometry/Sample_TextGeometry.ts +++ b/samples/geometry/Sample_TextGeometry.ts @@ -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"; @@ -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 = []; @@ -44,8 +45,6 @@ class Sample_TextGeometry { } mr.materials = mats; - obj.x = mr.geometry.bounds.size.x * -0.5; - scene.addChild(obj); }