@adatechnology/game-shapes-three 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +44 -2
- package/dist/index.d.ts +69 -0
- package/dist/index.global.js +1795 -0
- package/dist/index.global.js.map +1 -0
- package/dist/index.js +299 -0
- package/dist/index.js.map +1 -0
- package/package.json +57 -3
|
@@ -0,0 +1,1795 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var GameShapesThree = (() => {
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/index.ts
|
|
22
|
+
var src_exports = {};
|
|
23
|
+
__export(src_exports, {
|
|
24
|
+
SHAPE_GEOMETRY_TYPES: () => SHAPE_GEOMETRY_TYPES,
|
|
25
|
+
createMaterials: () => createMaterials,
|
|
26
|
+
mergeByMaterial: () => mergeByMaterial,
|
|
27
|
+
shapeGeometries: () => shapeGeometries,
|
|
28
|
+
toGeometry: () => toGeometry,
|
|
29
|
+
toObject3D: () => toObject3D
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
// src/geometry.ts
|
|
33
|
+
var UV_BEFORE_NORMAL = /* @__PURE__ */ new Set(["LatheGeometry", "ExtrudeGeometry"]);
|
|
34
|
+
var INDEX_FIRST = /* @__PURE__ */ new Set(["BoxGeometry", "CylinderGeometry", "ConeGeometry", "SphereGeometry", "PlaneGeometry", "CircleGeometry", "RingGeometry", "LatheGeometry", "TorusGeometry"]);
|
|
35
|
+
var isPrimitive = (value) => typeof value.type === "string";
|
|
36
|
+
function threeParameters(THREE, p) {
|
|
37
|
+
if (p.type === "LatheGeometry") {
|
|
38
|
+
const points = p.parameters.points.map(({ x, y }) => new THREE.Vector2(x, y));
|
|
39
|
+
return { ...p.parameters, points };
|
|
40
|
+
}
|
|
41
|
+
return { ...p.parameters };
|
|
42
|
+
}
|
|
43
|
+
function toGeometry(THREE, source, options = {}) {
|
|
44
|
+
const copy = options.copy !== false;
|
|
45
|
+
const primitive = isPrimitive(source) ? source : null;
|
|
46
|
+
const ThreeClass = primitive && primitive.type !== "BufferGeometry" ? THREE[primitive.type] : void 0;
|
|
47
|
+
const geometry = ThreeClass ? Reflect.construct(THREE.BufferGeometry, [], ThreeClass) : new THREE.BufferGeometry();
|
|
48
|
+
if (primitive && ThreeClass) {
|
|
49
|
+
Object.assign(geometry, { type: primitive.type, parameters: threeParameters(THREE, primitive) });
|
|
50
|
+
}
|
|
51
|
+
const attribute = (array, size) => copy ? new THREE.Float32BufferAttribute(array, size) : new THREE.BufferAttribute(array, size);
|
|
52
|
+
const index = source.indices;
|
|
53
|
+
const setIndex = () => {
|
|
54
|
+
if (!index) return;
|
|
55
|
+
geometry.setIndex(copy ? index instanceof Uint32Array ? new THREE.Uint32BufferAttribute(index, 1) : new THREE.Uint16BufferAttribute(index, 1) : new THREE.BufferAttribute(index, 1));
|
|
56
|
+
};
|
|
57
|
+
const type = primitive?.type ?? "BufferGeometry";
|
|
58
|
+
if (INDEX_FIRST.has(type)) setIndex();
|
|
59
|
+
geometry.setAttribute("position", attribute(source.positions, 3));
|
|
60
|
+
if (UV_BEFORE_NORMAL.has(type)) {
|
|
61
|
+
geometry.setAttribute("uv", attribute(source.uvs, 2));
|
|
62
|
+
geometry.setAttribute("normal", attribute(source.normals, 3));
|
|
63
|
+
} else {
|
|
64
|
+
geometry.setAttribute("normal", attribute(source.normals, 3));
|
|
65
|
+
geometry.setAttribute("uv", attribute(source.uvs, 2));
|
|
66
|
+
}
|
|
67
|
+
if (!primitive && source.colors) geometry.setAttribute("color", attribute(source.colors, 3));
|
|
68
|
+
if (!INDEX_FIRST.has(type)) setIndex();
|
|
69
|
+
if (primitive) for (const group of primitive.groups) geometry.addGroup(group.start, group.count, group.materialIndex);
|
|
70
|
+
return geometry;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// ../game-shapes/dist/chunk-R4RY23RH.js
|
|
74
|
+
function colorHex(color2, fallback = 16777215) {
|
|
75
|
+
if (color2 === void 0) return fallback;
|
|
76
|
+
if (typeof color2 === "number") return Math.floor(color2);
|
|
77
|
+
const match = /^#([0-9a-fA-F]{6})$/.exec(color2);
|
|
78
|
+
if (!match) throw new Error(`game-shapes: colour "${color2}" is not 0xRRGGBB or #rrggbb`);
|
|
79
|
+
return parseInt(match[1], 16);
|
|
80
|
+
}
|
|
81
|
+
function canonicalJson(value) {
|
|
82
|
+
if (value === null) return "null";
|
|
83
|
+
if (typeof value === "number") return Object.is(value, -0) ? "-0" : Number.isFinite(value) ? String(value) : JSON.stringify(String(value));
|
|
84
|
+
if (typeof value === "string") return JSON.stringify(value);
|
|
85
|
+
if (typeof value === "boolean") return value ? "true" : "false";
|
|
86
|
+
if (typeof value === "function") throw new Error("game-shapes: a function cannot be part of a shape (register it as a generator)");
|
|
87
|
+
if (ArrayBuffer.isView(value)) return canonicalJson(Array.from(value));
|
|
88
|
+
if (Array.isArray(value)) return `[${value.map((item) => item === void 0 ? "null" : canonicalJson(item)).join(",")}]`;
|
|
89
|
+
if (typeof value === "object") {
|
|
90
|
+
const record = value;
|
|
91
|
+
const keys = Object.keys(record).filter((key) => record[key] !== void 0).sort();
|
|
92
|
+
return `{${keys.map((key) => `${JSON.stringify(key)}:${canonicalJson(record[key])}`).join(",")}}`;
|
|
93
|
+
}
|
|
94
|
+
return "null";
|
|
95
|
+
}
|
|
96
|
+
function normalMatrix(me) {
|
|
97
|
+
const n11 = me[0], n21 = me[1], n31 = me[2];
|
|
98
|
+
const n12 = me[4], n22 = me[5], n32 = me[6];
|
|
99
|
+
const n13 = me[8], n23 = me[9], n33 = me[10];
|
|
100
|
+
const t11 = n33 * n22 - n32 * n23;
|
|
101
|
+
const t12 = n32 * n13 - n33 * n12;
|
|
102
|
+
const t13 = n23 * n12 - n22 * n13;
|
|
103
|
+
const det = n11 * t11 + n21 * t12 + n31 * t13;
|
|
104
|
+
if (det === 0) return [0, 0, 0, 0, 0, 0, 0, 0, 0];
|
|
105
|
+
const detInv = 1 / det;
|
|
106
|
+
const te = [
|
|
107
|
+
t11 * detInv,
|
|
108
|
+
(n31 * n23 - n33 * n21) * detInv,
|
|
109
|
+
(n32 * n21 - n31 * n22) * detInv,
|
|
110
|
+
t12 * detInv,
|
|
111
|
+
(n33 * n11 - n31 * n13) * detInv,
|
|
112
|
+
(n31 * n12 - n32 * n11) * detInv,
|
|
113
|
+
t13 * detInv,
|
|
114
|
+
(n21 * n13 - n23 * n11) * detInv,
|
|
115
|
+
(n22 * n11 - n21 * n12) * detInv
|
|
116
|
+
];
|
|
117
|
+
let tmp = te[1];
|
|
118
|
+
te[1] = te[3];
|
|
119
|
+
te[3] = tmp;
|
|
120
|
+
tmp = te[2];
|
|
121
|
+
te[2] = te[6];
|
|
122
|
+
te[6] = tmp;
|
|
123
|
+
tmp = te[5];
|
|
124
|
+
te[5] = te[7];
|
|
125
|
+
te[7] = tmp;
|
|
126
|
+
return te;
|
|
127
|
+
}
|
|
128
|
+
function transformPoint(e, x, y, z) {
|
|
129
|
+
const w = 1 / (e[3] * x + e[7] * y + e[11] * z + e[15]);
|
|
130
|
+
return [(e[0] * x + e[4] * y + e[8] * z + e[12]) * w, (e[1] * x + e[5] * y + e[9] * z + e[13]) * w, (e[2] * x + e[6] * y + e[10] * z + e[14]) * w];
|
|
131
|
+
}
|
|
132
|
+
function needsUint32(array) {
|
|
133
|
+
for (let i = array.length - 1; i >= 0; --i) if (array[i] >= 65535) return true;
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
var indexArray = (indices) => needsUint32(indices) ? new Uint32Array(indices) : new Uint16Array(indices);
|
|
137
|
+
function pack(type, parameters, a) {
|
|
138
|
+
return {
|
|
139
|
+
type,
|
|
140
|
+
parameters,
|
|
141
|
+
positions: new Float32Array(a.vertices),
|
|
142
|
+
normals: new Float32Array(a.normals),
|
|
143
|
+
uvs: new Float32Array(a.uvs),
|
|
144
|
+
...a.indices ? { indices: indexArray(a.indices) } : {},
|
|
145
|
+
groups: a.groups
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
var normalize3 = (x, y, z) => {
|
|
149
|
+
const length = Math.sqrt(x * x + y * y + z * z);
|
|
150
|
+
const s = 1 / (length || 1);
|
|
151
|
+
return [x * s, y * s, z * s];
|
|
152
|
+
};
|
|
153
|
+
function box(width = 1, height = 1, depth = 1, widthSegments = 1, heightSegments = 1, depthSegments = 1) {
|
|
154
|
+
const parameters = { width, height, depth, widthSegments, heightSegments, depthSegments };
|
|
155
|
+
widthSegments = Math.floor(widthSegments);
|
|
156
|
+
heightSegments = Math.floor(heightSegments);
|
|
157
|
+
depthSegments = Math.floor(depthSegments);
|
|
158
|
+
const indices = [];
|
|
159
|
+
const vertices = [];
|
|
160
|
+
const normals = [];
|
|
161
|
+
const uvs = [];
|
|
162
|
+
const groups = [];
|
|
163
|
+
let numberOfVertices = 0;
|
|
164
|
+
let groupStart = 0;
|
|
165
|
+
const buildPlane = (u, v, w, udir, vdir, width2, height2, depth2, gridX, gridY, materialIndex) => {
|
|
166
|
+
const segmentWidth = width2 / gridX;
|
|
167
|
+
const segmentHeight = height2 / gridY;
|
|
168
|
+
const widthHalf = width2 / 2;
|
|
169
|
+
const heightHalf = height2 / 2;
|
|
170
|
+
const depthHalf = depth2 / 2;
|
|
171
|
+
const gridX1 = gridX + 1;
|
|
172
|
+
const gridY1 = gridY + 1;
|
|
173
|
+
let vertexCounter = 0;
|
|
174
|
+
let groupCount = 0;
|
|
175
|
+
const vector = { x: 0, y: 0, z: 0 };
|
|
176
|
+
for (let iy = 0; iy < gridY1; iy++) {
|
|
177
|
+
const y = iy * segmentHeight - heightHalf;
|
|
178
|
+
for (let ix = 0; ix < gridX1; ix++) {
|
|
179
|
+
const x = ix * segmentWidth - widthHalf;
|
|
180
|
+
vector[u] = x * udir;
|
|
181
|
+
vector[v] = y * vdir;
|
|
182
|
+
vector[w] = depthHalf;
|
|
183
|
+
vertices.push(vector.x, vector.y, vector.z);
|
|
184
|
+
vector[u] = 0;
|
|
185
|
+
vector[v] = 0;
|
|
186
|
+
vector[w] = depth2 > 0 ? 1 : -1;
|
|
187
|
+
normals.push(vector.x, vector.y, vector.z);
|
|
188
|
+
uvs.push(ix / gridX);
|
|
189
|
+
uvs.push(1 - iy / gridY);
|
|
190
|
+
vertexCounter += 1;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
for (let iy = 0; iy < gridY; iy++) {
|
|
194
|
+
for (let ix = 0; ix < gridX; ix++) {
|
|
195
|
+
const a = numberOfVertices + ix + gridX1 * iy;
|
|
196
|
+
const b = numberOfVertices + ix + gridX1 * (iy + 1);
|
|
197
|
+
const c = numberOfVertices + (ix + 1) + gridX1 * (iy + 1);
|
|
198
|
+
const d = numberOfVertices + (ix + 1) + gridX1 * iy;
|
|
199
|
+
indices.push(a, b, d);
|
|
200
|
+
indices.push(b, c, d);
|
|
201
|
+
groupCount += 6;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
groups.push({ start: groupStart, count: groupCount, materialIndex });
|
|
205
|
+
groupStart += groupCount;
|
|
206
|
+
numberOfVertices += vertexCounter;
|
|
207
|
+
};
|
|
208
|
+
buildPlane("z", "y", "x", -1, -1, depth, height, width, depthSegments, heightSegments, 0);
|
|
209
|
+
buildPlane("z", "y", "x", 1, -1, depth, height, -width, depthSegments, heightSegments, 1);
|
|
210
|
+
buildPlane("x", "z", "y", 1, 1, width, depth, height, widthSegments, depthSegments, 2);
|
|
211
|
+
buildPlane("x", "z", "y", 1, -1, width, depth, -height, widthSegments, depthSegments, 3);
|
|
212
|
+
buildPlane("x", "y", "z", 1, -1, width, height, depth, widthSegments, heightSegments, 4);
|
|
213
|
+
buildPlane("x", "y", "z", -1, -1, width, height, -depth, widthSegments, heightSegments, 5);
|
|
214
|
+
return pack("BoxGeometry", parameters, { vertices, normals, uvs, indices, groups });
|
|
215
|
+
}
|
|
216
|
+
function cylinderArrays(radiusTop, radiusBottom, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength) {
|
|
217
|
+
radialSegments = Math.floor(radialSegments);
|
|
218
|
+
heightSegments = Math.floor(heightSegments);
|
|
219
|
+
const indices = [];
|
|
220
|
+
const vertices = [];
|
|
221
|
+
const normals = [];
|
|
222
|
+
const uvs = [];
|
|
223
|
+
const groups = [];
|
|
224
|
+
let index = 0;
|
|
225
|
+
const indexArray2 = [];
|
|
226
|
+
const halfHeight = height / 2;
|
|
227
|
+
let groupStart = 0;
|
|
228
|
+
const generateTorso = () => {
|
|
229
|
+
let groupCount = 0;
|
|
230
|
+
const slope = (radiusBottom - radiusTop) / height;
|
|
231
|
+
for (let y = 0; y <= heightSegments; y++) {
|
|
232
|
+
const indexRow = [];
|
|
233
|
+
const v = y / heightSegments;
|
|
234
|
+
const radius = v * (radiusBottom - radiusTop) + radiusTop;
|
|
235
|
+
for (let x = 0; x <= radialSegments; x++) {
|
|
236
|
+
const u = x / radialSegments;
|
|
237
|
+
const theta = u * thetaLength + thetaStart;
|
|
238
|
+
const sinTheta = Math.sin(theta);
|
|
239
|
+
const cosTheta = Math.cos(theta);
|
|
240
|
+
vertices.push(radius * sinTheta, -v * height + halfHeight, radius * cosTheta);
|
|
241
|
+
const n = normalize3(sinTheta, slope, cosTheta);
|
|
242
|
+
normals.push(n[0], n[1], n[2]);
|
|
243
|
+
uvs.push(u, 1 - v);
|
|
244
|
+
indexRow.push(index++);
|
|
245
|
+
}
|
|
246
|
+
indexArray2.push(indexRow);
|
|
247
|
+
}
|
|
248
|
+
for (let x = 0; x < radialSegments; x++) {
|
|
249
|
+
for (let y = 0; y < heightSegments; y++) {
|
|
250
|
+
const a = indexArray2[y][x];
|
|
251
|
+
const b = indexArray2[y + 1][x];
|
|
252
|
+
const c = indexArray2[y + 1][x + 1];
|
|
253
|
+
const d = indexArray2[y][x + 1];
|
|
254
|
+
indices.push(a, b, d);
|
|
255
|
+
indices.push(b, c, d);
|
|
256
|
+
groupCount += 6;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
groups.push({ start: groupStart, count: groupCount, materialIndex: 0 });
|
|
260
|
+
groupStart += groupCount;
|
|
261
|
+
};
|
|
262
|
+
const generateCap = (top) => {
|
|
263
|
+
const centerIndexStart = index;
|
|
264
|
+
let groupCount = 0;
|
|
265
|
+
const radius = top === true ? radiusTop : radiusBottom;
|
|
266
|
+
const sign2 = top === true ? 1 : -1;
|
|
267
|
+
for (let x = 1; x <= radialSegments; x++) {
|
|
268
|
+
vertices.push(0, halfHeight * sign2, 0);
|
|
269
|
+
normals.push(0, sign2, 0);
|
|
270
|
+
uvs.push(0.5, 0.5);
|
|
271
|
+
index++;
|
|
272
|
+
}
|
|
273
|
+
const centerIndexEnd = index;
|
|
274
|
+
for (let x = 0; x <= radialSegments; x++) {
|
|
275
|
+
const u = x / radialSegments;
|
|
276
|
+
const theta = u * thetaLength + thetaStart;
|
|
277
|
+
const cosTheta = Math.cos(theta);
|
|
278
|
+
const sinTheta = Math.sin(theta);
|
|
279
|
+
vertices.push(radius * sinTheta, halfHeight * sign2, radius * cosTheta);
|
|
280
|
+
normals.push(0, sign2, 0);
|
|
281
|
+
uvs.push(cosTheta * 0.5 + 0.5, sinTheta * 0.5 * sign2 + 0.5);
|
|
282
|
+
index++;
|
|
283
|
+
}
|
|
284
|
+
for (let x = 0; x < radialSegments; x++) {
|
|
285
|
+
const c = centerIndexStart + x;
|
|
286
|
+
const i = centerIndexEnd + x;
|
|
287
|
+
if (top === true) indices.push(i, i + 1, c);
|
|
288
|
+
else indices.push(i + 1, i, c);
|
|
289
|
+
groupCount += 3;
|
|
290
|
+
}
|
|
291
|
+
groups.push({ start: groupStart, count: groupCount, materialIndex: top === true ? 1 : 2 });
|
|
292
|
+
groupStart += groupCount;
|
|
293
|
+
};
|
|
294
|
+
generateTorso();
|
|
295
|
+
if (openEnded === false) {
|
|
296
|
+
if (radiusTop > 0) generateCap(true);
|
|
297
|
+
if (radiusBottom > 0) generateCap(false);
|
|
298
|
+
}
|
|
299
|
+
return { vertices, normals, uvs, indices, groups };
|
|
300
|
+
}
|
|
301
|
+
function cylinder(radiusTop = 1, radiusBottom = 1, height = 1, radialSegments = 32, heightSegments = 1, openEnded = false, thetaStart = 0, thetaLength = Math.PI * 2) {
|
|
302
|
+
const parameters = { radiusTop, radiusBottom, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength };
|
|
303
|
+
return pack("CylinderGeometry", parameters, cylinderArrays(radiusTop, radiusBottom, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength));
|
|
304
|
+
}
|
|
305
|
+
function cone(radius = 1, height = 1, radialSegments = 32, heightSegments = 1, openEnded = false, thetaStart = 0, thetaLength = Math.PI * 2) {
|
|
306
|
+
const parameters = { radius, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength };
|
|
307
|
+
return pack("ConeGeometry", parameters, cylinderArrays(0, radius, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength));
|
|
308
|
+
}
|
|
309
|
+
function sphere(radius = 1, widthSegments = 32, heightSegments = 16, phiStart = 0, phiLength = Math.PI * 2, thetaStart = 0, thetaLength = Math.PI) {
|
|
310
|
+
const parameters = { radius, widthSegments, heightSegments, phiStart, phiLength, thetaStart, thetaLength };
|
|
311
|
+
widthSegments = Math.max(3, Math.floor(widthSegments));
|
|
312
|
+
heightSegments = Math.max(2, Math.floor(heightSegments));
|
|
313
|
+
const thetaEnd = Math.min(thetaStart + thetaLength, Math.PI);
|
|
314
|
+
let index = 0;
|
|
315
|
+
const grid = [];
|
|
316
|
+
const indices = [];
|
|
317
|
+
const vertices = [];
|
|
318
|
+
const normals = [];
|
|
319
|
+
const uvs = [];
|
|
320
|
+
for (let iy = 0; iy <= heightSegments; iy++) {
|
|
321
|
+
const verticesRow = [];
|
|
322
|
+
const v = iy / heightSegments;
|
|
323
|
+
let uOffset = 0;
|
|
324
|
+
if (iy === 0 && thetaStart === 0) uOffset = 0.5 / widthSegments;
|
|
325
|
+
else if (iy === heightSegments && thetaEnd === Math.PI) uOffset = -0.5 / widthSegments;
|
|
326
|
+
for (let ix = 0; ix <= widthSegments; ix++) {
|
|
327
|
+
const u = ix / widthSegments;
|
|
328
|
+
const x = -radius * Math.cos(phiStart + u * phiLength) * Math.sin(thetaStart + v * thetaLength);
|
|
329
|
+
const y = radius * Math.cos(thetaStart + v * thetaLength);
|
|
330
|
+
const z = radius * Math.sin(phiStart + u * phiLength) * Math.sin(thetaStart + v * thetaLength);
|
|
331
|
+
vertices.push(x, y, z);
|
|
332
|
+
const n = normalize3(x, y, z);
|
|
333
|
+
normals.push(n[0], n[1], n[2]);
|
|
334
|
+
uvs.push(u + uOffset, 1 - v);
|
|
335
|
+
verticesRow.push(index++);
|
|
336
|
+
}
|
|
337
|
+
grid.push(verticesRow);
|
|
338
|
+
}
|
|
339
|
+
for (let iy = 0; iy < heightSegments; iy++) {
|
|
340
|
+
for (let ix = 0; ix < widthSegments; ix++) {
|
|
341
|
+
const a = grid[iy][ix + 1];
|
|
342
|
+
const b = grid[iy][ix];
|
|
343
|
+
const c = grid[iy + 1][ix];
|
|
344
|
+
const d = grid[iy + 1][ix + 1];
|
|
345
|
+
if (iy !== 0 || thetaStart > 0) indices.push(a, b, d);
|
|
346
|
+
if (iy !== heightSegments - 1 || thetaEnd < Math.PI) indices.push(b, c, d);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
return pack("SphereGeometry", parameters, { vertices, normals, uvs, indices, groups: [] });
|
|
350
|
+
}
|
|
351
|
+
function plane(width = 1, height = 1, widthSegments = 1, heightSegments = 1) {
|
|
352
|
+
const parameters = { width, height, widthSegments, heightSegments };
|
|
353
|
+
const widthHalf = width / 2;
|
|
354
|
+
const heightHalf = height / 2;
|
|
355
|
+
const gridX = Math.floor(widthSegments);
|
|
356
|
+
const gridY = Math.floor(heightSegments);
|
|
357
|
+
const gridX1 = gridX + 1;
|
|
358
|
+
const gridY1 = gridY + 1;
|
|
359
|
+
const segmentWidth = width / gridX;
|
|
360
|
+
const segmentHeight = height / gridY;
|
|
361
|
+
const indices = [];
|
|
362
|
+
const vertices = [];
|
|
363
|
+
const normals = [];
|
|
364
|
+
const uvs = [];
|
|
365
|
+
for (let iy = 0; iy < gridY1; iy++) {
|
|
366
|
+
const y = iy * segmentHeight - heightHalf;
|
|
367
|
+
for (let ix = 0; ix < gridX1; ix++) {
|
|
368
|
+
const x = ix * segmentWidth - widthHalf;
|
|
369
|
+
vertices.push(x, -y, 0);
|
|
370
|
+
normals.push(0, 0, 1);
|
|
371
|
+
uvs.push(ix / gridX);
|
|
372
|
+
uvs.push(1 - iy / gridY);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
for (let iy = 0; iy < gridY; iy++) {
|
|
376
|
+
for (let ix = 0; ix < gridX; ix++) {
|
|
377
|
+
const a = ix + gridX1 * iy;
|
|
378
|
+
const b = ix + gridX1 * (iy + 1);
|
|
379
|
+
const c = ix + 1 + gridX1 * (iy + 1);
|
|
380
|
+
const d = ix + 1 + gridX1 * iy;
|
|
381
|
+
indices.push(a, b, d);
|
|
382
|
+
indices.push(b, c, d);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
return pack("PlaneGeometry", parameters, { vertices, normals, uvs, indices, groups: [] });
|
|
386
|
+
}
|
|
387
|
+
function circle(radius = 1, segments = 32, thetaStart = 0, thetaLength = Math.PI * 2) {
|
|
388
|
+
const parameters = { radius, segments, thetaStart, thetaLength };
|
|
389
|
+
segments = Math.max(3, segments);
|
|
390
|
+
const indices = [];
|
|
391
|
+
const vertices = [];
|
|
392
|
+
const normals = [];
|
|
393
|
+
const uvs = [];
|
|
394
|
+
vertices.push(0, 0, 0);
|
|
395
|
+
normals.push(0, 0, 1);
|
|
396
|
+
uvs.push(0.5, 0.5);
|
|
397
|
+
for (let s = 0, i = 3; s <= segments; s++, i += 3) {
|
|
398
|
+
const segment = thetaStart + s / segments * thetaLength;
|
|
399
|
+
vertices.push(radius * Math.cos(segment), radius * Math.sin(segment), 0);
|
|
400
|
+
normals.push(0, 0, 1);
|
|
401
|
+
uvs.push((vertices[i] / radius + 1) / 2, (vertices[i + 1] / radius + 1) / 2);
|
|
402
|
+
}
|
|
403
|
+
for (let i = 1; i <= segments; i++) indices.push(i, i + 1, 0);
|
|
404
|
+
return pack("CircleGeometry", parameters, { vertices, normals, uvs, indices, groups: [] });
|
|
405
|
+
}
|
|
406
|
+
function ring(innerRadius = 0.5, outerRadius = 1, thetaSegments = 32, phiSegments = 1, thetaStart = 0, thetaLength = Math.PI * 2) {
|
|
407
|
+
const parameters = { innerRadius, outerRadius, thetaSegments, phiSegments, thetaStart, thetaLength };
|
|
408
|
+
thetaSegments = Math.max(3, thetaSegments);
|
|
409
|
+
phiSegments = Math.max(1, phiSegments);
|
|
410
|
+
const indices = [];
|
|
411
|
+
const vertices = [];
|
|
412
|
+
const normals = [];
|
|
413
|
+
const uvs = [];
|
|
414
|
+
let radius = innerRadius;
|
|
415
|
+
const radiusStep = (outerRadius - innerRadius) / phiSegments;
|
|
416
|
+
for (let j = 0; j <= phiSegments; j++) {
|
|
417
|
+
for (let i = 0; i <= thetaSegments; i++) {
|
|
418
|
+
const segment = thetaStart + i / thetaSegments * thetaLength;
|
|
419
|
+
const x = radius * Math.cos(segment);
|
|
420
|
+
const y = radius * Math.sin(segment);
|
|
421
|
+
vertices.push(x, y, 0);
|
|
422
|
+
normals.push(0, 0, 1);
|
|
423
|
+
uvs.push((x / outerRadius + 1) / 2, (y / outerRadius + 1) / 2);
|
|
424
|
+
}
|
|
425
|
+
radius += radiusStep;
|
|
426
|
+
}
|
|
427
|
+
for (let j = 0; j < phiSegments; j++) {
|
|
428
|
+
const thetaSegmentLevel = j * (thetaSegments + 1);
|
|
429
|
+
for (let i = 0; i < thetaSegments; i++) {
|
|
430
|
+
const segment = i + thetaSegmentLevel;
|
|
431
|
+
const a = segment;
|
|
432
|
+
const b = segment + thetaSegments + 1;
|
|
433
|
+
const c = segment + thetaSegments + 2;
|
|
434
|
+
const d = segment + 1;
|
|
435
|
+
indices.push(a, b, d);
|
|
436
|
+
indices.push(b, c, d);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
return pack("RingGeometry", parameters, { vertices, normals, uvs, indices, groups: [] });
|
|
440
|
+
}
|
|
441
|
+
function lathe(points, segments = 12, phiStart = 0, phiLength = Math.PI * 2) {
|
|
442
|
+
const parameters = { points: points.map(([x, y]) => ({ x, y })), segments, phiStart, phiLength };
|
|
443
|
+
segments = Math.floor(segments);
|
|
444
|
+
phiLength = Math.max(0, Math.min(Math.PI * 2, phiLength));
|
|
445
|
+
const indices = [];
|
|
446
|
+
const vertices = [];
|
|
447
|
+
const uvs = [];
|
|
448
|
+
const initNormals = [];
|
|
449
|
+
const normals = [];
|
|
450
|
+
const inverseSegments = 1 / segments;
|
|
451
|
+
const normal = { x: 0, y: 0, z: 0 };
|
|
452
|
+
const curNormal = { x: 0, y: 0, z: 0 };
|
|
453
|
+
const prevNormal = { x: 0, y: 0, z: 0 };
|
|
454
|
+
let dx = 0;
|
|
455
|
+
let dy = 0;
|
|
456
|
+
const last = points.length - 1;
|
|
457
|
+
const px = (j) => points[j][0];
|
|
458
|
+
const py = (j) => points[j][1];
|
|
459
|
+
const setNormalized = () => {
|
|
460
|
+
const n = normalize3(normal.x, normal.y, normal.z);
|
|
461
|
+
normal.x = n[0];
|
|
462
|
+
normal.y = n[1];
|
|
463
|
+
normal.z = n[2];
|
|
464
|
+
};
|
|
465
|
+
for (let j = 0; j <= last; j++) {
|
|
466
|
+
if (j === 0) {
|
|
467
|
+
dx = px(j + 1) - px(j);
|
|
468
|
+
dy = py(j + 1) - py(j);
|
|
469
|
+
normal.x = dy * 1;
|
|
470
|
+
normal.y = -dx;
|
|
471
|
+
normal.z = dy * 0;
|
|
472
|
+
prevNormal.x = normal.x;
|
|
473
|
+
prevNormal.y = normal.y;
|
|
474
|
+
prevNormal.z = normal.z;
|
|
475
|
+
setNormalized();
|
|
476
|
+
initNormals.push(normal.x, normal.y, normal.z);
|
|
477
|
+
} else if (j === last) {
|
|
478
|
+
initNormals.push(prevNormal.x, prevNormal.y, prevNormal.z);
|
|
479
|
+
} else {
|
|
480
|
+
dx = px(j + 1) - px(j);
|
|
481
|
+
dy = py(j + 1) - py(j);
|
|
482
|
+
normal.x = dy * 1;
|
|
483
|
+
normal.y = -dx;
|
|
484
|
+
normal.z = dy * 0;
|
|
485
|
+
curNormal.x = normal.x;
|
|
486
|
+
curNormal.y = normal.y;
|
|
487
|
+
curNormal.z = normal.z;
|
|
488
|
+
normal.x += prevNormal.x;
|
|
489
|
+
normal.y += prevNormal.y;
|
|
490
|
+
normal.z += prevNormal.z;
|
|
491
|
+
setNormalized();
|
|
492
|
+
initNormals.push(normal.x, normal.y, normal.z);
|
|
493
|
+
prevNormal.x = curNormal.x;
|
|
494
|
+
prevNormal.y = curNormal.y;
|
|
495
|
+
prevNormal.z = curNormal.z;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
for (let i = 0; i <= segments; i++) {
|
|
499
|
+
const phi = phiStart + i * inverseSegments * phiLength;
|
|
500
|
+
const sin = Math.sin(phi);
|
|
501
|
+
const cos = Math.cos(phi);
|
|
502
|
+
for (let j = 0; j <= last; j++) {
|
|
503
|
+
vertices.push(px(j) * sin, py(j), px(j) * cos);
|
|
504
|
+
uvs.push(i / segments, j / (points.length - 1));
|
|
505
|
+
const x = initNormals[3 * j + 0] * sin;
|
|
506
|
+
const y = initNormals[3 * j + 1];
|
|
507
|
+
const z = initNormals[3 * j + 0] * cos;
|
|
508
|
+
normals.push(x, y, z);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
for (let i = 0; i < segments; i++) {
|
|
512
|
+
for (let j = 0; j < points.length - 1; j++) {
|
|
513
|
+
const base = j + i * points.length;
|
|
514
|
+
const a = base;
|
|
515
|
+
const b = base + points.length;
|
|
516
|
+
const c = base + points.length + 1;
|
|
517
|
+
const d = base + 1;
|
|
518
|
+
indices.push(a, b, d);
|
|
519
|
+
indices.push(c, d, b);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
return pack("LatheGeometry", parameters, { vertices, normals, uvs, indices, groups: [] });
|
|
523
|
+
}
|
|
524
|
+
function torus(radius = 1, tube = 0.4, radialSegments = 12, tubularSegments = 48, arc = Math.PI * 2) {
|
|
525
|
+
const parameters = { radius, tube, radialSegments, tubularSegments, arc };
|
|
526
|
+
radialSegments = Math.floor(radialSegments);
|
|
527
|
+
tubularSegments = Math.floor(tubularSegments);
|
|
528
|
+
const indices = [];
|
|
529
|
+
const vertices = [];
|
|
530
|
+
const normals = [];
|
|
531
|
+
const uvs = [];
|
|
532
|
+
for (let j = 0; j <= radialSegments; j++) {
|
|
533
|
+
for (let i = 0; i <= tubularSegments; i++) {
|
|
534
|
+
const u = i / tubularSegments * arc;
|
|
535
|
+
const v = j / radialSegments * Math.PI * 2;
|
|
536
|
+
const x = (radius + tube * Math.cos(v)) * Math.cos(u);
|
|
537
|
+
const y = (radius + tube * Math.cos(v)) * Math.sin(u);
|
|
538
|
+
const z = tube * Math.sin(v);
|
|
539
|
+
vertices.push(x, y, z);
|
|
540
|
+
const cx = radius * Math.cos(u);
|
|
541
|
+
const cy = radius * Math.sin(u);
|
|
542
|
+
const n = normalize3(x - cx, y - cy, z - 0);
|
|
543
|
+
normals.push(n[0], n[1], n[2]);
|
|
544
|
+
uvs.push(i / tubularSegments);
|
|
545
|
+
uvs.push(j / radialSegments);
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
for (let j = 1; j <= radialSegments; j++) {
|
|
549
|
+
for (let i = 1; i <= tubularSegments; i++) {
|
|
550
|
+
const a = (tubularSegments + 1) * j + i - 1;
|
|
551
|
+
const b = (tubularSegments + 1) * (j - 1) + i - 1;
|
|
552
|
+
const c = (tubularSegments + 1) * (j - 1) + i;
|
|
553
|
+
const d = (tubularSegments + 1) * j + i;
|
|
554
|
+
indices.push(a, b, d);
|
|
555
|
+
indices.push(b, c, d);
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
return pack("TorusGeometry", parameters, { vertices, normals, uvs, indices, groups: [] });
|
|
559
|
+
}
|
|
560
|
+
function difference(size, subtract) {
|
|
561
|
+
const [w, h, d] = size;
|
|
562
|
+
const hw = w / 2;
|
|
563
|
+
const hh = h / 2;
|
|
564
|
+
const hd = d / 2;
|
|
565
|
+
const holes = subtract.map((hole) => ({ x0: Math.max(-hw, Math.min(hole.min[0], hole.max[0])), x1: Math.min(hw, Math.max(hole.min[0], hole.max[0])), y0: Math.max(-hh, Math.min(hole.min[1], hole.max[1])), y1: Math.min(hh, Math.max(hole.min[1], hole.max[1])) })).filter((hole) => hole.x1 > hole.x0 && hole.y1 > hole.y0);
|
|
566
|
+
const cuts = (lo, hi, values) => [.../* @__PURE__ */ new Set([lo, hi, ...values.filter((v) => v > lo && v < hi)])].sort((a, b) => a - b);
|
|
567
|
+
const xs = cuts(-hw, hw, holes.flatMap((hole) => [hole.x0, hole.x1]));
|
|
568
|
+
const ys = cuts(-hh, hh, holes.flatMap((hole) => [hole.y0, hole.y1]));
|
|
569
|
+
const nx = xs.length - 1;
|
|
570
|
+
const ny = ys.length - 1;
|
|
571
|
+
const solid = (i, j) => {
|
|
572
|
+
if (i < 0 || j < 0 || i >= nx || j >= ny) return false;
|
|
573
|
+
const cx = (xs[i] + xs[i + 1]) / 2;
|
|
574
|
+
const cy = (ys[j] + ys[j + 1]) / 2;
|
|
575
|
+
return !holes.some((hole) => cx > hole.x0 && cx < hole.x1 && cy > hole.y0 && cy < hole.y1);
|
|
576
|
+
};
|
|
577
|
+
const positions = [];
|
|
578
|
+
const normals = [];
|
|
579
|
+
const uvs = [];
|
|
580
|
+
const indices = [];
|
|
581
|
+
const quad = (corners, normal, uv) => {
|
|
582
|
+
const base = positions.length / 3;
|
|
583
|
+
for (let k = 0; k < 4; k++) {
|
|
584
|
+
positions.push(...corners[k]);
|
|
585
|
+
normals.push(...normal);
|
|
586
|
+
uvs.push(...uv[k]);
|
|
587
|
+
}
|
|
588
|
+
indices.push(base, base + 1, base + 2, base, base + 2, base + 3);
|
|
589
|
+
};
|
|
590
|
+
const fu = (x) => (x + hw) / w;
|
|
591
|
+
const fv = (y) => (y + hh) / h;
|
|
592
|
+
for (let j = 0; j < ny; j++) {
|
|
593
|
+
for (let i = 0; i < nx; i++) {
|
|
594
|
+
if (!solid(i, j)) continue;
|
|
595
|
+
const x0 = xs[i];
|
|
596
|
+
const x1 = xs[i + 1];
|
|
597
|
+
const y0 = ys[j];
|
|
598
|
+
const y1 = ys[j + 1];
|
|
599
|
+
quad([[x0, y0, hd], [x1, y0, hd], [x1, y1, hd], [x0, y1, hd]], [0, 0, 1], [[fu(x0), fv(y0)], [fu(x1), fv(y0)], [fu(x1), fv(y1)], [fu(x0), fv(y1)]]);
|
|
600
|
+
quad([[x1, y0, -hd], [x0, y0, -hd], [x0, y1, -hd], [x1, y1, -hd]], [0, 0, -1], [[1 - fu(x1), fv(y0)], [1 - fu(x0), fv(y0)], [1 - fu(x0), fv(y1)], [1 - fu(x1), fv(y1)]]);
|
|
601
|
+
const zu = (z) => (z + hd) / d;
|
|
602
|
+
if (!solid(i - 1, j)) quad([[x0, y0, -hd], [x0, y0, hd], [x0, y1, hd], [x0, y1, -hd]], [-1, 0, 0], [[zu(-hd), fv(y0)], [zu(hd), fv(y0)], [zu(hd), fv(y1)], [zu(-hd), fv(y1)]]);
|
|
603
|
+
if (!solid(i + 1, j)) quad([[x1, y0, hd], [x1, y0, -hd], [x1, y1, -hd], [x1, y1, hd]], [1, 0, 0], [[zu(hd), fv(y0)], [zu(-hd), fv(y0)], [zu(-hd), fv(y1)], [zu(hd), fv(y1)]]);
|
|
604
|
+
if (!solid(i, j - 1)) quad([[x0, y0, -hd], [x1, y0, -hd], [x1, y0, hd], [x0, y0, hd]], [0, -1, 0], [[fu(x0), zu(-hd)], [fu(x1), zu(-hd)], [fu(x1), zu(hd)], [fu(x0), zu(hd)]]);
|
|
605
|
+
if (!solid(i, j + 1)) quad([[x0, y1, hd], [x1, y1, hd], [x1, y1, -hd], [x0, y1, -hd]], [0, 1, 0], [[fu(x0), zu(hd)], [fu(x1), zu(hd)], [fu(x1), zu(-hd)], [fu(x0), zu(-hd)]]);
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
return {
|
|
609
|
+
type: "BufferGeometry",
|
|
610
|
+
parameters: { size: [w, h, d], subtract: holes.map((hole) => ({ min: [hole.x0, hole.y0], max: [hole.x1, hole.y1] })) },
|
|
611
|
+
positions: new Float32Array(positions),
|
|
612
|
+
normals: new Float32Array(normals),
|
|
613
|
+
uvs: new Float32Array(uvs),
|
|
614
|
+
indices: indexArray(indices),
|
|
615
|
+
groups: []
|
|
616
|
+
};
|
|
617
|
+
}
|
|
618
|
+
var Earcut = {
|
|
619
|
+
triangulate: function(data, holeIndices, dim = 2) {
|
|
620
|
+
const hasHoles = holeIndices && holeIndices.length;
|
|
621
|
+
const outerLen = hasHoles ? holeIndices[0] * dim : data.length;
|
|
622
|
+
let outerNode = linkedList(data, 0, outerLen, dim, true);
|
|
623
|
+
const triangles = [];
|
|
624
|
+
if (!outerNode || outerNode.next === outerNode.prev) return triangles;
|
|
625
|
+
let minX, minY, maxX, maxY, x, y, invSize;
|
|
626
|
+
if (hasHoles) outerNode = eliminateHoles(data, holeIndices, outerNode, dim);
|
|
627
|
+
if (data.length > 80 * dim) {
|
|
628
|
+
minX = maxX = data[0];
|
|
629
|
+
minY = maxY = data[1];
|
|
630
|
+
for (let i = dim; i < outerLen; i += dim) {
|
|
631
|
+
x = data[i];
|
|
632
|
+
y = data[i + 1];
|
|
633
|
+
if (x < minX) minX = x;
|
|
634
|
+
if (y < minY) minY = y;
|
|
635
|
+
if (x > maxX) maxX = x;
|
|
636
|
+
if (y > maxY) maxY = y;
|
|
637
|
+
}
|
|
638
|
+
invSize = Math.max(maxX - minX, maxY - minY);
|
|
639
|
+
invSize = invSize !== 0 ? 32767 / invSize : 0;
|
|
640
|
+
}
|
|
641
|
+
earcutLinked(outerNode, triangles, dim, minX, minY, invSize, 0);
|
|
642
|
+
return triangles;
|
|
643
|
+
}
|
|
644
|
+
};
|
|
645
|
+
function linkedList(data, start, end, dim, clockwise) {
|
|
646
|
+
let i, last;
|
|
647
|
+
if (clockwise === signedArea(data, start, end, dim) > 0) {
|
|
648
|
+
for (i = start; i < end; i += dim) last = insertNode(i, data[i], data[i + 1], last);
|
|
649
|
+
} else {
|
|
650
|
+
for (i = end - dim; i >= start; i -= dim) last = insertNode(i, data[i], data[i + 1], last);
|
|
651
|
+
}
|
|
652
|
+
if (last && equals(last, last.next)) {
|
|
653
|
+
removeNode(last);
|
|
654
|
+
last = last.next;
|
|
655
|
+
}
|
|
656
|
+
return last;
|
|
657
|
+
}
|
|
658
|
+
function filterPoints(start, end) {
|
|
659
|
+
if (!start) return start;
|
|
660
|
+
if (!end) end = start;
|
|
661
|
+
let p = start, again;
|
|
662
|
+
do {
|
|
663
|
+
again = false;
|
|
664
|
+
if (!p.steiner && (equals(p, p.next) || area(p.prev, p, p.next) === 0)) {
|
|
665
|
+
removeNode(p);
|
|
666
|
+
p = end = p.prev;
|
|
667
|
+
if (p === p.next) break;
|
|
668
|
+
again = true;
|
|
669
|
+
} else {
|
|
670
|
+
p = p.next;
|
|
671
|
+
}
|
|
672
|
+
} while (again || p !== end);
|
|
673
|
+
return end;
|
|
674
|
+
}
|
|
675
|
+
function earcutLinked(ear, triangles, dim, minX, minY, invSize, pass) {
|
|
676
|
+
if (!ear) return;
|
|
677
|
+
if (!pass && invSize) indexCurve(ear, minX, minY, invSize);
|
|
678
|
+
let stop = ear, prev, next;
|
|
679
|
+
while (ear.prev !== ear.next) {
|
|
680
|
+
prev = ear.prev;
|
|
681
|
+
next = ear.next;
|
|
682
|
+
if (invSize ? isEarHashed(ear, minX, minY, invSize) : isEar(ear)) {
|
|
683
|
+
triangles.push(prev.i / dim | 0);
|
|
684
|
+
triangles.push(ear.i / dim | 0);
|
|
685
|
+
triangles.push(next.i / dim | 0);
|
|
686
|
+
removeNode(ear);
|
|
687
|
+
ear = next.next;
|
|
688
|
+
stop = next.next;
|
|
689
|
+
continue;
|
|
690
|
+
}
|
|
691
|
+
ear = next;
|
|
692
|
+
if (ear === stop) {
|
|
693
|
+
if (!pass) {
|
|
694
|
+
earcutLinked(filterPoints(ear), triangles, dim, minX, minY, invSize, 1);
|
|
695
|
+
} else if (pass === 1) {
|
|
696
|
+
ear = cureLocalIntersections(filterPoints(ear), triangles, dim);
|
|
697
|
+
earcutLinked(ear, triangles, dim, minX, minY, invSize, 2);
|
|
698
|
+
} else if (pass === 2) {
|
|
699
|
+
splitEarcut(ear, triangles, dim, minX, minY, invSize);
|
|
700
|
+
}
|
|
701
|
+
break;
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
function isEar(ear) {
|
|
706
|
+
const a = ear.prev, b = ear, c = ear.next;
|
|
707
|
+
if (area(a, b, c) >= 0) return false;
|
|
708
|
+
const ax = a.x, bx = b.x, cx = c.x, ay = a.y, by = b.y, cy = c.y;
|
|
709
|
+
const x0 = ax < bx ? ax < cx ? ax : cx : bx < cx ? bx : cx, y0 = ay < by ? ay < cy ? ay : cy : by < cy ? by : cy, x1 = ax > bx ? ax > cx ? ax : cx : bx > cx ? bx : cx, y1 = ay > by ? ay > cy ? ay : cy : by > cy ? by : cy;
|
|
710
|
+
let p = c.next;
|
|
711
|
+
while (p !== a) {
|
|
712
|
+
if (p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 && pointInTriangle(ax, ay, bx, by, cx, cy, p.x, p.y) && area(p.prev, p, p.next) >= 0) return false;
|
|
713
|
+
p = p.next;
|
|
714
|
+
}
|
|
715
|
+
return true;
|
|
716
|
+
}
|
|
717
|
+
function isEarHashed(ear, minX, minY, invSize) {
|
|
718
|
+
const a = ear.prev, b = ear, c = ear.next;
|
|
719
|
+
if (area(a, b, c) >= 0) return false;
|
|
720
|
+
const ax = a.x, bx = b.x, cx = c.x, ay = a.y, by = b.y, cy = c.y;
|
|
721
|
+
const x0 = ax < bx ? ax < cx ? ax : cx : bx < cx ? bx : cx, y0 = ay < by ? ay < cy ? ay : cy : by < cy ? by : cy, x1 = ax > bx ? ax > cx ? ax : cx : bx > cx ? bx : cx, y1 = ay > by ? ay > cy ? ay : cy : by > cy ? by : cy;
|
|
722
|
+
const minZ = zOrder(x0, y0, minX, minY, invSize), maxZ = zOrder(x1, y1, minX, minY, invSize);
|
|
723
|
+
let p = ear.prevZ, n = ear.nextZ;
|
|
724
|
+
while (p && p.z >= minZ && n && n.z <= maxZ) {
|
|
725
|
+
if (p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 && p !== a && p !== c && pointInTriangle(ax, ay, bx, by, cx, cy, p.x, p.y) && area(p.prev, p, p.next) >= 0) return false;
|
|
726
|
+
p = p.prevZ;
|
|
727
|
+
if (n.x >= x0 && n.x <= x1 && n.y >= y0 && n.y <= y1 && n !== a && n !== c && pointInTriangle(ax, ay, bx, by, cx, cy, n.x, n.y) && area(n.prev, n, n.next) >= 0) return false;
|
|
728
|
+
n = n.nextZ;
|
|
729
|
+
}
|
|
730
|
+
while (p && p.z >= minZ) {
|
|
731
|
+
if (p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 && p !== a && p !== c && pointInTriangle(ax, ay, bx, by, cx, cy, p.x, p.y) && area(p.prev, p, p.next) >= 0) return false;
|
|
732
|
+
p = p.prevZ;
|
|
733
|
+
}
|
|
734
|
+
while (n && n.z <= maxZ) {
|
|
735
|
+
if (n.x >= x0 && n.x <= x1 && n.y >= y0 && n.y <= y1 && n !== a && n !== c && pointInTriangle(ax, ay, bx, by, cx, cy, n.x, n.y) && area(n.prev, n, n.next) >= 0) return false;
|
|
736
|
+
n = n.nextZ;
|
|
737
|
+
}
|
|
738
|
+
return true;
|
|
739
|
+
}
|
|
740
|
+
function cureLocalIntersections(start, triangles, dim) {
|
|
741
|
+
let p = start;
|
|
742
|
+
do {
|
|
743
|
+
const a = p.prev, b = p.next.next;
|
|
744
|
+
if (!equals(a, b) && intersects(a, p, p.next, b) && locallyInside(a, b) && locallyInside(b, a)) {
|
|
745
|
+
triangles.push(a.i / dim | 0);
|
|
746
|
+
triangles.push(p.i / dim | 0);
|
|
747
|
+
triangles.push(b.i / dim | 0);
|
|
748
|
+
removeNode(p);
|
|
749
|
+
removeNode(p.next);
|
|
750
|
+
p = start = b;
|
|
751
|
+
}
|
|
752
|
+
p = p.next;
|
|
753
|
+
} while (p !== start);
|
|
754
|
+
return filterPoints(p);
|
|
755
|
+
}
|
|
756
|
+
function splitEarcut(start, triangles, dim, minX, minY, invSize) {
|
|
757
|
+
let a = start;
|
|
758
|
+
do {
|
|
759
|
+
let b = a.next.next;
|
|
760
|
+
while (b !== a.prev) {
|
|
761
|
+
if (a.i !== b.i && isValidDiagonal(a, b)) {
|
|
762
|
+
let c = splitPolygon(a, b);
|
|
763
|
+
a = filterPoints(a, a.next);
|
|
764
|
+
c = filterPoints(c, c.next);
|
|
765
|
+
earcutLinked(a, triangles, dim, minX, minY, invSize, 0);
|
|
766
|
+
earcutLinked(c, triangles, dim, minX, minY, invSize, 0);
|
|
767
|
+
return;
|
|
768
|
+
}
|
|
769
|
+
b = b.next;
|
|
770
|
+
}
|
|
771
|
+
a = a.next;
|
|
772
|
+
} while (a !== start);
|
|
773
|
+
}
|
|
774
|
+
function eliminateHoles(data, holeIndices, outerNode, dim) {
|
|
775
|
+
const queue = [];
|
|
776
|
+
let i, len, start, end, list2;
|
|
777
|
+
for (i = 0, len = holeIndices.length; i < len; i++) {
|
|
778
|
+
start = holeIndices[i] * dim;
|
|
779
|
+
end = i < len - 1 ? holeIndices[i + 1] * dim : data.length;
|
|
780
|
+
list2 = linkedList(data, start, end, dim, false);
|
|
781
|
+
if (list2 === list2.next) list2.steiner = true;
|
|
782
|
+
queue.push(getLeftmost(list2));
|
|
783
|
+
}
|
|
784
|
+
queue.sort(compareX);
|
|
785
|
+
for (i = 0; i < queue.length; i++) {
|
|
786
|
+
outerNode = eliminateHole(queue[i], outerNode);
|
|
787
|
+
}
|
|
788
|
+
return outerNode;
|
|
789
|
+
}
|
|
790
|
+
function compareX(a, b) {
|
|
791
|
+
return a.x - b.x;
|
|
792
|
+
}
|
|
793
|
+
function eliminateHole(hole, outerNode) {
|
|
794
|
+
const bridge = findHoleBridge(hole, outerNode);
|
|
795
|
+
if (!bridge) {
|
|
796
|
+
return outerNode;
|
|
797
|
+
}
|
|
798
|
+
const bridgeReverse = splitPolygon(bridge, hole);
|
|
799
|
+
filterPoints(bridgeReverse, bridgeReverse.next);
|
|
800
|
+
return filterPoints(bridge, bridge.next);
|
|
801
|
+
}
|
|
802
|
+
function findHoleBridge(hole, outerNode) {
|
|
803
|
+
let p = outerNode, qx = -Infinity, m;
|
|
804
|
+
const hx = hole.x, hy = hole.y;
|
|
805
|
+
do {
|
|
806
|
+
if (hy <= p.y && hy >= p.next.y && p.next.y !== p.y) {
|
|
807
|
+
const x = p.x + (hy - p.y) * (p.next.x - p.x) / (p.next.y - p.y);
|
|
808
|
+
if (x <= hx && x > qx) {
|
|
809
|
+
qx = x;
|
|
810
|
+
m = p.x < p.next.x ? p : p.next;
|
|
811
|
+
if (x === hx) return m;
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
p = p.next;
|
|
815
|
+
} while (p !== outerNode);
|
|
816
|
+
if (!m) return null;
|
|
817
|
+
const stop = m, mx = m.x, my = m.y;
|
|
818
|
+
let tanMin = Infinity, tan;
|
|
819
|
+
p = m;
|
|
820
|
+
do {
|
|
821
|
+
if (hx >= p.x && p.x >= mx && hx !== p.x && pointInTriangle(hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p.x, p.y)) {
|
|
822
|
+
tan = Math.abs(hy - p.y) / (hx - p.x);
|
|
823
|
+
if (locallyInside(p, hole) && (tan < tanMin || tan === tanMin && (p.x > m.x || p.x === m.x && sectorContainsSector(m, p)))) {
|
|
824
|
+
m = p;
|
|
825
|
+
tanMin = tan;
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
p = p.next;
|
|
829
|
+
} while (p !== stop);
|
|
830
|
+
return m;
|
|
831
|
+
}
|
|
832
|
+
function sectorContainsSector(m, p) {
|
|
833
|
+
return area(m.prev, m, p.prev) < 0 && area(p.next, m, m.next) < 0;
|
|
834
|
+
}
|
|
835
|
+
function indexCurve(start, minX, minY, invSize) {
|
|
836
|
+
let p = start;
|
|
837
|
+
do {
|
|
838
|
+
if (p.z === 0) p.z = zOrder(p.x, p.y, minX, minY, invSize);
|
|
839
|
+
p.prevZ = p.prev;
|
|
840
|
+
p.nextZ = p.next;
|
|
841
|
+
p = p.next;
|
|
842
|
+
} while (p !== start);
|
|
843
|
+
p.prevZ.nextZ = null;
|
|
844
|
+
p.prevZ = null;
|
|
845
|
+
sortLinked(p);
|
|
846
|
+
}
|
|
847
|
+
function sortLinked(list2) {
|
|
848
|
+
let i, p, q, e, tail, numMerges, pSize, qSize, inSize = 1;
|
|
849
|
+
do {
|
|
850
|
+
p = list2;
|
|
851
|
+
list2 = null;
|
|
852
|
+
tail = null;
|
|
853
|
+
numMerges = 0;
|
|
854
|
+
while (p) {
|
|
855
|
+
numMerges++;
|
|
856
|
+
q = p;
|
|
857
|
+
pSize = 0;
|
|
858
|
+
for (i = 0; i < inSize; i++) {
|
|
859
|
+
pSize++;
|
|
860
|
+
q = q.nextZ;
|
|
861
|
+
if (!q) break;
|
|
862
|
+
}
|
|
863
|
+
qSize = inSize;
|
|
864
|
+
while (pSize > 0 || qSize > 0 && q) {
|
|
865
|
+
if (pSize !== 0 && (qSize === 0 || !q || p.z <= q.z)) {
|
|
866
|
+
e = p;
|
|
867
|
+
p = p.nextZ;
|
|
868
|
+
pSize--;
|
|
869
|
+
} else {
|
|
870
|
+
e = q;
|
|
871
|
+
q = q.nextZ;
|
|
872
|
+
qSize--;
|
|
873
|
+
}
|
|
874
|
+
if (tail) tail.nextZ = e;
|
|
875
|
+
else list2 = e;
|
|
876
|
+
e.prevZ = tail;
|
|
877
|
+
tail = e;
|
|
878
|
+
}
|
|
879
|
+
p = q;
|
|
880
|
+
}
|
|
881
|
+
tail.nextZ = null;
|
|
882
|
+
inSize *= 2;
|
|
883
|
+
} while (numMerges > 1);
|
|
884
|
+
return list2;
|
|
885
|
+
}
|
|
886
|
+
function zOrder(x, y, minX, minY, invSize) {
|
|
887
|
+
x = (x - minX) * invSize | 0;
|
|
888
|
+
y = (y - minY) * invSize | 0;
|
|
889
|
+
x = (x | x << 8) & 16711935;
|
|
890
|
+
x = (x | x << 4) & 252645135;
|
|
891
|
+
x = (x | x << 2) & 858993459;
|
|
892
|
+
x = (x | x << 1) & 1431655765;
|
|
893
|
+
y = (y | y << 8) & 16711935;
|
|
894
|
+
y = (y | y << 4) & 252645135;
|
|
895
|
+
y = (y | y << 2) & 858993459;
|
|
896
|
+
y = (y | y << 1) & 1431655765;
|
|
897
|
+
return x | y << 1;
|
|
898
|
+
}
|
|
899
|
+
function getLeftmost(start) {
|
|
900
|
+
let p = start, leftmost = start;
|
|
901
|
+
do {
|
|
902
|
+
if (p.x < leftmost.x || p.x === leftmost.x && p.y < leftmost.y) leftmost = p;
|
|
903
|
+
p = p.next;
|
|
904
|
+
} while (p !== start);
|
|
905
|
+
return leftmost;
|
|
906
|
+
}
|
|
907
|
+
function pointInTriangle(ax, ay, bx, by, cx, cy, px, py) {
|
|
908
|
+
return (cx - px) * (ay - py) >= (ax - px) * (cy - py) && (ax - px) * (by - py) >= (bx - px) * (ay - py) && (bx - px) * (cy - py) >= (cx - px) * (by - py);
|
|
909
|
+
}
|
|
910
|
+
function isValidDiagonal(a, b) {
|
|
911
|
+
return a.next.i !== b.i && a.prev.i !== b.i && !intersectsPolygon(a, b) && // dones't intersect other edges
|
|
912
|
+
(locallyInside(a, b) && locallyInside(b, a) && middleInside(a, b) && // locally visible
|
|
913
|
+
(area(a.prev, a, b.prev) || area(a, b.prev, b)) || // does not create opposite-facing sectors
|
|
914
|
+
equals(a, b) && area(a.prev, a, a.next) > 0 && area(b.prev, b, b.next) > 0);
|
|
915
|
+
}
|
|
916
|
+
function area(p, q, r) {
|
|
917
|
+
return (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
|
|
918
|
+
}
|
|
919
|
+
function equals(p1, p2) {
|
|
920
|
+
return p1.x === p2.x && p1.y === p2.y;
|
|
921
|
+
}
|
|
922
|
+
function intersects(p1, q1, p2, q2) {
|
|
923
|
+
const o1 = sign(area(p1, q1, p2));
|
|
924
|
+
const o2 = sign(area(p1, q1, q2));
|
|
925
|
+
const o3 = sign(area(p2, q2, p1));
|
|
926
|
+
const o4 = sign(area(p2, q2, q1));
|
|
927
|
+
if (o1 !== o2 && o3 !== o4) return true;
|
|
928
|
+
if (o1 === 0 && onSegment(p1, p2, q1)) return true;
|
|
929
|
+
if (o2 === 0 && onSegment(p1, q2, q1)) return true;
|
|
930
|
+
if (o3 === 0 && onSegment(p2, p1, q2)) return true;
|
|
931
|
+
if (o4 === 0 && onSegment(p2, q1, q2)) return true;
|
|
932
|
+
return false;
|
|
933
|
+
}
|
|
934
|
+
function onSegment(p, q, r) {
|
|
935
|
+
return q.x <= Math.max(p.x, r.x) && q.x >= Math.min(p.x, r.x) && q.y <= Math.max(p.y, r.y) && q.y >= Math.min(p.y, r.y);
|
|
936
|
+
}
|
|
937
|
+
function sign(num2) {
|
|
938
|
+
return num2 > 0 ? 1 : num2 < 0 ? -1 : 0;
|
|
939
|
+
}
|
|
940
|
+
function intersectsPolygon(a, b) {
|
|
941
|
+
let p = a;
|
|
942
|
+
do {
|
|
943
|
+
if (p.i !== a.i && p.next.i !== a.i && p.i !== b.i && p.next.i !== b.i && intersects(p, p.next, a, b)) return true;
|
|
944
|
+
p = p.next;
|
|
945
|
+
} while (p !== a);
|
|
946
|
+
return false;
|
|
947
|
+
}
|
|
948
|
+
function locallyInside(a, b) {
|
|
949
|
+
return area(a.prev, a, a.next) < 0 ? area(a, b, a.next) >= 0 && area(a, a.prev, b) >= 0 : area(a, b, a.prev) < 0 || area(a, a.next, b) < 0;
|
|
950
|
+
}
|
|
951
|
+
function middleInside(a, b) {
|
|
952
|
+
let p = a, inside = false;
|
|
953
|
+
const px = (a.x + b.x) / 2, py = (a.y + b.y) / 2;
|
|
954
|
+
do {
|
|
955
|
+
if (p.y > py !== p.next.y > py && p.next.y !== p.y && px < (p.next.x - p.x) * (py - p.y) / (p.next.y - p.y) + p.x)
|
|
956
|
+
inside = !inside;
|
|
957
|
+
p = p.next;
|
|
958
|
+
} while (p !== a);
|
|
959
|
+
return inside;
|
|
960
|
+
}
|
|
961
|
+
function splitPolygon(a, b) {
|
|
962
|
+
const a2 = new Node(a.i, a.x, a.y), b2 = new Node(b.i, b.x, b.y), an = a.next, bp = b.prev;
|
|
963
|
+
a.next = b;
|
|
964
|
+
b.prev = a;
|
|
965
|
+
a2.next = an;
|
|
966
|
+
an.prev = a2;
|
|
967
|
+
b2.next = a2;
|
|
968
|
+
a2.prev = b2;
|
|
969
|
+
bp.next = b2;
|
|
970
|
+
b2.prev = bp;
|
|
971
|
+
return b2;
|
|
972
|
+
}
|
|
973
|
+
function insertNode(i, x, y, last) {
|
|
974
|
+
const p = new Node(i, x, y);
|
|
975
|
+
if (!last) {
|
|
976
|
+
p.prev = p;
|
|
977
|
+
p.next = p;
|
|
978
|
+
} else {
|
|
979
|
+
p.next = last.next;
|
|
980
|
+
p.prev = last;
|
|
981
|
+
last.next.prev = p;
|
|
982
|
+
last.next = p;
|
|
983
|
+
}
|
|
984
|
+
return p;
|
|
985
|
+
}
|
|
986
|
+
function removeNode(p) {
|
|
987
|
+
p.next.prev = p.prev;
|
|
988
|
+
p.prev.next = p.next;
|
|
989
|
+
if (p.prevZ) p.prevZ.nextZ = p.nextZ;
|
|
990
|
+
if (p.nextZ) p.nextZ.prevZ = p.prevZ;
|
|
991
|
+
}
|
|
992
|
+
function Node(i, x, y) {
|
|
993
|
+
this.i = i;
|
|
994
|
+
this.x = x;
|
|
995
|
+
this.y = y;
|
|
996
|
+
this.prev = null;
|
|
997
|
+
this.next = null;
|
|
998
|
+
this.z = 0;
|
|
999
|
+
this.prevZ = null;
|
|
1000
|
+
this.nextZ = null;
|
|
1001
|
+
this.steiner = false;
|
|
1002
|
+
}
|
|
1003
|
+
function signedArea(data, start, end, dim) {
|
|
1004
|
+
let sum = 0;
|
|
1005
|
+
for (let i = start, j = end - dim; i < end; i += dim) {
|
|
1006
|
+
sum += (data[j] - data[i]) * (data[i + 1] + data[j + 1]);
|
|
1007
|
+
j = i;
|
|
1008
|
+
}
|
|
1009
|
+
return sum;
|
|
1010
|
+
}
|
|
1011
|
+
var equals2 = (a, b) => a.x === b.x && a.y === b.y;
|
|
1012
|
+
var linePoint = (v1, v2, t) => t === 1 ? { x: v2.x, y: v2.y } : { x: (v2.x - v1.x) * t + v1.x, y: (v2.y - v1.y) * t + v1.y };
|
|
1013
|
+
function pathPoints(points) {
|
|
1014
|
+
const out = [];
|
|
1015
|
+
let last;
|
|
1016
|
+
for (let i = 1; i < points.length; i++) {
|
|
1017
|
+
const v1 = { x: points[i - 1][0], y: points[i - 1][1] };
|
|
1018
|
+
const v2 = { x: points[i][0], y: points[i][1] };
|
|
1019
|
+
for (let d = 0; d <= 1; d++) {
|
|
1020
|
+
const point = linePoint(v1, v2, d / 1);
|
|
1021
|
+
if (last && equals2(last, point)) continue;
|
|
1022
|
+
out.push(point);
|
|
1023
|
+
last = point;
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
return out;
|
|
1027
|
+
}
|
|
1028
|
+
function area2(contour) {
|
|
1029
|
+
const n = contour.length;
|
|
1030
|
+
let a = 0;
|
|
1031
|
+
for (let p = n - 1, q = 0; q < n; p = q++) a += contour[p].x * contour[q].y - contour[q].x * contour[p].y;
|
|
1032
|
+
return a * 0.5;
|
|
1033
|
+
}
|
|
1034
|
+
var isClockWise = (pts) => area2(pts) < 0;
|
|
1035
|
+
function removeDupEndPts(points) {
|
|
1036
|
+
const l = points.length;
|
|
1037
|
+
if (l > 2 && equals2(points[l - 1], points[0])) points.pop();
|
|
1038
|
+
}
|
|
1039
|
+
function triangulateShape(contour, holes) {
|
|
1040
|
+
const vertices = [];
|
|
1041
|
+
const holeIndices = [];
|
|
1042
|
+
const faces = [];
|
|
1043
|
+
removeDupEndPts(contour);
|
|
1044
|
+
for (const p of contour) vertices.push(p.x, p.y);
|
|
1045
|
+
let holeIndex = contour.length;
|
|
1046
|
+
holes.forEach(removeDupEndPts);
|
|
1047
|
+
for (let i = 0; i < holes.length; i++) {
|
|
1048
|
+
holeIndices.push(holeIndex);
|
|
1049
|
+
holeIndex += holes[i].length;
|
|
1050
|
+
for (const p of holes[i]) vertices.push(p.x, p.y);
|
|
1051
|
+
}
|
|
1052
|
+
const triangles = Earcut.triangulate(vertices, holeIndices);
|
|
1053
|
+
for (let i = 0; i < triangles.length; i += 3) faces.push(triangles.slice(i, i + 3));
|
|
1054
|
+
return faces;
|
|
1055
|
+
}
|
|
1056
|
+
function computeVertexNormals(positions, indices) {
|
|
1057
|
+
const normals = new Float32Array(positions.length);
|
|
1058
|
+
const get = (array, i) => [array[i * 3], array[i * 3 + 1], array[i * 3 + 2]];
|
|
1059
|
+
const face = (vA, vB, vC) => {
|
|
1060
|
+
const pA = get(positions, vA);
|
|
1061
|
+
const pB = get(positions, vB);
|
|
1062
|
+
const pC = get(positions, vC);
|
|
1063
|
+
const cb = [pC[0] - pB[0], pC[1] - pB[1], pC[2] - pB[2]];
|
|
1064
|
+
const ab = [pA[0] - pB[0], pA[1] - pB[1], pA[2] - pB[2]];
|
|
1065
|
+
return [cb[1] * ab[2] - cb[2] * ab[1], cb[2] * ab[0] - cb[0] * ab[2], cb[0] * ab[1] - cb[1] * ab[0]];
|
|
1066
|
+
};
|
|
1067
|
+
const set = (i, x, y, z) => {
|
|
1068
|
+
normals[i * 3] = x;
|
|
1069
|
+
normals[i * 3 + 1] = y;
|
|
1070
|
+
normals[i * 3 + 2] = z;
|
|
1071
|
+
};
|
|
1072
|
+
if (indices) {
|
|
1073
|
+
for (let i = 0; i < indices.length; i += 3) {
|
|
1074
|
+
const vA = indices[i];
|
|
1075
|
+
const vB = indices[i + 1];
|
|
1076
|
+
const vC = indices[i + 2];
|
|
1077
|
+
const cb = face(vA, vB, vC);
|
|
1078
|
+
for (const v of [vA, vB, vC]) {
|
|
1079
|
+
const n = get(normals, v);
|
|
1080
|
+
set(v, n[0] + cb[0], n[1] + cb[1], n[2] + cb[2]);
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
} else {
|
|
1084
|
+
for (let i = 0; i < positions.length / 3; i += 3) {
|
|
1085
|
+
const cb = face(i, i + 1, i + 2);
|
|
1086
|
+
set(i, cb[0], cb[1], cb[2]);
|
|
1087
|
+
set(i + 1, cb[0], cb[1], cb[2]);
|
|
1088
|
+
set(i + 2, cb[0], cb[1], cb[2]);
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
for (let i = 0; i < normals.length / 3; i++) {
|
|
1092
|
+
const [x, y, z] = get(normals, i);
|
|
1093
|
+
const s = 1 / (Math.sqrt(x * x + y * y + z * z) || 1);
|
|
1094
|
+
set(i, x * s, y * s, z * s);
|
|
1095
|
+
}
|
|
1096
|
+
return normals;
|
|
1097
|
+
}
|
|
1098
|
+
function extrude(outline, depth = 1, steps = 1, bevel = void 0) {
|
|
1099
|
+
const parameters = { shape: outline, depth, steps, bevel: bevel ?? {} };
|
|
1100
|
+
const verticesArray = [];
|
|
1101
|
+
const uvArray = [];
|
|
1102
|
+
const groups = [];
|
|
1103
|
+
const placeholder = [];
|
|
1104
|
+
const bevelEnabled = bevel !== false;
|
|
1105
|
+
const spec = bevel === false || bevel === void 0 ? {} : bevel;
|
|
1106
|
+
let bevelThickness = spec.thickness !== void 0 ? spec.thickness : 0.2;
|
|
1107
|
+
let bevelSize = spec.size !== void 0 ? spec.size : bevelThickness - 0.1;
|
|
1108
|
+
let bevelOffset = spec.offset !== void 0 ? spec.offset : 0;
|
|
1109
|
+
let bevelSegments = spec.segments !== void 0 ? spec.segments : 3;
|
|
1110
|
+
if (!bevelEnabled) {
|
|
1111
|
+
bevelSegments = 0;
|
|
1112
|
+
bevelThickness = 0;
|
|
1113
|
+
bevelSize = 0;
|
|
1114
|
+
bevelOffset = 0;
|
|
1115
|
+
}
|
|
1116
|
+
let vertices = pathPoints(outline.outer);
|
|
1117
|
+
const holes = (outline.holes ?? []).map(pathPoints);
|
|
1118
|
+
const reverse = !isClockWise(vertices);
|
|
1119
|
+
if (reverse) {
|
|
1120
|
+
vertices = vertices.reverse();
|
|
1121
|
+
for (let h = 0; h < holes.length; h++) {
|
|
1122
|
+
const ahole = holes[h];
|
|
1123
|
+
if (isClockWise(ahole)) holes[h] = ahole.reverse();
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
const faces = triangulateShape(vertices, holes);
|
|
1127
|
+
const contour = vertices;
|
|
1128
|
+
for (const ahole of holes) vertices = vertices.concat(ahole);
|
|
1129
|
+
const scalePt2 = (pt, vec, size) => ({ x: pt.x + vec.x * size, y: pt.y + vec.y * size });
|
|
1130
|
+
const vlen = vertices.length;
|
|
1131
|
+
const flen = faces.length;
|
|
1132
|
+
const getBevelVec = (inPt, inPrev, inNext) => {
|
|
1133
|
+
let vTransX;
|
|
1134
|
+
let vTransY;
|
|
1135
|
+
let shrinkBy;
|
|
1136
|
+
const vPrevX = inPt.x - inPrev.x;
|
|
1137
|
+
const vPrevY = inPt.y - inPrev.y;
|
|
1138
|
+
const vNextX = inNext.x - inPt.x;
|
|
1139
|
+
const vNextY = inNext.y - inPt.y;
|
|
1140
|
+
const vPrevLensq = vPrevX * vPrevX + vPrevY * vPrevY;
|
|
1141
|
+
const collinear0 = vPrevX * vNextY - vPrevY * vNextX;
|
|
1142
|
+
if (Math.abs(collinear0) > Number.EPSILON) {
|
|
1143
|
+
const vPrevLen = Math.sqrt(vPrevLensq);
|
|
1144
|
+
const vNextLen = Math.sqrt(vNextX * vNextX + vNextY * vNextY);
|
|
1145
|
+
const ptPrevShiftX = inPrev.x - vPrevY / vPrevLen;
|
|
1146
|
+
const ptPrevShiftY = inPrev.y + vPrevX / vPrevLen;
|
|
1147
|
+
const ptNextShiftX = inNext.x - vNextY / vNextLen;
|
|
1148
|
+
const ptNextShiftY = inNext.y + vNextX / vNextLen;
|
|
1149
|
+
const sf = ((ptNextShiftX - ptPrevShiftX) * vNextY - (ptNextShiftY - ptPrevShiftY) * vNextX) / (vPrevX * vNextY - vPrevY * vNextX);
|
|
1150
|
+
vTransX = ptPrevShiftX + vPrevX * sf - inPt.x;
|
|
1151
|
+
vTransY = ptPrevShiftY + vPrevY * sf - inPt.y;
|
|
1152
|
+
const vTransLensq = vTransX * vTransX + vTransY * vTransY;
|
|
1153
|
+
if (vTransLensq <= 2) return { x: vTransX, y: vTransY };
|
|
1154
|
+
shrinkBy = Math.sqrt(vTransLensq / 2);
|
|
1155
|
+
} else {
|
|
1156
|
+
let directionEq = false;
|
|
1157
|
+
if (vPrevX > Number.EPSILON) {
|
|
1158
|
+
if (vNextX > Number.EPSILON) directionEq = true;
|
|
1159
|
+
} else if (vPrevX < -Number.EPSILON) {
|
|
1160
|
+
if (vNextX < -Number.EPSILON) directionEq = true;
|
|
1161
|
+
} else if (Math.sign(vPrevY) === Math.sign(vNextY)) {
|
|
1162
|
+
directionEq = true;
|
|
1163
|
+
}
|
|
1164
|
+
if (directionEq) {
|
|
1165
|
+
vTransX = -vPrevY;
|
|
1166
|
+
vTransY = vPrevX;
|
|
1167
|
+
shrinkBy = Math.sqrt(vPrevLensq);
|
|
1168
|
+
} else {
|
|
1169
|
+
vTransX = vPrevX;
|
|
1170
|
+
vTransY = vPrevY;
|
|
1171
|
+
shrinkBy = Math.sqrt(vPrevLensq / 2);
|
|
1172
|
+
}
|
|
1173
|
+
}
|
|
1174
|
+
return { x: vTransX / shrinkBy, y: vTransY / shrinkBy };
|
|
1175
|
+
};
|
|
1176
|
+
const movements = (ring2) => {
|
|
1177
|
+
const out = [];
|
|
1178
|
+
for (let i = 0, il = ring2.length, j = il - 1, k = i + 1; i < il; i++, j++, k++) {
|
|
1179
|
+
if (j === il) j = 0;
|
|
1180
|
+
if (k === il) k = 0;
|
|
1181
|
+
out[i] = getBevelVec(ring2[i], ring2[j], ring2[k]);
|
|
1182
|
+
}
|
|
1183
|
+
return out;
|
|
1184
|
+
};
|
|
1185
|
+
const contourMovements = movements(contour);
|
|
1186
|
+
const holesMovements = [];
|
|
1187
|
+
let verticesMovements = contourMovements.concat();
|
|
1188
|
+
for (const ahole of holes) {
|
|
1189
|
+
const oneHoleMovements = movements(ahole);
|
|
1190
|
+
holesMovements.push(oneHoleMovements);
|
|
1191
|
+
verticesMovements = verticesMovements.concat(oneHoleMovements);
|
|
1192
|
+
}
|
|
1193
|
+
const v = (x, y, z) => {
|
|
1194
|
+
placeholder.push(x);
|
|
1195
|
+
placeholder.push(y);
|
|
1196
|
+
placeholder.push(z);
|
|
1197
|
+
};
|
|
1198
|
+
const bevelLayer = (b, zOf) => {
|
|
1199
|
+
const t = b / bevelSegments;
|
|
1200
|
+
const z = bevelThickness * Math.cos(t * Math.PI / 2);
|
|
1201
|
+
const bs2 = bevelSize * Math.sin(t * Math.PI / 2) + bevelOffset;
|
|
1202
|
+
for (let i = 0; i < contour.length; i++) {
|
|
1203
|
+
const vert = scalePt2(contour[i], contourMovements[i], bs2);
|
|
1204
|
+
v(vert.x, vert.y, zOf(z));
|
|
1205
|
+
}
|
|
1206
|
+
for (let h = 0; h < holes.length; h++) {
|
|
1207
|
+
const ahole = holes[h];
|
|
1208
|
+
const oneHoleMovements = holesMovements[h];
|
|
1209
|
+
for (let i = 0; i < ahole.length; i++) {
|
|
1210
|
+
const vert = scalePt2(ahole[i], oneHoleMovements[i], bs2);
|
|
1211
|
+
v(vert.x, vert.y, zOf(z));
|
|
1212
|
+
}
|
|
1213
|
+
}
|
|
1214
|
+
};
|
|
1215
|
+
for (let b = 0; b < bevelSegments; b++) bevelLayer(b, (z) => -z);
|
|
1216
|
+
const bs = bevelSize + bevelOffset;
|
|
1217
|
+
for (let i = 0; i < vlen; i++) {
|
|
1218
|
+
const vert = bevelEnabled ? scalePt2(vertices[i], verticesMovements[i], bs) : vertices[i];
|
|
1219
|
+
v(vert.x, vert.y, 0);
|
|
1220
|
+
}
|
|
1221
|
+
for (let s = 1; s <= steps; s++) {
|
|
1222
|
+
for (let i = 0; i < vlen; i++) {
|
|
1223
|
+
const vert = bevelEnabled ? scalePt2(vertices[i], verticesMovements[i], bs) : vertices[i];
|
|
1224
|
+
v(vert.x, vert.y, depth / steps * s);
|
|
1225
|
+
}
|
|
1226
|
+
}
|
|
1227
|
+
for (let b = bevelSegments - 1; b >= 0; b--) bevelLayer(b, (z) => depth + z);
|
|
1228
|
+
const addVertex = (index) => {
|
|
1229
|
+
verticesArray.push(placeholder[index * 3 + 0]);
|
|
1230
|
+
verticesArray.push(placeholder[index * 3 + 1]);
|
|
1231
|
+
verticesArray.push(placeholder[index * 3 + 2]);
|
|
1232
|
+
};
|
|
1233
|
+
const addUV = (x, y) => {
|
|
1234
|
+
uvArray.push(x);
|
|
1235
|
+
uvArray.push(y);
|
|
1236
|
+
};
|
|
1237
|
+
const at = (index, axis) => verticesArray[index * 3 + axis];
|
|
1238
|
+
const f3 = (a, b, c) => {
|
|
1239
|
+
addVertex(a);
|
|
1240
|
+
addVertex(b);
|
|
1241
|
+
addVertex(c);
|
|
1242
|
+
const nextIndex = verticesArray.length / 3;
|
|
1243
|
+
for (const index of [nextIndex - 3, nextIndex - 2, nextIndex - 1]) addUV(at(index, 0), at(index, 1));
|
|
1244
|
+
};
|
|
1245
|
+
const f4 = (a, b, c, d) => {
|
|
1246
|
+
addVertex(a);
|
|
1247
|
+
addVertex(b);
|
|
1248
|
+
addVertex(d);
|
|
1249
|
+
addVertex(b);
|
|
1250
|
+
addVertex(c);
|
|
1251
|
+
addVertex(d);
|
|
1252
|
+
const nextIndex = verticesArray.length / 3;
|
|
1253
|
+
const iA = nextIndex - 6;
|
|
1254
|
+
const iB = nextIndex - 3;
|
|
1255
|
+
const iC = nextIndex - 2;
|
|
1256
|
+
const iD = nextIndex - 1;
|
|
1257
|
+
const byX = Math.abs(at(iA, 1) - at(iB, 1)) < Math.abs(at(iA, 0) - at(iB, 0));
|
|
1258
|
+
const uv = (index) => [byX ? at(index, 0) : at(index, 1), 1 - at(index, 2)];
|
|
1259
|
+
const uvs = [uv(iA), uv(iB), uv(iC), uv(iD)];
|
|
1260
|
+
for (const k of [0, 1, 3, 1, 2, 3]) addUV(...uvs[k]);
|
|
1261
|
+
};
|
|
1262
|
+
let start = verticesArray.length / 3;
|
|
1263
|
+
if (bevelEnabled) {
|
|
1264
|
+
let offset = 0;
|
|
1265
|
+
for (let i = 0; i < flen; i++) {
|
|
1266
|
+
const face = faces[i];
|
|
1267
|
+
f3(face[2] + offset, face[1] + offset, face[0] + offset);
|
|
1268
|
+
}
|
|
1269
|
+
const layer = steps + bevelSegments * 2;
|
|
1270
|
+
offset = vlen * layer;
|
|
1271
|
+
for (let i = 0; i < flen; i++) {
|
|
1272
|
+
const face = faces[i];
|
|
1273
|
+
f3(face[0] + offset, face[1] + offset, face[2] + offset);
|
|
1274
|
+
}
|
|
1275
|
+
} else {
|
|
1276
|
+
for (let i = 0; i < flen; i++) {
|
|
1277
|
+
const face = faces[i];
|
|
1278
|
+
f3(face[2], face[1], face[0]);
|
|
1279
|
+
}
|
|
1280
|
+
for (let i = 0; i < flen; i++) {
|
|
1281
|
+
const face = faces[i];
|
|
1282
|
+
f3(face[0] + vlen * steps, face[1] + vlen * steps, face[2] + vlen * steps);
|
|
1283
|
+
}
|
|
1284
|
+
}
|
|
1285
|
+
groups.push({ start, count: verticesArray.length / 3 - start, materialIndex: 0 });
|
|
1286
|
+
start = verticesArray.length / 3;
|
|
1287
|
+
const sidewalls = (ring2, layeroffset2) => {
|
|
1288
|
+
let i = ring2.length;
|
|
1289
|
+
while (--i >= 0) {
|
|
1290
|
+
const j = i;
|
|
1291
|
+
let k = i - 1;
|
|
1292
|
+
if (k < 0) k = ring2.length - 1;
|
|
1293
|
+
for (let s = 0, sl = steps + bevelSegments * 2; s < sl; s++) {
|
|
1294
|
+
const slen1 = vlen * s;
|
|
1295
|
+
const slen2 = vlen * (s + 1);
|
|
1296
|
+
f4(layeroffset2 + j + slen1, layeroffset2 + k + slen1, layeroffset2 + k + slen2, layeroffset2 + j + slen2);
|
|
1297
|
+
}
|
|
1298
|
+
}
|
|
1299
|
+
};
|
|
1300
|
+
let layeroffset = 0;
|
|
1301
|
+
sidewalls(contour, layeroffset);
|
|
1302
|
+
layeroffset += contour.length;
|
|
1303
|
+
for (const ahole of holes) {
|
|
1304
|
+
sidewalls(ahole, layeroffset);
|
|
1305
|
+
layeroffset += ahole.length;
|
|
1306
|
+
}
|
|
1307
|
+
groups.push({ start, count: verticesArray.length / 3 - start, materialIndex: 1 });
|
|
1308
|
+
const positions = new Float32Array(verticesArray);
|
|
1309
|
+
return { type: "ExtrudeGeometry", parameters, positions, normals: computeVertexNormals(positions), uvs: new Float32Array(uvArray), groups };
|
|
1310
|
+
}
|
|
1311
|
+
var PARAMETER_KEYS = {
|
|
1312
|
+
box: ["size", "segments"],
|
|
1313
|
+
cylinder: ["radiusTop", "radiusBottom", "height", "radialSegments", "heightSegments", "openEnded", "thetaStart", "thetaLength"],
|
|
1314
|
+
cone: ["radius", "height", "radialSegments", "heightSegments", "openEnded", "thetaStart", "thetaLength"],
|
|
1315
|
+
sphere: ["radius", "widthSegments", "heightSegments", "phiStart", "phiLength", "thetaStart", "thetaLength"],
|
|
1316
|
+
plane: ["width", "height", "widthSegments", "heightSegments"],
|
|
1317
|
+
circle: ["radius", "segments", "thetaStart", "thetaLength"],
|
|
1318
|
+
ring: ["innerRadius", "outerRadius", "thetaSegments", "phiSegments", "thetaStart", "thetaLength"],
|
|
1319
|
+
lathe: ["points", "segments", "phiStart", "phiLength"],
|
|
1320
|
+
torus: ["radius", "tube", "radialSegments", "tubularSegments", "arc"],
|
|
1321
|
+
extrude: ["shape", "depth", "steps", "bevel"],
|
|
1322
|
+
difference: ["size", "subtract"],
|
|
1323
|
+
mesh: ["positions", "normals", "uvs", "indices"]
|
|
1324
|
+
};
|
|
1325
|
+
var SEGMENT_KEYS = {
|
|
1326
|
+
cylinder: ["radialSegments"],
|
|
1327
|
+
cone: ["radialSegments"],
|
|
1328
|
+
sphere: ["widthSegments", "heightSegments"],
|
|
1329
|
+
circle: ["segments"],
|
|
1330
|
+
ring: ["thetaSegments"],
|
|
1331
|
+
lathe: ["segments"],
|
|
1332
|
+
torus: ["radialSegments", "tubularSegments"]
|
|
1333
|
+
};
|
|
1334
|
+
var SEGMENT_DEFAULTS = { radialSegments: 32, widthSegments: 32, heightSegments: 16, segments: 12, thetaSegments: 32, tubularSegments: 48 };
|
|
1335
|
+
var segmentDefault = (kind, key) => kind === "circle" && key === "segments" ? 32 : kind === "torus" && key === "radialSegments" ? 12 : SEGMENT_DEFAULTS[key];
|
|
1336
|
+
function primitiveParameters(node, options = {}) {
|
|
1337
|
+
const out = {};
|
|
1338
|
+
const source = node;
|
|
1339
|
+
for (const key of PARAMETER_KEYS[node.kind]) if (source[key] !== void 0) out[key] = source[key];
|
|
1340
|
+
const detail = options.detail ?? 1;
|
|
1341
|
+
if (detail !== 1) {
|
|
1342
|
+
const floor = options.minSegments ?? 3;
|
|
1343
|
+
for (const key of SEGMENT_KEYS[node.kind] ?? []) {
|
|
1344
|
+
const given = out[key] ?? segmentDefault(node.kind, key);
|
|
1345
|
+
out[key] = Math.max(floor, Math.round(given * detail));
|
|
1346
|
+
}
|
|
1347
|
+
}
|
|
1348
|
+
return out;
|
|
1349
|
+
}
|
|
1350
|
+
function mesh(p) {
|
|
1351
|
+
const positions = new Float32Array(p.positions);
|
|
1352
|
+
const indices = p.indices ? indexArray(p.indices) : void 0;
|
|
1353
|
+
return {
|
|
1354
|
+
type: "BufferGeometry",
|
|
1355
|
+
parameters: {},
|
|
1356
|
+
positions,
|
|
1357
|
+
normals: p.normals ? new Float32Array(p.normals) : computeVertexNormals(positions, indices),
|
|
1358
|
+
uvs: p.uvs ? new Float32Array(p.uvs) : new Float32Array(positions.length / 3 * 2),
|
|
1359
|
+
...indices ? { indices } : {},
|
|
1360
|
+
groups: []
|
|
1361
|
+
};
|
|
1362
|
+
}
|
|
1363
|
+
function make(kind, p) {
|
|
1364
|
+
switch (kind) {
|
|
1365
|
+
case "box": {
|
|
1366
|
+
const size = p.size ?? [1, 1, 1];
|
|
1367
|
+
const segments = p.segments ?? [1, 1, 1];
|
|
1368
|
+
return box(size[0], size[1], size[2], segments[0], segments[1], segments[2]);
|
|
1369
|
+
}
|
|
1370
|
+
case "cylinder":
|
|
1371
|
+
return cylinder(p.radiusTop, p.radiusBottom, p.height, p.radialSegments, p.heightSegments, p.openEnded, p.thetaStart, p.thetaLength);
|
|
1372
|
+
case "cone":
|
|
1373
|
+
return cone(p.radius, p.height, p.radialSegments, p.heightSegments, p.openEnded, p.thetaStart, p.thetaLength);
|
|
1374
|
+
case "sphere":
|
|
1375
|
+
return sphere(p.radius, p.widthSegments, p.heightSegments, p.phiStart, p.phiLength, p.thetaStart, p.thetaLength);
|
|
1376
|
+
case "plane":
|
|
1377
|
+
return plane(p.width, p.height, p.widthSegments, p.heightSegments);
|
|
1378
|
+
case "circle":
|
|
1379
|
+
return circle(p.radius, p.segments, p.thetaStart, p.thetaLength);
|
|
1380
|
+
case "ring":
|
|
1381
|
+
return ring(p.innerRadius, p.outerRadius, p.thetaSegments, p.phiSegments, p.thetaStart, p.thetaLength);
|
|
1382
|
+
case "lathe":
|
|
1383
|
+
return lathe(p.points, p.segments, p.phiStart, p.phiLength);
|
|
1384
|
+
case "torus":
|
|
1385
|
+
return torus(p.radius, p.tube, p.radialSegments, p.tubularSegments, p.arc);
|
|
1386
|
+
case "extrude":
|
|
1387
|
+
return extrude(p.shape, p.depth, p.steps, p.bevel);
|
|
1388
|
+
case "difference":
|
|
1389
|
+
return difference(p.size, p.subtract ?? []);
|
|
1390
|
+
case "mesh":
|
|
1391
|
+
return mesh(p);
|
|
1392
|
+
default:
|
|
1393
|
+
throw new Error(`game-shapes: unknown primitive kind "${String(kind)}"`);
|
|
1394
|
+
}
|
|
1395
|
+
}
|
|
1396
|
+
var templates = /* @__PURE__ */ new Map();
|
|
1397
|
+
var templateLimit = 4096;
|
|
1398
|
+
var stats = { hits: 0, misses: 0 };
|
|
1399
|
+
function buildPrimitive(node, options = {}) {
|
|
1400
|
+
const parameters = primitiveParameters(node, options);
|
|
1401
|
+
if (node.kind === "mesh" || templateLimit === 0) return make(node.kind, parameters);
|
|
1402
|
+
const key = `${node.kind}:${canonicalJson(parameters)}`;
|
|
1403
|
+
const cached = templates.get(key);
|
|
1404
|
+
if (cached) {
|
|
1405
|
+
stats.hits++;
|
|
1406
|
+
templates.delete(key);
|
|
1407
|
+
templates.set(key, cached);
|
|
1408
|
+
return cached;
|
|
1409
|
+
}
|
|
1410
|
+
stats.misses++;
|
|
1411
|
+
const built = make(node.kind, parameters);
|
|
1412
|
+
templates.set(key, built);
|
|
1413
|
+
if (templates.size > templateLimit) templates.delete(templates.keys().next().value);
|
|
1414
|
+
return built;
|
|
1415
|
+
}
|
|
1416
|
+
var num = (v) => typeof v === "number" && Number.isFinite(v) ? null : "expected a finite number";
|
|
1417
|
+
var bool = (v) => typeof v === "boolean" ? null : "expected a boolean";
|
|
1418
|
+
var str = (v) => typeof v === "string" ? null : "expected a string";
|
|
1419
|
+
var int = (min) => (v) => typeof v === "number" && Number.isInteger(v) && v >= min ? null : `expected an integer >= ${min}`;
|
|
1420
|
+
var range = (lo, hi, loOpen = false) => (v) => typeof v === "number" && (loOpen ? v > lo : v >= lo) && v <= hi ? null : `expected a number in ${loOpen ? "(" : "["}${lo}, ${hi}]`;
|
|
1421
|
+
var tuple = (n, item = num) => (v) => {
|
|
1422
|
+
if (!Array.isArray(v) || v.length !== n) return `expected an array of ${n}`;
|
|
1423
|
+
for (const x of v) if (item(x)) return `expected an array of ${n} numbers`;
|
|
1424
|
+
return null;
|
|
1425
|
+
};
|
|
1426
|
+
var list = (item, min = 0) => (v) => {
|
|
1427
|
+
if (!Array.isArray(v)) return "expected an array";
|
|
1428
|
+
if (v.length < min) return `expected at least ${min} items`;
|
|
1429
|
+
for (let i = 0; i < v.length; i++) {
|
|
1430
|
+
const e = item(v[i]);
|
|
1431
|
+
if (e) return `[${i}]: ${e}`;
|
|
1432
|
+
}
|
|
1433
|
+
return null;
|
|
1434
|
+
};
|
|
1435
|
+
var color = (v) => typeof v === "number" && Number.isInteger(v) && v >= 0 && v <= 16777215 || typeof v === "string" && /^#[0-9a-fA-F]{6}$/.test(v) ? null : 'expected 0xRRGGBB or "#rrggbb"';
|
|
1436
|
+
var oneOf = (values) => (v) => values.includes(v) ? null : `expected one of ${values.map((x) => JSON.stringify(x)).join(", ")}`;
|
|
1437
|
+
var object = (fields, required = []) => (v) => {
|
|
1438
|
+
if (!v || typeof v !== "object" || Array.isArray(v)) return "expected an object";
|
|
1439
|
+
const record = v;
|
|
1440
|
+
for (const key of required) if (record[key] === void 0) return `${key}: required`;
|
|
1441
|
+
for (const key of Object.keys(record)) {
|
|
1442
|
+
if (record[key] === void 0) continue;
|
|
1443
|
+
const check = fields[key];
|
|
1444
|
+
if (!check) return `${key}: unknown field`;
|
|
1445
|
+
const e = check(record[key]);
|
|
1446
|
+
if (e) return `${key}: ${e}`;
|
|
1447
|
+
}
|
|
1448
|
+
return null;
|
|
1449
|
+
};
|
|
1450
|
+
var ORDERS = ["XYZ", "YXZ", "ZXY", "ZYX", "YZX", "XZY"];
|
|
1451
|
+
var TRANSFORM = {
|
|
1452
|
+
position: tuple(3),
|
|
1453
|
+
rotation: tuple(3),
|
|
1454
|
+
rotationOrder: oneOf(ORDERS),
|
|
1455
|
+
scale: (v) => typeof v === "number" ? num(v) : tuple(3)(v),
|
|
1456
|
+
heading: num,
|
|
1457
|
+
matrix: tuple(16)
|
|
1458
|
+
};
|
|
1459
|
+
var UV = object({ u0: num, v0: num, u1: num, v1: num }, ["u0", "v0", "u1", "v1"]);
|
|
1460
|
+
var INSTANCE = object({ ...TRANSFORM, color });
|
|
1461
|
+
var COMMON = {
|
|
1462
|
+
...TRANSFORM,
|
|
1463
|
+
kind: str,
|
|
1464
|
+
name: str,
|
|
1465
|
+
part: str,
|
|
1466
|
+
material: str,
|
|
1467
|
+
color,
|
|
1468
|
+
uv: UV,
|
|
1469
|
+
instances: list(INSTANCE),
|
|
1470
|
+
minDetail: range(0, 1, true),
|
|
1471
|
+
tags: list(str)
|
|
1472
|
+
};
|
|
1473
|
+
var vec2 = tuple(2);
|
|
1474
|
+
var BEVEL = (v) => v === false ? null : object({ thickness: num, size: num, offset: num, segments: int(1) })(v);
|
|
1475
|
+
var KINDS = {
|
|
1476
|
+
group: { fields: {} },
|
|
1477
|
+
box: { fields: { size: tuple(3), segments: tuple(3, int(1)) } },
|
|
1478
|
+
cylinder: { fields: { radiusTop: num, radiusBottom: num, height: num, radialSegments: int(1), heightSegments: int(1), openEnded: bool, thetaStart: num, thetaLength: num } },
|
|
1479
|
+
cone: { fields: { radius: num, height: num, radialSegments: int(1), heightSegments: int(1), openEnded: bool, thetaStart: num, thetaLength: num } },
|
|
1480
|
+
sphere: { fields: { radius: num, widthSegments: int(3), heightSegments: int(2), phiStart: num, phiLength: num, thetaStart: num, thetaLength: num } },
|
|
1481
|
+
plane: { fields: { width: num, height: num, widthSegments: int(1), heightSegments: int(1) } },
|
|
1482
|
+
circle: { fields: { radius: num, segments: int(3), thetaStart: num, thetaLength: num } },
|
|
1483
|
+
ring: { fields: { innerRadius: num, outerRadius: num, thetaSegments: int(3), phiSegments: int(1), thetaStart: num, thetaLength: num } },
|
|
1484
|
+
lathe: { fields: { points: list(vec2, 2), segments: int(1), phiStart: num, phiLength: num }, required: ["points"] },
|
|
1485
|
+
torus: { fields: { radius: num, tube: num, radialSegments: int(1), tubularSegments: int(1), arc: num } },
|
|
1486
|
+
extrude: {
|
|
1487
|
+
fields: { shape: object({ outer: list(vec2, 3), holes: list(list(vec2, 3)) }, ["outer"]), depth: num, steps: int(1), bevel: BEVEL },
|
|
1488
|
+
required: ["shape"]
|
|
1489
|
+
},
|
|
1490
|
+
difference: { fields: { size: tuple(3), subtract: list(object({ min: vec2, max: vec2 }, ["min", "max"])) }, required: ["size", "subtract"] },
|
|
1491
|
+
mesh: {
|
|
1492
|
+
fields: {
|
|
1493
|
+
positions: (v) => list(num)(v) ?? (v.length % 3 === 0 ? null : "expected a multiple of 3 numbers"),
|
|
1494
|
+
normals: list(num),
|
|
1495
|
+
uvs: list(num),
|
|
1496
|
+
indices: list(int(0))
|
|
1497
|
+
},
|
|
1498
|
+
required: ["positions"]
|
|
1499
|
+
},
|
|
1500
|
+
point: { fields: { data: () => null } },
|
|
1501
|
+
custom: { fields: { generator: str, params: () => null }, required: ["generator"] }
|
|
1502
|
+
};
|
|
1503
|
+
function mergeTransformed(pieces) {
|
|
1504
|
+
let total = 0;
|
|
1505
|
+
for (const piece2 of pieces) total += piece2.indices ? piece2.indices.length : piece2.positions.length / 3;
|
|
1506
|
+
const positions = new Float32Array(total * 3);
|
|
1507
|
+
const normals = new Float32Array(total * 3);
|
|
1508
|
+
const uvs = new Float32Array(total * 2);
|
|
1509
|
+
let at = 0;
|
|
1510
|
+
for (const piece2 of pieces) {
|
|
1511
|
+
const e = piece2.matrix;
|
|
1512
|
+
const nm = normalMatrix(e);
|
|
1513
|
+
const count = piece2.indices ? piece2.indices.length : piece2.positions.length / 3;
|
|
1514
|
+
const p = new Float32Array(count * 3);
|
|
1515
|
+
const n = new Float32Array(count * 3);
|
|
1516
|
+
for (let k = 0; k < count; k++) {
|
|
1517
|
+
const i = piece2.indices ? piece2.indices[k] : k;
|
|
1518
|
+
for (let c = 0; c < 3; c++) {
|
|
1519
|
+
p[k * 3 + c] = piece2.positions[i * 3 + c];
|
|
1520
|
+
if (piece2.normals) n[k * 3 + c] = piece2.normals[i * 3 + c];
|
|
1521
|
+
}
|
|
1522
|
+
if (piece2.uvs) {
|
|
1523
|
+
uvs[(at + k) * 2] = piece2.uvs[i * 2];
|
|
1524
|
+
uvs[(at + k) * 2 + 1] = piece2.uvs[i * 2 + 1];
|
|
1525
|
+
}
|
|
1526
|
+
}
|
|
1527
|
+
for (let k = 0; k < count; k++) {
|
|
1528
|
+
const q = transformPoint(e, p[k * 3], p[k * 3 + 1], p[k * 3 + 2]);
|
|
1529
|
+
positions[(at + k) * 3] = q[0];
|
|
1530
|
+
positions[(at + k) * 3 + 1] = q[1];
|
|
1531
|
+
positions[(at + k) * 3 + 2] = q[2];
|
|
1532
|
+
const x = n[k * 3];
|
|
1533
|
+
const y = n[k * 3 + 1];
|
|
1534
|
+
const z = n[k * 3 + 2];
|
|
1535
|
+
const vx = nm[0] * x + nm[3] * y + nm[6] * z;
|
|
1536
|
+
const vy = nm[1] * x + nm[4] * y + nm[7] * z;
|
|
1537
|
+
const vz = nm[2] * x + nm[5] * y + nm[8] * z;
|
|
1538
|
+
const s = 1 / (Math.sqrt(vx * vx + vy * vy + vz * vz) || 1);
|
|
1539
|
+
normals[(at + k) * 3] = vx * s;
|
|
1540
|
+
normals[(at + k) * 3 + 1] = vy * s;
|
|
1541
|
+
normals[(at + k) * 3 + 2] = vz * s;
|
|
1542
|
+
}
|
|
1543
|
+
at += count;
|
|
1544
|
+
}
|
|
1545
|
+
return { positions, normals, uvs };
|
|
1546
|
+
}
|
|
1547
|
+
|
|
1548
|
+
// src/namespace.ts
|
|
1549
|
+
var SHAPE_GEOMETRY_TYPES = ["BoxGeometry", "CylinderGeometry", "ConeGeometry", "SphereGeometry", "PlaneGeometry", "CircleGeometry", "RingGeometry", "LatheGeometry", "TorusGeometry", "ExtrudeGeometry"];
|
|
1550
|
+
function linePoints(path) {
|
|
1551
|
+
if (path.autoClose || path.curves.length === 0) return null;
|
|
1552
|
+
const points = [];
|
|
1553
|
+
for (const [index, curve] of path.curves.entries()) {
|
|
1554
|
+
if (!curve.isLineCurve || !curve.v1 || !curve.v2) return null;
|
|
1555
|
+
if (index === 0) points.push([curve.v1.x, curve.v1.y]);
|
|
1556
|
+
else {
|
|
1557
|
+
const last = points[points.length - 1];
|
|
1558
|
+
if (last[0] !== curve.v1.x || last[1] !== curve.v1.y) return null;
|
|
1559
|
+
}
|
|
1560
|
+
points.push([curve.v2.x, curve.v2.y]);
|
|
1561
|
+
}
|
|
1562
|
+
return points;
|
|
1563
|
+
}
|
|
1564
|
+
function extrudeOf(shapes, options = {}) {
|
|
1565
|
+
if (Array.isArray(shapes) || !shapes || options.extrudePath !== void 0 || options.UVGenerator !== void 0) return null;
|
|
1566
|
+
const shape = shapes;
|
|
1567
|
+
const outer = linePoints(shape);
|
|
1568
|
+
if (!outer) return null;
|
|
1569
|
+
const holes = [];
|
|
1570
|
+
for (const hole of shape.holes ?? []) {
|
|
1571
|
+
const points = linePoints(hole);
|
|
1572
|
+
if (!points) return null;
|
|
1573
|
+
holes.push(points);
|
|
1574
|
+
}
|
|
1575
|
+
const outline = holes.length ? { outer, holes } : { outer };
|
|
1576
|
+
const bevel = options.bevelEnabled === false ? false : {
|
|
1577
|
+
...options.bevelThickness !== void 0 ? { thickness: options.bevelThickness } : {},
|
|
1578
|
+
...options.bevelSize !== void 0 ? { size: options.bevelSize } : {},
|
|
1579
|
+
...options.bevelOffset !== void 0 ? { offset: options.bevelOffset } : {},
|
|
1580
|
+
...options.bevelSegments !== void 0 ? { segments: options.bevelSegments } : {}
|
|
1581
|
+
};
|
|
1582
|
+
return extrude(outline, options.depth, options.steps, bevel);
|
|
1583
|
+
}
|
|
1584
|
+
var namespaces = /* @__PURE__ */ new WeakMap();
|
|
1585
|
+
function shapeGeometries(THREE, options = {}) {
|
|
1586
|
+
const cache = options.cache !== false;
|
|
1587
|
+
const key = cache ? "cache" : "nocache";
|
|
1588
|
+
let byKey = namespaces.get(THREE);
|
|
1589
|
+
if (!byKey) {
|
|
1590
|
+
byKey = /* @__PURE__ */ new Map();
|
|
1591
|
+
namespaces.set(THREE, byKey);
|
|
1592
|
+
}
|
|
1593
|
+
const existing = byKey.get(key);
|
|
1594
|
+
if (existing) return existing;
|
|
1595
|
+
const made = (p) => toGeometry(THREE, p);
|
|
1596
|
+
const viaCache = (node, direct) => cache ? buildPrimitive(node) : direct();
|
|
1597
|
+
const defined = (o) => Object.fromEntries(Object.entries(o).filter(([, v]) => v !== void 0));
|
|
1598
|
+
const constructors = {
|
|
1599
|
+
BoxGeometry: (width, height, depth, ws = 1, hs = 1, ds = 1) => made(viaCache({ kind: "box", size: [width ?? 1, height ?? 1, depth ?? 1], segments: [ws, hs, ds] }, () => box(width, height, depth, ws, hs, ds))),
|
|
1600
|
+
CylinderGeometry: (radiusTop, radiusBottom, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength) => made(viaCache({ kind: "cylinder", ...defined({ radiusTop, radiusBottom, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength }) }, () => cylinder(radiusTop, radiusBottom, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength))),
|
|
1601
|
+
ConeGeometry: (radius, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength) => made(viaCache({ kind: "cone", ...defined({ radius, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength }) }, () => cone(radius, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength))),
|
|
1602
|
+
SphereGeometry: (radius, widthSegments, heightSegments, phiStart, phiLength, thetaStart, thetaLength) => made(viaCache({ kind: "sphere", ...defined({ radius, widthSegments, heightSegments, phiStart, phiLength, thetaStart, thetaLength }) }, () => sphere(radius, widthSegments, heightSegments, phiStart, phiLength, thetaStart, thetaLength))),
|
|
1603
|
+
PlaneGeometry: (width, height, widthSegments, heightSegments) => made(viaCache({ kind: "plane", ...defined({ width, height, widthSegments, heightSegments }) }, () => plane(width, height, widthSegments, heightSegments))),
|
|
1604
|
+
CircleGeometry: (radius, segments, thetaStart, thetaLength) => made(viaCache({ kind: "circle", ...defined({ radius, segments, thetaStart, thetaLength }) }, () => circle(radius, segments, thetaStart, thetaLength))),
|
|
1605
|
+
RingGeometry: (innerRadius, outerRadius, thetaSegments, phiSegments, thetaStart, thetaLength) => made(viaCache({ kind: "ring", ...defined({ innerRadius, outerRadius, thetaSegments, phiSegments, thetaStart, thetaLength }) }, () => ring(innerRadius, outerRadius, thetaSegments, phiSegments, thetaStart, thetaLength))),
|
|
1606
|
+
LatheGeometry: (points, segments, phiStart, phiLength) => {
|
|
1607
|
+
if (!points) return new THREE.LatheGeometry(void 0, segments, phiStart, phiLength);
|
|
1608
|
+
const profile = points.map((p) => [p.x, p.y]);
|
|
1609
|
+
return made(viaCache({ kind: "lathe", points: profile, ...defined({ segments, phiStart, phiLength }) }, () => lathe(profile, segments, phiStart, phiLength)));
|
|
1610
|
+
},
|
|
1611
|
+
TorusGeometry: (radius, tube, radialSegments, tubularSegments, arc) => made(viaCache({ kind: "torus", ...defined({ radius, tube, radialSegments, tubularSegments, arc }) }, () => torus(radius, tube, radialSegments, tubularSegments, arc))),
|
|
1612
|
+
ExtrudeGeometry: (shapes, extrudeOptions) => {
|
|
1613
|
+
const buffers = shapes === void 0 ? null : extrudeOf(shapes, extrudeOptions);
|
|
1614
|
+
if (!buffers) return new THREE.ExtrudeGeometry(shapes, extrudeOptions);
|
|
1615
|
+
const geometry = toGeometry(THREE, buffers);
|
|
1616
|
+
geometry.parameters = { shapes, options: extrudeOptions ?? {} };
|
|
1617
|
+
return geometry;
|
|
1618
|
+
}
|
|
1619
|
+
};
|
|
1620
|
+
const namespace = { ...THREE };
|
|
1621
|
+
for (const type of SHAPE_GEOMETRY_TYPES) {
|
|
1622
|
+
const make2 = constructors[type];
|
|
1623
|
+
const Constructor = function(...args) {
|
|
1624
|
+
return make2(...args);
|
|
1625
|
+
};
|
|
1626
|
+
Constructor.prototype = THREE[type].prototype;
|
|
1627
|
+
Object.defineProperty(Constructor, "name", { value: type });
|
|
1628
|
+
namespace[type] = Constructor;
|
|
1629
|
+
}
|
|
1630
|
+
const result = namespace;
|
|
1631
|
+
byKey.set(key, result);
|
|
1632
|
+
return result;
|
|
1633
|
+
}
|
|
1634
|
+
|
|
1635
|
+
// src/object.ts
|
|
1636
|
+
function createMaterials(THREE, hints = {}, ids = []) {
|
|
1637
|
+
const out = {};
|
|
1638
|
+
const all = /* @__PURE__ */ new Set([...Object.keys(hints), ...ids]);
|
|
1639
|
+
for (const id of all) {
|
|
1640
|
+
const hint = hints[id] ?? {};
|
|
1641
|
+
const parameters = { vertexColors: hint.vertexColors ?? true };
|
|
1642
|
+
if (hint.color !== void 0) parameters.color = colorHex(hint.color);
|
|
1643
|
+
if (hint.roughness !== void 0) parameters.roughness = hint.roughness;
|
|
1644
|
+
if (hint.metalness !== void 0) parameters.metalness = hint.metalness;
|
|
1645
|
+
if (hint.opacity !== void 0) parameters.opacity = hint.opacity;
|
|
1646
|
+
if (hint.transparent !== void 0) parameters.transparent = hint.transparent;
|
|
1647
|
+
if (hint.emissive !== void 0) parameters.emissive = colorHex(hint.emissive);
|
|
1648
|
+
if (hint.emissiveIntensity !== void 0) parameters.emissiveIntensity = hint.emissiveIntensity;
|
|
1649
|
+
if (hint.flatShading !== void 0) parameters.flatShading = hint.flatShading;
|
|
1650
|
+
if (hint.depthWrite !== void 0) parameters.depthWrite = hint.depthWrite;
|
|
1651
|
+
if (hint.side !== void 0) parameters.side = hint.side === "double" ? THREE.DoubleSide : hint.side === "back" ? THREE.BackSide : THREE.FrontSide;
|
|
1652
|
+
const material = new THREE.MeshStandardMaterial(parameters);
|
|
1653
|
+
material.name = id;
|
|
1654
|
+
out[id] = material;
|
|
1655
|
+
}
|
|
1656
|
+
return out;
|
|
1657
|
+
}
|
|
1658
|
+
function placePart(THREE, group, part) {
|
|
1659
|
+
if (part.matrix && part.position === void 0 && part.rotation === void 0 && part.scale === void 0 && part.heading === void 0) {
|
|
1660
|
+
new THREE.Matrix4().fromArray(part.matrix).decompose(group.position, group.quaternion, group.scale);
|
|
1661
|
+
return;
|
|
1662
|
+
}
|
|
1663
|
+
if (part.position) group.position.set(part.position[0], part.position[1], part.position[2]);
|
|
1664
|
+
if (part.heading !== void 0) group.rotation.y = part.heading;
|
|
1665
|
+
else if (part.rotation) group.rotation.set(part.rotation[0], part.rotation[1], part.rotation[2], part.rotationOrder ?? "XYZ");
|
|
1666
|
+
if (part.scale !== void 0) {
|
|
1667
|
+
if (typeof part.scale === "number") group.scale.setScalar(part.scale);
|
|
1668
|
+
else group.scale.set(part.scale[0], part.scale[1], part.scale[2]);
|
|
1669
|
+
}
|
|
1670
|
+
group.updateMatrix();
|
|
1671
|
+
}
|
|
1672
|
+
function toObject3D(THREE, buffers, options = {}) {
|
|
1673
|
+
const shadows = options.shadows !== false;
|
|
1674
|
+
const ids = /* @__PURE__ */ new Set();
|
|
1675
|
+
for (const level of buffers.levels) {
|
|
1676
|
+
for (const b of level.batches) ids.add(b.material);
|
|
1677
|
+
for (const b of level.instanced) ids.add(b.material);
|
|
1678
|
+
}
|
|
1679
|
+
const made = createMaterials(THREE, buffers.materials, [...ids].filter((id) => !options.materials?.[id]));
|
|
1680
|
+
const materials = { ...made, ...options.materials ?? {} };
|
|
1681
|
+
const materialOf = (id, part) => options.materialFor?.(id, part) ?? materials[id];
|
|
1682
|
+
const meshes = [];
|
|
1683
|
+
const instanced = [];
|
|
1684
|
+
const build = (level, withParts) => {
|
|
1685
|
+
const group = new THREE.Group();
|
|
1686
|
+
group.name = options.name ?? buffers.name ?? "shape";
|
|
1687
|
+
const parts = {};
|
|
1688
|
+
if (withParts) {
|
|
1689
|
+
for (const part of buffers.parts) {
|
|
1690
|
+
const g = new THREE.Group();
|
|
1691
|
+
g.name = `part:${part.id}`;
|
|
1692
|
+
g.userData.part = part.id;
|
|
1693
|
+
placePart(THREE, g, part);
|
|
1694
|
+
parts[part.id] = g;
|
|
1695
|
+
}
|
|
1696
|
+
}
|
|
1697
|
+
const into = (part) => part !== null && parts[part] || group;
|
|
1698
|
+
for (const batch of level.batches) {
|
|
1699
|
+
const mesh2 = new THREE.Mesh(toGeometry(THREE, batch, { copy: options.copy !== false }), materialOf(batch.material, batch.part));
|
|
1700
|
+
mesh2.geometry.computeBoundingSphere();
|
|
1701
|
+
mesh2.geometry.computeBoundingBox();
|
|
1702
|
+
mesh2.name = batch.part === null ? `static:${batch.material}` : `part:${batch.part}`;
|
|
1703
|
+
if (batch.part !== null) mesh2.userData.part = batch.part;
|
|
1704
|
+
mesh2.userData.material = batch.material;
|
|
1705
|
+
if (batch.ranges.length) mesh2.userData.ranges = batch.ranges;
|
|
1706
|
+
mesh2.castShadow = shadows;
|
|
1707
|
+
mesh2.receiveShadow = shadows;
|
|
1708
|
+
into(batch.part).add(mesh2);
|
|
1709
|
+
meshes.push(mesh2);
|
|
1710
|
+
}
|
|
1711
|
+
for (const batch of level.instanced) {
|
|
1712
|
+
const mesh2 = new THREE.InstancedMesh(toGeometry(THREE, batch.geometry, { copy: options.copy !== false }), materialOf(batch.material, batch.part), batch.count);
|
|
1713
|
+
mesh2.instanceMatrix.array.set(batch.matrices);
|
|
1714
|
+
mesh2.instanceMatrix.needsUpdate = true;
|
|
1715
|
+
if (batch.colors) mesh2.instanceColor = new THREE.InstancedBufferAttribute(batch.colors.slice(), 3);
|
|
1716
|
+
mesh2.name = `instanced:${batch.name ?? batch.material}`;
|
|
1717
|
+
mesh2.userData.material = batch.material;
|
|
1718
|
+
if (batch.part !== null) mesh2.userData.part = batch.part;
|
|
1719
|
+
mesh2.castShadow = shadows;
|
|
1720
|
+
mesh2.receiveShadow = shadows;
|
|
1721
|
+
mesh2.computeBoundingSphere();
|
|
1722
|
+
into(batch.part).add(mesh2);
|
|
1723
|
+
instanced.push(mesh2);
|
|
1724
|
+
}
|
|
1725
|
+
for (const part of Object.values(parts)) group.add(part);
|
|
1726
|
+
return { group, parts };
|
|
1727
|
+
};
|
|
1728
|
+
const levels = buffers.levels;
|
|
1729
|
+
const first = build(levels[0] ?? { distance: 0, detail: 1, batches: [], instanced: [] }, true);
|
|
1730
|
+
let object2 = first.group;
|
|
1731
|
+
if (levels.length > 1 || buffers.cullDistance !== void 0) {
|
|
1732
|
+
const lod = new THREE.LOD();
|
|
1733
|
+
lod.name = first.group.name;
|
|
1734
|
+
lod.addLevel(first.group, 0);
|
|
1735
|
+
for (const level of levels.slice(1)) lod.addLevel(build(level, false).group, level.distance);
|
|
1736
|
+
if (buffers.cullDistance !== void 0) lod.addLevel(new THREE.Group(), buffers.cullDistance);
|
|
1737
|
+
object2 = lod;
|
|
1738
|
+
}
|
|
1739
|
+
object2.userData.points = buffers.points;
|
|
1740
|
+
return { object: object2, near: first.group, parts: first.parts, meshes, instanced, points: buffers.points, materials };
|
|
1741
|
+
}
|
|
1742
|
+
|
|
1743
|
+
// src/merge-by-material.ts
|
|
1744
|
+
var isVisible = (object2) => {
|
|
1745
|
+
for (let node = object2; node; node = node.parent) if (!node.visible) return false;
|
|
1746
|
+
return true;
|
|
1747
|
+
};
|
|
1748
|
+
var plain = (attribute) => attribute !== void 0 && !attribute.isInterleavedBufferAttribute && attribute.array instanceof Float32Array && !attribute.normalized;
|
|
1749
|
+
function piece(geometry, matrix) {
|
|
1750
|
+
const position = geometry.getAttribute("position");
|
|
1751
|
+
const normal = geometry.getAttribute("normal");
|
|
1752
|
+
const uv = geometry.getAttribute("uv");
|
|
1753
|
+
const ok = plain(position) && (normal === void 0 || plain(normal)) && (uv === void 0 || plain(uv)) && position.itemSize === 3 && (uv === void 0 || uv.itemSize === 2);
|
|
1754
|
+
if (ok) {
|
|
1755
|
+
return { positions: position.array, ...normal ? { normals: normal.array } : {}, ...uv ? { uvs: uv.array } : {}, indices: geometry.index ? geometry.index.array : null, matrix: matrix.elements };
|
|
1756
|
+
}
|
|
1757
|
+
const flat = geometry.index ? geometry.toNonIndexed() : geometry;
|
|
1758
|
+
const read = (name, size) => {
|
|
1759
|
+
const attribute = flat.getAttribute(name);
|
|
1760
|
+
if (!attribute) return void 0;
|
|
1761
|
+
const out = new Float32Array(attribute.count * size);
|
|
1762
|
+
for (let i = 0; i < attribute.count; i++) for (let c = 0; c < size; c++) out[i * size + c] = c === 0 ? attribute.getX(i) : c === 1 ? attribute.getY(i) : attribute.getZ(i);
|
|
1763
|
+
return out;
|
|
1764
|
+
};
|
|
1765
|
+
const positions = read("position", 3);
|
|
1766
|
+
const normals = read("normal", 3);
|
|
1767
|
+
const uvs = read("uv", 2);
|
|
1768
|
+
if (flat !== geometry) flat.dispose();
|
|
1769
|
+
return { positions, ...normals ? { normals } : {}, ...uvs ? { uvs } : {}, indices: null, matrix: matrix.elements };
|
|
1770
|
+
}
|
|
1771
|
+
function mergeByMaterial(THREE, root, space) {
|
|
1772
|
+
const toSpace = space.matrixWorld.clone().invert();
|
|
1773
|
+
const byMaterial = /* @__PURE__ */ new Map();
|
|
1774
|
+
root.traverse((object2) => {
|
|
1775
|
+
const mesh2 = object2;
|
|
1776
|
+
if (!mesh2.isMesh || Array.isArray(mesh2.material) || !isVisible(mesh2)) return;
|
|
1777
|
+
const list2 = byMaterial.get(mesh2.material) ?? [];
|
|
1778
|
+
list2.push(piece(mesh2.geometry, new THREE.Matrix4().multiplyMatrices(toSpace, mesh2.matrixWorld)));
|
|
1779
|
+
byMaterial.set(mesh2.material, list2);
|
|
1780
|
+
});
|
|
1781
|
+
return [...byMaterial].map(([material, pieces]) => {
|
|
1782
|
+
const merged = mergeTransformed(pieces);
|
|
1783
|
+
const geometry = new THREE.BufferGeometry();
|
|
1784
|
+
geometry.setAttribute("position", new THREE.BufferAttribute(merged.positions, 3));
|
|
1785
|
+
geometry.setAttribute("normal", new THREE.BufferAttribute(merged.normals, 3));
|
|
1786
|
+
geometry.setAttribute("uv", new THREE.BufferAttribute(merged.uvs, 2));
|
|
1787
|
+
const mesh2 = new THREE.Mesh(geometry, material);
|
|
1788
|
+
mesh2.castShadow = false;
|
|
1789
|
+
mesh2.receiveShadow = true;
|
|
1790
|
+
return mesh2;
|
|
1791
|
+
});
|
|
1792
|
+
}
|
|
1793
|
+
return __toCommonJS(src_exports);
|
|
1794
|
+
})();
|
|
1795
|
+
//# sourceMappingURL=index.global.js.map
|