From dc45666ed7a34826ebf7fd5bccf6cd7f3247b476 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Wed, 15 Feb 2023 17:06:27 +0100 Subject: [PATCH] Wireframe, and fixes to mitering --- scene/lobby.pr | 26 +++++++++++++++- src/turtle.ts | 84 +++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 94 insertions(+), 16 deletions(-) diff --git a/scene/lobby.pr b/scene/lobby.pr index 43bbdf2..1d05234 100644 --- a/scene/lobby.pr +++ b/scene/lobby.pr @@ -35,7 +35,28 @@ >>>>> - + + + >>> + +@ { + const r = p.multiply(this.templateScale).applyRotationQuaternion(q).addInPlace(pos); + const d = pointDistance(r, thisDir, miterPlane); + if (d !== null) r.addInPlace(thisDir.scale(d)); + paths[i].push(r); + }); + } else { + this.templatePath.forEach((p, i) => { + const r = p.multiply(this.templateScale).applyRotationQuaternion(q).addInPlace(pos); + paths[i].push(r); + }); } - this.templatePath.forEach((p, i) => { - paths[i].push(p.multiply(this.templateScale).applyRotationQuaternion(q).addInPlace(pos)); - }); - directions.push(q); } }; +// Adapted from Ray.intersectsPlane +function pointDistance(origin: Vector3, direction: Vector3, plane: Plane): number | null { + const result1 = Vector3.Dot(plane.normal, direction); + if (Math.abs(result1) < 1e-6) return null; // direction parallel to plane + const result2 = Vector3.Dot(plane.normal, origin); + return (-plane.d - result2) / result1; +} + const D2R = Math.PI / 180; export class TurtleVM extends Cat.VM { container: Mesh; meshes: Mesh[] = []; + wireframe = false; counter = 0; sideOrientation = Mesh.DEFAULTSIDE; @@ -121,10 +145,37 @@ export class TurtleVM extends Cat.VM { throw new Error('todo'); } - const m = new Mesh(this.container.name + this.counter++); - // TODO: this.sideOrientation + if (this.wireframe) { + this.emitWireframe(); + } else { + this.emitSolid(); + } - const vertexData = new VertexData(); + this.pen.up(); + } + + emitWireframe() { + const lines: Vector3[][] = []; + const paths = this.pen.paths!; + const pathCount = paths.length; + const stepCount = paths[0].length; + + for (let pathIndex = 1; pathIndex < pathCount; pathIndex++) { + for (let stepIndex = 1; stepIndex < stepCount; stepIndex++) { + lines.push([paths[pathIndex - 1][stepIndex - 1], + paths[pathIndex - 1][stepIndex], + paths[pathIndex][stepIndex], + paths[pathIndex][stepIndex - 1]]); + } + } + + const meshName = this.container.name + this.counter++; + const ls = MeshBuilder.CreateLineSystem(meshName, { lines }, null); + ls.parent = this.container; + this.meshes.push(ls); + } + + emitSolid() { const positions: number[] = []; const indices: number[] = []; const normals: number[] = []; @@ -139,6 +190,10 @@ export class TurtleVM extends Cat.VM { const uTotal: number[] = []; const vTotal: number[] = []; + const capVerts = paths.flatMap(p => p[0].asArray()); + console.log(capVerts); + console.log(earcut(capVerts, void 0, 3).map(i => paths[0][i].asArray())); + for (let pathIndex = 0; pathIndex < pathCount; pathIndex++) { uTotal.push(0); us.push([]); @@ -167,7 +222,8 @@ export class TurtleVM extends Cat.VM { const pointIndex = positions.length / 3; const p = paths[pathIndex][stepIndex]; positions.push(... p.asArray()); - uvs.push((p.x - p.z) / 1.2, (p.y) / 0.9); + uvs.push((p.x - p.z), (p.y)); + // uvs.push((p.x - p.z) / 1.2, (p.y) / 0.9); // uvs.push(us[pathIndex][stepIndex] / uTotal[pathIndex], // vs[stepIndex][pathIndex] / vTotal[stepIndex]); return pointIndex; @@ -195,18 +251,15 @@ export class TurtleVM extends Cat.VM { VertexData.ComputeNormals(positions, indices, normals); VertexData._ComputeSides(this.sideOrientation, positions, indices, normals, uvs); + + const vertexData = new VertexData(); vertexData.positions = new Float32Array(positions); vertexData.indices = new Int32Array(indices); vertexData.normals = new Float32Array(normals); vertexData.uvs = new Float32Array(uvs); + const m = new Mesh(this.container.name + this.counter++); vertexData.applyToMesh(m); - - // const m = MeshBuilder.CreateRibbon(this.container.name + this.counter++, { - // pathArray: this.pen.paths!, - // sideOrientation: this.sideOrientation, - // }); - m.parent = this.container; this.meshes.push(m); } @@ -275,6 +328,7 @@ export const TurtlePrimitives: Cat.Environment = Object.assign({}, Cat 'PenUp'() { this.penUp(false); return []; }, 'Close'() { this.penUp(true); return []; }, + 'SetWireframe'(b) { this.wireframe = b as boolean; return []; }, 'SetSmooth'(b) { this.smooth = b as boolean; return []; }, 'SetMiter'(b) { this.miter = b as boolean; return []; }, 'SetSideOrientation'(s) {