@adatechnology/game-shapes 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 +147 -2
- package/dist/chunk-R4RY23RH.js +2972 -0
- package/dist/chunk-R4RY23RH.js.map +1 -0
- package/dist/index.d.ts +221 -0
- package/dist/index.global.js +3146 -0
- package/dist/index.global.js.map +1 -0
- package/dist/index.js +259 -0
- package/dist/index.js.map +1 -0
- package/dist/worker-W5A14FiP.d.ts +392 -0
- package/dist/worker.d.ts +1 -0
- package/dist/worker.js +7 -0
- package/dist/worker.js.map +1 -0
- package/package.json +56 -3
- package/schema/shape.schema.json +703 -0
|
@@ -0,0 +1,3146 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var GameShapes = (() => {
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
21
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
22
|
+
|
|
23
|
+
// src/index.ts
|
|
24
|
+
var src_exports = {};
|
|
25
|
+
__export(src_exports, {
|
|
26
|
+
SHAPE_SCHEMA: () => SHAPE_SCHEMA,
|
|
27
|
+
assertValidShape: () => assertValidShape,
|
|
28
|
+
box: () => box,
|
|
29
|
+
buildPrimitive: () => buildPrimitive,
|
|
30
|
+
byteSizeOf: () => byteSizeOf,
|
|
31
|
+
canonicalJson: () => canonicalJson,
|
|
32
|
+
circle: () => circle,
|
|
33
|
+
clearTemplateCache: () => clearTemplateCache,
|
|
34
|
+
cloneBuffers: () => cloneBuffers,
|
|
35
|
+
clonePrimitive: () => clonePrimitive,
|
|
36
|
+
colorChannels: () => colorChannels,
|
|
37
|
+
colorHex: () => colorHex,
|
|
38
|
+
compose: () => compose,
|
|
39
|
+
computeVertexNormals: () => computeVertexNormals,
|
|
40
|
+
cone: () => cone,
|
|
41
|
+
createShapeCache: () => createShapeCache,
|
|
42
|
+
createShapePool: () => createShapePool,
|
|
43
|
+
cylinder: () => cylinder,
|
|
44
|
+
difference: () => difference,
|
|
45
|
+
extrude: () => extrude,
|
|
46
|
+
generate: () => generate,
|
|
47
|
+
getGenerator: () => getGenerator,
|
|
48
|
+
hash64: () => hash64,
|
|
49
|
+
hashValue: () => hashValue,
|
|
50
|
+
headingMatrix: () => headingMatrix,
|
|
51
|
+
identity4: () => identity4,
|
|
52
|
+
installShapeWorker: () => installShapeWorker,
|
|
53
|
+
isAffine: () => isAffine,
|
|
54
|
+
isPrimitiveNode: () => isPrimitiveNode,
|
|
55
|
+
keyOptions: () => keyOptions,
|
|
56
|
+
lathe: () => lathe,
|
|
57
|
+
listGenerators: () => listGenerators,
|
|
58
|
+
matrixOf: () => matrixOf,
|
|
59
|
+
mergeTransformed: () => mergeTransformed,
|
|
60
|
+
mulberry32: () => mulberry32,
|
|
61
|
+
multiply: () => multiply,
|
|
62
|
+
normalMatrix: () => normalMatrix,
|
|
63
|
+
plane: () => plane,
|
|
64
|
+
primitiveParameters: () => primitiveParameters,
|
|
65
|
+
quaternionFromEuler: () => quaternionFromEuler,
|
|
66
|
+
registerGenerator: () => registerGenerator,
|
|
67
|
+
ring: () => ring,
|
|
68
|
+
setTemplateCacheSize: () => setTemplateCacheSize,
|
|
69
|
+
shapeKey: () => shapeKey,
|
|
70
|
+
sphere: () => sphere,
|
|
71
|
+
srgbToLinear: () => srgbToLinear,
|
|
72
|
+
streamFor: () => streamFor,
|
|
73
|
+
templateCacheStats: () => templateCacheStats,
|
|
74
|
+
torus: () => torus,
|
|
75
|
+
transferablesOf: () => transferablesOf,
|
|
76
|
+
transformPoint: () => transformPoint,
|
|
77
|
+
unregisterGenerator: () => unregisterGenerator,
|
|
78
|
+
validateShape: () => validateShape
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// src/color.ts
|
|
82
|
+
function colorHex(color2, fallback = 16777215) {
|
|
83
|
+
if (color2 === void 0) return fallback;
|
|
84
|
+
if (typeof color2 === "number") return Math.floor(color2);
|
|
85
|
+
const match = /^#([0-9a-fA-F]{6})$/.exec(color2);
|
|
86
|
+
if (!match) throw new Error(`game-shapes: colour "${color2}" is not 0xRRGGBB or #rrggbb`);
|
|
87
|
+
return parseInt(match[1], 16);
|
|
88
|
+
}
|
|
89
|
+
var srgbToLinear = (c) => c < 0.04045 ? c * 0.0773993808 : Math.pow(c * 0.9478672986 + 0.0521327014, 2.4);
|
|
90
|
+
function colorChannels(hex, space) {
|
|
91
|
+
const r = (hex >> 16 & 255) / 255;
|
|
92
|
+
const g = (hex >> 8 & 255) / 255;
|
|
93
|
+
const b = (hex & 255) / 255;
|
|
94
|
+
return space === "linear" ? [srgbToLinear(r), srgbToLinear(g), srgbToLinear(b)] : [r, g, b];
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// src/hash.ts
|
|
98
|
+
function canonicalJson(value) {
|
|
99
|
+
if (value === null) return "null";
|
|
100
|
+
if (typeof value === "number") return Object.is(value, -0) ? "-0" : Number.isFinite(value) ? String(value) : JSON.stringify(String(value));
|
|
101
|
+
if (typeof value === "string") return JSON.stringify(value);
|
|
102
|
+
if (typeof value === "boolean") return value ? "true" : "false";
|
|
103
|
+
if (typeof value === "function") throw new Error("game-shapes: a function cannot be part of a shape (register it as a generator)");
|
|
104
|
+
if (ArrayBuffer.isView(value)) return canonicalJson(Array.from(value));
|
|
105
|
+
if (Array.isArray(value)) return `[${value.map((item) => item === void 0 ? "null" : canonicalJson(item)).join(",")}]`;
|
|
106
|
+
if (typeof value === "object") {
|
|
107
|
+
const record = value;
|
|
108
|
+
const keys = Object.keys(record).filter((key) => record[key] !== void 0).sort();
|
|
109
|
+
return `{${keys.map((key) => `${JSON.stringify(key)}:${canonicalJson(record[key])}`).join(",")}}`;
|
|
110
|
+
}
|
|
111
|
+
return "null";
|
|
112
|
+
}
|
|
113
|
+
var lane = (text, offset, prime) => {
|
|
114
|
+
let hash = offset;
|
|
115
|
+
for (let index = 0; index < text.length; index++) {
|
|
116
|
+
hash ^= text.charCodeAt(index);
|
|
117
|
+
hash = Math.imul(hash, prime);
|
|
118
|
+
}
|
|
119
|
+
return (hash >>> 0).toString(16).padStart(8, "0");
|
|
120
|
+
};
|
|
121
|
+
var hash64 = (text) => lane(text, 2166136261, 16777619) + lane(text, 84696351, 1540483477);
|
|
122
|
+
var hashValue = (value) => hash64(canonicalJson(value));
|
|
123
|
+
|
|
124
|
+
// src/math.ts
|
|
125
|
+
var identity4 = () => [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
|
|
126
|
+
function quaternionFromEuler(x, y, z, order = "XYZ") {
|
|
127
|
+
const cos = Math.cos;
|
|
128
|
+
const sin = Math.sin;
|
|
129
|
+
const c1 = cos(x / 2);
|
|
130
|
+
const c2 = cos(y / 2);
|
|
131
|
+
const c3 = cos(z / 2);
|
|
132
|
+
const s1 = sin(x / 2);
|
|
133
|
+
const s2 = sin(y / 2);
|
|
134
|
+
const s3 = sin(z / 2);
|
|
135
|
+
switch (order) {
|
|
136
|
+
case "XYZ":
|
|
137
|
+
return [s1 * c2 * c3 + c1 * s2 * s3, c1 * s2 * c3 - s1 * c2 * s3, c1 * c2 * s3 + s1 * s2 * c3, c1 * c2 * c3 - s1 * s2 * s3];
|
|
138
|
+
case "YXZ":
|
|
139
|
+
return [s1 * c2 * c3 + c1 * s2 * s3, c1 * s2 * c3 - s1 * c2 * s3, c1 * c2 * s3 - s1 * s2 * c3, c1 * c2 * c3 + s1 * s2 * s3];
|
|
140
|
+
case "ZXY":
|
|
141
|
+
return [s1 * c2 * c3 - c1 * s2 * s3, c1 * s2 * c3 + s1 * c2 * s3, c1 * c2 * s3 + s1 * s2 * c3, c1 * c2 * c3 - s1 * s2 * s3];
|
|
142
|
+
case "ZYX":
|
|
143
|
+
return [s1 * c2 * c3 - c1 * s2 * s3, c1 * s2 * c3 + s1 * c2 * s3, c1 * c2 * s3 - s1 * s2 * c3, c1 * c2 * c3 + s1 * s2 * s3];
|
|
144
|
+
case "YZX":
|
|
145
|
+
return [s1 * c2 * c3 + c1 * s2 * s3, c1 * s2 * c3 + s1 * c2 * s3, c1 * c2 * s3 - s1 * s2 * c3, c1 * c2 * c3 - s1 * s2 * s3];
|
|
146
|
+
case "XZY":
|
|
147
|
+
return [s1 * c2 * c3 - c1 * s2 * s3, c1 * s2 * c3 - s1 * c2 * s3, c1 * c2 * s3 + s1 * s2 * c3, c1 * c2 * c3 + s1 * s2 * s3];
|
|
148
|
+
default:
|
|
149
|
+
throw new Error(`game-shapes: unknown rotation order "${String(order)}"`);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
function compose(px, py, pz, q, sx, sy, sz) {
|
|
153
|
+
const [x, y, z, w] = q;
|
|
154
|
+
const x2 = x + x;
|
|
155
|
+
const y2 = y + y;
|
|
156
|
+
const z2 = z + z;
|
|
157
|
+
const xx = x * x2;
|
|
158
|
+
const xy = x * y2;
|
|
159
|
+
const xz = x * z2;
|
|
160
|
+
const yy = y * y2;
|
|
161
|
+
const yz = y * z2;
|
|
162
|
+
const zz = z * z2;
|
|
163
|
+
const wx = w * x2;
|
|
164
|
+
const wy = w * y2;
|
|
165
|
+
const wz = w * z2;
|
|
166
|
+
return [
|
|
167
|
+
(1 - (yy + zz)) * sx,
|
|
168
|
+
(xy + wz) * sx,
|
|
169
|
+
(xz - wy) * sx,
|
|
170
|
+
0,
|
|
171
|
+
(xy - wz) * sy,
|
|
172
|
+
(1 - (xx + zz)) * sy,
|
|
173
|
+
(yz + wx) * sy,
|
|
174
|
+
0,
|
|
175
|
+
(xz + wy) * sz,
|
|
176
|
+
(yz - wx) * sz,
|
|
177
|
+
(1 - (xx + yy)) * sz,
|
|
178
|
+
0,
|
|
179
|
+
px,
|
|
180
|
+
py,
|
|
181
|
+
pz,
|
|
182
|
+
1
|
|
183
|
+
];
|
|
184
|
+
}
|
|
185
|
+
function headingMatrix(theta, x = 0, y = 0, z = 0) {
|
|
186
|
+
const c = Math.cos(theta);
|
|
187
|
+
const s = Math.sin(theta);
|
|
188
|
+
return [c, 0, -s, 0, 0, 1, 0, 0, s, 0, c, 0, x, y, z, 1];
|
|
189
|
+
}
|
|
190
|
+
function multiply(ae, be) {
|
|
191
|
+
const a11 = ae[0], a12 = ae[4], a13 = ae[8], a14 = ae[12];
|
|
192
|
+
const a21 = ae[1], a22 = ae[5], a23 = ae[9], a24 = ae[13];
|
|
193
|
+
const a31 = ae[2], a32 = ae[6], a33 = ae[10], a34 = ae[14];
|
|
194
|
+
const a41 = ae[3], a42 = ae[7], a43 = ae[11], a44 = ae[15];
|
|
195
|
+
const b11 = be[0], b12 = be[4], b13 = be[8], b14 = be[12];
|
|
196
|
+
const b21 = be[1], b22 = be[5], b23 = be[9], b24 = be[13];
|
|
197
|
+
const b31 = be[2], b32 = be[6], b33 = be[10], b34 = be[14];
|
|
198
|
+
const b41 = be[3], b42 = be[7], b43 = be[11], b44 = be[15];
|
|
199
|
+
return [
|
|
200
|
+
a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41,
|
|
201
|
+
a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41,
|
|
202
|
+
a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41,
|
|
203
|
+
a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41,
|
|
204
|
+
a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42,
|
|
205
|
+
a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42,
|
|
206
|
+
a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42,
|
|
207
|
+
a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42,
|
|
208
|
+
a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43,
|
|
209
|
+
a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43,
|
|
210
|
+
a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43,
|
|
211
|
+
a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43,
|
|
212
|
+
a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44,
|
|
213
|
+
a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44,
|
|
214
|
+
a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44,
|
|
215
|
+
a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44
|
|
216
|
+
];
|
|
217
|
+
}
|
|
218
|
+
function normalMatrix(me) {
|
|
219
|
+
const n11 = me[0], n21 = me[1], n31 = me[2];
|
|
220
|
+
const n12 = me[4], n22 = me[5], n32 = me[6];
|
|
221
|
+
const n13 = me[8], n23 = me[9], n33 = me[10];
|
|
222
|
+
const t11 = n33 * n22 - n32 * n23;
|
|
223
|
+
const t12 = n32 * n13 - n33 * n12;
|
|
224
|
+
const t13 = n23 * n12 - n22 * n13;
|
|
225
|
+
const det = n11 * t11 + n21 * t12 + n31 * t13;
|
|
226
|
+
if (det === 0) return [0, 0, 0, 0, 0, 0, 0, 0, 0];
|
|
227
|
+
const detInv = 1 / det;
|
|
228
|
+
const te = [
|
|
229
|
+
t11 * detInv,
|
|
230
|
+
(n31 * n23 - n33 * n21) * detInv,
|
|
231
|
+
(n32 * n21 - n31 * n22) * detInv,
|
|
232
|
+
t12 * detInv,
|
|
233
|
+
(n33 * n11 - n31 * n13) * detInv,
|
|
234
|
+
(n31 * n12 - n32 * n11) * detInv,
|
|
235
|
+
t13 * detInv,
|
|
236
|
+
(n21 * n13 - n23 * n11) * detInv,
|
|
237
|
+
(n22 * n11 - n21 * n12) * detInv
|
|
238
|
+
];
|
|
239
|
+
let tmp = te[1];
|
|
240
|
+
te[1] = te[3];
|
|
241
|
+
te[3] = tmp;
|
|
242
|
+
tmp = te[2];
|
|
243
|
+
te[2] = te[6];
|
|
244
|
+
te[6] = tmp;
|
|
245
|
+
tmp = te[5];
|
|
246
|
+
te[5] = te[7];
|
|
247
|
+
te[7] = tmp;
|
|
248
|
+
return te;
|
|
249
|
+
}
|
|
250
|
+
var isAffine = (e) => e[3] === 0 && e[7] === 0 && e[11] === 0 && e[15] === 1;
|
|
251
|
+
function transformPoint(e, x, y, z) {
|
|
252
|
+
const w = 1 / (e[3] * x + e[7] * y + e[11] * z + e[15]);
|
|
253
|
+
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];
|
|
254
|
+
}
|
|
255
|
+
var toVec3 = (scale) => scale === void 0 ? [1, 1, 1] : typeof scale === "number" ? [scale, scale, scale] : scale;
|
|
256
|
+
function matrixOf(t) {
|
|
257
|
+
if (t.matrix !== void 0) return [...t.matrix];
|
|
258
|
+
if (t.heading !== void 0) {
|
|
259
|
+
const p2 = t.position ?? [0, 0, 0];
|
|
260
|
+
return headingMatrix(t.heading, p2[0], p2[1], p2[2]);
|
|
261
|
+
}
|
|
262
|
+
if (t.position === void 0 && t.rotation === void 0 && t.scale === void 0) return null;
|
|
263
|
+
const p = t.position ?? [0, 0, 0];
|
|
264
|
+
const r = t.rotation ?? [0, 0, 0];
|
|
265
|
+
const s = toVec3(t.scale);
|
|
266
|
+
return compose(p[0], p[1], p[2], quaternionFromEuler(r[0], r[1], r[2], t.rotationOrder ?? "XYZ"), s[0], s[1], s[2]);
|
|
267
|
+
}
|
|
268
|
+
var asMat4 = (m) => m ? [...m] : identity4();
|
|
269
|
+
|
|
270
|
+
// src/primitives/three-generators.ts
|
|
271
|
+
function needsUint32(array) {
|
|
272
|
+
for (let i = array.length - 1; i >= 0; --i) if (array[i] >= 65535) return true;
|
|
273
|
+
return false;
|
|
274
|
+
}
|
|
275
|
+
var indexArray = (indices) => needsUint32(indices) ? new Uint32Array(indices) : new Uint16Array(indices);
|
|
276
|
+
function pack(type, parameters, a) {
|
|
277
|
+
return {
|
|
278
|
+
type,
|
|
279
|
+
parameters,
|
|
280
|
+
positions: new Float32Array(a.vertices),
|
|
281
|
+
normals: new Float32Array(a.normals),
|
|
282
|
+
uvs: new Float32Array(a.uvs),
|
|
283
|
+
...a.indices ? { indices: indexArray(a.indices) } : {},
|
|
284
|
+
groups: a.groups
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
var normalize3 = (x, y, z) => {
|
|
288
|
+
const length = Math.sqrt(x * x + y * y + z * z);
|
|
289
|
+
const s = 1 / (length || 1);
|
|
290
|
+
return [x * s, y * s, z * s];
|
|
291
|
+
};
|
|
292
|
+
function box(width = 1, height = 1, depth = 1, widthSegments = 1, heightSegments = 1, depthSegments = 1) {
|
|
293
|
+
const parameters = { width, height, depth, widthSegments, heightSegments, depthSegments };
|
|
294
|
+
widthSegments = Math.floor(widthSegments);
|
|
295
|
+
heightSegments = Math.floor(heightSegments);
|
|
296
|
+
depthSegments = Math.floor(depthSegments);
|
|
297
|
+
const indices = [];
|
|
298
|
+
const vertices = [];
|
|
299
|
+
const normals = [];
|
|
300
|
+
const uvs = [];
|
|
301
|
+
const groups = [];
|
|
302
|
+
let numberOfVertices = 0;
|
|
303
|
+
let groupStart = 0;
|
|
304
|
+
const buildPlane = (u, v, w, udir, vdir, width2, height2, depth2, gridX, gridY, materialIndex) => {
|
|
305
|
+
const segmentWidth = width2 / gridX;
|
|
306
|
+
const segmentHeight = height2 / gridY;
|
|
307
|
+
const widthHalf = width2 / 2;
|
|
308
|
+
const heightHalf = height2 / 2;
|
|
309
|
+
const depthHalf = depth2 / 2;
|
|
310
|
+
const gridX1 = gridX + 1;
|
|
311
|
+
const gridY1 = gridY + 1;
|
|
312
|
+
let vertexCounter = 0;
|
|
313
|
+
let groupCount = 0;
|
|
314
|
+
const vector = { x: 0, y: 0, z: 0 };
|
|
315
|
+
for (let iy = 0; iy < gridY1; iy++) {
|
|
316
|
+
const y = iy * segmentHeight - heightHalf;
|
|
317
|
+
for (let ix = 0; ix < gridX1; ix++) {
|
|
318
|
+
const x = ix * segmentWidth - widthHalf;
|
|
319
|
+
vector[u] = x * udir;
|
|
320
|
+
vector[v] = y * vdir;
|
|
321
|
+
vector[w] = depthHalf;
|
|
322
|
+
vertices.push(vector.x, vector.y, vector.z);
|
|
323
|
+
vector[u] = 0;
|
|
324
|
+
vector[v] = 0;
|
|
325
|
+
vector[w] = depth2 > 0 ? 1 : -1;
|
|
326
|
+
normals.push(vector.x, vector.y, vector.z);
|
|
327
|
+
uvs.push(ix / gridX);
|
|
328
|
+
uvs.push(1 - iy / gridY);
|
|
329
|
+
vertexCounter += 1;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
for (let iy = 0; iy < gridY; iy++) {
|
|
333
|
+
for (let ix = 0; ix < gridX; ix++) {
|
|
334
|
+
const a = numberOfVertices + ix + gridX1 * iy;
|
|
335
|
+
const b = numberOfVertices + ix + gridX1 * (iy + 1);
|
|
336
|
+
const c = numberOfVertices + (ix + 1) + gridX1 * (iy + 1);
|
|
337
|
+
const d = numberOfVertices + (ix + 1) + gridX1 * iy;
|
|
338
|
+
indices.push(a, b, d);
|
|
339
|
+
indices.push(b, c, d);
|
|
340
|
+
groupCount += 6;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
groups.push({ start: groupStart, count: groupCount, materialIndex });
|
|
344
|
+
groupStart += groupCount;
|
|
345
|
+
numberOfVertices += vertexCounter;
|
|
346
|
+
};
|
|
347
|
+
buildPlane("z", "y", "x", -1, -1, depth, height, width, depthSegments, heightSegments, 0);
|
|
348
|
+
buildPlane("z", "y", "x", 1, -1, depth, height, -width, depthSegments, heightSegments, 1);
|
|
349
|
+
buildPlane("x", "z", "y", 1, 1, width, depth, height, widthSegments, depthSegments, 2);
|
|
350
|
+
buildPlane("x", "z", "y", 1, -1, width, depth, -height, widthSegments, depthSegments, 3);
|
|
351
|
+
buildPlane("x", "y", "z", 1, -1, width, height, depth, widthSegments, heightSegments, 4);
|
|
352
|
+
buildPlane("x", "y", "z", -1, -1, width, height, -depth, widthSegments, heightSegments, 5);
|
|
353
|
+
return pack("BoxGeometry", parameters, { vertices, normals, uvs, indices, groups });
|
|
354
|
+
}
|
|
355
|
+
function cylinderArrays(radiusTop, radiusBottom, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength) {
|
|
356
|
+
radialSegments = Math.floor(radialSegments);
|
|
357
|
+
heightSegments = Math.floor(heightSegments);
|
|
358
|
+
const indices = [];
|
|
359
|
+
const vertices = [];
|
|
360
|
+
const normals = [];
|
|
361
|
+
const uvs = [];
|
|
362
|
+
const groups = [];
|
|
363
|
+
let index = 0;
|
|
364
|
+
const indexArray2 = [];
|
|
365
|
+
const halfHeight = height / 2;
|
|
366
|
+
let groupStart = 0;
|
|
367
|
+
const generateTorso = () => {
|
|
368
|
+
let groupCount = 0;
|
|
369
|
+
const slope = (radiusBottom - radiusTop) / height;
|
|
370
|
+
for (let y = 0; y <= heightSegments; y++) {
|
|
371
|
+
const indexRow = [];
|
|
372
|
+
const v = y / heightSegments;
|
|
373
|
+
const radius = v * (radiusBottom - radiusTop) + radiusTop;
|
|
374
|
+
for (let x = 0; x <= radialSegments; x++) {
|
|
375
|
+
const u = x / radialSegments;
|
|
376
|
+
const theta = u * thetaLength + thetaStart;
|
|
377
|
+
const sinTheta = Math.sin(theta);
|
|
378
|
+
const cosTheta = Math.cos(theta);
|
|
379
|
+
vertices.push(radius * sinTheta, -v * height + halfHeight, radius * cosTheta);
|
|
380
|
+
const n = normalize3(sinTheta, slope, cosTheta);
|
|
381
|
+
normals.push(n[0], n[1], n[2]);
|
|
382
|
+
uvs.push(u, 1 - v);
|
|
383
|
+
indexRow.push(index++);
|
|
384
|
+
}
|
|
385
|
+
indexArray2.push(indexRow);
|
|
386
|
+
}
|
|
387
|
+
for (let x = 0; x < radialSegments; x++) {
|
|
388
|
+
for (let y = 0; y < heightSegments; y++) {
|
|
389
|
+
const a = indexArray2[y][x];
|
|
390
|
+
const b = indexArray2[y + 1][x];
|
|
391
|
+
const c = indexArray2[y + 1][x + 1];
|
|
392
|
+
const d = indexArray2[y][x + 1];
|
|
393
|
+
indices.push(a, b, d);
|
|
394
|
+
indices.push(b, c, d);
|
|
395
|
+
groupCount += 6;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
groups.push({ start: groupStart, count: groupCount, materialIndex: 0 });
|
|
399
|
+
groupStart += groupCount;
|
|
400
|
+
};
|
|
401
|
+
const generateCap = (top) => {
|
|
402
|
+
const centerIndexStart = index;
|
|
403
|
+
let groupCount = 0;
|
|
404
|
+
const radius = top === true ? radiusTop : radiusBottom;
|
|
405
|
+
const sign2 = top === true ? 1 : -1;
|
|
406
|
+
for (let x = 1; x <= radialSegments; x++) {
|
|
407
|
+
vertices.push(0, halfHeight * sign2, 0);
|
|
408
|
+
normals.push(0, sign2, 0);
|
|
409
|
+
uvs.push(0.5, 0.5);
|
|
410
|
+
index++;
|
|
411
|
+
}
|
|
412
|
+
const centerIndexEnd = index;
|
|
413
|
+
for (let x = 0; x <= radialSegments; x++) {
|
|
414
|
+
const u = x / radialSegments;
|
|
415
|
+
const theta = u * thetaLength + thetaStart;
|
|
416
|
+
const cosTheta = Math.cos(theta);
|
|
417
|
+
const sinTheta = Math.sin(theta);
|
|
418
|
+
vertices.push(radius * sinTheta, halfHeight * sign2, radius * cosTheta);
|
|
419
|
+
normals.push(0, sign2, 0);
|
|
420
|
+
uvs.push(cosTheta * 0.5 + 0.5, sinTheta * 0.5 * sign2 + 0.5);
|
|
421
|
+
index++;
|
|
422
|
+
}
|
|
423
|
+
for (let x = 0; x < radialSegments; x++) {
|
|
424
|
+
const c = centerIndexStart + x;
|
|
425
|
+
const i = centerIndexEnd + x;
|
|
426
|
+
if (top === true) indices.push(i, i + 1, c);
|
|
427
|
+
else indices.push(i + 1, i, c);
|
|
428
|
+
groupCount += 3;
|
|
429
|
+
}
|
|
430
|
+
groups.push({ start: groupStart, count: groupCount, materialIndex: top === true ? 1 : 2 });
|
|
431
|
+
groupStart += groupCount;
|
|
432
|
+
};
|
|
433
|
+
generateTorso();
|
|
434
|
+
if (openEnded === false) {
|
|
435
|
+
if (radiusTop > 0) generateCap(true);
|
|
436
|
+
if (radiusBottom > 0) generateCap(false);
|
|
437
|
+
}
|
|
438
|
+
return { vertices, normals, uvs, indices, groups };
|
|
439
|
+
}
|
|
440
|
+
function cylinder(radiusTop = 1, radiusBottom = 1, height = 1, radialSegments = 32, heightSegments = 1, openEnded = false, thetaStart = 0, thetaLength = Math.PI * 2) {
|
|
441
|
+
const parameters = { radiusTop, radiusBottom, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength };
|
|
442
|
+
return pack("CylinderGeometry", parameters, cylinderArrays(radiusTop, radiusBottom, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength));
|
|
443
|
+
}
|
|
444
|
+
function cone(radius = 1, height = 1, radialSegments = 32, heightSegments = 1, openEnded = false, thetaStart = 0, thetaLength = Math.PI * 2) {
|
|
445
|
+
const parameters = { radius, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength };
|
|
446
|
+
return pack("ConeGeometry", parameters, cylinderArrays(0, radius, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength));
|
|
447
|
+
}
|
|
448
|
+
function sphere(radius = 1, widthSegments = 32, heightSegments = 16, phiStart = 0, phiLength = Math.PI * 2, thetaStart = 0, thetaLength = Math.PI) {
|
|
449
|
+
const parameters = { radius, widthSegments, heightSegments, phiStart, phiLength, thetaStart, thetaLength };
|
|
450
|
+
widthSegments = Math.max(3, Math.floor(widthSegments));
|
|
451
|
+
heightSegments = Math.max(2, Math.floor(heightSegments));
|
|
452
|
+
const thetaEnd = Math.min(thetaStart + thetaLength, Math.PI);
|
|
453
|
+
let index = 0;
|
|
454
|
+
const grid = [];
|
|
455
|
+
const indices = [];
|
|
456
|
+
const vertices = [];
|
|
457
|
+
const normals = [];
|
|
458
|
+
const uvs = [];
|
|
459
|
+
for (let iy = 0; iy <= heightSegments; iy++) {
|
|
460
|
+
const verticesRow = [];
|
|
461
|
+
const v = iy / heightSegments;
|
|
462
|
+
let uOffset = 0;
|
|
463
|
+
if (iy === 0 && thetaStart === 0) uOffset = 0.5 / widthSegments;
|
|
464
|
+
else if (iy === heightSegments && thetaEnd === Math.PI) uOffset = -0.5 / widthSegments;
|
|
465
|
+
for (let ix = 0; ix <= widthSegments; ix++) {
|
|
466
|
+
const u = ix / widthSegments;
|
|
467
|
+
const x = -radius * Math.cos(phiStart + u * phiLength) * Math.sin(thetaStart + v * thetaLength);
|
|
468
|
+
const y = radius * Math.cos(thetaStart + v * thetaLength);
|
|
469
|
+
const z = radius * Math.sin(phiStart + u * phiLength) * Math.sin(thetaStart + v * thetaLength);
|
|
470
|
+
vertices.push(x, y, z);
|
|
471
|
+
const n = normalize3(x, y, z);
|
|
472
|
+
normals.push(n[0], n[1], n[2]);
|
|
473
|
+
uvs.push(u + uOffset, 1 - v);
|
|
474
|
+
verticesRow.push(index++);
|
|
475
|
+
}
|
|
476
|
+
grid.push(verticesRow);
|
|
477
|
+
}
|
|
478
|
+
for (let iy = 0; iy < heightSegments; iy++) {
|
|
479
|
+
for (let ix = 0; ix < widthSegments; ix++) {
|
|
480
|
+
const a = grid[iy][ix + 1];
|
|
481
|
+
const b = grid[iy][ix];
|
|
482
|
+
const c = grid[iy + 1][ix];
|
|
483
|
+
const d = grid[iy + 1][ix + 1];
|
|
484
|
+
if (iy !== 0 || thetaStart > 0) indices.push(a, b, d);
|
|
485
|
+
if (iy !== heightSegments - 1 || thetaEnd < Math.PI) indices.push(b, c, d);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
return pack("SphereGeometry", parameters, { vertices, normals, uvs, indices, groups: [] });
|
|
489
|
+
}
|
|
490
|
+
function plane(width = 1, height = 1, widthSegments = 1, heightSegments = 1) {
|
|
491
|
+
const parameters = { width, height, widthSegments, heightSegments };
|
|
492
|
+
const widthHalf = width / 2;
|
|
493
|
+
const heightHalf = height / 2;
|
|
494
|
+
const gridX = Math.floor(widthSegments);
|
|
495
|
+
const gridY = Math.floor(heightSegments);
|
|
496
|
+
const gridX1 = gridX + 1;
|
|
497
|
+
const gridY1 = gridY + 1;
|
|
498
|
+
const segmentWidth = width / gridX;
|
|
499
|
+
const segmentHeight = height / gridY;
|
|
500
|
+
const indices = [];
|
|
501
|
+
const vertices = [];
|
|
502
|
+
const normals = [];
|
|
503
|
+
const uvs = [];
|
|
504
|
+
for (let iy = 0; iy < gridY1; iy++) {
|
|
505
|
+
const y = iy * segmentHeight - heightHalf;
|
|
506
|
+
for (let ix = 0; ix < gridX1; ix++) {
|
|
507
|
+
const x = ix * segmentWidth - widthHalf;
|
|
508
|
+
vertices.push(x, -y, 0);
|
|
509
|
+
normals.push(0, 0, 1);
|
|
510
|
+
uvs.push(ix / gridX);
|
|
511
|
+
uvs.push(1 - iy / gridY);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
for (let iy = 0; iy < gridY; iy++) {
|
|
515
|
+
for (let ix = 0; ix < gridX; ix++) {
|
|
516
|
+
const a = ix + gridX1 * iy;
|
|
517
|
+
const b = ix + gridX1 * (iy + 1);
|
|
518
|
+
const c = ix + 1 + gridX1 * (iy + 1);
|
|
519
|
+
const d = ix + 1 + gridX1 * iy;
|
|
520
|
+
indices.push(a, b, d);
|
|
521
|
+
indices.push(b, c, d);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
return pack("PlaneGeometry", parameters, { vertices, normals, uvs, indices, groups: [] });
|
|
525
|
+
}
|
|
526
|
+
function circle(radius = 1, segments = 32, thetaStart = 0, thetaLength = Math.PI * 2) {
|
|
527
|
+
const parameters = { radius, segments, thetaStart, thetaLength };
|
|
528
|
+
segments = Math.max(3, segments);
|
|
529
|
+
const indices = [];
|
|
530
|
+
const vertices = [];
|
|
531
|
+
const normals = [];
|
|
532
|
+
const uvs = [];
|
|
533
|
+
vertices.push(0, 0, 0);
|
|
534
|
+
normals.push(0, 0, 1);
|
|
535
|
+
uvs.push(0.5, 0.5);
|
|
536
|
+
for (let s = 0, i = 3; s <= segments; s++, i += 3) {
|
|
537
|
+
const segment = thetaStart + s / segments * thetaLength;
|
|
538
|
+
vertices.push(radius * Math.cos(segment), radius * Math.sin(segment), 0);
|
|
539
|
+
normals.push(0, 0, 1);
|
|
540
|
+
uvs.push((vertices[i] / radius + 1) / 2, (vertices[i + 1] / radius + 1) / 2);
|
|
541
|
+
}
|
|
542
|
+
for (let i = 1; i <= segments; i++) indices.push(i, i + 1, 0);
|
|
543
|
+
return pack("CircleGeometry", parameters, { vertices, normals, uvs, indices, groups: [] });
|
|
544
|
+
}
|
|
545
|
+
function ring(innerRadius = 0.5, outerRadius = 1, thetaSegments = 32, phiSegments = 1, thetaStart = 0, thetaLength = Math.PI * 2) {
|
|
546
|
+
const parameters = { innerRadius, outerRadius, thetaSegments, phiSegments, thetaStart, thetaLength };
|
|
547
|
+
thetaSegments = Math.max(3, thetaSegments);
|
|
548
|
+
phiSegments = Math.max(1, phiSegments);
|
|
549
|
+
const indices = [];
|
|
550
|
+
const vertices = [];
|
|
551
|
+
const normals = [];
|
|
552
|
+
const uvs = [];
|
|
553
|
+
let radius = innerRadius;
|
|
554
|
+
const radiusStep = (outerRadius - innerRadius) / phiSegments;
|
|
555
|
+
for (let j = 0; j <= phiSegments; j++) {
|
|
556
|
+
for (let i = 0; i <= thetaSegments; i++) {
|
|
557
|
+
const segment = thetaStart + i / thetaSegments * thetaLength;
|
|
558
|
+
const x = radius * Math.cos(segment);
|
|
559
|
+
const y = radius * Math.sin(segment);
|
|
560
|
+
vertices.push(x, y, 0);
|
|
561
|
+
normals.push(0, 0, 1);
|
|
562
|
+
uvs.push((x / outerRadius + 1) / 2, (y / outerRadius + 1) / 2);
|
|
563
|
+
}
|
|
564
|
+
radius += radiusStep;
|
|
565
|
+
}
|
|
566
|
+
for (let j = 0; j < phiSegments; j++) {
|
|
567
|
+
const thetaSegmentLevel = j * (thetaSegments + 1);
|
|
568
|
+
for (let i = 0; i < thetaSegments; i++) {
|
|
569
|
+
const segment = i + thetaSegmentLevel;
|
|
570
|
+
const a = segment;
|
|
571
|
+
const b = segment + thetaSegments + 1;
|
|
572
|
+
const c = segment + thetaSegments + 2;
|
|
573
|
+
const d = segment + 1;
|
|
574
|
+
indices.push(a, b, d);
|
|
575
|
+
indices.push(b, c, d);
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
return pack("RingGeometry", parameters, { vertices, normals, uvs, indices, groups: [] });
|
|
579
|
+
}
|
|
580
|
+
function lathe(points, segments = 12, phiStart = 0, phiLength = Math.PI * 2) {
|
|
581
|
+
const parameters = { points: points.map(([x, y]) => ({ x, y })), segments, phiStart, phiLength };
|
|
582
|
+
segments = Math.floor(segments);
|
|
583
|
+
phiLength = Math.max(0, Math.min(Math.PI * 2, phiLength));
|
|
584
|
+
const indices = [];
|
|
585
|
+
const vertices = [];
|
|
586
|
+
const uvs = [];
|
|
587
|
+
const initNormals = [];
|
|
588
|
+
const normals = [];
|
|
589
|
+
const inverseSegments = 1 / segments;
|
|
590
|
+
const normal = { x: 0, y: 0, z: 0 };
|
|
591
|
+
const curNormal = { x: 0, y: 0, z: 0 };
|
|
592
|
+
const prevNormal = { x: 0, y: 0, z: 0 };
|
|
593
|
+
let dx = 0;
|
|
594
|
+
let dy = 0;
|
|
595
|
+
const last = points.length - 1;
|
|
596
|
+
const px = (j) => points[j][0];
|
|
597
|
+
const py = (j) => points[j][1];
|
|
598
|
+
const setNormalized = () => {
|
|
599
|
+
const n = normalize3(normal.x, normal.y, normal.z);
|
|
600
|
+
normal.x = n[0];
|
|
601
|
+
normal.y = n[1];
|
|
602
|
+
normal.z = n[2];
|
|
603
|
+
};
|
|
604
|
+
for (let j = 0; j <= last; j++) {
|
|
605
|
+
if (j === 0) {
|
|
606
|
+
dx = px(j + 1) - px(j);
|
|
607
|
+
dy = py(j + 1) - py(j);
|
|
608
|
+
normal.x = dy * 1;
|
|
609
|
+
normal.y = -dx;
|
|
610
|
+
normal.z = dy * 0;
|
|
611
|
+
prevNormal.x = normal.x;
|
|
612
|
+
prevNormal.y = normal.y;
|
|
613
|
+
prevNormal.z = normal.z;
|
|
614
|
+
setNormalized();
|
|
615
|
+
initNormals.push(normal.x, normal.y, normal.z);
|
|
616
|
+
} else if (j === last) {
|
|
617
|
+
initNormals.push(prevNormal.x, prevNormal.y, prevNormal.z);
|
|
618
|
+
} else {
|
|
619
|
+
dx = px(j + 1) - px(j);
|
|
620
|
+
dy = py(j + 1) - py(j);
|
|
621
|
+
normal.x = dy * 1;
|
|
622
|
+
normal.y = -dx;
|
|
623
|
+
normal.z = dy * 0;
|
|
624
|
+
curNormal.x = normal.x;
|
|
625
|
+
curNormal.y = normal.y;
|
|
626
|
+
curNormal.z = normal.z;
|
|
627
|
+
normal.x += prevNormal.x;
|
|
628
|
+
normal.y += prevNormal.y;
|
|
629
|
+
normal.z += prevNormal.z;
|
|
630
|
+
setNormalized();
|
|
631
|
+
initNormals.push(normal.x, normal.y, normal.z);
|
|
632
|
+
prevNormal.x = curNormal.x;
|
|
633
|
+
prevNormal.y = curNormal.y;
|
|
634
|
+
prevNormal.z = curNormal.z;
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
for (let i = 0; i <= segments; i++) {
|
|
638
|
+
const phi = phiStart + i * inverseSegments * phiLength;
|
|
639
|
+
const sin = Math.sin(phi);
|
|
640
|
+
const cos = Math.cos(phi);
|
|
641
|
+
for (let j = 0; j <= last; j++) {
|
|
642
|
+
vertices.push(px(j) * sin, py(j), px(j) * cos);
|
|
643
|
+
uvs.push(i / segments, j / (points.length - 1));
|
|
644
|
+
const x = initNormals[3 * j + 0] * sin;
|
|
645
|
+
const y = initNormals[3 * j + 1];
|
|
646
|
+
const z = initNormals[3 * j + 0] * cos;
|
|
647
|
+
normals.push(x, y, z);
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
for (let i = 0; i < segments; i++) {
|
|
651
|
+
for (let j = 0; j < points.length - 1; j++) {
|
|
652
|
+
const base = j + i * points.length;
|
|
653
|
+
const a = base;
|
|
654
|
+
const b = base + points.length;
|
|
655
|
+
const c = base + points.length + 1;
|
|
656
|
+
const d = base + 1;
|
|
657
|
+
indices.push(a, b, d);
|
|
658
|
+
indices.push(c, d, b);
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
return pack("LatheGeometry", parameters, { vertices, normals, uvs, indices, groups: [] });
|
|
662
|
+
}
|
|
663
|
+
function torus(radius = 1, tube = 0.4, radialSegments = 12, tubularSegments = 48, arc = Math.PI * 2) {
|
|
664
|
+
const parameters = { radius, tube, radialSegments, tubularSegments, arc };
|
|
665
|
+
radialSegments = Math.floor(radialSegments);
|
|
666
|
+
tubularSegments = Math.floor(tubularSegments);
|
|
667
|
+
const indices = [];
|
|
668
|
+
const vertices = [];
|
|
669
|
+
const normals = [];
|
|
670
|
+
const uvs = [];
|
|
671
|
+
for (let j = 0; j <= radialSegments; j++) {
|
|
672
|
+
for (let i = 0; i <= tubularSegments; i++) {
|
|
673
|
+
const u = i / tubularSegments * arc;
|
|
674
|
+
const v = j / radialSegments * Math.PI * 2;
|
|
675
|
+
const x = (radius + tube * Math.cos(v)) * Math.cos(u);
|
|
676
|
+
const y = (radius + tube * Math.cos(v)) * Math.sin(u);
|
|
677
|
+
const z = tube * Math.sin(v);
|
|
678
|
+
vertices.push(x, y, z);
|
|
679
|
+
const cx = radius * Math.cos(u);
|
|
680
|
+
const cy = radius * Math.sin(u);
|
|
681
|
+
const n = normalize3(x - cx, y - cy, z - 0);
|
|
682
|
+
normals.push(n[0], n[1], n[2]);
|
|
683
|
+
uvs.push(i / tubularSegments);
|
|
684
|
+
uvs.push(j / radialSegments);
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
for (let j = 1; j <= radialSegments; j++) {
|
|
688
|
+
for (let i = 1; i <= tubularSegments; i++) {
|
|
689
|
+
const a = (tubularSegments + 1) * j + i - 1;
|
|
690
|
+
const b = (tubularSegments + 1) * (j - 1) + i - 1;
|
|
691
|
+
const c = (tubularSegments + 1) * (j - 1) + i;
|
|
692
|
+
const d = (tubularSegments + 1) * j + i;
|
|
693
|
+
indices.push(a, b, d);
|
|
694
|
+
indices.push(b, c, d);
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
return pack("TorusGeometry", parameters, { vertices, normals, uvs, indices, groups: [] });
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
// src/primitives/difference.ts
|
|
701
|
+
function difference(size, subtract) {
|
|
702
|
+
const [w, h, d] = size;
|
|
703
|
+
const hw = w / 2;
|
|
704
|
+
const hh = h / 2;
|
|
705
|
+
const hd = d / 2;
|
|
706
|
+
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);
|
|
707
|
+
const cuts = (lo, hi, values) => [.../* @__PURE__ */ new Set([lo, hi, ...values.filter((v) => v > lo && v < hi)])].sort((a, b) => a - b);
|
|
708
|
+
const xs = cuts(-hw, hw, holes.flatMap((hole) => [hole.x0, hole.x1]));
|
|
709
|
+
const ys = cuts(-hh, hh, holes.flatMap((hole) => [hole.y0, hole.y1]));
|
|
710
|
+
const nx = xs.length - 1;
|
|
711
|
+
const ny = ys.length - 1;
|
|
712
|
+
const solid = (i, j) => {
|
|
713
|
+
if (i < 0 || j < 0 || i >= nx || j >= ny) return false;
|
|
714
|
+
const cx = (xs[i] + xs[i + 1]) / 2;
|
|
715
|
+
const cy = (ys[j] + ys[j + 1]) / 2;
|
|
716
|
+
return !holes.some((hole) => cx > hole.x0 && cx < hole.x1 && cy > hole.y0 && cy < hole.y1);
|
|
717
|
+
};
|
|
718
|
+
const positions = [];
|
|
719
|
+
const normals = [];
|
|
720
|
+
const uvs = [];
|
|
721
|
+
const indices = [];
|
|
722
|
+
const quad = (corners, normal, uv) => {
|
|
723
|
+
const base = positions.length / 3;
|
|
724
|
+
for (let k = 0; k < 4; k++) {
|
|
725
|
+
positions.push(...corners[k]);
|
|
726
|
+
normals.push(...normal);
|
|
727
|
+
uvs.push(...uv[k]);
|
|
728
|
+
}
|
|
729
|
+
indices.push(base, base + 1, base + 2, base, base + 2, base + 3);
|
|
730
|
+
};
|
|
731
|
+
const fu = (x) => (x + hw) / w;
|
|
732
|
+
const fv = (y) => (y + hh) / h;
|
|
733
|
+
for (let j = 0; j < ny; j++) {
|
|
734
|
+
for (let i = 0; i < nx; i++) {
|
|
735
|
+
if (!solid(i, j)) continue;
|
|
736
|
+
const x0 = xs[i];
|
|
737
|
+
const x1 = xs[i + 1];
|
|
738
|
+
const y0 = ys[j];
|
|
739
|
+
const y1 = ys[j + 1];
|
|
740
|
+
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)]]);
|
|
741
|
+
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)]]);
|
|
742
|
+
const zu = (z) => (z + hd) / d;
|
|
743
|
+
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)]]);
|
|
744
|
+
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)]]);
|
|
745
|
+
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)]]);
|
|
746
|
+
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)]]);
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
return {
|
|
750
|
+
type: "BufferGeometry",
|
|
751
|
+
parameters: { size: [w, h, d], subtract: holes.map((hole) => ({ min: [hole.x0, hole.y0], max: [hole.x1, hole.y1] })) },
|
|
752
|
+
positions: new Float32Array(positions),
|
|
753
|
+
normals: new Float32Array(normals),
|
|
754
|
+
uvs: new Float32Array(uvs),
|
|
755
|
+
indices: indexArray(indices),
|
|
756
|
+
groups: []
|
|
757
|
+
};
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
// src/primitives/earcut.ts
|
|
761
|
+
var Earcut = {
|
|
762
|
+
triangulate: function(data, holeIndices, dim = 2) {
|
|
763
|
+
const hasHoles = holeIndices && holeIndices.length;
|
|
764
|
+
const outerLen = hasHoles ? holeIndices[0] * dim : data.length;
|
|
765
|
+
let outerNode = linkedList(data, 0, outerLen, dim, true);
|
|
766
|
+
const triangles = [];
|
|
767
|
+
if (!outerNode || outerNode.next === outerNode.prev) return triangles;
|
|
768
|
+
let minX, minY, maxX, maxY, x, y, invSize;
|
|
769
|
+
if (hasHoles) outerNode = eliminateHoles(data, holeIndices, outerNode, dim);
|
|
770
|
+
if (data.length > 80 * dim) {
|
|
771
|
+
minX = maxX = data[0];
|
|
772
|
+
minY = maxY = data[1];
|
|
773
|
+
for (let i = dim; i < outerLen; i += dim) {
|
|
774
|
+
x = data[i];
|
|
775
|
+
y = data[i + 1];
|
|
776
|
+
if (x < minX) minX = x;
|
|
777
|
+
if (y < minY) minY = y;
|
|
778
|
+
if (x > maxX) maxX = x;
|
|
779
|
+
if (y > maxY) maxY = y;
|
|
780
|
+
}
|
|
781
|
+
invSize = Math.max(maxX - minX, maxY - minY);
|
|
782
|
+
invSize = invSize !== 0 ? 32767 / invSize : 0;
|
|
783
|
+
}
|
|
784
|
+
earcutLinked(outerNode, triangles, dim, minX, minY, invSize, 0);
|
|
785
|
+
return triangles;
|
|
786
|
+
}
|
|
787
|
+
};
|
|
788
|
+
function linkedList(data, start, end, dim, clockwise) {
|
|
789
|
+
let i, last;
|
|
790
|
+
if (clockwise === signedArea(data, start, end, dim) > 0) {
|
|
791
|
+
for (i = start; i < end; i += dim) last = insertNode(i, data[i], data[i + 1], last);
|
|
792
|
+
} else {
|
|
793
|
+
for (i = end - dim; i >= start; i -= dim) last = insertNode(i, data[i], data[i + 1], last);
|
|
794
|
+
}
|
|
795
|
+
if (last && equals(last, last.next)) {
|
|
796
|
+
removeNode(last);
|
|
797
|
+
last = last.next;
|
|
798
|
+
}
|
|
799
|
+
return last;
|
|
800
|
+
}
|
|
801
|
+
function filterPoints(start, end) {
|
|
802
|
+
if (!start) return start;
|
|
803
|
+
if (!end) end = start;
|
|
804
|
+
let p = start, again;
|
|
805
|
+
do {
|
|
806
|
+
again = false;
|
|
807
|
+
if (!p.steiner && (equals(p, p.next) || area(p.prev, p, p.next) === 0)) {
|
|
808
|
+
removeNode(p);
|
|
809
|
+
p = end = p.prev;
|
|
810
|
+
if (p === p.next) break;
|
|
811
|
+
again = true;
|
|
812
|
+
} else {
|
|
813
|
+
p = p.next;
|
|
814
|
+
}
|
|
815
|
+
} while (again || p !== end);
|
|
816
|
+
return end;
|
|
817
|
+
}
|
|
818
|
+
function earcutLinked(ear, triangles, dim, minX, minY, invSize, pass) {
|
|
819
|
+
if (!ear) return;
|
|
820
|
+
if (!pass && invSize) indexCurve(ear, minX, minY, invSize);
|
|
821
|
+
let stop = ear, prev, next;
|
|
822
|
+
while (ear.prev !== ear.next) {
|
|
823
|
+
prev = ear.prev;
|
|
824
|
+
next = ear.next;
|
|
825
|
+
if (invSize ? isEarHashed(ear, minX, minY, invSize) : isEar(ear)) {
|
|
826
|
+
triangles.push(prev.i / dim | 0);
|
|
827
|
+
triangles.push(ear.i / dim | 0);
|
|
828
|
+
triangles.push(next.i / dim | 0);
|
|
829
|
+
removeNode(ear);
|
|
830
|
+
ear = next.next;
|
|
831
|
+
stop = next.next;
|
|
832
|
+
continue;
|
|
833
|
+
}
|
|
834
|
+
ear = next;
|
|
835
|
+
if (ear === stop) {
|
|
836
|
+
if (!pass) {
|
|
837
|
+
earcutLinked(filterPoints(ear), triangles, dim, minX, minY, invSize, 1);
|
|
838
|
+
} else if (pass === 1) {
|
|
839
|
+
ear = cureLocalIntersections(filterPoints(ear), triangles, dim);
|
|
840
|
+
earcutLinked(ear, triangles, dim, minX, minY, invSize, 2);
|
|
841
|
+
} else if (pass === 2) {
|
|
842
|
+
splitEarcut(ear, triangles, dim, minX, minY, invSize);
|
|
843
|
+
}
|
|
844
|
+
break;
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
function isEar(ear) {
|
|
849
|
+
const a = ear.prev, b = ear, c = ear.next;
|
|
850
|
+
if (area(a, b, c) >= 0) return false;
|
|
851
|
+
const ax = a.x, bx = b.x, cx = c.x, ay = a.y, by = b.y, cy = c.y;
|
|
852
|
+
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;
|
|
853
|
+
let p = c.next;
|
|
854
|
+
while (p !== a) {
|
|
855
|
+
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;
|
|
856
|
+
p = p.next;
|
|
857
|
+
}
|
|
858
|
+
return true;
|
|
859
|
+
}
|
|
860
|
+
function isEarHashed(ear, minX, minY, invSize) {
|
|
861
|
+
const a = ear.prev, b = ear, c = ear.next;
|
|
862
|
+
if (area(a, b, c) >= 0) return false;
|
|
863
|
+
const ax = a.x, bx = b.x, cx = c.x, ay = a.y, by = b.y, cy = c.y;
|
|
864
|
+
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;
|
|
865
|
+
const minZ = zOrder(x0, y0, minX, minY, invSize), maxZ = zOrder(x1, y1, minX, minY, invSize);
|
|
866
|
+
let p = ear.prevZ, n = ear.nextZ;
|
|
867
|
+
while (p && p.z >= minZ && n && n.z <= maxZ) {
|
|
868
|
+
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;
|
|
869
|
+
p = p.prevZ;
|
|
870
|
+
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;
|
|
871
|
+
n = n.nextZ;
|
|
872
|
+
}
|
|
873
|
+
while (p && p.z >= minZ) {
|
|
874
|
+
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;
|
|
875
|
+
p = p.prevZ;
|
|
876
|
+
}
|
|
877
|
+
while (n && n.z <= maxZ) {
|
|
878
|
+
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;
|
|
879
|
+
n = n.nextZ;
|
|
880
|
+
}
|
|
881
|
+
return true;
|
|
882
|
+
}
|
|
883
|
+
function cureLocalIntersections(start, triangles, dim) {
|
|
884
|
+
let p = start;
|
|
885
|
+
do {
|
|
886
|
+
const a = p.prev, b = p.next.next;
|
|
887
|
+
if (!equals(a, b) && intersects(a, p, p.next, b) && locallyInside(a, b) && locallyInside(b, a)) {
|
|
888
|
+
triangles.push(a.i / dim | 0);
|
|
889
|
+
triangles.push(p.i / dim | 0);
|
|
890
|
+
triangles.push(b.i / dim | 0);
|
|
891
|
+
removeNode(p);
|
|
892
|
+
removeNode(p.next);
|
|
893
|
+
p = start = b;
|
|
894
|
+
}
|
|
895
|
+
p = p.next;
|
|
896
|
+
} while (p !== start);
|
|
897
|
+
return filterPoints(p);
|
|
898
|
+
}
|
|
899
|
+
function splitEarcut(start, triangles, dim, minX, minY, invSize) {
|
|
900
|
+
let a = start;
|
|
901
|
+
do {
|
|
902
|
+
let b = a.next.next;
|
|
903
|
+
while (b !== a.prev) {
|
|
904
|
+
if (a.i !== b.i && isValidDiagonal(a, b)) {
|
|
905
|
+
let c = splitPolygon(a, b);
|
|
906
|
+
a = filterPoints(a, a.next);
|
|
907
|
+
c = filterPoints(c, c.next);
|
|
908
|
+
earcutLinked(a, triangles, dim, minX, minY, invSize, 0);
|
|
909
|
+
earcutLinked(c, triangles, dim, minX, minY, invSize, 0);
|
|
910
|
+
return;
|
|
911
|
+
}
|
|
912
|
+
b = b.next;
|
|
913
|
+
}
|
|
914
|
+
a = a.next;
|
|
915
|
+
} while (a !== start);
|
|
916
|
+
}
|
|
917
|
+
function eliminateHoles(data, holeIndices, outerNode, dim) {
|
|
918
|
+
const queue = [];
|
|
919
|
+
let i, len, start, end, list2;
|
|
920
|
+
for (i = 0, len = holeIndices.length; i < len; i++) {
|
|
921
|
+
start = holeIndices[i] * dim;
|
|
922
|
+
end = i < len - 1 ? holeIndices[i + 1] * dim : data.length;
|
|
923
|
+
list2 = linkedList(data, start, end, dim, false);
|
|
924
|
+
if (list2 === list2.next) list2.steiner = true;
|
|
925
|
+
queue.push(getLeftmost(list2));
|
|
926
|
+
}
|
|
927
|
+
queue.sort(compareX);
|
|
928
|
+
for (i = 0; i < queue.length; i++) {
|
|
929
|
+
outerNode = eliminateHole(queue[i], outerNode);
|
|
930
|
+
}
|
|
931
|
+
return outerNode;
|
|
932
|
+
}
|
|
933
|
+
function compareX(a, b) {
|
|
934
|
+
return a.x - b.x;
|
|
935
|
+
}
|
|
936
|
+
function eliminateHole(hole, outerNode) {
|
|
937
|
+
const bridge = findHoleBridge(hole, outerNode);
|
|
938
|
+
if (!bridge) {
|
|
939
|
+
return outerNode;
|
|
940
|
+
}
|
|
941
|
+
const bridgeReverse = splitPolygon(bridge, hole);
|
|
942
|
+
filterPoints(bridgeReverse, bridgeReverse.next);
|
|
943
|
+
return filterPoints(bridge, bridge.next);
|
|
944
|
+
}
|
|
945
|
+
function findHoleBridge(hole, outerNode) {
|
|
946
|
+
let p = outerNode, qx = -Infinity, m;
|
|
947
|
+
const hx = hole.x, hy = hole.y;
|
|
948
|
+
do {
|
|
949
|
+
if (hy <= p.y && hy >= p.next.y && p.next.y !== p.y) {
|
|
950
|
+
const x = p.x + (hy - p.y) * (p.next.x - p.x) / (p.next.y - p.y);
|
|
951
|
+
if (x <= hx && x > qx) {
|
|
952
|
+
qx = x;
|
|
953
|
+
m = p.x < p.next.x ? p : p.next;
|
|
954
|
+
if (x === hx) return m;
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
p = p.next;
|
|
958
|
+
} while (p !== outerNode);
|
|
959
|
+
if (!m) return null;
|
|
960
|
+
const stop = m, mx = m.x, my = m.y;
|
|
961
|
+
let tanMin = Infinity, tan;
|
|
962
|
+
p = m;
|
|
963
|
+
do {
|
|
964
|
+
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)) {
|
|
965
|
+
tan = Math.abs(hy - p.y) / (hx - p.x);
|
|
966
|
+
if (locallyInside(p, hole) && (tan < tanMin || tan === tanMin && (p.x > m.x || p.x === m.x && sectorContainsSector(m, p)))) {
|
|
967
|
+
m = p;
|
|
968
|
+
tanMin = tan;
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
p = p.next;
|
|
972
|
+
} while (p !== stop);
|
|
973
|
+
return m;
|
|
974
|
+
}
|
|
975
|
+
function sectorContainsSector(m, p) {
|
|
976
|
+
return area(m.prev, m, p.prev) < 0 && area(p.next, m, m.next) < 0;
|
|
977
|
+
}
|
|
978
|
+
function indexCurve(start, minX, minY, invSize) {
|
|
979
|
+
let p = start;
|
|
980
|
+
do {
|
|
981
|
+
if (p.z === 0) p.z = zOrder(p.x, p.y, minX, minY, invSize);
|
|
982
|
+
p.prevZ = p.prev;
|
|
983
|
+
p.nextZ = p.next;
|
|
984
|
+
p = p.next;
|
|
985
|
+
} while (p !== start);
|
|
986
|
+
p.prevZ.nextZ = null;
|
|
987
|
+
p.prevZ = null;
|
|
988
|
+
sortLinked(p);
|
|
989
|
+
}
|
|
990
|
+
function sortLinked(list2) {
|
|
991
|
+
let i, p, q, e, tail, numMerges, pSize, qSize, inSize = 1;
|
|
992
|
+
do {
|
|
993
|
+
p = list2;
|
|
994
|
+
list2 = null;
|
|
995
|
+
tail = null;
|
|
996
|
+
numMerges = 0;
|
|
997
|
+
while (p) {
|
|
998
|
+
numMerges++;
|
|
999
|
+
q = p;
|
|
1000
|
+
pSize = 0;
|
|
1001
|
+
for (i = 0; i < inSize; i++) {
|
|
1002
|
+
pSize++;
|
|
1003
|
+
q = q.nextZ;
|
|
1004
|
+
if (!q) break;
|
|
1005
|
+
}
|
|
1006
|
+
qSize = inSize;
|
|
1007
|
+
while (pSize > 0 || qSize > 0 && q) {
|
|
1008
|
+
if (pSize !== 0 && (qSize === 0 || !q || p.z <= q.z)) {
|
|
1009
|
+
e = p;
|
|
1010
|
+
p = p.nextZ;
|
|
1011
|
+
pSize--;
|
|
1012
|
+
} else {
|
|
1013
|
+
e = q;
|
|
1014
|
+
q = q.nextZ;
|
|
1015
|
+
qSize--;
|
|
1016
|
+
}
|
|
1017
|
+
if (tail) tail.nextZ = e;
|
|
1018
|
+
else list2 = e;
|
|
1019
|
+
e.prevZ = tail;
|
|
1020
|
+
tail = e;
|
|
1021
|
+
}
|
|
1022
|
+
p = q;
|
|
1023
|
+
}
|
|
1024
|
+
tail.nextZ = null;
|
|
1025
|
+
inSize *= 2;
|
|
1026
|
+
} while (numMerges > 1);
|
|
1027
|
+
return list2;
|
|
1028
|
+
}
|
|
1029
|
+
function zOrder(x, y, minX, minY, invSize) {
|
|
1030
|
+
x = (x - minX) * invSize | 0;
|
|
1031
|
+
y = (y - minY) * invSize | 0;
|
|
1032
|
+
x = (x | x << 8) & 16711935;
|
|
1033
|
+
x = (x | x << 4) & 252645135;
|
|
1034
|
+
x = (x | x << 2) & 858993459;
|
|
1035
|
+
x = (x | x << 1) & 1431655765;
|
|
1036
|
+
y = (y | y << 8) & 16711935;
|
|
1037
|
+
y = (y | y << 4) & 252645135;
|
|
1038
|
+
y = (y | y << 2) & 858993459;
|
|
1039
|
+
y = (y | y << 1) & 1431655765;
|
|
1040
|
+
return x | y << 1;
|
|
1041
|
+
}
|
|
1042
|
+
function getLeftmost(start) {
|
|
1043
|
+
let p = start, leftmost = start;
|
|
1044
|
+
do {
|
|
1045
|
+
if (p.x < leftmost.x || p.x === leftmost.x && p.y < leftmost.y) leftmost = p;
|
|
1046
|
+
p = p.next;
|
|
1047
|
+
} while (p !== start);
|
|
1048
|
+
return leftmost;
|
|
1049
|
+
}
|
|
1050
|
+
function pointInTriangle(ax, ay, bx, by, cx, cy, px, py) {
|
|
1051
|
+
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);
|
|
1052
|
+
}
|
|
1053
|
+
function isValidDiagonal(a, b) {
|
|
1054
|
+
return a.next.i !== b.i && a.prev.i !== b.i && !intersectsPolygon(a, b) && // dones't intersect other edges
|
|
1055
|
+
(locallyInside(a, b) && locallyInside(b, a) && middleInside(a, b) && // locally visible
|
|
1056
|
+
(area(a.prev, a, b.prev) || area(a, b.prev, b)) || // does not create opposite-facing sectors
|
|
1057
|
+
equals(a, b) && area(a.prev, a, a.next) > 0 && area(b.prev, b, b.next) > 0);
|
|
1058
|
+
}
|
|
1059
|
+
function area(p, q, r) {
|
|
1060
|
+
return (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
|
|
1061
|
+
}
|
|
1062
|
+
function equals(p1, p2) {
|
|
1063
|
+
return p1.x === p2.x && p1.y === p2.y;
|
|
1064
|
+
}
|
|
1065
|
+
function intersects(p1, q1, p2, q2) {
|
|
1066
|
+
const o1 = sign(area(p1, q1, p2));
|
|
1067
|
+
const o2 = sign(area(p1, q1, q2));
|
|
1068
|
+
const o3 = sign(area(p2, q2, p1));
|
|
1069
|
+
const o4 = sign(area(p2, q2, q1));
|
|
1070
|
+
if (o1 !== o2 && o3 !== o4) return true;
|
|
1071
|
+
if (o1 === 0 && onSegment(p1, p2, q1)) return true;
|
|
1072
|
+
if (o2 === 0 && onSegment(p1, q2, q1)) return true;
|
|
1073
|
+
if (o3 === 0 && onSegment(p2, p1, q2)) return true;
|
|
1074
|
+
if (o4 === 0 && onSegment(p2, q1, q2)) return true;
|
|
1075
|
+
return false;
|
|
1076
|
+
}
|
|
1077
|
+
function onSegment(p, q, r) {
|
|
1078
|
+
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);
|
|
1079
|
+
}
|
|
1080
|
+
function sign(num2) {
|
|
1081
|
+
return num2 > 0 ? 1 : num2 < 0 ? -1 : 0;
|
|
1082
|
+
}
|
|
1083
|
+
function intersectsPolygon(a, b) {
|
|
1084
|
+
let p = a;
|
|
1085
|
+
do {
|
|
1086
|
+
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;
|
|
1087
|
+
p = p.next;
|
|
1088
|
+
} while (p !== a);
|
|
1089
|
+
return false;
|
|
1090
|
+
}
|
|
1091
|
+
function locallyInside(a, b) {
|
|
1092
|
+
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;
|
|
1093
|
+
}
|
|
1094
|
+
function middleInside(a, b) {
|
|
1095
|
+
let p = a, inside = false;
|
|
1096
|
+
const px = (a.x + b.x) / 2, py = (a.y + b.y) / 2;
|
|
1097
|
+
do {
|
|
1098
|
+
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)
|
|
1099
|
+
inside = !inside;
|
|
1100
|
+
p = p.next;
|
|
1101
|
+
} while (p !== a);
|
|
1102
|
+
return inside;
|
|
1103
|
+
}
|
|
1104
|
+
function splitPolygon(a, b) {
|
|
1105
|
+
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;
|
|
1106
|
+
a.next = b;
|
|
1107
|
+
b.prev = a;
|
|
1108
|
+
a2.next = an;
|
|
1109
|
+
an.prev = a2;
|
|
1110
|
+
b2.next = a2;
|
|
1111
|
+
a2.prev = b2;
|
|
1112
|
+
bp.next = b2;
|
|
1113
|
+
b2.prev = bp;
|
|
1114
|
+
return b2;
|
|
1115
|
+
}
|
|
1116
|
+
function insertNode(i, x, y, last) {
|
|
1117
|
+
const p = new Node(i, x, y);
|
|
1118
|
+
if (!last) {
|
|
1119
|
+
p.prev = p;
|
|
1120
|
+
p.next = p;
|
|
1121
|
+
} else {
|
|
1122
|
+
p.next = last.next;
|
|
1123
|
+
p.prev = last;
|
|
1124
|
+
last.next.prev = p;
|
|
1125
|
+
last.next = p;
|
|
1126
|
+
}
|
|
1127
|
+
return p;
|
|
1128
|
+
}
|
|
1129
|
+
function removeNode(p) {
|
|
1130
|
+
p.next.prev = p.prev;
|
|
1131
|
+
p.prev.next = p.next;
|
|
1132
|
+
if (p.prevZ) p.prevZ.nextZ = p.nextZ;
|
|
1133
|
+
if (p.nextZ) p.nextZ.prevZ = p.prevZ;
|
|
1134
|
+
}
|
|
1135
|
+
function Node(i, x, y) {
|
|
1136
|
+
this.i = i;
|
|
1137
|
+
this.x = x;
|
|
1138
|
+
this.y = y;
|
|
1139
|
+
this.prev = null;
|
|
1140
|
+
this.next = null;
|
|
1141
|
+
this.z = 0;
|
|
1142
|
+
this.prevZ = null;
|
|
1143
|
+
this.nextZ = null;
|
|
1144
|
+
this.steiner = false;
|
|
1145
|
+
}
|
|
1146
|
+
function signedArea(data, start, end, dim) {
|
|
1147
|
+
let sum = 0;
|
|
1148
|
+
for (let i = start, j = end - dim; i < end; i += dim) {
|
|
1149
|
+
sum += (data[j] - data[i]) * (data[i + 1] + data[j + 1]);
|
|
1150
|
+
j = i;
|
|
1151
|
+
}
|
|
1152
|
+
return sum;
|
|
1153
|
+
}
|
|
1154
|
+
|
|
1155
|
+
// src/primitives/extrude.ts
|
|
1156
|
+
var equals2 = (a, b) => a.x === b.x && a.y === b.y;
|
|
1157
|
+
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 };
|
|
1158
|
+
function pathPoints(points) {
|
|
1159
|
+
const out = [];
|
|
1160
|
+
let last;
|
|
1161
|
+
for (let i = 1; i < points.length; i++) {
|
|
1162
|
+
const v1 = { x: points[i - 1][0], y: points[i - 1][1] };
|
|
1163
|
+
const v2 = { x: points[i][0], y: points[i][1] };
|
|
1164
|
+
for (let d = 0; d <= 1; d++) {
|
|
1165
|
+
const point = linePoint(v1, v2, d / 1);
|
|
1166
|
+
if (last && equals2(last, point)) continue;
|
|
1167
|
+
out.push(point);
|
|
1168
|
+
last = point;
|
|
1169
|
+
}
|
|
1170
|
+
}
|
|
1171
|
+
return out;
|
|
1172
|
+
}
|
|
1173
|
+
function area2(contour) {
|
|
1174
|
+
const n = contour.length;
|
|
1175
|
+
let a = 0;
|
|
1176
|
+
for (let p = n - 1, q = 0; q < n; p = q++) a += contour[p].x * contour[q].y - contour[q].x * contour[p].y;
|
|
1177
|
+
return a * 0.5;
|
|
1178
|
+
}
|
|
1179
|
+
var isClockWise = (pts) => area2(pts) < 0;
|
|
1180
|
+
function removeDupEndPts(points) {
|
|
1181
|
+
const l = points.length;
|
|
1182
|
+
if (l > 2 && equals2(points[l - 1], points[0])) points.pop();
|
|
1183
|
+
}
|
|
1184
|
+
function triangulateShape(contour, holes) {
|
|
1185
|
+
const vertices = [];
|
|
1186
|
+
const holeIndices = [];
|
|
1187
|
+
const faces = [];
|
|
1188
|
+
removeDupEndPts(contour);
|
|
1189
|
+
for (const p of contour) vertices.push(p.x, p.y);
|
|
1190
|
+
let holeIndex = contour.length;
|
|
1191
|
+
holes.forEach(removeDupEndPts);
|
|
1192
|
+
for (let i = 0; i < holes.length; i++) {
|
|
1193
|
+
holeIndices.push(holeIndex);
|
|
1194
|
+
holeIndex += holes[i].length;
|
|
1195
|
+
for (const p of holes[i]) vertices.push(p.x, p.y);
|
|
1196
|
+
}
|
|
1197
|
+
const triangles = Earcut.triangulate(vertices, holeIndices);
|
|
1198
|
+
for (let i = 0; i < triangles.length; i += 3) faces.push(triangles.slice(i, i + 3));
|
|
1199
|
+
return faces;
|
|
1200
|
+
}
|
|
1201
|
+
function computeVertexNormals(positions, indices) {
|
|
1202
|
+
const normals = new Float32Array(positions.length);
|
|
1203
|
+
const get = (array, i) => [array[i * 3], array[i * 3 + 1], array[i * 3 + 2]];
|
|
1204
|
+
const face = (vA, vB, vC) => {
|
|
1205
|
+
const pA = get(positions, vA);
|
|
1206
|
+
const pB = get(positions, vB);
|
|
1207
|
+
const pC = get(positions, vC);
|
|
1208
|
+
const cb = [pC[0] - pB[0], pC[1] - pB[1], pC[2] - pB[2]];
|
|
1209
|
+
const ab = [pA[0] - pB[0], pA[1] - pB[1], pA[2] - pB[2]];
|
|
1210
|
+
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]];
|
|
1211
|
+
};
|
|
1212
|
+
const set = (i, x, y, z) => {
|
|
1213
|
+
normals[i * 3] = x;
|
|
1214
|
+
normals[i * 3 + 1] = y;
|
|
1215
|
+
normals[i * 3 + 2] = z;
|
|
1216
|
+
};
|
|
1217
|
+
if (indices) {
|
|
1218
|
+
for (let i = 0; i < indices.length; i += 3) {
|
|
1219
|
+
const vA = indices[i];
|
|
1220
|
+
const vB = indices[i + 1];
|
|
1221
|
+
const vC = indices[i + 2];
|
|
1222
|
+
const cb = face(vA, vB, vC);
|
|
1223
|
+
for (const v of [vA, vB, vC]) {
|
|
1224
|
+
const n = get(normals, v);
|
|
1225
|
+
set(v, n[0] + cb[0], n[1] + cb[1], n[2] + cb[2]);
|
|
1226
|
+
}
|
|
1227
|
+
}
|
|
1228
|
+
} else {
|
|
1229
|
+
for (let i = 0; i < positions.length / 3; i += 3) {
|
|
1230
|
+
const cb = face(i, i + 1, i + 2);
|
|
1231
|
+
set(i, cb[0], cb[1], cb[2]);
|
|
1232
|
+
set(i + 1, cb[0], cb[1], cb[2]);
|
|
1233
|
+
set(i + 2, cb[0], cb[1], cb[2]);
|
|
1234
|
+
}
|
|
1235
|
+
}
|
|
1236
|
+
for (let i = 0; i < normals.length / 3; i++) {
|
|
1237
|
+
const [x, y, z] = get(normals, i);
|
|
1238
|
+
const s = 1 / (Math.sqrt(x * x + y * y + z * z) || 1);
|
|
1239
|
+
set(i, x * s, y * s, z * s);
|
|
1240
|
+
}
|
|
1241
|
+
return normals;
|
|
1242
|
+
}
|
|
1243
|
+
function extrude(outline, depth = 1, steps = 1, bevel = void 0) {
|
|
1244
|
+
const parameters = { shape: outline, depth, steps, bevel: bevel ?? {} };
|
|
1245
|
+
const verticesArray = [];
|
|
1246
|
+
const uvArray = [];
|
|
1247
|
+
const groups = [];
|
|
1248
|
+
const placeholder = [];
|
|
1249
|
+
const bevelEnabled = bevel !== false;
|
|
1250
|
+
const spec = bevel === false || bevel === void 0 ? {} : bevel;
|
|
1251
|
+
let bevelThickness = spec.thickness !== void 0 ? spec.thickness : 0.2;
|
|
1252
|
+
let bevelSize = spec.size !== void 0 ? spec.size : bevelThickness - 0.1;
|
|
1253
|
+
let bevelOffset = spec.offset !== void 0 ? spec.offset : 0;
|
|
1254
|
+
let bevelSegments = spec.segments !== void 0 ? spec.segments : 3;
|
|
1255
|
+
if (!bevelEnabled) {
|
|
1256
|
+
bevelSegments = 0;
|
|
1257
|
+
bevelThickness = 0;
|
|
1258
|
+
bevelSize = 0;
|
|
1259
|
+
bevelOffset = 0;
|
|
1260
|
+
}
|
|
1261
|
+
let vertices = pathPoints(outline.outer);
|
|
1262
|
+
const holes = (outline.holes ?? []).map(pathPoints);
|
|
1263
|
+
const reverse = !isClockWise(vertices);
|
|
1264
|
+
if (reverse) {
|
|
1265
|
+
vertices = vertices.reverse();
|
|
1266
|
+
for (let h = 0; h < holes.length; h++) {
|
|
1267
|
+
const ahole = holes[h];
|
|
1268
|
+
if (isClockWise(ahole)) holes[h] = ahole.reverse();
|
|
1269
|
+
}
|
|
1270
|
+
}
|
|
1271
|
+
const faces = triangulateShape(vertices, holes);
|
|
1272
|
+
const contour = vertices;
|
|
1273
|
+
for (const ahole of holes) vertices = vertices.concat(ahole);
|
|
1274
|
+
const scalePt2 = (pt, vec, size) => ({ x: pt.x + vec.x * size, y: pt.y + vec.y * size });
|
|
1275
|
+
const vlen = vertices.length;
|
|
1276
|
+
const flen = faces.length;
|
|
1277
|
+
const getBevelVec = (inPt, inPrev, inNext) => {
|
|
1278
|
+
let vTransX;
|
|
1279
|
+
let vTransY;
|
|
1280
|
+
let shrinkBy;
|
|
1281
|
+
const vPrevX = inPt.x - inPrev.x;
|
|
1282
|
+
const vPrevY = inPt.y - inPrev.y;
|
|
1283
|
+
const vNextX = inNext.x - inPt.x;
|
|
1284
|
+
const vNextY = inNext.y - inPt.y;
|
|
1285
|
+
const vPrevLensq = vPrevX * vPrevX + vPrevY * vPrevY;
|
|
1286
|
+
const collinear0 = vPrevX * vNextY - vPrevY * vNextX;
|
|
1287
|
+
if (Math.abs(collinear0) > Number.EPSILON) {
|
|
1288
|
+
const vPrevLen = Math.sqrt(vPrevLensq);
|
|
1289
|
+
const vNextLen = Math.sqrt(vNextX * vNextX + vNextY * vNextY);
|
|
1290
|
+
const ptPrevShiftX = inPrev.x - vPrevY / vPrevLen;
|
|
1291
|
+
const ptPrevShiftY = inPrev.y + vPrevX / vPrevLen;
|
|
1292
|
+
const ptNextShiftX = inNext.x - vNextY / vNextLen;
|
|
1293
|
+
const ptNextShiftY = inNext.y + vNextX / vNextLen;
|
|
1294
|
+
const sf = ((ptNextShiftX - ptPrevShiftX) * vNextY - (ptNextShiftY - ptPrevShiftY) * vNextX) / (vPrevX * vNextY - vPrevY * vNextX);
|
|
1295
|
+
vTransX = ptPrevShiftX + vPrevX * sf - inPt.x;
|
|
1296
|
+
vTransY = ptPrevShiftY + vPrevY * sf - inPt.y;
|
|
1297
|
+
const vTransLensq = vTransX * vTransX + vTransY * vTransY;
|
|
1298
|
+
if (vTransLensq <= 2) return { x: vTransX, y: vTransY };
|
|
1299
|
+
shrinkBy = Math.sqrt(vTransLensq / 2);
|
|
1300
|
+
} else {
|
|
1301
|
+
let directionEq = false;
|
|
1302
|
+
if (vPrevX > Number.EPSILON) {
|
|
1303
|
+
if (vNextX > Number.EPSILON) directionEq = true;
|
|
1304
|
+
} else if (vPrevX < -Number.EPSILON) {
|
|
1305
|
+
if (vNextX < -Number.EPSILON) directionEq = true;
|
|
1306
|
+
} else if (Math.sign(vPrevY) === Math.sign(vNextY)) {
|
|
1307
|
+
directionEq = true;
|
|
1308
|
+
}
|
|
1309
|
+
if (directionEq) {
|
|
1310
|
+
vTransX = -vPrevY;
|
|
1311
|
+
vTransY = vPrevX;
|
|
1312
|
+
shrinkBy = Math.sqrt(vPrevLensq);
|
|
1313
|
+
} else {
|
|
1314
|
+
vTransX = vPrevX;
|
|
1315
|
+
vTransY = vPrevY;
|
|
1316
|
+
shrinkBy = Math.sqrt(vPrevLensq / 2);
|
|
1317
|
+
}
|
|
1318
|
+
}
|
|
1319
|
+
return { x: vTransX / shrinkBy, y: vTransY / shrinkBy };
|
|
1320
|
+
};
|
|
1321
|
+
const movements = (ring2) => {
|
|
1322
|
+
const out = [];
|
|
1323
|
+
for (let i = 0, il = ring2.length, j = il - 1, k = i + 1; i < il; i++, j++, k++) {
|
|
1324
|
+
if (j === il) j = 0;
|
|
1325
|
+
if (k === il) k = 0;
|
|
1326
|
+
out[i] = getBevelVec(ring2[i], ring2[j], ring2[k]);
|
|
1327
|
+
}
|
|
1328
|
+
return out;
|
|
1329
|
+
};
|
|
1330
|
+
const contourMovements = movements(contour);
|
|
1331
|
+
const holesMovements = [];
|
|
1332
|
+
let verticesMovements = contourMovements.concat();
|
|
1333
|
+
for (const ahole of holes) {
|
|
1334
|
+
const oneHoleMovements = movements(ahole);
|
|
1335
|
+
holesMovements.push(oneHoleMovements);
|
|
1336
|
+
verticesMovements = verticesMovements.concat(oneHoleMovements);
|
|
1337
|
+
}
|
|
1338
|
+
const v = (x, y, z) => {
|
|
1339
|
+
placeholder.push(x);
|
|
1340
|
+
placeholder.push(y);
|
|
1341
|
+
placeholder.push(z);
|
|
1342
|
+
};
|
|
1343
|
+
const bevelLayer = (b, zOf) => {
|
|
1344
|
+
const t = b / bevelSegments;
|
|
1345
|
+
const z = bevelThickness * Math.cos(t * Math.PI / 2);
|
|
1346
|
+
const bs2 = bevelSize * Math.sin(t * Math.PI / 2) + bevelOffset;
|
|
1347
|
+
for (let i = 0; i < contour.length; i++) {
|
|
1348
|
+
const vert = scalePt2(contour[i], contourMovements[i], bs2);
|
|
1349
|
+
v(vert.x, vert.y, zOf(z));
|
|
1350
|
+
}
|
|
1351
|
+
for (let h = 0; h < holes.length; h++) {
|
|
1352
|
+
const ahole = holes[h];
|
|
1353
|
+
const oneHoleMovements = holesMovements[h];
|
|
1354
|
+
for (let i = 0; i < ahole.length; i++) {
|
|
1355
|
+
const vert = scalePt2(ahole[i], oneHoleMovements[i], bs2);
|
|
1356
|
+
v(vert.x, vert.y, zOf(z));
|
|
1357
|
+
}
|
|
1358
|
+
}
|
|
1359
|
+
};
|
|
1360
|
+
for (let b = 0; b < bevelSegments; b++) bevelLayer(b, (z) => -z);
|
|
1361
|
+
const bs = bevelSize + bevelOffset;
|
|
1362
|
+
for (let i = 0; i < vlen; i++) {
|
|
1363
|
+
const vert = bevelEnabled ? scalePt2(vertices[i], verticesMovements[i], bs) : vertices[i];
|
|
1364
|
+
v(vert.x, vert.y, 0);
|
|
1365
|
+
}
|
|
1366
|
+
for (let s = 1; s <= steps; s++) {
|
|
1367
|
+
for (let i = 0; i < vlen; i++) {
|
|
1368
|
+
const vert = bevelEnabled ? scalePt2(vertices[i], verticesMovements[i], bs) : vertices[i];
|
|
1369
|
+
v(vert.x, vert.y, depth / steps * s);
|
|
1370
|
+
}
|
|
1371
|
+
}
|
|
1372
|
+
for (let b = bevelSegments - 1; b >= 0; b--) bevelLayer(b, (z) => depth + z);
|
|
1373
|
+
const addVertex = (index) => {
|
|
1374
|
+
verticesArray.push(placeholder[index * 3 + 0]);
|
|
1375
|
+
verticesArray.push(placeholder[index * 3 + 1]);
|
|
1376
|
+
verticesArray.push(placeholder[index * 3 + 2]);
|
|
1377
|
+
};
|
|
1378
|
+
const addUV = (x, y) => {
|
|
1379
|
+
uvArray.push(x);
|
|
1380
|
+
uvArray.push(y);
|
|
1381
|
+
};
|
|
1382
|
+
const at = (index, axis) => verticesArray[index * 3 + axis];
|
|
1383
|
+
const f3 = (a, b, c) => {
|
|
1384
|
+
addVertex(a);
|
|
1385
|
+
addVertex(b);
|
|
1386
|
+
addVertex(c);
|
|
1387
|
+
const nextIndex = verticesArray.length / 3;
|
|
1388
|
+
for (const index of [nextIndex - 3, nextIndex - 2, nextIndex - 1]) addUV(at(index, 0), at(index, 1));
|
|
1389
|
+
};
|
|
1390
|
+
const f4 = (a, b, c, d) => {
|
|
1391
|
+
addVertex(a);
|
|
1392
|
+
addVertex(b);
|
|
1393
|
+
addVertex(d);
|
|
1394
|
+
addVertex(b);
|
|
1395
|
+
addVertex(c);
|
|
1396
|
+
addVertex(d);
|
|
1397
|
+
const nextIndex = verticesArray.length / 3;
|
|
1398
|
+
const iA = nextIndex - 6;
|
|
1399
|
+
const iB = nextIndex - 3;
|
|
1400
|
+
const iC = nextIndex - 2;
|
|
1401
|
+
const iD = nextIndex - 1;
|
|
1402
|
+
const byX = Math.abs(at(iA, 1) - at(iB, 1)) < Math.abs(at(iA, 0) - at(iB, 0));
|
|
1403
|
+
const uv = (index) => [byX ? at(index, 0) : at(index, 1), 1 - at(index, 2)];
|
|
1404
|
+
const uvs = [uv(iA), uv(iB), uv(iC), uv(iD)];
|
|
1405
|
+
for (const k of [0, 1, 3, 1, 2, 3]) addUV(...uvs[k]);
|
|
1406
|
+
};
|
|
1407
|
+
let start = verticesArray.length / 3;
|
|
1408
|
+
if (bevelEnabled) {
|
|
1409
|
+
let offset = 0;
|
|
1410
|
+
for (let i = 0; i < flen; i++) {
|
|
1411
|
+
const face = faces[i];
|
|
1412
|
+
f3(face[2] + offset, face[1] + offset, face[0] + offset);
|
|
1413
|
+
}
|
|
1414
|
+
const layer = steps + bevelSegments * 2;
|
|
1415
|
+
offset = vlen * layer;
|
|
1416
|
+
for (let i = 0; i < flen; i++) {
|
|
1417
|
+
const face = faces[i];
|
|
1418
|
+
f3(face[0] + offset, face[1] + offset, face[2] + offset);
|
|
1419
|
+
}
|
|
1420
|
+
} else {
|
|
1421
|
+
for (let i = 0; i < flen; i++) {
|
|
1422
|
+
const face = faces[i];
|
|
1423
|
+
f3(face[2], face[1], face[0]);
|
|
1424
|
+
}
|
|
1425
|
+
for (let i = 0; i < flen; i++) {
|
|
1426
|
+
const face = faces[i];
|
|
1427
|
+
f3(face[0] + vlen * steps, face[1] + vlen * steps, face[2] + vlen * steps);
|
|
1428
|
+
}
|
|
1429
|
+
}
|
|
1430
|
+
groups.push({ start, count: verticesArray.length / 3 - start, materialIndex: 0 });
|
|
1431
|
+
start = verticesArray.length / 3;
|
|
1432
|
+
const sidewalls = (ring2, layeroffset2) => {
|
|
1433
|
+
let i = ring2.length;
|
|
1434
|
+
while (--i >= 0) {
|
|
1435
|
+
const j = i;
|
|
1436
|
+
let k = i - 1;
|
|
1437
|
+
if (k < 0) k = ring2.length - 1;
|
|
1438
|
+
for (let s = 0, sl = steps + bevelSegments * 2; s < sl; s++) {
|
|
1439
|
+
const slen1 = vlen * s;
|
|
1440
|
+
const slen2 = vlen * (s + 1);
|
|
1441
|
+
f4(layeroffset2 + j + slen1, layeroffset2 + k + slen1, layeroffset2 + k + slen2, layeroffset2 + j + slen2);
|
|
1442
|
+
}
|
|
1443
|
+
}
|
|
1444
|
+
};
|
|
1445
|
+
let layeroffset = 0;
|
|
1446
|
+
sidewalls(contour, layeroffset);
|
|
1447
|
+
layeroffset += contour.length;
|
|
1448
|
+
for (const ahole of holes) {
|
|
1449
|
+
sidewalls(ahole, layeroffset);
|
|
1450
|
+
layeroffset += ahole.length;
|
|
1451
|
+
}
|
|
1452
|
+
groups.push({ start, count: verticesArray.length / 3 - start, materialIndex: 1 });
|
|
1453
|
+
const positions = new Float32Array(verticesArray);
|
|
1454
|
+
return { type: "ExtrudeGeometry", parameters, positions, normals: computeVertexNormals(positions), uvs: new Float32Array(uvArray), groups };
|
|
1455
|
+
}
|
|
1456
|
+
|
|
1457
|
+
// src/primitives/index.ts
|
|
1458
|
+
var PARAMETER_KEYS = {
|
|
1459
|
+
box: ["size", "segments"],
|
|
1460
|
+
cylinder: ["radiusTop", "radiusBottom", "height", "radialSegments", "heightSegments", "openEnded", "thetaStart", "thetaLength"],
|
|
1461
|
+
cone: ["radius", "height", "radialSegments", "heightSegments", "openEnded", "thetaStart", "thetaLength"],
|
|
1462
|
+
sphere: ["radius", "widthSegments", "heightSegments", "phiStart", "phiLength", "thetaStart", "thetaLength"],
|
|
1463
|
+
plane: ["width", "height", "widthSegments", "heightSegments"],
|
|
1464
|
+
circle: ["radius", "segments", "thetaStart", "thetaLength"],
|
|
1465
|
+
ring: ["innerRadius", "outerRadius", "thetaSegments", "phiSegments", "thetaStart", "thetaLength"],
|
|
1466
|
+
lathe: ["points", "segments", "phiStart", "phiLength"],
|
|
1467
|
+
torus: ["radius", "tube", "radialSegments", "tubularSegments", "arc"],
|
|
1468
|
+
extrude: ["shape", "depth", "steps", "bevel"],
|
|
1469
|
+
difference: ["size", "subtract"],
|
|
1470
|
+
mesh: ["positions", "normals", "uvs", "indices"]
|
|
1471
|
+
};
|
|
1472
|
+
var SEGMENT_KEYS = {
|
|
1473
|
+
cylinder: ["radialSegments"],
|
|
1474
|
+
cone: ["radialSegments"],
|
|
1475
|
+
sphere: ["widthSegments", "heightSegments"],
|
|
1476
|
+
circle: ["segments"],
|
|
1477
|
+
ring: ["thetaSegments"],
|
|
1478
|
+
lathe: ["segments"],
|
|
1479
|
+
torus: ["radialSegments", "tubularSegments"]
|
|
1480
|
+
};
|
|
1481
|
+
var SEGMENT_DEFAULTS = { radialSegments: 32, widthSegments: 32, heightSegments: 16, segments: 12, thetaSegments: 32, tubularSegments: 48 };
|
|
1482
|
+
var segmentDefault = (kind, key) => kind === "circle" && key === "segments" ? 32 : kind === "torus" && key === "radialSegments" ? 12 : SEGMENT_DEFAULTS[key];
|
|
1483
|
+
function primitiveParameters(node, options = {}) {
|
|
1484
|
+
const out = {};
|
|
1485
|
+
const source = node;
|
|
1486
|
+
for (const key of PARAMETER_KEYS[node.kind]) if (source[key] !== void 0) out[key] = source[key];
|
|
1487
|
+
const detail = options.detail ?? 1;
|
|
1488
|
+
if (detail !== 1) {
|
|
1489
|
+
const floor = options.minSegments ?? 3;
|
|
1490
|
+
for (const key of SEGMENT_KEYS[node.kind] ?? []) {
|
|
1491
|
+
const given = out[key] ?? segmentDefault(node.kind, key);
|
|
1492
|
+
out[key] = Math.max(floor, Math.round(given * detail));
|
|
1493
|
+
}
|
|
1494
|
+
}
|
|
1495
|
+
return out;
|
|
1496
|
+
}
|
|
1497
|
+
function mesh(p) {
|
|
1498
|
+
const positions = new Float32Array(p.positions);
|
|
1499
|
+
const indices = p.indices ? indexArray(p.indices) : void 0;
|
|
1500
|
+
return {
|
|
1501
|
+
type: "BufferGeometry",
|
|
1502
|
+
parameters: {},
|
|
1503
|
+
positions,
|
|
1504
|
+
normals: p.normals ? new Float32Array(p.normals) : computeVertexNormals(positions, indices),
|
|
1505
|
+
uvs: p.uvs ? new Float32Array(p.uvs) : new Float32Array(positions.length / 3 * 2),
|
|
1506
|
+
...indices ? { indices } : {},
|
|
1507
|
+
groups: []
|
|
1508
|
+
};
|
|
1509
|
+
}
|
|
1510
|
+
function make(kind, p) {
|
|
1511
|
+
switch (kind) {
|
|
1512
|
+
case "box": {
|
|
1513
|
+
const size = p.size ?? [1, 1, 1];
|
|
1514
|
+
const segments = p.segments ?? [1, 1, 1];
|
|
1515
|
+
return box(size[0], size[1], size[2], segments[0], segments[1], segments[2]);
|
|
1516
|
+
}
|
|
1517
|
+
case "cylinder":
|
|
1518
|
+
return cylinder(p.radiusTop, p.radiusBottom, p.height, p.radialSegments, p.heightSegments, p.openEnded, p.thetaStart, p.thetaLength);
|
|
1519
|
+
case "cone":
|
|
1520
|
+
return cone(p.radius, p.height, p.radialSegments, p.heightSegments, p.openEnded, p.thetaStart, p.thetaLength);
|
|
1521
|
+
case "sphere":
|
|
1522
|
+
return sphere(p.radius, p.widthSegments, p.heightSegments, p.phiStart, p.phiLength, p.thetaStart, p.thetaLength);
|
|
1523
|
+
case "plane":
|
|
1524
|
+
return plane(p.width, p.height, p.widthSegments, p.heightSegments);
|
|
1525
|
+
case "circle":
|
|
1526
|
+
return circle(p.radius, p.segments, p.thetaStart, p.thetaLength);
|
|
1527
|
+
case "ring":
|
|
1528
|
+
return ring(p.innerRadius, p.outerRadius, p.thetaSegments, p.phiSegments, p.thetaStart, p.thetaLength);
|
|
1529
|
+
case "lathe":
|
|
1530
|
+
return lathe(p.points, p.segments, p.phiStart, p.phiLength);
|
|
1531
|
+
case "torus":
|
|
1532
|
+
return torus(p.radius, p.tube, p.radialSegments, p.tubularSegments, p.arc);
|
|
1533
|
+
case "extrude":
|
|
1534
|
+
return extrude(p.shape, p.depth, p.steps, p.bevel);
|
|
1535
|
+
case "difference":
|
|
1536
|
+
return difference(p.size, p.subtract ?? []);
|
|
1537
|
+
case "mesh":
|
|
1538
|
+
return mesh(p);
|
|
1539
|
+
default:
|
|
1540
|
+
throw new Error(`game-shapes: unknown primitive kind "${String(kind)}"`);
|
|
1541
|
+
}
|
|
1542
|
+
}
|
|
1543
|
+
var templates = /* @__PURE__ */ new Map();
|
|
1544
|
+
var templateLimit = 4096;
|
|
1545
|
+
var stats = { hits: 0, misses: 0 };
|
|
1546
|
+
function setTemplateCacheSize(size) {
|
|
1547
|
+
templateLimit = Math.max(0, Math.floor(size));
|
|
1548
|
+
while (templates.size > templateLimit) templates.delete(templates.keys().next().value);
|
|
1549
|
+
}
|
|
1550
|
+
function clearTemplateCache() {
|
|
1551
|
+
templates.clear();
|
|
1552
|
+
stats.hits = 0;
|
|
1553
|
+
stats.misses = 0;
|
|
1554
|
+
}
|
|
1555
|
+
var templateCacheStats = () => ({ size: templates.size, ...stats });
|
|
1556
|
+
function buildPrimitive(node, options = {}) {
|
|
1557
|
+
const parameters = primitiveParameters(node, options);
|
|
1558
|
+
if (node.kind === "mesh" || templateLimit === 0) return make(node.kind, parameters);
|
|
1559
|
+
const key = `${node.kind}:${canonicalJson(parameters)}`;
|
|
1560
|
+
const cached = templates.get(key);
|
|
1561
|
+
if (cached) {
|
|
1562
|
+
stats.hits++;
|
|
1563
|
+
templates.delete(key);
|
|
1564
|
+
templates.set(key, cached);
|
|
1565
|
+
return cached;
|
|
1566
|
+
}
|
|
1567
|
+
stats.misses++;
|
|
1568
|
+
const built = make(node.kind, parameters);
|
|
1569
|
+
templates.set(key, built);
|
|
1570
|
+
if (templates.size > templateLimit) templates.delete(templates.keys().next().value);
|
|
1571
|
+
return built;
|
|
1572
|
+
}
|
|
1573
|
+
function clonePrimitive(p) {
|
|
1574
|
+
return {
|
|
1575
|
+
type: p.type,
|
|
1576
|
+
parameters: p.parameters,
|
|
1577
|
+
positions: p.positions.slice(),
|
|
1578
|
+
normals: p.normals.slice(),
|
|
1579
|
+
uvs: p.uvs.slice(),
|
|
1580
|
+
...p.indices ? { indices: p.indices.slice() } : {},
|
|
1581
|
+
groups: p.groups.map((group) => ({ ...group }))
|
|
1582
|
+
};
|
|
1583
|
+
}
|
|
1584
|
+
|
|
1585
|
+
// src/random.ts
|
|
1586
|
+
function mulberry32(seed) {
|
|
1587
|
+
let state = seed >>> 0;
|
|
1588
|
+
return () => {
|
|
1589
|
+
state = state + 1831565813 >>> 0;
|
|
1590
|
+
let t = state;
|
|
1591
|
+
t = Math.imul(t ^ t >>> 15, t | 1);
|
|
1592
|
+
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
|
|
1593
|
+
return ((t ^ t >>> 14) >>> 0) / 4294967296;
|
|
1594
|
+
};
|
|
1595
|
+
}
|
|
1596
|
+
var streamFor = (seed, generator, path) => mulberry32(parseInt(hash64(`${String(seed)}\0${generator}\0${path}`).slice(0, 8), 16));
|
|
1597
|
+
|
|
1598
|
+
// src/registry.ts
|
|
1599
|
+
var generators = /* @__PURE__ */ new Map();
|
|
1600
|
+
function registerGenerator(name, generator) {
|
|
1601
|
+
if (!/^[a-zA-Z][\w.:-]*$/.test(name)) throw new Error(`game-shapes: generator name "${name}" must start with a letter and use letters, digits, ".", ":", "-" or "_"`);
|
|
1602
|
+
generators.set(name, generator);
|
|
1603
|
+
}
|
|
1604
|
+
var unregisterGenerator = (name) => generators.delete(name);
|
|
1605
|
+
var getGenerator = (name) => generators.get(name);
|
|
1606
|
+
var listGenerators = () => [...generators.keys()].sort();
|
|
1607
|
+
|
|
1608
|
+
// schema/shape.schema.json
|
|
1609
|
+
var shape_schema_default = {
|
|
1610
|
+
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
1611
|
+
$id: "https://github.com/Andersonfrfilho/adatechnology-game-kit/packages/game-shapes/schema/shape.schema.json",
|
|
1612
|
+
title: "game-shapes shape",
|
|
1613
|
+
type: "object",
|
|
1614
|
+
required: [
|
|
1615
|
+
"version",
|
|
1616
|
+
"root"
|
|
1617
|
+
],
|
|
1618
|
+
additionalProperties: false,
|
|
1619
|
+
properties: {
|
|
1620
|
+
version: {
|
|
1621
|
+
const: 1
|
|
1622
|
+
},
|
|
1623
|
+
name: {
|
|
1624
|
+
type: "string"
|
|
1625
|
+
},
|
|
1626
|
+
materials: {
|
|
1627
|
+
type: "object",
|
|
1628
|
+
additionalProperties: {
|
|
1629
|
+
$ref: "#/$defs/material"
|
|
1630
|
+
}
|
|
1631
|
+
},
|
|
1632
|
+
root: {
|
|
1633
|
+
$ref: "#/$defs/node"
|
|
1634
|
+
},
|
|
1635
|
+
lod: {
|
|
1636
|
+
type: "array",
|
|
1637
|
+
items: {
|
|
1638
|
+
$ref: "#/$defs/lodLevel"
|
|
1639
|
+
}
|
|
1640
|
+
},
|
|
1641
|
+
cullDistance: {
|
|
1642
|
+
type: "number",
|
|
1643
|
+
exclusiveMinimum: 0
|
|
1644
|
+
}
|
|
1645
|
+
},
|
|
1646
|
+
$defs: {
|
|
1647
|
+
number: {
|
|
1648
|
+
type: "number"
|
|
1649
|
+
},
|
|
1650
|
+
count: {
|
|
1651
|
+
type: "integer",
|
|
1652
|
+
minimum: 0
|
|
1653
|
+
},
|
|
1654
|
+
vec2: {
|
|
1655
|
+
type: "array",
|
|
1656
|
+
items: {
|
|
1657
|
+
type: "number"
|
|
1658
|
+
},
|
|
1659
|
+
minItems: 2,
|
|
1660
|
+
maxItems: 2
|
|
1661
|
+
},
|
|
1662
|
+
vec3: {
|
|
1663
|
+
type: "array",
|
|
1664
|
+
items: {
|
|
1665
|
+
type: "number"
|
|
1666
|
+
},
|
|
1667
|
+
minItems: 3,
|
|
1668
|
+
maxItems: 3
|
|
1669
|
+
},
|
|
1670
|
+
mat4: {
|
|
1671
|
+
type: "array",
|
|
1672
|
+
items: {
|
|
1673
|
+
type: "number"
|
|
1674
|
+
},
|
|
1675
|
+
minItems: 16,
|
|
1676
|
+
maxItems: 16
|
|
1677
|
+
},
|
|
1678
|
+
color: {
|
|
1679
|
+
oneOf: [
|
|
1680
|
+
{
|
|
1681
|
+
type: "integer",
|
|
1682
|
+
minimum: 0,
|
|
1683
|
+
maximum: 16777215
|
|
1684
|
+
},
|
|
1685
|
+
{
|
|
1686
|
+
type: "string",
|
|
1687
|
+
pattern: "^#[0-9a-fA-F]{6}$"
|
|
1688
|
+
}
|
|
1689
|
+
]
|
|
1690
|
+
},
|
|
1691
|
+
uvRect: {
|
|
1692
|
+
type: "object",
|
|
1693
|
+
additionalProperties: false,
|
|
1694
|
+
required: [
|
|
1695
|
+
"u0",
|
|
1696
|
+
"v0",
|
|
1697
|
+
"u1",
|
|
1698
|
+
"v1"
|
|
1699
|
+
],
|
|
1700
|
+
properties: {
|
|
1701
|
+
u0: {
|
|
1702
|
+
type: "number"
|
|
1703
|
+
},
|
|
1704
|
+
v0: {
|
|
1705
|
+
type: "number"
|
|
1706
|
+
},
|
|
1707
|
+
u1: {
|
|
1708
|
+
type: "number"
|
|
1709
|
+
},
|
|
1710
|
+
v1: {
|
|
1711
|
+
type: "number"
|
|
1712
|
+
}
|
|
1713
|
+
}
|
|
1714
|
+
},
|
|
1715
|
+
rotationOrder: {
|
|
1716
|
+
enum: [
|
|
1717
|
+
"XYZ",
|
|
1718
|
+
"YXZ",
|
|
1719
|
+
"ZXY",
|
|
1720
|
+
"ZYX",
|
|
1721
|
+
"YZX",
|
|
1722
|
+
"XZY"
|
|
1723
|
+
]
|
|
1724
|
+
},
|
|
1725
|
+
transform: {
|
|
1726
|
+
type: "object",
|
|
1727
|
+
properties: {
|
|
1728
|
+
position: {
|
|
1729
|
+
$ref: "#/$defs/vec3"
|
|
1730
|
+
},
|
|
1731
|
+
rotation: {
|
|
1732
|
+
$ref: "#/$defs/vec3"
|
|
1733
|
+
},
|
|
1734
|
+
rotationOrder: {
|
|
1735
|
+
$ref: "#/$defs/rotationOrder"
|
|
1736
|
+
},
|
|
1737
|
+
scale: {
|
|
1738
|
+
oneOf: [
|
|
1739
|
+
{
|
|
1740
|
+
type: "number"
|
|
1741
|
+
},
|
|
1742
|
+
{
|
|
1743
|
+
$ref: "#/$defs/vec3"
|
|
1744
|
+
}
|
|
1745
|
+
]
|
|
1746
|
+
},
|
|
1747
|
+
heading: {
|
|
1748
|
+
type: "number"
|
|
1749
|
+
},
|
|
1750
|
+
matrix: {
|
|
1751
|
+
$ref: "#/$defs/mat4"
|
|
1752
|
+
}
|
|
1753
|
+
}
|
|
1754
|
+
},
|
|
1755
|
+
instance: {
|
|
1756
|
+
allOf: [
|
|
1757
|
+
{
|
|
1758
|
+
$ref: "#/$defs/transform"
|
|
1759
|
+
}
|
|
1760
|
+
],
|
|
1761
|
+
properties: {
|
|
1762
|
+
position: true,
|
|
1763
|
+
rotation: true,
|
|
1764
|
+
rotationOrder: true,
|
|
1765
|
+
scale: true,
|
|
1766
|
+
heading: true,
|
|
1767
|
+
matrix: true,
|
|
1768
|
+
color: {
|
|
1769
|
+
$ref: "#/$defs/color"
|
|
1770
|
+
}
|
|
1771
|
+
},
|
|
1772
|
+
unevaluatedProperties: false
|
|
1773
|
+
},
|
|
1774
|
+
material: {
|
|
1775
|
+
type: "object",
|
|
1776
|
+
additionalProperties: false,
|
|
1777
|
+
properties: {
|
|
1778
|
+
color: {
|
|
1779
|
+
$ref: "#/$defs/color"
|
|
1780
|
+
},
|
|
1781
|
+
roughness: {
|
|
1782
|
+
type: "number",
|
|
1783
|
+
minimum: 0,
|
|
1784
|
+
maximum: 1
|
|
1785
|
+
},
|
|
1786
|
+
metalness: {
|
|
1787
|
+
type: "number",
|
|
1788
|
+
minimum: 0,
|
|
1789
|
+
maximum: 1
|
|
1790
|
+
},
|
|
1791
|
+
opacity: {
|
|
1792
|
+
type: "number",
|
|
1793
|
+
minimum: 0,
|
|
1794
|
+
maximum: 1
|
|
1795
|
+
},
|
|
1796
|
+
transparent: {
|
|
1797
|
+
type: "boolean"
|
|
1798
|
+
},
|
|
1799
|
+
emissive: {
|
|
1800
|
+
$ref: "#/$defs/color"
|
|
1801
|
+
},
|
|
1802
|
+
emissiveIntensity: {
|
|
1803
|
+
type: "number",
|
|
1804
|
+
minimum: 0
|
|
1805
|
+
},
|
|
1806
|
+
side: {
|
|
1807
|
+
enum: [
|
|
1808
|
+
"front",
|
|
1809
|
+
"back",
|
|
1810
|
+
"double"
|
|
1811
|
+
]
|
|
1812
|
+
},
|
|
1813
|
+
flatShading: {
|
|
1814
|
+
type: "boolean"
|
|
1815
|
+
},
|
|
1816
|
+
vertexColors: {
|
|
1817
|
+
type: "boolean"
|
|
1818
|
+
},
|
|
1819
|
+
depthWrite: {
|
|
1820
|
+
type: "boolean"
|
|
1821
|
+
}
|
|
1822
|
+
}
|
|
1823
|
+
},
|
|
1824
|
+
lodLevel: {
|
|
1825
|
+
type: "object",
|
|
1826
|
+
additionalProperties: false,
|
|
1827
|
+
required: [
|
|
1828
|
+
"distance",
|
|
1829
|
+
"detail"
|
|
1830
|
+
],
|
|
1831
|
+
properties: {
|
|
1832
|
+
distance: {
|
|
1833
|
+
type: "number",
|
|
1834
|
+
minimum: 0
|
|
1835
|
+
},
|
|
1836
|
+
detail: {
|
|
1837
|
+
type: "number",
|
|
1838
|
+
exclusiveMinimum: 0,
|
|
1839
|
+
maximum: 1
|
|
1840
|
+
},
|
|
1841
|
+
minSize: {
|
|
1842
|
+
type: "number",
|
|
1843
|
+
minimum: 0
|
|
1844
|
+
},
|
|
1845
|
+
minSegments: {
|
|
1846
|
+
type: "integer",
|
|
1847
|
+
minimum: 3
|
|
1848
|
+
}
|
|
1849
|
+
}
|
|
1850
|
+
},
|
|
1851
|
+
common: {
|
|
1852
|
+
allOf: [
|
|
1853
|
+
{
|
|
1854
|
+
$ref: "#/$defs/transform"
|
|
1855
|
+
}
|
|
1856
|
+
],
|
|
1857
|
+
properties: {
|
|
1858
|
+
kind: {
|
|
1859
|
+
type: "string"
|
|
1860
|
+
},
|
|
1861
|
+
name: {
|
|
1862
|
+
type: "string"
|
|
1863
|
+
},
|
|
1864
|
+
part: {
|
|
1865
|
+
type: "string"
|
|
1866
|
+
},
|
|
1867
|
+
material: {
|
|
1868
|
+
type: "string"
|
|
1869
|
+
},
|
|
1870
|
+
color: {
|
|
1871
|
+
$ref: "#/$defs/color"
|
|
1872
|
+
},
|
|
1873
|
+
uv: {
|
|
1874
|
+
$ref: "#/$defs/uvRect"
|
|
1875
|
+
},
|
|
1876
|
+
instances: {
|
|
1877
|
+
type: "array",
|
|
1878
|
+
items: {
|
|
1879
|
+
$ref: "#/$defs/instance"
|
|
1880
|
+
}
|
|
1881
|
+
},
|
|
1882
|
+
minDetail: {
|
|
1883
|
+
type: "number",
|
|
1884
|
+
exclusiveMinimum: 0,
|
|
1885
|
+
maximum: 1
|
|
1886
|
+
},
|
|
1887
|
+
tags: {
|
|
1888
|
+
type: "array",
|
|
1889
|
+
items: {
|
|
1890
|
+
type: "string"
|
|
1891
|
+
}
|
|
1892
|
+
}
|
|
1893
|
+
}
|
|
1894
|
+
},
|
|
1895
|
+
node: {
|
|
1896
|
+
type: "object",
|
|
1897
|
+
required: [
|
|
1898
|
+
"kind"
|
|
1899
|
+
],
|
|
1900
|
+
allOf: [
|
|
1901
|
+
{
|
|
1902
|
+
$ref: "#/$defs/common"
|
|
1903
|
+
}
|
|
1904
|
+
],
|
|
1905
|
+
oneOf: [
|
|
1906
|
+
{
|
|
1907
|
+
properties: {
|
|
1908
|
+
kind: {
|
|
1909
|
+
const: "group"
|
|
1910
|
+
},
|
|
1911
|
+
children: {
|
|
1912
|
+
type: "array",
|
|
1913
|
+
items: {
|
|
1914
|
+
$ref: "#/$defs/node"
|
|
1915
|
+
}
|
|
1916
|
+
}
|
|
1917
|
+
}
|
|
1918
|
+
},
|
|
1919
|
+
{
|
|
1920
|
+
properties: {
|
|
1921
|
+
kind: {
|
|
1922
|
+
const: "box"
|
|
1923
|
+
},
|
|
1924
|
+
size: {
|
|
1925
|
+
$ref: "#/$defs/vec3"
|
|
1926
|
+
},
|
|
1927
|
+
segments: {
|
|
1928
|
+
type: "array",
|
|
1929
|
+
items: {
|
|
1930
|
+
type: "integer",
|
|
1931
|
+
minimum: 1
|
|
1932
|
+
},
|
|
1933
|
+
minItems: 3,
|
|
1934
|
+
maxItems: 3
|
|
1935
|
+
}
|
|
1936
|
+
}
|
|
1937
|
+
},
|
|
1938
|
+
{
|
|
1939
|
+
properties: {
|
|
1940
|
+
kind: {
|
|
1941
|
+
const: "cylinder"
|
|
1942
|
+
},
|
|
1943
|
+
radiusTop: {
|
|
1944
|
+
type: "number"
|
|
1945
|
+
},
|
|
1946
|
+
radiusBottom: {
|
|
1947
|
+
type: "number"
|
|
1948
|
+
},
|
|
1949
|
+
height: {
|
|
1950
|
+
type: "number"
|
|
1951
|
+
},
|
|
1952
|
+
radialSegments: {
|
|
1953
|
+
type: "integer",
|
|
1954
|
+
minimum: 1
|
|
1955
|
+
},
|
|
1956
|
+
heightSegments: {
|
|
1957
|
+
type: "integer",
|
|
1958
|
+
minimum: 1
|
|
1959
|
+
},
|
|
1960
|
+
openEnded: {
|
|
1961
|
+
type: "boolean"
|
|
1962
|
+
},
|
|
1963
|
+
thetaStart: {
|
|
1964
|
+
type: "number"
|
|
1965
|
+
},
|
|
1966
|
+
thetaLength: {
|
|
1967
|
+
type: "number"
|
|
1968
|
+
}
|
|
1969
|
+
}
|
|
1970
|
+
},
|
|
1971
|
+
{
|
|
1972
|
+
properties: {
|
|
1973
|
+
kind: {
|
|
1974
|
+
const: "cone"
|
|
1975
|
+
},
|
|
1976
|
+
radius: {
|
|
1977
|
+
type: "number"
|
|
1978
|
+
},
|
|
1979
|
+
height: {
|
|
1980
|
+
type: "number"
|
|
1981
|
+
},
|
|
1982
|
+
radialSegments: {
|
|
1983
|
+
type: "integer",
|
|
1984
|
+
minimum: 1
|
|
1985
|
+
},
|
|
1986
|
+
heightSegments: {
|
|
1987
|
+
type: "integer",
|
|
1988
|
+
minimum: 1
|
|
1989
|
+
},
|
|
1990
|
+
openEnded: {
|
|
1991
|
+
type: "boolean"
|
|
1992
|
+
},
|
|
1993
|
+
thetaStart: {
|
|
1994
|
+
type: "number"
|
|
1995
|
+
},
|
|
1996
|
+
thetaLength: {
|
|
1997
|
+
type: "number"
|
|
1998
|
+
}
|
|
1999
|
+
}
|
|
2000
|
+
},
|
|
2001
|
+
{
|
|
2002
|
+
properties: {
|
|
2003
|
+
kind: {
|
|
2004
|
+
const: "sphere"
|
|
2005
|
+
},
|
|
2006
|
+
radius: {
|
|
2007
|
+
type: "number"
|
|
2008
|
+
},
|
|
2009
|
+
widthSegments: {
|
|
2010
|
+
type: "integer",
|
|
2011
|
+
minimum: 3
|
|
2012
|
+
},
|
|
2013
|
+
heightSegments: {
|
|
2014
|
+
type: "integer",
|
|
2015
|
+
minimum: 2
|
|
2016
|
+
},
|
|
2017
|
+
phiStart: {
|
|
2018
|
+
type: "number"
|
|
2019
|
+
},
|
|
2020
|
+
phiLength: {
|
|
2021
|
+
type: "number"
|
|
2022
|
+
},
|
|
2023
|
+
thetaStart: {
|
|
2024
|
+
type: "number"
|
|
2025
|
+
},
|
|
2026
|
+
thetaLength: {
|
|
2027
|
+
type: "number"
|
|
2028
|
+
}
|
|
2029
|
+
}
|
|
2030
|
+
},
|
|
2031
|
+
{
|
|
2032
|
+
properties: {
|
|
2033
|
+
kind: {
|
|
2034
|
+
const: "plane"
|
|
2035
|
+
},
|
|
2036
|
+
width: {
|
|
2037
|
+
type: "number"
|
|
2038
|
+
},
|
|
2039
|
+
height: {
|
|
2040
|
+
type: "number"
|
|
2041
|
+
},
|
|
2042
|
+
widthSegments: {
|
|
2043
|
+
type: "integer",
|
|
2044
|
+
minimum: 1
|
|
2045
|
+
},
|
|
2046
|
+
heightSegments: {
|
|
2047
|
+
type: "integer",
|
|
2048
|
+
minimum: 1
|
|
2049
|
+
}
|
|
2050
|
+
}
|
|
2051
|
+
},
|
|
2052
|
+
{
|
|
2053
|
+
properties: {
|
|
2054
|
+
kind: {
|
|
2055
|
+
const: "circle"
|
|
2056
|
+
},
|
|
2057
|
+
radius: {
|
|
2058
|
+
type: "number"
|
|
2059
|
+
},
|
|
2060
|
+
segments: {
|
|
2061
|
+
type: "integer",
|
|
2062
|
+
minimum: 3
|
|
2063
|
+
},
|
|
2064
|
+
thetaStart: {
|
|
2065
|
+
type: "number"
|
|
2066
|
+
},
|
|
2067
|
+
thetaLength: {
|
|
2068
|
+
type: "number"
|
|
2069
|
+
}
|
|
2070
|
+
}
|
|
2071
|
+
},
|
|
2072
|
+
{
|
|
2073
|
+
properties: {
|
|
2074
|
+
kind: {
|
|
2075
|
+
const: "ring"
|
|
2076
|
+
},
|
|
2077
|
+
innerRadius: {
|
|
2078
|
+
type: "number"
|
|
2079
|
+
},
|
|
2080
|
+
outerRadius: {
|
|
2081
|
+
type: "number"
|
|
2082
|
+
},
|
|
2083
|
+
thetaSegments: {
|
|
2084
|
+
type: "integer",
|
|
2085
|
+
minimum: 3
|
|
2086
|
+
},
|
|
2087
|
+
phiSegments: {
|
|
2088
|
+
type: "integer",
|
|
2089
|
+
minimum: 1
|
|
2090
|
+
},
|
|
2091
|
+
thetaStart: {
|
|
2092
|
+
type: "number"
|
|
2093
|
+
},
|
|
2094
|
+
thetaLength: {
|
|
2095
|
+
type: "number"
|
|
2096
|
+
}
|
|
2097
|
+
}
|
|
2098
|
+
},
|
|
2099
|
+
{
|
|
2100
|
+
required: [
|
|
2101
|
+
"points"
|
|
2102
|
+
],
|
|
2103
|
+
properties: {
|
|
2104
|
+
kind: {
|
|
2105
|
+
const: "lathe"
|
|
2106
|
+
},
|
|
2107
|
+
points: {
|
|
2108
|
+
type: "array",
|
|
2109
|
+
items: {
|
|
2110
|
+
$ref: "#/$defs/vec2"
|
|
2111
|
+
},
|
|
2112
|
+
minItems: 2
|
|
2113
|
+
},
|
|
2114
|
+
segments: {
|
|
2115
|
+
type: "integer",
|
|
2116
|
+
minimum: 1
|
|
2117
|
+
},
|
|
2118
|
+
phiStart: {
|
|
2119
|
+
type: "number"
|
|
2120
|
+
},
|
|
2121
|
+
phiLength: {
|
|
2122
|
+
type: "number"
|
|
2123
|
+
}
|
|
2124
|
+
}
|
|
2125
|
+
},
|
|
2126
|
+
{
|
|
2127
|
+
properties: {
|
|
2128
|
+
kind: {
|
|
2129
|
+
const: "torus"
|
|
2130
|
+
},
|
|
2131
|
+
radius: {
|
|
2132
|
+
type: "number"
|
|
2133
|
+
},
|
|
2134
|
+
tube: {
|
|
2135
|
+
type: "number"
|
|
2136
|
+
},
|
|
2137
|
+
radialSegments: {
|
|
2138
|
+
type: "integer",
|
|
2139
|
+
minimum: 1
|
|
2140
|
+
},
|
|
2141
|
+
tubularSegments: {
|
|
2142
|
+
type: "integer",
|
|
2143
|
+
minimum: 1
|
|
2144
|
+
},
|
|
2145
|
+
arc: {
|
|
2146
|
+
type: "number"
|
|
2147
|
+
}
|
|
2148
|
+
}
|
|
2149
|
+
},
|
|
2150
|
+
{
|
|
2151
|
+
required: [
|
|
2152
|
+
"shape"
|
|
2153
|
+
],
|
|
2154
|
+
properties: {
|
|
2155
|
+
kind: {
|
|
2156
|
+
const: "extrude"
|
|
2157
|
+
},
|
|
2158
|
+
shape: {
|
|
2159
|
+
type: "object",
|
|
2160
|
+
required: [
|
|
2161
|
+
"outer"
|
|
2162
|
+
],
|
|
2163
|
+
additionalProperties: false,
|
|
2164
|
+
properties: {
|
|
2165
|
+
outer: {
|
|
2166
|
+
type: "array",
|
|
2167
|
+
items: {
|
|
2168
|
+
$ref: "#/$defs/vec2"
|
|
2169
|
+
},
|
|
2170
|
+
minItems: 3
|
|
2171
|
+
},
|
|
2172
|
+
holes: {
|
|
2173
|
+
type: "array",
|
|
2174
|
+
items: {
|
|
2175
|
+
type: "array",
|
|
2176
|
+
items: {
|
|
2177
|
+
$ref: "#/$defs/vec2"
|
|
2178
|
+
},
|
|
2179
|
+
minItems: 3
|
|
2180
|
+
}
|
|
2181
|
+
}
|
|
2182
|
+
}
|
|
2183
|
+
},
|
|
2184
|
+
depth: {
|
|
2185
|
+
type: "number"
|
|
2186
|
+
},
|
|
2187
|
+
steps: {
|
|
2188
|
+
type: "integer",
|
|
2189
|
+
minimum: 1
|
|
2190
|
+
},
|
|
2191
|
+
bevel: {
|
|
2192
|
+
oneOf: [
|
|
2193
|
+
{
|
|
2194
|
+
const: false
|
|
2195
|
+
},
|
|
2196
|
+
{
|
|
2197
|
+
type: "object",
|
|
2198
|
+
additionalProperties: false,
|
|
2199
|
+
properties: {
|
|
2200
|
+
thickness: {
|
|
2201
|
+
type: "number"
|
|
2202
|
+
},
|
|
2203
|
+
size: {
|
|
2204
|
+
type: "number"
|
|
2205
|
+
},
|
|
2206
|
+
offset: {
|
|
2207
|
+
type: "number"
|
|
2208
|
+
},
|
|
2209
|
+
segments: {
|
|
2210
|
+
type: "integer",
|
|
2211
|
+
minimum: 1
|
|
2212
|
+
}
|
|
2213
|
+
}
|
|
2214
|
+
}
|
|
2215
|
+
]
|
|
2216
|
+
}
|
|
2217
|
+
}
|
|
2218
|
+
},
|
|
2219
|
+
{
|
|
2220
|
+
required: [
|
|
2221
|
+
"size",
|
|
2222
|
+
"subtract"
|
|
2223
|
+
],
|
|
2224
|
+
properties: {
|
|
2225
|
+
kind: {
|
|
2226
|
+
const: "difference"
|
|
2227
|
+
},
|
|
2228
|
+
size: {
|
|
2229
|
+
$ref: "#/$defs/vec3"
|
|
2230
|
+
},
|
|
2231
|
+
subtract: {
|
|
2232
|
+
type: "array",
|
|
2233
|
+
items: {
|
|
2234
|
+
type: "object",
|
|
2235
|
+
required: [
|
|
2236
|
+
"min",
|
|
2237
|
+
"max"
|
|
2238
|
+
],
|
|
2239
|
+
additionalProperties: false,
|
|
2240
|
+
properties: {
|
|
2241
|
+
min: {
|
|
2242
|
+
$ref: "#/$defs/vec2"
|
|
2243
|
+
},
|
|
2244
|
+
max: {
|
|
2245
|
+
$ref: "#/$defs/vec2"
|
|
2246
|
+
}
|
|
2247
|
+
}
|
|
2248
|
+
}
|
|
2249
|
+
}
|
|
2250
|
+
}
|
|
2251
|
+
},
|
|
2252
|
+
{
|
|
2253
|
+
required: [
|
|
2254
|
+
"positions"
|
|
2255
|
+
],
|
|
2256
|
+
properties: {
|
|
2257
|
+
kind: {
|
|
2258
|
+
const: "mesh"
|
|
2259
|
+
},
|
|
2260
|
+
positions: {
|
|
2261
|
+
type: "array",
|
|
2262
|
+
items: {
|
|
2263
|
+
type: "number"
|
|
2264
|
+
}
|
|
2265
|
+
},
|
|
2266
|
+
normals: {
|
|
2267
|
+
type: "array",
|
|
2268
|
+
items: {
|
|
2269
|
+
type: "number"
|
|
2270
|
+
}
|
|
2271
|
+
},
|
|
2272
|
+
uvs: {
|
|
2273
|
+
type: "array",
|
|
2274
|
+
items: {
|
|
2275
|
+
type: "number"
|
|
2276
|
+
}
|
|
2277
|
+
},
|
|
2278
|
+
indices: {
|
|
2279
|
+
type: "array",
|
|
2280
|
+
items: {
|
|
2281
|
+
$ref: "#/$defs/count"
|
|
2282
|
+
}
|
|
2283
|
+
}
|
|
2284
|
+
}
|
|
2285
|
+
},
|
|
2286
|
+
{
|
|
2287
|
+
properties: {
|
|
2288
|
+
kind: {
|
|
2289
|
+
const: "point"
|
|
2290
|
+
},
|
|
2291
|
+
data: {}
|
|
2292
|
+
}
|
|
2293
|
+
},
|
|
2294
|
+
{
|
|
2295
|
+
required: [
|
|
2296
|
+
"generator"
|
|
2297
|
+
],
|
|
2298
|
+
properties: {
|
|
2299
|
+
kind: {
|
|
2300
|
+
const: "custom"
|
|
2301
|
+
},
|
|
2302
|
+
generator: {
|
|
2303
|
+
type: "string"
|
|
2304
|
+
},
|
|
2305
|
+
params: {}
|
|
2306
|
+
}
|
|
2307
|
+
}
|
|
2308
|
+
]
|
|
2309
|
+
}
|
|
2310
|
+
}
|
|
2311
|
+
};
|
|
2312
|
+
|
|
2313
|
+
// src/schema.ts
|
|
2314
|
+
var SHAPE_SCHEMA = shape_schema_default;
|
|
2315
|
+
var num = (v) => typeof v === "number" && Number.isFinite(v) ? null : "expected a finite number";
|
|
2316
|
+
var bool = (v) => typeof v === "boolean" ? null : "expected a boolean";
|
|
2317
|
+
var str = (v) => typeof v === "string" ? null : "expected a string";
|
|
2318
|
+
var int = (min) => (v) => typeof v === "number" && Number.isInteger(v) && v >= min ? null : `expected an integer >= ${min}`;
|
|
2319
|
+
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}]`;
|
|
2320
|
+
var tuple = (n, item = num) => (v) => {
|
|
2321
|
+
if (!Array.isArray(v) || v.length !== n) return `expected an array of ${n}`;
|
|
2322
|
+
for (const x of v) if (item(x)) return `expected an array of ${n} numbers`;
|
|
2323
|
+
return null;
|
|
2324
|
+
};
|
|
2325
|
+
var list = (item, min = 0) => (v) => {
|
|
2326
|
+
if (!Array.isArray(v)) return "expected an array";
|
|
2327
|
+
if (v.length < min) return `expected at least ${min} items`;
|
|
2328
|
+
for (let i = 0; i < v.length; i++) {
|
|
2329
|
+
const e = item(v[i]);
|
|
2330
|
+
if (e) return `[${i}]: ${e}`;
|
|
2331
|
+
}
|
|
2332
|
+
return null;
|
|
2333
|
+
};
|
|
2334
|
+
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"';
|
|
2335
|
+
var oneOf = (values) => (v) => values.includes(v) ? null : `expected one of ${values.map((x) => JSON.stringify(x)).join(", ")}`;
|
|
2336
|
+
var object = (fields, required = []) => (v) => {
|
|
2337
|
+
if (!v || typeof v !== "object" || Array.isArray(v)) return "expected an object";
|
|
2338
|
+
const record = v;
|
|
2339
|
+
for (const key of required) if (record[key] === void 0) return `${key}: required`;
|
|
2340
|
+
for (const key of Object.keys(record)) {
|
|
2341
|
+
if (record[key] === void 0) continue;
|
|
2342
|
+
const check = fields[key];
|
|
2343
|
+
if (!check) return `${key}: unknown field`;
|
|
2344
|
+
const e = check(record[key]);
|
|
2345
|
+
if (e) return `${key}: ${e}`;
|
|
2346
|
+
}
|
|
2347
|
+
return null;
|
|
2348
|
+
};
|
|
2349
|
+
var ORDERS = ["XYZ", "YXZ", "ZXY", "ZYX", "YZX", "XZY"];
|
|
2350
|
+
var TRANSFORM = {
|
|
2351
|
+
position: tuple(3),
|
|
2352
|
+
rotation: tuple(3),
|
|
2353
|
+
rotationOrder: oneOf(ORDERS),
|
|
2354
|
+
scale: (v) => typeof v === "number" ? num(v) : tuple(3)(v),
|
|
2355
|
+
heading: num,
|
|
2356
|
+
matrix: tuple(16)
|
|
2357
|
+
};
|
|
2358
|
+
var UV = object({ u0: num, v0: num, u1: num, v1: num }, ["u0", "v0", "u1", "v1"]);
|
|
2359
|
+
var INSTANCE = object({ ...TRANSFORM, color });
|
|
2360
|
+
var COMMON = {
|
|
2361
|
+
...TRANSFORM,
|
|
2362
|
+
kind: str,
|
|
2363
|
+
name: str,
|
|
2364
|
+
part: str,
|
|
2365
|
+
material: str,
|
|
2366
|
+
color,
|
|
2367
|
+
uv: UV,
|
|
2368
|
+
instances: list(INSTANCE),
|
|
2369
|
+
minDetail: range(0, 1, true),
|
|
2370
|
+
tags: list(str)
|
|
2371
|
+
};
|
|
2372
|
+
var vec2 = tuple(2);
|
|
2373
|
+
var BEVEL = (v) => v === false ? null : object({ thickness: num, size: num, offset: num, segments: int(1) })(v);
|
|
2374
|
+
var KINDS = {
|
|
2375
|
+
group: { fields: {} },
|
|
2376
|
+
box: { fields: { size: tuple(3), segments: tuple(3, int(1)) } },
|
|
2377
|
+
cylinder: { fields: { radiusTop: num, radiusBottom: num, height: num, radialSegments: int(1), heightSegments: int(1), openEnded: bool, thetaStart: num, thetaLength: num } },
|
|
2378
|
+
cone: { fields: { radius: num, height: num, radialSegments: int(1), heightSegments: int(1), openEnded: bool, thetaStart: num, thetaLength: num } },
|
|
2379
|
+
sphere: { fields: { radius: num, widthSegments: int(3), heightSegments: int(2), phiStart: num, phiLength: num, thetaStart: num, thetaLength: num } },
|
|
2380
|
+
plane: { fields: { width: num, height: num, widthSegments: int(1), heightSegments: int(1) } },
|
|
2381
|
+
circle: { fields: { radius: num, segments: int(3), thetaStart: num, thetaLength: num } },
|
|
2382
|
+
ring: { fields: { innerRadius: num, outerRadius: num, thetaSegments: int(3), phiSegments: int(1), thetaStart: num, thetaLength: num } },
|
|
2383
|
+
lathe: { fields: { points: list(vec2, 2), segments: int(1), phiStart: num, phiLength: num }, required: ["points"] },
|
|
2384
|
+
torus: { fields: { radius: num, tube: num, radialSegments: int(1), tubularSegments: int(1), arc: num } },
|
|
2385
|
+
extrude: {
|
|
2386
|
+
fields: { shape: object({ outer: list(vec2, 3), holes: list(list(vec2, 3)) }, ["outer"]), depth: num, steps: int(1), bevel: BEVEL },
|
|
2387
|
+
required: ["shape"]
|
|
2388
|
+
},
|
|
2389
|
+
difference: { fields: { size: tuple(3), subtract: list(object({ min: vec2, max: vec2 }, ["min", "max"])) }, required: ["size", "subtract"] },
|
|
2390
|
+
mesh: {
|
|
2391
|
+
fields: {
|
|
2392
|
+
positions: (v) => list(num)(v) ?? (v.length % 3 === 0 ? null : "expected a multiple of 3 numbers"),
|
|
2393
|
+
normals: list(num),
|
|
2394
|
+
uvs: list(num),
|
|
2395
|
+
indices: list(int(0))
|
|
2396
|
+
},
|
|
2397
|
+
required: ["positions"]
|
|
2398
|
+
},
|
|
2399
|
+
point: { fields: { data: () => null } },
|
|
2400
|
+
custom: { fields: { generator: str, params: () => null }, required: ["generator"] }
|
|
2401
|
+
};
|
|
2402
|
+
function validateNode(node, path, errors) {
|
|
2403
|
+
if (!node || typeof node !== "object" || Array.isArray(node)) {
|
|
2404
|
+
errors.push(`${path}: expected a node object`);
|
|
2405
|
+
return;
|
|
2406
|
+
}
|
|
2407
|
+
const record = node;
|
|
2408
|
+
const spec = typeof record.kind === "string" ? KINDS[record.kind] : void 0;
|
|
2409
|
+
if (!spec) {
|
|
2410
|
+
errors.push(`${path}.kind: expected one of ${Object.keys(KINDS).join(", ")}`);
|
|
2411
|
+
return;
|
|
2412
|
+
}
|
|
2413
|
+
for (const key of spec.required ?? []) if (record[key] === void 0) errors.push(`${path}.${key}: required`);
|
|
2414
|
+
for (const key of Object.keys(record)) {
|
|
2415
|
+
if (record[key] === void 0 || key === "children") continue;
|
|
2416
|
+
const check = spec.fields[key] ?? COMMON[key];
|
|
2417
|
+
if (!check) {
|
|
2418
|
+
errors.push(`${path}.${key}: unknown field for a ${String(record.kind)}`);
|
|
2419
|
+
continue;
|
|
2420
|
+
}
|
|
2421
|
+
const e = check(record[key]);
|
|
2422
|
+
if (e) errors.push(`${path}.${key}: ${e}`);
|
|
2423
|
+
}
|
|
2424
|
+
const exclusive = ["matrix", "heading"].filter((key) => record[key] !== void 0);
|
|
2425
|
+
if (record.matrix !== void 0 && (record.position !== void 0 || record.rotation !== void 0 || record.scale !== void 0 || record.heading !== void 0))
|
|
2426
|
+
errors.push(`${path}.matrix: cannot be combined with position, rotation, scale or heading`);
|
|
2427
|
+
else if (exclusive.includes("heading") && (record.rotation !== void 0 || record.scale !== void 0)) errors.push(`${path}.heading: cannot be combined with rotation or scale`);
|
|
2428
|
+
if (record.children !== void 0) {
|
|
2429
|
+
if (record.kind !== "group") errors.push(`${path}.children: only a group has children`);
|
|
2430
|
+
else if (!Array.isArray(record.children)) errors.push(`${path}.children: expected an array`);
|
|
2431
|
+
else record.children.forEach((child, index) => validateNode(child, `${path}.children[${index}]`, errors));
|
|
2432
|
+
}
|
|
2433
|
+
}
|
|
2434
|
+
function validateShape(schema) {
|
|
2435
|
+
const errors = [];
|
|
2436
|
+
if (!schema || typeof schema !== "object" || Array.isArray(schema)) return ["expected a shape object"];
|
|
2437
|
+
const record = schema;
|
|
2438
|
+
for (const key of Object.keys(record)) if (!["version", "name", "materials", "root", "lod", "cullDistance"].includes(key) && record[key] !== void 0) errors.push(`${key}: unknown field`);
|
|
2439
|
+
if (record.version !== 1) errors.push("version: expected 1");
|
|
2440
|
+
if (record.name !== void 0 && typeof record.name !== "string") errors.push("name: expected a string");
|
|
2441
|
+
if (record.materials !== void 0) {
|
|
2442
|
+
if (!record.materials || typeof record.materials !== "object" || Array.isArray(record.materials)) errors.push("materials: expected an object");
|
|
2443
|
+
else {
|
|
2444
|
+
const material = object({
|
|
2445
|
+
color,
|
|
2446
|
+
roughness: range(0, 1),
|
|
2447
|
+
metalness: range(0, 1),
|
|
2448
|
+
opacity: range(0, 1),
|
|
2449
|
+
transparent: bool,
|
|
2450
|
+
emissive: color,
|
|
2451
|
+
emissiveIntensity: (v) => typeof v === "number" && v >= 0 ? null : "expected a number >= 0",
|
|
2452
|
+
side: oneOf(["front", "back", "double"]),
|
|
2453
|
+
flatShading: bool,
|
|
2454
|
+
vertexColors: bool,
|
|
2455
|
+
depthWrite: bool
|
|
2456
|
+
});
|
|
2457
|
+
for (const [id, hint] of Object.entries(record.materials)) {
|
|
2458
|
+
const e = material(hint);
|
|
2459
|
+
if (e) errors.push(`materials.${id}: ${e}`);
|
|
2460
|
+
}
|
|
2461
|
+
}
|
|
2462
|
+
}
|
|
2463
|
+
if (record.lod !== void 0) {
|
|
2464
|
+
const e = list(object({ distance: (v) => typeof v === "number" && v >= 0 ? null : "expected a number >= 0", detail: range(0, 1, true), minSize: (v) => typeof v === "number" && v >= 0 ? null : "expected a number >= 0", minSegments: int(3) }, ["distance", "detail"]))(record.lod);
|
|
2465
|
+
if (e) errors.push(`lod${e.startsWith("[") ? "" : ": "}${e}`);
|
|
2466
|
+
}
|
|
2467
|
+
if (record.cullDistance !== void 0 && !(typeof record.cullDistance === "number" && record.cullDistance > 0)) errors.push("cullDistance: expected a number > 0");
|
|
2468
|
+
if (record.root === void 0) errors.push("root: required");
|
|
2469
|
+
else validateNode(record.root, "root", errors);
|
|
2470
|
+
return errors;
|
|
2471
|
+
}
|
|
2472
|
+
function assertValidShape(schema) {
|
|
2473
|
+
const errors = validateShape(schema);
|
|
2474
|
+
if (errors.length) throw new Error(`game-shapes: invalid shape
|
|
2475
|
+
${errors.slice(0, 20).join("\n ")}${errors.length > 20 ? `
|
|
2476
|
+
... ${errors.length - 20} more` : ""}`);
|
|
2477
|
+
}
|
|
2478
|
+
|
|
2479
|
+
// src/generate.ts
|
|
2480
|
+
var PRIMITIVE_KINDS = /* @__PURE__ */ new Set(["box", "cylinder", "cone", "sphere", "plane", "circle", "ring", "lathe", "torus", "extrude", "difference", "mesh"]);
|
|
2481
|
+
var isPrimitiveNode = (node) => PRIMITIVE_KINDS.has(node.kind);
|
|
2482
|
+
function keyOptions(options = {}) {
|
|
2483
|
+
return {
|
|
2484
|
+
seed: options.seed ?? 0,
|
|
2485
|
+
colors: options.colors ?? "linear",
|
|
2486
|
+
indexed: options.indexed ?? true,
|
|
2487
|
+
materials: options.materials,
|
|
2488
|
+
fallbackMaterial: options.fallbackMaterial,
|
|
2489
|
+
materialAlias: options.materialAlias,
|
|
2490
|
+
instancing: options.instancing,
|
|
2491
|
+
levels: options.levels
|
|
2492
|
+
};
|
|
2493
|
+
}
|
|
2494
|
+
var shapeKey = (schema, options = {}) => hashValue({ schema, options: keyOptions(options) });
|
|
2495
|
+
var IDENTITY_UV = { u0: 0, v0: 0, u1: 1, v1: 1 };
|
|
2496
|
+
var FloatList = class {
|
|
2497
|
+
constructor(capacity = 256) {
|
|
2498
|
+
__publicField(this, "array");
|
|
2499
|
+
__publicField(this, "length", 0);
|
|
2500
|
+
this.array = new Float32Array(capacity);
|
|
2501
|
+
}
|
|
2502
|
+
reserve(extra) {
|
|
2503
|
+
if (this.length + extra <= this.array.length) return;
|
|
2504
|
+
let capacity = this.array.length * 2;
|
|
2505
|
+
while (capacity < this.length + extra) capacity *= 2;
|
|
2506
|
+
const next = new Float32Array(capacity);
|
|
2507
|
+
next.set(this.array.subarray(0, this.length));
|
|
2508
|
+
this.array = next;
|
|
2509
|
+
}
|
|
2510
|
+
done() {
|
|
2511
|
+
return this.array.slice(0, this.length);
|
|
2512
|
+
}
|
|
2513
|
+
};
|
|
2514
|
+
var IndexList = class {
|
|
2515
|
+
constructor(capacity = 256) {
|
|
2516
|
+
__publicField(this, "array");
|
|
2517
|
+
__publicField(this, "length", 0);
|
|
2518
|
+
this.array = new Uint32Array(capacity);
|
|
2519
|
+
}
|
|
2520
|
+
reserve(extra) {
|
|
2521
|
+
if (this.length + extra <= this.array.length) return;
|
|
2522
|
+
let capacity = this.array.length * 2;
|
|
2523
|
+
while (capacity < this.length + extra) capacity *= 2;
|
|
2524
|
+
const next = new Uint32Array(capacity);
|
|
2525
|
+
next.set(this.array.subarray(0, this.length));
|
|
2526
|
+
this.array = next;
|
|
2527
|
+
}
|
|
2528
|
+
};
|
|
2529
|
+
var Accumulator = class {
|
|
2530
|
+
constructor(material, part) {
|
|
2531
|
+
__publicField(this, "material", material);
|
|
2532
|
+
__publicField(this, "part", part);
|
|
2533
|
+
__publicField(this, "positions", new FloatList());
|
|
2534
|
+
__publicField(this, "normals", new FloatList());
|
|
2535
|
+
__publicField(this, "uvs", new FloatList(170));
|
|
2536
|
+
__publicField(this, "colors", new FloatList());
|
|
2537
|
+
__publicField(this, "indices", new IndexList());
|
|
2538
|
+
__publicField(this, "ranges", []);
|
|
2539
|
+
__publicField(this, "vertices", 0);
|
|
2540
|
+
}
|
|
2541
|
+
};
|
|
2542
|
+
function bake(acc, t, world, rgb, u, indexed) {
|
|
2543
|
+
const e = world;
|
|
2544
|
+
const nm = normalMatrix(world);
|
|
2545
|
+
const affine = isAffine(world);
|
|
2546
|
+
const source = t.positions;
|
|
2547
|
+
const count = source.length / 3;
|
|
2548
|
+
const order = indexed ? null : t.indices ?? null;
|
|
2549
|
+
const emitted = order ? order.length : count;
|
|
2550
|
+
acc.positions.reserve(emitted * 3);
|
|
2551
|
+
acc.normals.reserve(emitted * 3);
|
|
2552
|
+
acc.uvs.reserve(emitted * 2);
|
|
2553
|
+
if (rgb) acc.colors.reserve(emitted * 3);
|
|
2554
|
+
const P = acc.positions.array;
|
|
2555
|
+
const N = acc.normals.array;
|
|
2556
|
+
const U = acc.uvs.array;
|
|
2557
|
+
const C = acc.colors.array;
|
|
2558
|
+
let pi = acc.positions.length;
|
|
2559
|
+
let ni = acc.normals.length;
|
|
2560
|
+
let ui = acc.uvs.length;
|
|
2561
|
+
let ci = acc.colors.length;
|
|
2562
|
+
const du = u.u1 - u.u0;
|
|
2563
|
+
const dv = u.v1 - u.v0;
|
|
2564
|
+
const tn = t.normals;
|
|
2565
|
+
const tu = t.uvs;
|
|
2566
|
+
for (let k = 0; k < emitted; k++) {
|
|
2567
|
+
const i = order ? order[k] : k;
|
|
2568
|
+
const x = source[i * 3];
|
|
2569
|
+
const y = source[i * 3 + 1];
|
|
2570
|
+
const z = source[i * 3 + 2];
|
|
2571
|
+
if (affine) {
|
|
2572
|
+
P[pi++] = e[0] * x + e[4] * y + e[8] * z + e[12];
|
|
2573
|
+
P[pi++] = e[1] * x + e[5] * y + e[9] * z + e[13];
|
|
2574
|
+
P[pi++] = e[2] * x + e[6] * y + e[10] * z + e[14];
|
|
2575
|
+
} else {
|
|
2576
|
+
const p = transformPoint(e, x, y, z);
|
|
2577
|
+
P[pi++] = p[0];
|
|
2578
|
+
P[pi++] = p[1];
|
|
2579
|
+
P[pi++] = p[2];
|
|
2580
|
+
}
|
|
2581
|
+
const nx = tn[i * 3];
|
|
2582
|
+
const ny = tn[i * 3 + 1];
|
|
2583
|
+
const nz = tn[i * 3 + 2];
|
|
2584
|
+
const vx = nm[0] * nx + nm[3] * ny + nm[6] * nz;
|
|
2585
|
+
const vy = nm[1] * nx + nm[4] * ny + nm[7] * nz;
|
|
2586
|
+
const vz = nm[2] * nx + nm[5] * ny + nm[8] * nz;
|
|
2587
|
+
const s = 1 / (Math.sqrt(vx * vx + vy * vy + vz * vz) || 1);
|
|
2588
|
+
N[ni++] = vx * s;
|
|
2589
|
+
N[ni++] = vy * s;
|
|
2590
|
+
N[ni++] = vz * s;
|
|
2591
|
+
U[ui++] = u.u0 + tu[i * 2] * du;
|
|
2592
|
+
U[ui++] = u.v0 + tu[i * 2 + 1] * dv;
|
|
2593
|
+
if (rgb) {
|
|
2594
|
+
C[ci++] = rgb[0];
|
|
2595
|
+
C[ci++] = rgb[1];
|
|
2596
|
+
C[ci++] = rgb[2];
|
|
2597
|
+
}
|
|
2598
|
+
}
|
|
2599
|
+
acc.positions.length = pi;
|
|
2600
|
+
acc.normals.length = ni;
|
|
2601
|
+
acc.uvs.length = ui;
|
|
2602
|
+
acc.colors.length = ci;
|
|
2603
|
+
if (indexed) {
|
|
2604
|
+
const base = acc.vertices;
|
|
2605
|
+
const idx = t.indices;
|
|
2606
|
+
const n = idx ? idx.length : count;
|
|
2607
|
+
acc.indices.reserve(n);
|
|
2608
|
+
const I = acc.indices.array;
|
|
2609
|
+
let ii = acc.indices.length;
|
|
2610
|
+
for (let k = 0; k < n; k++) I[ii++] = base + (idx ? idx[k] : k);
|
|
2611
|
+
acc.indices.length = ii;
|
|
2612
|
+
}
|
|
2613
|
+
acc.vertices += emitted;
|
|
2614
|
+
}
|
|
2615
|
+
function boundsOf(positions) {
|
|
2616
|
+
const min = [Infinity, Infinity, Infinity];
|
|
2617
|
+
const max = [-Infinity, -Infinity, -Infinity];
|
|
2618
|
+
for (let i = 0; i < positions.length; i += 3) {
|
|
2619
|
+
for (let axis = 0; axis < 3; axis++) {
|
|
2620
|
+
const v = positions[i + axis];
|
|
2621
|
+
if (v < min[axis]) min[axis] = v;
|
|
2622
|
+
if (v > max[axis]) max[axis] = v;
|
|
2623
|
+
}
|
|
2624
|
+
}
|
|
2625
|
+
return { min, max };
|
|
2626
|
+
}
|
|
2627
|
+
function finish(acc, indexed, withColors) {
|
|
2628
|
+
const positions = acc.positions.done();
|
|
2629
|
+
const indices = indexed ? acc.vertices > 65535 ? acc.indices.array.slice(0, acc.indices.length) : Uint16Array.from(acc.indices.array.subarray(0, acc.indices.length)) : void 0;
|
|
2630
|
+
return {
|
|
2631
|
+
material: acc.material,
|
|
2632
|
+
part: acc.part,
|
|
2633
|
+
positions,
|
|
2634
|
+
normals: acc.normals.done(),
|
|
2635
|
+
uvs: acc.uvs.done(),
|
|
2636
|
+
...withColors ? { colors: acc.colors.done() } : {},
|
|
2637
|
+
...indices ? { indices } : {},
|
|
2638
|
+
ranges: acc.ranges,
|
|
2639
|
+
bounds: boundsOf(positions)
|
|
2640
|
+
};
|
|
2641
|
+
}
|
|
2642
|
+
var radiusOf = (t, world) => {
|
|
2643
|
+
let r = 0;
|
|
2644
|
+
const p = t.positions;
|
|
2645
|
+
for (let i = 0; i < p.length; i += 3) r = Math.max(r, Math.hypot(p[i], p[i + 1], p[i + 2]));
|
|
2646
|
+
const sx = Math.hypot(world[0], world[1], world[2]);
|
|
2647
|
+
const sy = Math.hypot(world[4], world[5], world[6]);
|
|
2648
|
+
const sz = Math.hypot(world[8], world[9], world[10]);
|
|
2649
|
+
return r * Math.max(sx, sy, sz);
|
|
2650
|
+
};
|
|
2651
|
+
function expandCustom(node, settings, detail, path) {
|
|
2652
|
+
const generator = settings.generators?.[node.generator] ?? getGenerator(node.generator);
|
|
2653
|
+
if (!generator) throw new Error(`game-shapes: no generator "${node.generator}" (register it with registerGenerator, in the worker too) at ${path}`);
|
|
2654
|
+
const generated = generator(node.params, { random: streamFor(settings.seed, node.generator, path), seed: settings.seed, detail, path });
|
|
2655
|
+
const { kind: _kind, generator: _generator, params: _params, ...common } = node;
|
|
2656
|
+
return { ...common, kind: "group", children: [generated] };
|
|
2657
|
+
}
|
|
2658
|
+
var leafKey = (node, material, color2, uv, level) => `${node.kind}|${canonicalJson(primitiveParameters(node, level))}|${material}|${color2}|${uv.u0},${uv.v0},${uv.u1},${uv.v1}`;
|
|
2659
|
+
function runLevel(schema, settings, level) {
|
|
2660
|
+
const bakeParts = level.index > 0;
|
|
2661
|
+
const root = { part: null, accs: /* @__PURE__ */ new Map() };
|
|
2662
|
+
const targets = [root];
|
|
2663
|
+
const partTargets = /* @__PURE__ */ new Map();
|
|
2664
|
+
const parts = [];
|
|
2665
|
+
const points = [];
|
|
2666
|
+
const instanced = [];
|
|
2667
|
+
let primitives = 0;
|
|
2668
|
+
const space = settings.colors === "none" ? null : settings.colors;
|
|
2669
|
+
const channels = /* @__PURE__ */ new Map();
|
|
2670
|
+
const rgbOf = (hex) => {
|
|
2671
|
+
if (!space) return null;
|
|
2672
|
+
let c = channels.get(hex);
|
|
2673
|
+
if (!c) {
|
|
2674
|
+
c = colorChannels(hex, space);
|
|
2675
|
+
channels.set(hex, c);
|
|
2676
|
+
}
|
|
2677
|
+
return c;
|
|
2678
|
+
};
|
|
2679
|
+
const resolveMaterial = (id) => {
|
|
2680
|
+
let out = id;
|
|
2681
|
+
if (settings.materials && !settings.materials.has(out)) out = settings.fallback ?? out;
|
|
2682
|
+
return settings.alias?.[out] ?? out;
|
|
2683
|
+
};
|
|
2684
|
+
const accOf = (target, material) => {
|
|
2685
|
+
let acc = target.accs.get(material);
|
|
2686
|
+
if (!acc) {
|
|
2687
|
+
acc = new Accumulator(material, target.part);
|
|
2688
|
+
target.accs.set(material, acc);
|
|
2689
|
+
}
|
|
2690
|
+
return acc;
|
|
2691
|
+
};
|
|
2692
|
+
const autoCounts = /* @__PURE__ */ new Map();
|
|
2693
|
+
const autoGroups = /* @__PURE__ */ new Map();
|
|
2694
|
+
const count = (node, inherited, path) => {
|
|
2695
|
+
if (node.minDetail !== void 0 && level.detail < node.minDetail) return;
|
|
2696
|
+
if (node.kind === "custom") return count(expandCustom(node, settings, level.detail, path), inherited, path);
|
|
2697
|
+
const next = {
|
|
2698
|
+
material: node.material !== void 0 ? resolveMaterial(node.material) : inherited.material,
|
|
2699
|
+
color: node.color !== void 0 ? colorHex(node.color) : inherited.color,
|
|
2700
|
+
uv: node.uv ?? inherited.uv,
|
|
2701
|
+
inPart: inherited.inPart || node.part !== void 0 && !bakeParts
|
|
2702
|
+
};
|
|
2703
|
+
if (node.instances) return;
|
|
2704
|
+
if (node.kind === "group") {
|
|
2705
|
+
node.children?.forEach((child, index) => count(child, next, `${path}/${index}`));
|
|
2706
|
+
return;
|
|
2707
|
+
}
|
|
2708
|
+
if (isPrimitiveNode(node) && node.name === void 0 && !next.inPart) {
|
|
2709
|
+
const key = leafKey(node, next.material, next.color, next.uv, level);
|
|
2710
|
+
autoCounts.set(key, (autoCounts.get(key) ?? 0) + 1);
|
|
2711
|
+
}
|
|
2712
|
+
};
|
|
2713
|
+
const rootMaterial = resolveMaterial("default");
|
|
2714
|
+
if (settings.autoMin !== void 0) count(schema.root, { material: rootMaterial, color: 16777215, uv: IDENTITY_UV, inPart: false }, "root");
|
|
2715
|
+
const visit = (node, parent, inherited, path) => {
|
|
2716
|
+
if (node.minDetail !== void 0 && level.detail < node.minDetail) return;
|
|
2717
|
+
if (node.kind === "custom") return visit(expandCustom(node, settings, level.detail, path), parent, inherited, path);
|
|
2718
|
+
let target = inherited.target;
|
|
2719
|
+
let parentWorld = parent;
|
|
2720
|
+
if (node.part !== void 0) {
|
|
2721
|
+
if (bakeParts) {
|
|
2722
|
+
parentWorld = null;
|
|
2723
|
+
} else {
|
|
2724
|
+
let partTarget = partTargets.get(node.part);
|
|
2725
|
+
if (!partTarget) {
|
|
2726
|
+
partTarget = { part: node.part, accs: /* @__PURE__ */ new Map() };
|
|
2727
|
+
partTargets.set(node.part, partTarget);
|
|
2728
|
+
targets.push(partTarget);
|
|
2729
|
+
const { position, rotation, rotationOrder, scale, heading, matrix } = node;
|
|
2730
|
+
parts.push({
|
|
2731
|
+
id: node.part,
|
|
2732
|
+
...position ? { position } : {},
|
|
2733
|
+
...rotation ? { rotation } : {},
|
|
2734
|
+
...rotationOrder ? { rotationOrder } : {},
|
|
2735
|
+
...scale !== void 0 ? { scale } : {},
|
|
2736
|
+
...heading !== void 0 ? { heading } : {},
|
|
2737
|
+
...matrix ? { matrix } : { matrix: asMat4(matrixOf(node)) }
|
|
2738
|
+
});
|
|
2739
|
+
}
|
|
2740
|
+
target = partTarget;
|
|
2741
|
+
}
|
|
2742
|
+
}
|
|
2743
|
+
const isPart = node.part !== void 0 && !bakeParts;
|
|
2744
|
+
const local = isPart ? null : matrixOf(node);
|
|
2745
|
+
const world = isPart ? null : local ? parentWorld ? multiply(parentWorld, local) : local : parentWorld;
|
|
2746
|
+
const next = {
|
|
2747
|
+
material: node.material !== void 0 ? resolveMaterial(node.material) : inherited.material,
|
|
2748
|
+
color: node.color !== void 0 ? colorHex(node.color) : inherited.color,
|
|
2749
|
+
uv: node.uv ?? inherited.uv,
|
|
2750
|
+
target
|
|
2751
|
+
};
|
|
2752
|
+
if (node.instances) {
|
|
2753
|
+
drawInstanced(node, parentWorld, next, path);
|
|
2754
|
+
return;
|
|
2755
|
+
}
|
|
2756
|
+
const snapshot = node.name !== void 0 ? new Map([...target.accs].map(([m, a]) => [m, { v: a.vertices, i: a.indices.length }])) : null;
|
|
2757
|
+
if (node.kind === "group") {
|
|
2758
|
+
node.children?.forEach((child, index) => visit(child, world, next, `${path}/${index}`));
|
|
2759
|
+
} else if (node.kind === "point") {
|
|
2760
|
+
const p = node.position ?? [0, 0, 0];
|
|
2761
|
+
points.push({
|
|
2762
|
+
...node.name !== void 0 ? { name: node.name } : {},
|
|
2763
|
+
part: target.part,
|
|
2764
|
+
position: parent ? transformPoint(parent, p[0], p[1], p[2]) : [p[0], p[1], p[2]],
|
|
2765
|
+
...node.data !== void 0 ? { data: node.data } : {},
|
|
2766
|
+
...node.tags ? { tags: node.tags } : {}
|
|
2767
|
+
});
|
|
2768
|
+
} else if (isPrimitiveNode(node)) {
|
|
2769
|
+
const template = buildPrimitive(node, level);
|
|
2770
|
+
const m = world ?? identity4();
|
|
2771
|
+
if (level.minSize !== void 0 && radiusOf(template, m) < level.minSize) return;
|
|
2772
|
+
primitives++;
|
|
2773
|
+
if (settings.autoMin !== void 0 && node.name === void 0 && target.part === null) {
|
|
2774
|
+
const key = leafKey(node, next.material, next.color, next.uv, level);
|
|
2775
|
+
if ((autoCounts.get(key) ?? 0) >= settings.autoMin) {
|
|
2776
|
+
let group = autoGroups.get(key);
|
|
2777
|
+
if (!group) {
|
|
2778
|
+
group = { node, material: next.material, color: next.color, part: null, matrices: [] };
|
|
2779
|
+
autoGroups.set(key, group);
|
|
2780
|
+
}
|
|
2781
|
+
group.matrices.push(...m);
|
|
2782
|
+
return;
|
|
2783
|
+
}
|
|
2784
|
+
}
|
|
2785
|
+
bake(accOf(target, next.material), template, m, rgbOf(next.color), next.uv, settings.indexed);
|
|
2786
|
+
}
|
|
2787
|
+
if (snapshot) {
|
|
2788
|
+
for (const [material, acc] of target.accs) {
|
|
2789
|
+
const before = snapshot.get(material) ?? { v: 0, i: 0 };
|
|
2790
|
+
if (acc.vertices === before.v) continue;
|
|
2791
|
+
acc.ranges.push({
|
|
2792
|
+
name: node.name,
|
|
2793
|
+
...node.tags ? { tags: node.tags } : {},
|
|
2794
|
+
vertexStart: before.v,
|
|
2795
|
+
vertexCount: acc.vertices - before.v,
|
|
2796
|
+
indexStart: before.i,
|
|
2797
|
+
indexCount: acc.indices.length - before.i
|
|
2798
|
+
});
|
|
2799
|
+
}
|
|
2800
|
+
}
|
|
2801
|
+
};
|
|
2802
|
+
const drawInstanced = (node, parentWorld, inherited, path) => {
|
|
2803
|
+
const collector = { part: inherited.target.part, accs: /* @__PURE__ */ new Map() };
|
|
2804
|
+
const { instances, ...single } = node;
|
|
2805
|
+
const before = parts.length;
|
|
2806
|
+
visit(single, null, { ...inherited, target: collector }, path);
|
|
2807
|
+
if (parts.length !== before) throw new Error(`game-shapes: an instanced node cannot hold parts (${path})`);
|
|
2808
|
+
const matrices = new Float32Array(instances.length * 16);
|
|
2809
|
+
const hasColors = instances.some((instance) => instance.color !== void 0);
|
|
2810
|
+
const colors = hasColors ? new Float32Array(instances.length * 3) : void 0;
|
|
2811
|
+
instances.forEach((instance, index) => {
|
|
2812
|
+
const local = matrixOf(instance);
|
|
2813
|
+
const m = local ? parentWorld ? multiply(parentWorld, local) : local : parentWorld ?? identity4();
|
|
2814
|
+
matrices.set(m, index * 16);
|
|
2815
|
+
if (colors) colors.set(colorChannels(colorHex(instance.color), space ?? "srgb"), index * 3);
|
|
2816
|
+
});
|
|
2817
|
+
for (const acc of collector.accs.values()) {
|
|
2818
|
+
const batch = finish(acc, settings.indexed, false);
|
|
2819
|
+
instanced.push({
|
|
2820
|
+
material: acc.material,
|
|
2821
|
+
part: collector.part,
|
|
2822
|
+
...node.name !== void 0 ? { name: node.name } : {},
|
|
2823
|
+
geometry: { type: "BufferGeometry", parameters: {}, positions: batch.positions, normals: batch.normals, uvs: batch.uvs, ...batch.indices ? { indices: batch.indices } : {}, groups: [] },
|
|
2824
|
+
matrices: matrices.slice(),
|
|
2825
|
+
...colors ? { colors: colors.slice() } : {},
|
|
2826
|
+
count: instances.length
|
|
2827
|
+
});
|
|
2828
|
+
}
|
|
2829
|
+
};
|
|
2830
|
+
visit(schema.root, null, { material: rootMaterial, color: 16777215, uv: IDENTITY_UV, target: root }, "root");
|
|
2831
|
+
for (const group of autoGroups.values()) {
|
|
2832
|
+
const template = buildPrimitive(group.node, level);
|
|
2833
|
+
const n = group.matrices.length / 16;
|
|
2834
|
+
const rgb = colorChannels(group.color, space ?? "srgb");
|
|
2835
|
+
const colors = new Float32Array(n * 3);
|
|
2836
|
+
for (let i = 0; i < n; i++) colors.set(rgb, i * 3);
|
|
2837
|
+
instanced.push({
|
|
2838
|
+
material: group.material,
|
|
2839
|
+
part: null,
|
|
2840
|
+
geometry: { type: template.type, parameters: template.parameters, positions: template.positions.slice(), normals: template.normals.slice(), uvs: template.uvs.slice(), ...template.indices ? { indices: template.indices.slice() } : {}, groups: template.groups.map((g) => ({ ...g })) },
|
|
2841
|
+
matrices: new Float32Array(group.matrices),
|
|
2842
|
+
...space ? { colors } : {},
|
|
2843
|
+
count: n
|
|
2844
|
+
});
|
|
2845
|
+
}
|
|
2846
|
+
const batches = [];
|
|
2847
|
+
for (const target of targets) for (const acc of target.accs.values()) if (acc.vertices > 0 && (!settings.indexed || acc.indices.length > 0)) batches.push(finish(acc, settings.indexed, space !== null));
|
|
2848
|
+
return { level: { distance: level.distance, detail: level.detail, batches, instanced }, parts, points, primitives };
|
|
2849
|
+
}
|
|
2850
|
+
function generate(schema, options = {}) {
|
|
2851
|
+
if (options.validate !== false) assertValidShape(schema);
|
|
2852
|
+
const settings = {
|
|
2853
|
+
colors: options.colors ?? "linear",
|
|
2854
|
+
indexed: options.indexed ?? true,
|
|
2855
|
+
...options.materials ? { materials: new Set(options.materials) } : {},
|
|
2856
|
+
...options.fallbackMaterial !== void 0 ? { fallback: options.fallbackMaterial } : {},
|
|
2857
|
+
...options.materialAlias ? { alias: options.materialAlias } : {},
|
|
2858
|
+
seed: options.seed ?? 0,
|
|
2859
|
+
...options.generators ? { generators: options.generators } : {},
|
|
2860
|
+
...options.instancing ? { autoMin: Math.max(2, options.instancing.minCount) } : {}
|
|
2861
|
+
};
|
|
2862
|
+
const specs = [{ distance: 0, detail: 1, index: 0 }, ...(schema.lod ?? []).map((rule, index) => ({ ...rule, index: index + 1 }))];
|
|
2863
|
+
const wanted = options.levels ? specs.filter((spec) => options.levels.includes(spec.index)) : specs;
|
|
2864
|
+
let parts = [];
|
|
2865
|
+
let points = [];
|
|
2866
|
+
let primitives = 0;
|
|
2867
|
+
const levels = [];
|
|
2868
|
+
for (const spec of wanted) {
|
|
2869
|
+
const result = runLevel(schema, settings, spec);
|
|
2870
|
+
if (spec.index === 0 || levels.length === 0) {
|
|
2871
|
+
parts = result.parts;
|
|
2872
|
+
points = result.points;
|
|
2873
|
+
primitives = result.primitives;
|
|
2874
|
+
}
|
|
2875
|
+
levels.push(result.level);
|
|
2876
|
+
}
|
|
2877
|
+
const first = levels[0];
|
|
2878
|
+
const vertices = first ? first.batches.reduce((sum, batch) => sum + batch.positions.length / 3, 0) : 0;
|
|
2879
|
+
const triangles = first ? first.batches.reduce((sum, batch) => sum + (batch.indices ? batch.indices.length : batch.positions.length / 3) / 3, 0) + first.instanced.reduce((sum, batch) => sum + (batch.geometry.indices ? batch.geometry.indices.length : batch.geometry.positions.length / 3) / 3 * batch.count, 0) : 0;
|
|
2880
|
+
return {
|
|
2881
|
+
key: options.key === false ? "" : options.key ?? shapeKey(schema, options),
|
|
2882
|
+
...schema.name !== void 0 ? { name: schema.name } : {},
|
|
2883
|
+
levels,
|
|
2884
|
+
parts,
|
|
2885
|
+
points,
|
|
2886
|
+
...schema.materials ? { materials: schema.materials } : {},
|
|
2887
|
+
...schema.cullDistance !== void 0 ? { cullDistance: schema.cullDistance } : {},
|
|
2888
|
+
stats: { vertices, triangles, batches: first ? first.batches.length : 0, instances: first ? first.instanced.reduce((sum, batch) => sum + batch.count, 0) : 0, primitives }
|
|
2889
|
+
};
|
|
2890
|
+
}
|
|
2891
|
+
|
|
2892
|
+
// src/merge.ts
|
|
2893
|
+
function mergeTransformed(pieces) {
|
|
2894
|
+
let total = 0;
|
|
2895
|
+
for (const piece of pieces) total += piece.indices ? piece.indices.length : piece.positions.length / 3;
|
|
2896
|
+
const positions = new Float32Array(total * 3);
|
|
2897
|
+
const normals = new Float32Array(total * 3);
|
|
2898
|
+
const uvs = new Float32Array(total * 2);
|
|
2899
|
+
let at = 0;
|
|
2900
|
+
for (const piece of pieces) {
|
|
2901
|
+
const e = piece.matrix;
|
|
2902
|
+
const nm = normalMatrix(e);
|
|
2903
|
+
const count = piece.indices ? piece.indices.length : piece.positions.length / 3;
|
|
2904
|
+
const p = new Float32Array(count * 3);
|
|
2905
|
+
const n = new Float32Array(count * 3);
|
|
2906
|
+
for (let k = 0; k < count; k++) {
|
|
2907
|
+
const i = piece.indices ? piece.indices[k] : k;
|
|
2908
|
+
for (let c = 0; c < 3; c++) {
|
|
2909
|
+
p[k * 3 + c] = piece.positions[i * 3 + c];
|
|
2910
|
+
if (piece.normals) n[k * 3 + c] = piece.normals[i * 3 + c];
|
|
2911
|
+
}
|
|
2912
|
+
if (piece.uvs) {
|
|
2913
|
+
uvs[(at + k) * 2] = piece.uvs[i * 2];
|
|
2914
|
+
uvs[(at + k) * 2 + 1] = piece.uvs[i * 2 + 1];
|
|
2915
|
+
}
|
|
2916
|
+
}
|
|
2917
|
+
for (let k = 0; k < count; k++) {
|
|
2918
|
+
const q = transformPoint(e, p[k * 3], p[k * 3 + 1], p[k * 3 + 2]);
|
|
2919
|
+
positions[(at + k) * 3] = q[0];
|
|
2920
|
+
positions[(at + k) * 3 + 1] = q[1];
|
|
2921
|
+
positions[(at + k) * 3 + 2] = q[2];
|
|
2922
|
+
const x = n[k * 3];
|
|
2923
|
+
const y = n[k * 3 + 1];
|
|
2924
|
+
const z = n[k * 3 + 2];
|
|
2925
|
+
const vx = nm[0] * x + nm[3] * y + nm[6] * z;
|
|
2926
|
+
const vy = nm[1] * x + nm[4] * y + nm[7] * z;
|
|
2927
|
+
const vz = nm[2] * x + nm[5] * y + nm[8] * z;
|
|
2928
|
+
const s = 1 / (Math.sqrt(vx * vx + vy * vy + vz * vz) || 1);
|
|
2929
|
+
normals[(at + k) * 3] = vx * s;
|
|
2930
|
+
normals[(at + k) * 3 + 1] = vy * s;
|
|
2931
|
+
normals[(at + k) * 3 + 2] = vz * s;
|
|
2932
|
+
}
|
|
2933
|
+
at += count;
|
|
2934
|
+
}
|
|
2935
|
+
return { positions, normals, uvs };
|
|
2936
|
+
}
|
|
2937
|
+
var primitiveArrays = (p) => [p.positions, p.normals, p.uvs, ...p.indices ? [p.indices] : []].map((a) => a.buffer);
|
|
2938
|
+
var batchArrays = (b) => [b.positions, b.normals, b.uvs, ...b.colors ? [b.colors] : [], ...b.indices ? [b.indices] : []].map((a) => a.buffer);
|
|
2939
|
+
var instancedArrays = (b) => [...primitiveArrays(b.geometry), b.matrices.buffer, ...b.colors ? [b.colors.buffer] : []];
|
|
2940
|
+
function transferablesOf(buffers) {
|
|
2941
|
+
const out = /* @__PURE__ */ new Set();
|
|
2942
|
+
for (const level of buffers.levels) {
|
|
2943
|
+
for (const batch of level.batches) for (const a of batchArrays(batch)) out.add(a);
|
|
2944
|
+
for (const batch of level.instanced) for (const a of instancedArrays(batch)) out.add(a);
|
|
2945
|
+
}
|
|
2946
|
+
return [...out];
|
|
2947
|
+
}
|
|
2948
|
+
var copyPrimitive = (p) => ({
|
|
2949
|
+
...p,
|
|
2950
|
+
positions: p.positions.slice(),
|
|
2951
|
+
normals: p.normals.slice(),
|
|
2952
|
+
uvs: p.uvs.slice(),
|
|
2953
|
+
...p.indices ? { indices: p.indices.slice() } : {},
|
|
2954
|
+
groups: p.groups.map((g) => ({ ...g }))
|
|
2955
|
+
});
|
|
2956
|
+
function cloneBuffers(buffers) {
|
|
2957
|
+
return {
|
|
2958
|
+
...buffers,
|
|
2959
|
+
levels: buffers.levels.map((level) => ({
|
|
2960
|
+
...level,
|
|
2961
|
+
batches: level.batches.map((b) => ({
|
|
2962
|
+
...b,
|
|
2963
|
+
positions: b.positions.slice(),
|
|
2964
|
+
normals: b.normals.slice(),
|
|
2965
|
+
uvs: b.uvs.slice(),
|
|
2966
|
+
...b.colors ? { colors: b.colors.slice() } : {},
|
|
2967
|
+
...b.indices ? { indices: b.indices.slice() } : {},
|
|
2968
|
+
ranges: b.ranges.map((r) => ({ ...r })),
|
|
2969
|
+
bounds: { min: [...b.bounds.min], max: [...b.bounds.max] }
|
|
2970
|
+
})),
|
|
2971
|
+
instanced: level.instanced.map((b) => ({ ...b, geometry: copyPrimitive(b.geometry), matrices: b.matrices.slice(), ...b.colors ? { colors: b.colors.slice() } : {} }))
|
|
2972
|
+
}))
|
|
2973
|
+
};
|
|
2974
|
+
}
|
|
2975
|
+
function byteSizeOf(buffers) {
|
|
2976
|
+
return transferablesOf(buffers).reduce((sum, buffer) => sum + buffer.byteLength, 0);
|
|
2977
|
+
}
|
|
2978
|
+
|
|
2979
|
+
// src/cache.ts
|
|
2980
|
+
function createShapeCache(options = {}) {
|
|
2981
|
+
const maxEntries = options.maxEntries ?? 256;
|
|
2982
|
+
const maxBytes = options.maxBytes ?? 64 * 1024 * 1024;
|
|
2983
|
+
const entries = /* @__PURE__ */ new Map();
|
|
2984
|
+
let bytes = 0;
|
|
2985
|
+
let hits = 0;
|
|
2986
|
+
let misses = 0;
|
|
2987
|
+
let evictions = 0;
|
|
2988
|
+
const drop = (key) => {
|
|
2989
|
+
const entry = entries.get(key);
|
|
2990
|
+
if (!entry) return false;
|
|
2991
|
+
entries.delete(key);
|
|
2992
|
+
bytes -= entry.bytes;
|
|
2993
|
+
return true;
|
|
2994
|
+
};
|
|
2995
|
+
return {
|
|
2996
|
+
get(key) {
|
|
2997
|
+
const entry = entries.get(key);
|
|
2998
|
+
if (!entry) {
|
|
2999
|
+
misses++;
|
|
3000
|
+
return void 0;
|
|
3001
|
+
}
|
|
3002
|
+
hits++;
|
|
3003
|
+
entries.delete(key);
|
|
3004
|
+
entries.set(key, entry);
|
|
3005
|
+
return entry.buffers;
|
|
3006
|
+
},
|
|
3007
|
+
set(key, buffers) {
|
|
3008
|
+
drop(key);
|
|
3009
|
+
const size = byteSizeOf(buffers);
|
|
3010
|
+
if (size > maxBytes || maxEntries <= 0) return;
|
|
3011
|
+
entries.set(key, { buffers, bytes: size });
|
|
3012
|
+
bytes += size;
|
|
3013
|
+
for (const oldest of entries.keys()) {
|
|
3014
|
+
if (entries.size <= maxEntries && bytes <= maxBytes) break;
|
|
3015
|
+
drop(oldest);
|
|
3016
|
+
evictions++;
|
|
3017
|
+
}
|
|
3018
|
+
},
|
|
3019
|
+
has: (key) => entries.has(key),
|
|
3020
|
+
delete: drop,
|
|
3021
|
+
clear() {
|
|
3022
|
+
entries.clear();
|
|
3023
|
+
bytes = 0;
|
|
3024
|
+
},
|
|
3025
|
+
stats: () => ({ entries: entries.size, bytes, hits, misses, evictions })
|
|
3026
|
+
};
|
|
3027
|
+
}
|
|
3028
|
+
|
|
3029
|
+
// src/pool.ts
|
|
3030
|
+
var isRequest = (value) => value.schema !== void 0 && value.root === void 0;
|
|
3031
|
+
function createShapePool(options = {}) {
|
|
3032
|
+
const size = options.spawn ? Math.max(0, options.size ?? 0) : 0;
|
|
3033
|
+
const cache = options.cache;
|
|
3034
|
+
const slots = [];
|
|
3035
|
+
const inFlight = /* @__PURE__ */ new Map();
|
|
3036
|
+
let nextId = 1;
|
|
3037
|
+
let jobs = 0;
|
|
3038
|
+
let cacheHits = 0;
|
|
3039
|
+
let shared = 0;
|
|
3040
|
+
let inThread = 0;
|
|
3041
|
+
let terminated = false;
|
|
3042
|
+
const failAll = (slot, error) => {
|
|
3043
|
+
for (const job of slot.pending.values()) job.reject(error);
|
|
3044
|
+
slot.pending.clear();
|
|
3045
|
+
};
|
|
3046
|
+
const spawnSlot = () => {
|
|
3047
|
+
const slot = { worker: options.spawn(), pending: /* @__PURE__ */ new Map() };
|
|
3048
|
+
slot.worker.onmessage = (event) => {
|
|
3049
|
+
const reply = event.data;
|
|
3050
|
+
const job = slot.pending.get(reply.id);
|
|
3051
|
+
if (!job) return;
|
|
3052
|
+
slot.pending.delete(reply.id);
|
|
3053
|
+
if (reply.ok) job.resolve(reply.buffers);
|
|
3054
|
+
else job.reject(new Error(reply.error));
|
|
3055
|
+
};
|
|
3056
|
+
slot.worker.onerror = (event) => {
|
|
3057
|
+
failAll(slot, new Error(`game-shapes worker failed: ${event?.message ?? String(event)}`));
|
|
3058
|
+
slot.worker.terminate();
|
|
3059
|
+
const index = slots.indexOf(slot);
|
|
3060
|
+
if (index >= 0 && !terminated) slots[index] = spawnSlot();
|
|
3061
|
+
};
|
|
3062
|
+
return slot;
|
|
3063
|
+
};
|
|
3064
|
+
for (let i = 0; i < size; i++) slots.push(spawnSlot());
|
|
3065
|
+
const run = (schema, generateOptions) => {
|
|
3066
|
+
if (slots.length === 0 || generateOptions.generators) {
|
|
3067
|
+
inThread++;
|
|
3068
|
+
try {
|
|
3069
|
+
const generators2 = { ...options.generators ?? {}, ...generateOptions.generators ?? {} };
|
|
3070
|
+
return Promise.resolve(generate(schema, { ...generateOptions, ...Object.keys(generators2).length ? { generators: generators2 } : {} }));
|
|
3071
|
+
} catch (error) {
|
|
3072
|
+
return Promise.reject(error);
|
|
3073
|
+
}
|
|
3074
|
+
}
|
|
3075
|
+
let slot = slots[0];
|
|
3076
|
+
for (const s of slots) if (s.pending.size < slot.pending.size) slot = s;
|
|
3077
|
+
const id = nextId++;
|
|
3078
|
+
const job = { id, schema, options: generateOptions };
|
|
3079
|
+
return new Promise((resolve, reject) => {
|
|
3080
|
+
slot.pending.set(id, { resolve, reject });
|
|
3081
|
+
slot.worker.postMessage(job);
|
|
3082
|
+
});
|
|
3083
|
+
};
|
|
3084
|
+
const pool = {
|
|
3085
|
+
generate(schema, generateOptions = {}) {
|
|
3086
|
+
if (terminated) return Promise.reject(new Error("createShapePool: the pool was terminated"));
|
|
3087
|
+
jobs++;
|
|
3088
|
+
const key = shapeKey(schema, generateOptions);
|
|
3089
|
+
const hit = cache?.get(key);
|
|
3090
|
+
if (hit) {
|
|
3091
|
+
cacheHits++;
|
|
3092
|
+
return Promise.resolve(hit);
|
|
3093
|
+
}
|
|
3094
|
+
const flying = inFlight.get(key);
|
|
3095
|
+
if (flying) {
|
|
3096
|
+
shared++;
|
|
3097
|
+
return flying;
|
|
3098
|
+
}
|
|
3099
|
+
const promise = run(schema, generateOptions).then(
|
|
3100
|
+
(buffers) => {
|
|
3101
|
+
inFlight.delete(key);
|
|
3102
|
+
cache?.set(key, buffers);
|
|
3103
|
+
return buffers;
|
|
3104
|
+
},
|
|
3105
|
+
(error) => {
|
|
3106
|
+
inFlight.delete(key);
|
|
3107
|
+
throw error;
|
|
3108
|
+
}
|
|
3109
|
+
);
|
|
3110
|
+
inFlight.set(key, promise);
|
|
3111
|
+
return promise;
|
|
3112
|
+
},
|
|
3113
|
+
generateMany(list2) {
|
|
3114
|
+
return Promise.all(list2.map((item) => isRequest(item) ? pool.generate(item.schema, item.options) : pool.generate(item)));
|
|
3115
|
+
},
|
|
3116
|
+
stats: () => ({ workers: slots.length, jobs, cacheHits, shared, inThread, busy: slots.reduce((n, s) => n + s.pending.size, 0) }),
|
|
3117
|
+
terminate() {
|
|
3118
|
+
terminated = true;
|
|
3119
|
+
for (const slot of slots) {
|
|
3120
|
+
failAll(slot, new Error("createShapePool: the pool was terminated"));
|
|
3121
|
+
slot.worker.terminate();
|
|
3122
|
+
}
|
|
3123
|
+
slots.length = 0;
|
|
3124
|
+
}
|
|
3125
|
+
};
|
|
3126
|
+
return pool;
|
|
3127
|
+
}
|
|
3128
|
+
|
|
3129
|
+
// src/worker.ts
|
|
3130
|
+
function installShapeWorker(scope, options = {}) {
|
|
3131
|
+
const transfer = options.transfer !== false;
|
|
3132
|
+
scope.onmessage = (event) => {
|
|
3133
|
+
const job = event.data;
|
|
3134
|
+
try {
|
|
3135
|
+
const buffers = generate(job.schema, { ...job.options ?? {}, ...options.generators ? { generators: options.generators } : {} });
|
|
3136
|
+
const reply = { id: job.id, ok: true, buffers };
|
|
3137
|
+
scope.postMessage(reply, transfer ? transferablesOf(buffers) : []);
|
|
3138
|
+
} catch (error) {
|
|
3139
|
+
const reply = { id: job.id, ok: false, error: error instanceof Error ? error.message : String(error) };
|
|
3140
|
+
scope.postMessage(reply);
|
|
3141
|
+
}
|
|
3142
|
+
};
|
|
3143
|
+
}
|
|
3144
|
+
return __toCommonJS(src_exports);
|
|
3145
|
+
})();
|
|
3146
|
+
//# sourceMappingURL=index.global.js.map
|