@a2f0/skyline 0.2.75 → 0.2.76

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/docs/package.md CHANGED
@@ -417,7 +417,8 @@ viewer's colour toggle, published only because every model module is; they are n
417
417
  API and may change or disappear in any release.
418
418
 
419
419
  `@a2f0/skyline/models/building-kit` exposes the same plan runs and mesh builder
420
- used by this repository, for adding buildings. Coordinate and model assumptions
420
+ used by this repository, for adding buildings. Its `silhouetteEdges`, which `finish` outlines
421
+ with, matches the vendored engine's `EdgesGeometry` and follows it; it is not a stable API. Coordinate and model assumptions
421
422
  are in [the geography notes](skyline-geography.md). Asset paths are also available
422
423
  through `@a2f0/skyline/assets/<path>` for build tooling that resolves package
423
424
  files directly; bundlers do not automatically copy that directory.
@@ -79,6 +79,13 @@ export interface Builder {
79
79
  prism(target: BatchData, runs: Plan, heights: number[], options?: PrismOptions): Vec2[];
80
80
  finish(options: FinishOptions): BuildingModel;
81
81
  }
82
+ /**
83
+ * Appends to `out` the line segments three's EdgesGeometry (r186) makes from non-indexed
84
+ * triangle `positions` at `thresholdAngle` degrees: the same segments, in the same order, with
85
+ * the same values. It finds shared vertices and edges by number where three builds a string
86
+ * for every vertex and edge, which cost most of a model's finishing time.
87
+ */
88
+ export declare function silhouetteEdges(positions: ArrayLike<number>, thresholdAngle: number, out: number[]): void;
82
89
  export declare function createBuilder(name: string, buildingId: string, { gradient }?: {
83
90
  gradient?: number[];
84
91
  }): Builder;
@@ -71,6 +71,88 @@ export function inside(polygon, [x, z]) {
71
71
  }
72
72
  return result;
73
73
  }
74
+ /**
75
+ * Appends to `out` the line segments three's EdgesGeometry (r186) makes from non-indexed
76
+ * triangle `positions` at `thresholdAngle` degrees: the same segments, in the same order, with
77
+ * the same values. It finds shared vertices and edges by number where three builds a string
78
+ * for every vertex and edge, which cost most of a model's finishing time.
79
+ */
80
+ export function silhouetteEdges(positions, thresholdAngle, out) {
81
+ // As three: vertices meet when their coordinates round to the same ten-thousandth.
82
+ const precision = Math.pow(10, 4);
83
+ const thresholdDot = Math.cos(deg * thresholdAngle);
84
+ const count = positions.length / 3;
85
+ // Rounded x, then y, then z, to the vertex's number; an edge's key is its two numbers, which
86
+ // stays exact while count squared is below 2^53.
87
+ const vertices = new Map();
88
+ let next = 0;
89
+ const vertex = (index) => {
90
+ const x = Math.round(positions[index * 3] * precision), y = Math.round(positions[index * 3 + 1] * precision), z = Math.round(positions[index * 3 + 2] * precision);
91
+ let ys = vertices.get(x);
92
+ if (!ys)
93
+ vertices.set(x, ys = new Map());
94
+ let zs = ys.get(y);
95
+ if (!zs)
96
+ ys.set(y, zs = new Map());
97
+ let id = zs.get(z);
98
+ if (id === undefined)
99
+ zs.set(z, id = next++);
100
+ return id;
101
+ };
102
+ // Insertion order is three's: a key's first sighting, and a matched edge stays as null.
103
+ const edges = new Map();
104
+ const corners = [0, 0, 0], ids = [0, 0, 0];
105
+ const push = (index) => out.push(positions[index * 3], positions[index * 3 + 1], positions[index * 3 + 2]);
106
+ for (let i = 0; i < count; i += 3) {
107
+ corners[0] = i;
108
+ corners[1] = i + 1;
109
+ corners[2] = i + 2;
110
+ // Triangle.getNormal: (c - b) x (a - b), normalized, or zero.
111
+ const ax = positions[i * 3], ay = positions[i * 3 + 1], az = positions[i * 3 + 2];
112
+ const bx = positions[i * 3 + 3], by = positions[i * 3 + 4], bz = positions[i * 3 + 5];
113
+ const cx = positions[i * 3 + 6], cy = positions[i * 3 + 7], cz = positions[i * 3 + 8];
114
+ const tx = cx - bx, ty = cy - by, tz = cz - bz, ux = ax - bx, uy = ay - by, uz = az - bz;
115
+ let nx = ty * uz - tz * uy, ny = tz * ux - tx * uz, nz = tx * uy - ty * ux;
116
+ const lengthSq = nx * nx + ny * ny + nz * nz;
117
+ if (lengthSq > 0) {
118
+ const scale = 1 / Math.sqrt(lengthSq);
119
+ nx *= scale;
120
+ ny *= scale;
121
+ nz *= scale;
122
+ }
123
+ else
124
+ nx = ny = nz = 0;
125
+ ids[0] = vertex(i);
126
+ ids[1] = vertex(i + 1);
127
+ ids[2] = vertex(i + 2);
128
+ // Degenerate triangles add no edges.
129
+ if (ids[0] === ids[1] || ids[1] === ids[2] || ids[2] === ids[0])
130
+ continue;
131
+ for (let j = 0; j < 3; j += 1) {
132
+ const k = (j + 1) % 3;
133
+ const key = ids[j] * count + ids[k], reverse = ids[k] * count + ids[j];
134
+ const sibling = edges.get(reverse);
135
+ if (sibling) {
136
+ // A shared edge shows where its two faces turn by more than the threshold.
137
+ if (nx * sibling.x + ny * sibling.y + nz * sibling.z <= thresholdDot) {
138
+ push(corners[j]);
139
+ push(corners[k]);
140
+ }
141
+ edges.set(reverse, null);
142
+ }
143
+ else if (!edges.has(key)) {
144
+ edges.set(key, { start: corners[j], end: corners[k], x: nx, y: ny, z: nz });
145
+ }
146
+ }
147
+ }
148
+ // Edges with no partner are the open boundary, and always show.
149
+ for (const edge of edges.values()) {
150
+ if (!edge)
151
+ continue;
152
+ push(edge.start);
153
+ push(edge.end);
154
+ }
155
+ }
74
156
  // A model under construction: toon materials, batched geometry, and the standard model
75
157
  // API once finished. Solids are closed by default. A caller may omit only faces another
76
158
  // surface covers, by name, and each omission is kept in building.userData.omitted so a
@@ -335,7 +417,9 @@ export function createBuilder(name, buildingId, { gradient = [70, 135, 200, 255]
335
417
  building.add(mesh);
336
418
  return [data, mesh];
337
419
  }));
338
- const lines = outlines.flatMap((data) => [...new THREE.EdgesGeometry(meshes.get(data).geometry, threshold).getAttribute("position").array]);
420
+ const lines = [];
421
+ for (const data of outlines)
422
+ silhouetteEdges(meshes.get(data).geometry.getAttribute("position").array, threshold, lines);
339
423
  const edgeGeometry = new THREE.BufferGeometry();
340
424
  edgeGeometry.setAttribute("position", new THREE.Float32BufferAttribute(lines, 3));
341
425
  const edges = new THREE.LineSegments(edgeGeometry, new THREE.LineBasicMaterial({ color: 0xcccccc, transparent: true, opacity }));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@a2f0/skyline",
3
- "version": "0.2.75",
3
+ "version": "0.2.76",
4
4
  "description": "Interactive Chicago skyline viewer and reusable Three.js building models",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
@@ -71,6 +71,88 @@ export function inside(polygon, [x, z]) {
71
71
  }
72
72
  return result;
73
73
  }
74
+ /**
75
+ * Appends to `out` the line segments three's EdgesGeometry (r186) makes from non-indexed
76
+ * triangle `positions` at `thresholdAngle` degrees: the same segments, in the same order, with
77
+ * the same values. It finds shared vertices and edges by number where three builds a string
78
+ * for every vertex and edge, which cost most of a model's finishing time.
79
+ */
80
+ export function silhouetteEdges(positions, thresholdAngle, out) {
81
+ // As three: vertices meet when their coordinates round to the same ten-thousandth.
82
+ const precision = Math.pow(10, 4);
83
+ const thresholdDot = Math.cos(deg * thresholdAngle);
84
+ const count = positions.length / 3;
85
+ // Rounded x, then y, then z, to the vertex's number; an edge's key is its two numbers, which
86
+ // stays exact while count squared is below 2^53.
87
+ const vertices = new Map();
88
+ let next = 0;
89
+ const vertex = (index) => {
90
+ const x = Math.round(positions[index * 3] * precision), y = Math.round(positions[index * 3 + 1] * precision), z = Math.round(positions[index * 3 + 2] * precision);
91
+ let ys = vertices.get(x);
92
+ if (!ys)
93
+ vertices.set(x, ys = new Map());
94
+ let zs = ys.get(y);
95
+ if (!zs)
96
+ ys.set(y, zs = new Map());
97
+ let id = zs.get(z);
98
+ if (id === undefined)
99
+ zs.set(z, id = next++);
100
+ return id;
101
+ };
102
+ // Insertion order is three's: a key's first sighting, and a matched edge stays as null.
103
+ const edges = new Map();
104
+ const corners = [0, 0, 0], ids = [0, 0, 0];
105
+ const push = (index) => out.push(positions[index * 3], positions[index * 3 + 1], positions[index * 3 + 2]);
106
+ for (let i = 0; i < count; i += 3) {
107
+ corners[0] = i;
108
+ corners[1] = i + 1;
109
+ corners[2] = i + 2;
110
+ // Triangle.getNormal: (c - b) x (a - b), normalized, or zero.
111
+ const ax = positions[i * 3], ay = positions[i * 3 + 1], az = positions[i * 3 + 2];
112
+ const bx = positions[i * 3 + 3], by = positions[i * 3 + 4], bz = positions[i * 3 + 5];
113
+ const cx = positions[i * 3 + 6], cy = positions[i * 3 + 7], cz = positions[i * 3 + 8];
114
+ const tx = cx - bx, ty = cy - by, tz = cz - bz, ux = ax - bx, uy = ay - by, uz = az - bz;
115
+ let nx = ty * uz - tz * uy, ny = tz * ux - tx * uz, nz = tx * uy - ty * ux;
116
+ const lengthSq = nx * nx + ny * ny + nz * nz;
117
+ if (lengthSq > 0) {
118
+ const scale = 1 / Math.sqrt(lengthSq);
119
+ nx *= scale;
120
+ ny *= scale;
121
+ nz *= scale;
122
+ }
123
+ else
124
+ nx = ny = nz = 0;
125
+ ids[0] = vertex(i);
126
+ ids[1] = vertex(i + 1);
127
+ ids[2] = vertex(i + 2);
128
+ // Degenerate triangles add no edges.
129
+ if (ids[0] === ids[1] || ids[1] === ids[2] || ids[2] === ids[0])
130
+ continue;
131
+ for (let j = 0; j < 3; j += 1) {
132
+ const k = (j + 1) % 3;
133
+ const key = ids[j] * count + ids[k], reverse = ids[k] * count + ids[j];
134
+ const sibling = edges.get(reverse);
135
+ if (sibling) {
136
+ // A shared edge shows where its two faces turn by more than the threshold.
137
+ if (nx * sibling.x + ny * sibling.y + nz * sibling.z <= thresholdDot) {
138
+ push(corners[j]);
139
+ push(corners[k]);
140
+ }
141
+ edges.set(reverse, null);
142
+ }
143
+ else if (!edges.has(key)) {
144
+ edges.set(key, { start: corners[j], end: corners[k], x: nx, y: ny, z: nz });
145
+ }
146
+ }
147
+ }
148
+ // Edges with no partner are the open boundary, and always show.
149
+ for (const edge of edges.values()) {
150
+ if (!edge)
151
+ continue;
152
+ push(edge.start);
153
+ push(edge.end);
154
+ }
155
+ }
74
156
  // A model under construction: toon materials, batched geometry, and the standard model
75
157
  // API once finished. Solids are closed by default. A caller may omit only faces another
76
158
  // surface covers, by name, and each omission is kept in building.userData.omitted so a
@@ -335,7 +417,9 @@ export function createBuilder(name, buildingId, { gradient = [70, 135, 200, 255]
335
417
  building.add(mesh);
336
418
  return [data, mesh];
337
419
  }));
338
- const lines = outlines.flatMap((data) => [...new THREE.EdgesGeometry(meshes.get(data).geometry, threshold).getAttribute("position").array]);
420
+ const lines = [];
421
+ for (const data of outlines)
422
+ silhouetteEdges(meshes.get(data).geometry.getAttribute("position").array, threshold, lines);
339
423
  const edgeGeometry = new THREE.BufferGeometry();
340
424
  edgeGeometry.setAttribute("position", new THREE.Float32BufferAttribute(lines, 3));
341
425
  const edges = new THREE.LineSegments(edgeGeometry, new THREE.LineBasicMaterial({ color: 0xcccccc, transparent: true, opacity }));