ldr 0.0.0-alpha.1 → 0.3.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 +121 -1
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +1472 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +1239 -0
- package/dist/model.d.ts +44 -0
- package/dist/node/build-pack.d.ts +35 -0
- package/dist/node/compile.d.ts +70 -0
- package/dist/node/index.d.ts +7 -0
- package/dist/node/index.js +2354 -0
- package/dist/node/library.d.ts +42 -0
- package/dist/node/resolver.d.ts +33 -0
- package/dist/pack-io.d.ts +2 -0
- package/dist/render.d.ts +69 -0
- package/dist/scene.d.ts +74 -0
- package/dist/spec.d.ts +149 -0
- package/package.json +56 -36
- package/lib/.DS_Store +0 -0
- package/lib/index.js +0 -1
|
@@ -0,0 +1,2354 @@
|
|
|
1
|
+
// src/spec.ts
|
|
2
|
+
var norm = (s) => s.trim().toLowerCase().replace(/\\/g, "/").replace(/\/+/g, "/");
|
|
3
|
+
function parseColor(tok) {
|
|
4
|
+
if (/^0x/i.test(tok))
|
|
5
|
+
return parseInt(tok, 16);
|
|
6
|
+
return +tok;
|
|
7
|
+
}
|
|
8
|
+
var MOVED_RE = /^~?\s*moved\s+to\s+(.+)$/i;
|
|
9
|
+
function texNames(rest) {
|
|
10
|
+
const out = [];
|
|
11
|
+
let i = 0;
|
|
12
|
+
while (i < rest.length) {
|
|
13
|
+
while (rest[i] === " " || rest[i] === "\t")
|
|
14
|
+
i++;
|
|
15
|
+
if (i >= rest.length)
|
|
16
|
+
break;
|
|
17
|
+
if (rest[i] === '"') {
|
|
18
|
+
let v = "";
|
|
19
|
+
i++;
|
|
20
|
+
while (i < rest.length && rest[i] !== '"') {
|
|
21
|
+
if (rest[i] === "\\" && i + 1 < rest.length)
|
|
22
|
+
i++;
|
|
23
|
+
v += rest[i++];
|
|
24
|
+
}
|
|
25
|
+
i++;
|
|
26
|
+
out.push(v);
|
|
27
|
+
} else {
|
|
28
|
+
let j = i;
|
|
29
|
+
while (j < rest.length && rest[j] !== " " && rest[j] !== "\t")
|
|
30
|
+
j++;
|
|
31
|
+
out.push(rest.slice(i, j));
|
|
32
|
+
i = j;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return out;
|
|
36
|
+
}
|
|
37
|
+
function parseTexMap(tail, next = false) {
|
|
38
|
+
const m = /^\s*(\S+)\s+(.*)$/.exec(tail);
|
|
39
|
+
if (!m)
|
|
40
|
+
return null;
|
|
41
|
+
const method = m[1].toUpperCase();
|
|
42
|
+
const n = method === "PLANAR" ? 9 : method === "CYLINDRICAL" ? 10 : method === "SPHERICAL" ? 11 : 0;
|
|
43
|
+
if (!n)
|
|
44
|
+
return null;
|
|
45
|
+
const parts = m[2].trim().split(/\s+/);
|
|
46
|
+
if (parts.length < n + 1)
|
|
47
|
+
return null;
|
|
48
|
+
const v = parts.slice(0, n).map(Number);
|
|
49
|
+
if (!v.every(Number.isFinite))
|
|
50
|
+
return null;
|
|
51
|
+
let rest = m[2].trim();
|
|
52
|
+
for (let k = 0;k < n; k++)
|
|
53
|
+
rest = rest.replace(/^\S+\s*/, "");
|
|
54
|
+
const names = texNames(rest);
|
|
55
|
+
if (!names.length)
|
|
56
|
+
return null;
|
|
57
|
+
const t = { method, p1: v.slice(0, 3), p2: v.slice(3, 6), p3: v.slice(6, 9), texture: names[0] };
|
|
58
|
+
if (n >= 10)
|
|
59
|
+
t.a = v[9];
|
|
60
|
+
if (n === 11)
|
|
61
|
+
t.b = v[10];
|
|
62
|
+
const g = names.findIndex((x, i) => i > 0 && x.toUpperCase() === "GLOSSMAP");
|
|
63
|
+
if (g > 0 && names[g + 1])
|
|
64
|
+
t.glossmap = names[g + 1];
|
|
65
|
+
if (next)
|
|
66
|
+
t.next = true;
|
|
67
|
+
return t;
|
|
68
|
+
}
|
|
69
|
+
var b64 = (s) => {
|
|
70
|
+
const clean = s.replace(/[^A-Za-z0-9+/]/g, "");
|
|
71
|
+
const pad = clean + "=".repeat((4 - clean.length % 4) % 4);
|
|
72
|
+
const bin = atob(pad);
|
|
73
|
+
const out = new Uint8Array(bin.length);
|
|
74
|
+
for (let i = 0;i < bin.length; i++)
|
|
75
|
+
out[i] = bin.charCodeAt(i);
|
|
76
|
+
return out;
|
|
77
|
+
};
|
|
78
|
+
function geomLine(tk) {
|
|
79
|
+
const t = +tk[0];
|
|
80
|
+
if (!(t >= 1 && t <= 5) || tk.length < 2)
|
|
81
|
+
return null;
|
|
82
|
+
const color = parseColor(tk[1]);
|
|
83
|
+
const nums = tk.slice(2).map(Number);
|
|
84
|
+
if (t === 1)
|
|
85
|
+
return { t: 1, color, m: nums.slice(0, 12), file: norm(tk.slice(14).join(" ")) };
|
|
86
|
+
if (t === 2)
|
|
87
|
+
return { t: 2, color, p: nums.slice(0, 6) };
|
|
88
|
+
if (t === 3)
|
|
89
|
+
return { t: 3, color, p: nums.slice(0, 9) };
|
|
90
|
+
if (t === 4)
|
|
91
|
+
return { t: 4, color, p: nums.slice(0, 12) };
|
|
92
|
+
return { t: 5, color, p: nums.slice(0, 12) };
|
|
93
|
+
}
|
|
94
|
+
function parse(src, name = "main.ldr") {
|
|
95
|
+
const files = new Map;
|
|
96
|
+
let cur = null;
|
|
97
|
+
let main = "";
|
|
98
|
+
let data;
|
|
99
|
+
let dataCur = null;
|
|
100
|
+
let texStack = [];
|
|
101
|
+
let texNext = null;
|
|
102
|
+
const closeData = () => {
|
|
103
|
+
if (!dataCur)
|
|
104
|
+
return;
|
|
105
|
+
const bytes = b64(dataCur.chunks.join(""));
|
|
106
|
+
const png = bytes[0] === 137 && bytes[1] === 80 && bytes[2] === 78 && bytes[3] === 71;
|
|
107
|
+
(data ??= new Map).set(norm(dataCur.name), { name: dataCur.name, bytes, ...png ? { mime: "image/png" } : {} });
|
|
108
|
+
dataCur = null;
|
|
109
|
+
};
|
|
110
|
+
const start = (n) => {
|
|
111
|
+
closeData();
|
|
112
|
+
cur = { name: norm(n), cmds: [] };
|
|
113
|
+
files.set(cur.name, cur);
|
|
114
|
+
if (!main)
|
|
115
|
+
main = cur.name;
|
|
116
|
+
texStack = [];
|
|
117
|
+
texNext = null;
|
|
118
|
+
};
|
|
119
|
+
let skip = false, sawBlock = false;
|
|
120
|
+
const firstBlock = () => {
|
|
121
|
+
if (sawBlock)
|
|
122
|
+
return;
|
|
123
|
+
sawBlock = true;
|
|
124
|
+
files.clear();
|
|
125
|
+
cur = null;
|
|
126
|
+
main = "";
|
|
127
|
+
};
|
|
128
|
+
const annotate = (c, fromColon) => {
|
|
129
|
+
if (texNext) {
|
|
130
|
+
c.tex = texNext;
|
|
131
|
+
texNext = null;
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
const top = texStack[texStack.length - 1];
|
|
135
|
+
if (!top)
|
|
136
|
+
return;
|
|
137
|
+
if (top.fallback) {
|
|
138
|
+
if (!fromColon)
|
|
139
|
+
c.fallback = true;
|
|
140
|
+
} else
|
|
141
|
+
c.tex = top.map;
|
|
142
|
+
};
|
|
143
|
+
for (const raw of src.split(/\r?\n/)) {
|
|
144
|
+
const line = raw.trim();
|
|
145
|
+
if (!line)
|
|
146
|
+
continue;
|
|
147
|
+
const tk = line.split(/\s+/);
|
|
148
|
+
const t = +tk[0];
|
|
149
|
+
if (t === 0) {
|
|
150
|
+
const text = line.slice(1).trim();
|
|
151
|
+
if (tk[1] === "FILE") {
|
|
152
|
+
firstBlock();
|
|
153
|
+
skip = false;
|
|
154
|
+
start(text.slice(5));
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
if (tk[1] === "NOFILE") {
|
|
158
|
+
closeData();
|
|
159
|
+
cur = null;
|
|
160
|
+
skip = true;
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
if (tk[1] === "!DATA") {
|
|
164
|
+
firstBlock();
|
|
165
|
+
closeData();
|
|
166
|
+
cur = null;
|
|
167
|
+
skip = false;
|
|
168
|
+
dataCur = { name: text.slice(5).trim(), chunks: [] };
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
if (skip)
|
|
172
|
+
continue;
|
|
173
|
+
if (dataCur) {
|
|
174
|
+
if (tk[1] === "!:")
|
|
175
|
+
dataCur.chunks.push(tk.slice(2).join(""));
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
if (!cur)
|
|
179
|
+
start(name);
|
|
180
|
+
const c = { t: 0, text };
|
|
181
|
+
if (tk[1] === "STEP") {
|
|
182
|
+
c.step = true;
|
|
183
|
+
texStack = [];
|
|
184
|
+
texNext = null;
|
|
185
|
+
} else if (tk[1] === "BFC")
|
|
186
|
+
c.bfc = tk.slice(2).join(" ");
|
|
187
|
+
else if (tk[1] === "ROTSTEP") {
|
|
188
|
+
c.meta = "ROTSTEP";
|
|
189
|
+
if ((tk[2] ?? "").toUpperCase() === "END")
|
|
190
|
+
c.rotstep = { x: 0, y: 0, z: 0, mode: "END" };
|
|
191
|
+
else {
|
|
192
|
+
const mode = (tk[5] ?? "REL").toUpperCase();
|
|
193
|
+
c.rotstep = { x: +tk[2] || 0, y: +tk[3] || 0, z: +tk[4] || 0, mode: ["REL", "ABS", "ADD"].includes(mode) ? mode : "REL" };
|
|
194
|
+
}
|
|
195
|
+
} else if (tk[1] === "BUFEXCHG") {
|
|
196
|
+
c.meta = "BUFEXCHG";
|
|
197
|
+
const op = (tk[2] ?? "").toUpperCase();
|
|
198
|
+
c.bufexchg = { op: op === "RETRIEVE" ? "RETRIEVE" : "STORE", buf: tk[3] ?? "A" };
|
|
199
|
+
} else if (tk[1]?.startsWith("!"))
|
|
200
|
+
c.meta = tk[1];
|
|
201
|
+
if (tk[1] === "!TEXMAP") {
|
|
202
|
+
const op = (tk[2] ?? "").toUpperCase();
|
|
203
|
+
texNext = null;
|
|
204
|
+
if (op === "START" || op === "NEXT") {
|
|
205
|
+
const map = parseTexMap(text.replace(/^!TEXMAP\s+\S+/i, ""), op === "NEXT");
|
|
206
|
+
c.texmap = { op, ...map ? { map } : {} };
|
|
207
|
+
if (map) {
|
|
208
|
+
(cur.texmaps ??= []).push(map);
|
|
209
|
+
if (op === "START")
|
|
210
|
+
texStack.push({ map, fallback: false });
|
|
211
|
+
else
|
|
212
|
+
texNext = map;
|
|
213
|
+
}
|
|
214
|
+
} else if (op === "FALLBACK") {
|
|
215
|
+
c.texmap = { op };
|
|
216
|
+
const top = texStack[texStack.length - 1];
|
|
217
|
+
if (top)
|
|
218
|
+
top.fallback = true;
|
|
219
|
+
} else if (op === "END") {
|
|
220
|
+
c.texmap = { op };
|
|
221
|
+
texStack.pop();
|
|
222
|
+
}
|
|
223
|
+
} else if (tk[1] === "!:") {
|
|
224
|
+
texNext = null;
|
|
225
|
+
const g = geomLine(tk.slice(2));
|
|
226
|
+
if (g) {
|
|
227
|
+
annotate(g, true);
|
|
228
|
+
c.geom = g;
|
|
229
|
+
}
|
|
230
|
+
} else
|
|
231
|
+
texNext = null;
|
|
232
|
+
const moved = text.match(MOVED_RE);
|
|
233
|
+
if (moved && !cur.movedTo) {
|
|
234
|
+
let target = moved[1].trim().replace(/\\/g, "/");
|
|
235
|
+
if (!/\.(dat|ldr|mpd)$/i.test(target))
|
|
236
|
+
target += ".dat";
|
|
237
|
+
cur.movedTo = norm(target);
|
|
238
|
+
}
|
|
239
|
+
cur.cmds.push(c);
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
if (dataCur || skip)
|
|
243
|
+
continue;
|
|
244
|
+
if (!cur)
|
|
245
|
+
start(name);
|
|
246
|
+
const g = geomLine(tk);
|
|
247
|
+
if (!g)
|
|
248
|
+
continue;
|
|
249
|
+
annotate(g, false);
|
|
250
|
+
cur.cmds.push(g);
|
|
251
|
+
}
|
|
252
|
+
closeData();
|
|
253
|
+
return data ? { main, files, data } : { main, files };
|
|
254
|
+
}
|
|
255
|
+
function resolveTexture(doc, name) {
|
|
256
|
+
if (!doc.data)
|
|
257
|
+
return null;
|
|
258
|
+
const n = norm(name);
|
|
259
|
+
return doc.data.get(n) ?? doc.data.get(n.replace(/^textures\//, "")) ?? doc.data.get("textures/" + n) ?? null;
|
|
260
|
+
}
|
|
261
|
+
var sub3 = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
|
|
262
|
+
var dot3 = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
|
|
263
|
+
var cross3 = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
|
|
264
|
+
var unit3 = (a) => {
|
|
265
|
+
const l = Math.hypot(a[0], a[1], a[2]) || 1;
|
|
266
|
+
return [a[0] / l, a[1] / l, a[2] / l];
|
|
267
|
+
};
|
|
268
|
+
var DEG = Math.PI / 180;
|
|
269
|
+
function texmapUV(map, p) {
|
|
270
|
+
const w = texmapTriangleUV(map, [p, p, p]);
|
|
271
|
+
const u = w[0], v = w[1];
|
|
272
|
+
return { u, v, inside: u >= -0.000001 && u <= 1 + 0.000001 && v >= -0.000001 && v <= 1 + 0.000001 };
|
|
273
|
+
}
|
|
274
|
+
var radial = (p, c, n) => {
|
|
275
|
+
const d = sub3(p, c);
|
|
276
|
+
const k = dot3(d, n);
|
|
277
|
+
const r = [d[0] - n[0] * k, d[1] - n[1] * k, d[2] - n[2] * k];
|
|
278
|
+
return Math.hypot(r[0], r[1], r[2]) < 0.00001 ? null : unit3(r);
|
|
279
|
+
};
|
|
280
|
+
function texmapTriangleUV(map, pts) {
|
|
281
|
+
const { p1, p2, p3 } = map;
|
|
282
|
+
const out = new Array(pts.length * 2).fill(0);
|
|
283
|
+
if (map.method === "PLANAR") {
|
|
284
|
+
const e1 = sub3(p2, p1), e2 = sub3(p3, p1);
|
|
285
|
+
const l1 = dot3(e1, e1) || 1, l2 = dot3(e2, e2) || 1;
|
|
286
|
+
pts.forEach((p, i) => {
|
|
287
|
+
const d = sub3(p, p1);
|
|
288
|
+
out[i * 2] = dot3(d, e1) / l1;
|
|
289
|
+
out[i * 2 + 1] = dot3(d, e2) / l2;
|
|
290
|
+
});
|
|
291
|
+
return out;
|
|
292
|
+
}
|
|
293
|
+
const cyl = map.method === "CYLINDRICAL";
|
|
294
|
+
const n = cyl ? unit3(sub3(p2, p1)) : unit3(cross3(sub3(p1, p2), sub3(p3, p2)).map((x) => -x));
|
|
295
|
+
const dir = cyl ? radial(p3, p1, n) : unit3(sub3(p2, p1));
|
|
296
|
+
const A = (map.a ?? 360) * DEG;
|
|
297
|
+
let base = null, baseAng = 0;
|
|
298
|
+
const sAng = (q) => {
|
|
299
|
+
if (!base) {
|
|
300
|
+
base = q;
|
|
301
|
+
baseAng = Math.atan2(dot3(cross3(dir, q), n), dot3(q, dir));
|
|
302
|
+
return baseAng;
|
|
303
|
+
}
|
|
304
|
+
return Math.atan2(dot3(cross3(base, q), n), dot3(q, base)) + baseAng;
|
|
305
|
+
};
|
|
306
|
+
const miss = [];
|
|
307
|
+
let uSum = 0;
|
|
308
|
+
const h = cyl ? Math.hypot(...sub3(p2, p1)) || 1 : 0, n2 = cyl ? null : unit3(cross3(n, sub3(p2, p1))), B = (map.b ?? 180) * DEG;
|
|
309
|
+
pts.forEach((p, i) => {
|
|
310
|
+
if (cyl) {
|
|
311
|
+
const q = dir ? radial(p, p1, n) : null;
|
|
312
|
+
if (q) {
|
|
313
|
+
const a = sAng(q);
|
|
314
|
+
out[i * 2] = 0.5 + a / A;
|
|
315
|
+
uSum += out[i * 2];
|
|
316
|
+
} else
|
|
317
|
+
miss.push(i);
|
|
318
|
+
out[i * 2 + 1] = dot3(n, sub3(p2, p)) / h;
|
|
319
|
+
} else {
|
|
320
|
+
const pd = unit3(sub3(p, p1));
|
|
321
|
+
const c = dot3(pd, n);
|
|
322
|
+
if (Math.abs(c) > 1 - 0.000000001) {
|
|
323
|
+
miss.push(i);
|
|
324
|
+
out[i * 2 + 1] = 0.5 + (c > 0 ? -Math.PI / 2 : Math.PI / 2) / B;
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
const a = sAng(radial(p, p1, n));
|
|
328
|
+
uSum += a;
|
|
329
|
+
out[i * 2] = 0.5 + a / A;
|
|
330
|
+
const th = -a, ct = Math.cos(th), st = Math.sin(th), cr = cross3(n, pd), k = dot3(n, pd) * (1 - ct);
|
|
331
|
+
const r = unit3([pd[0] * ct + cr[0] * st + n[0] * k, pd[1] * ct + cr[1] * st + n[1] * k, pd[2] * ct + cr[2] * st + n[2] * k]);
|
|
332
|
+
out[i * 2 + 1] = 0.5 + Math.atan2(dot3(cross3(dir, r), n2), dot3(r, dir)) / B;
|
|
333
|
+
}
|
|
334
|
+
});
|
|
335
|
+
if (miss.length && miss.length < pts.length) {
|
|
336
|
+
const avg = cyl ? uSum / (pts.length - miss.length) : 0.5 + uSum / (pts.length - miss.length) / A;
|
|
337
|
+
for (const i of miss)
|
|
338
|
+
out[i * 2] = avg;
|
|
339
|
+
}
|
|
340
|
+
return out;
|
|
341
|
+
}
|
|
342
|
+
function transformTexMap(map, m) {
|
|
343
|
+
const x = (q) => [m[3] * q[0] + m[4] * q[1] + m[5] * q[2] + m[0], m[6] * q[0] + m[7] * q[1] + m[8] * q[2] + m[1], m[9] * q[0] + m[10] * q[1] + m[11] * q[2] + m[2]];
|
|
344
|
+
return { ...map, p1: x(map.p1), p2: x(map.p2), p3: x(map.p3) };
|
|
345
|
+
}
|
|
346
|
+
function mul(a, b) {
|
|
347
|
+
const [ax, ay, az, a0, a1, a2, a3, a4, a5, a6, a7, a8] = a, [bx, by, bz, b0, b1, b2, b3, b4, b5, b6, b7, b8] = b;
|
|
348
|
+
return [
|
|
349
|
+
a0 * bx + a1 * by + a2 * bz + ax,
|
|
350
|
+
a3 * bx + a4 * by + a5 * bz + ay,
|
|
351
|
+
a6 * bx + a7 * by + a8 * bz + az,
|
|
352
|
+
a0 * b0 + a1 * b3 + a2 * b6,
|
|
353
|
+
a0 * b1 + a1 * b4 + a2 * b7,
|
|
354
|
+
a0 * b2 + a1 * b5 + a2 * b8,
|
|
355
|
+
a3 * b0 + a4 * b3 + a5 * b6,
|
|
356
|
+
a3 * b1 + a4 * b4 + a5 * b7,
|
|
357
|
+
a3 * b2 + a4 * b5 + a5 * b8,
|
|
358
|
+
a6 * b0 + a7 * b3 + a8 * b6,
|
|
359
|
+
a6 * b1 + a7 * b4 + a8 * b7,
|
|
360
|
+
a6 * b2 + a7 * b5 + a8 * b8
|
|
361
|
+
];
|
|
362
|
+
}
|
|
363
|
+
var I = [0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1];
|
|
364
|
+
var isDirectColor = (c) => c > 16777215;
|
|
365
|
+
function smoothNormals(pos, edges, cond, creaseDeg = 35) {
|
|
366
|
+
const q = (i, a) => `${a[i].toFixed(2)},${a[i + 1].toFixed(2)},${a[i + 2].toFixed(2)}`;
|
|
367
|
+
const ek = (a, b) => a < b ? a + "|" + b : b + "|" + a;
|
|
368
|
+
const hard = new Set, soft = new Set;
|
|
369
|
+
for (let i = 0;i < edges.length; i += 6)
|
|
370
|
+
hard.add(ek(q(i, edges), q(i + 3, edges)));
|
|
371
|
+
const stride = cond.length > 0 && cond.length % 12 === 0 ? 12 : 6;
|
|
372
|
+
for (let i = 0;i < cond.length; i += stride)
|
|
373
|
+
soft.add(ek(q(i, cond), q(i + 3, cond)));
|
|
374
|
+
const nT = pos.length / 9, fn = [], vk = [];
|
|
375
|
+
for (let t = 0;t < nT; t++) {
|
|
376
|
+
const a = t * 9;
|
|
377
|
+
const u = [pos[a + 3] - pos[a], pos[a + 4] - pos[a + 1], pos[a + 5] - pos[a + 2]], v = [pos[a + 6] - pos[a], pos[a + 7] - pos[a + 1], pos[a + 8] - pos[a + 2]];
|
|
378
|
+
const n = [u[1] * v[2] - u[2] * v[1], u[2] * v[0] - u[0] * v[2], u[0] * v[1] - u[1] * v[0]];
|
|
379
|
+
const l = Math.hypot(...n) || 1;
|
|
380
|
+
fn.push(n.map((x) => x / l));
|
|
381
|
+
for (let k = 0;k < 3; k++)
|
|
382
|
+
vk.push(q(a + k * 3, pos));
|
|
383
|
+
}
|
|
384
|
+
const byV = new Map;
|
|
385
|
+
vk.forEach((k, i) => (byV.get(k) ?? byV.set(k, []).get(k)).push(i));
|
|
386
|
+
const edgeFaces = new Map;
|
|
387
|
+
for (let t = 0;t < nT; t++)
|
|
388
|
+
for (let k = 0;k < 3; k++) {
|
|
389
|
+
const e = ek(vk[t * 3 + k], vk[t * 3 + (k + 1) % 3]);
|
|
390
|
+
(edgeFaces.get(e) ?? edgeFaces.set(e, []).get(e)).push(t);
|
|
391
|
+
}
|
|
392
|
+
const cosC = Math.cos(creaseDeg * Math.PI / 180), out = new Float32Array(pos.length);
|
|
393
|
+
const joinable = (e, a, b) => {
|
|
394
|
+
if (hard.has(e))
|
|
395
|
+
return false;
|
|
396
|
+
const d = fn[a][0] * fn[b][0] + fn[a][1] * fn[b][1] + fn[a][2] * fn[b][2];
|
|
397
|
+
return soft.has(e) ? d > -0.2 : d > cosC;
|
|
398
|
+
};
|
|
399
|
+
for (const [key, corners] of byV) {
|
|
400
|
+
const faces = corners.map((c) => c / 3 | 0), par = faces.map((_, i) => i);
|
|
401
|
+
const f = (i) => par[i] === i ? i : par[i] = f(par[i]);
|
|
402
|
+
for (let i = 0;i < faces.length; i++)
|
|
403
|
+
for (let j = i + 1;j < faces.length; j++) {
|
|
404
|
+
const A = faces[i], B = faces[j];
|
|
405
|
+
const va = [0, 1, 2].map((k) => vk[A * 3 + k]), vb = new Set([0, 1, 2].map((k) => vk[B * 3 + k]));
|
|
406
|
+
for (const o of va)
|
|
407
|
+
if (o !== key && vb.has(o)) {
|
|
408
|
+
const e = ek(key, o);
|
|
409
|
+
if ((edgeFaces.get(e)?.length ?? 0) >= 2 && joinable(e, A, B))
|
|
410
|
+
par[f(i)] = f(j);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
const acc = new Map;
|
|
414
|
+
faces.forEach((F, i) => {
|
|
415
|
+
const r = f(i), s = acc.get(r) ?? [0, 0, 0];
|
|
416
|
+
s[0] += fn[F][0];
|
|
417
|
+
s[1] += fn[F][1];
|
|
418
|
+
s[2] += fn[F][2];
|
|
419
|
+
acc.set(r, s);
|
|
420
|
+
});
|
|
421
|
+
corners.forEach((c, i) => {
|
|
422
|
+
const s = acc.get(f(i)), l = Math.hypot(...s) || 1;
|
|
423
|
+
out[c * 3] = s[0] / l;
|
|
424
|
+
out[c * 3 + 1] = s[1] / l;
|
|
425
|
+
out[c * 3 + 2] = s[2] / l;
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
return out;
|
|
429
|
+
}
|
|
430
|
+
// src/model.ts
|
|
431
|
+
function flatten(doc, opts = {}) {
|
|
432
|
+
const out = [];
|
|
433
|
+
const maxDepth = opts.maxDepth ?? 64;
|
|
434
|
+
const walk = (d, fname, m, color, path, stepPath, depth) => {
|
|
435
|
+
const f = d.files.get(fname);
|
|
436
|
+
if (!f || depth > maxDepth)
|
|
437
|
+
return;
|
|
438
|
+
let s = 0;
|
|
439
|
+
for (const c of f.cmds) {
|
|
440
|
+
if (c.t === 0 && c.step)
|
|
441
|
+
s++;
|
|
442
|
+
if (c.t !== 1)
|
|
443
|
+
continue;
|
|
444
|
+
const col = c.color === 16 ? color : c.color, mm = mul(m, c.m), sp = [...stepPath, s];
|
|
445
|
+
if (d.files.has(c.file)) {
|
|
446
|
+
walk(d, c.file, mm, col, path + "/" + c.file, sp, depth + 1);
|
|
447
|
+
continue;
|
|
448
|
+
}
|
|
449
|
+
const ext = opts.resolveModel?.(c.file);
|
|
450
|
+
if (ext) {
|
|
451
|
+
walk(ext, ext.main, mm, col, path + "/" + c.file, sp, depth + 1);
|
|
452
|
+
continue;
|
|
453
|
+
}
|
|
454
|
+
out.push({ part: c.file, color: col, m: mm, step: sp[0], steps: sp, path });
|
|
455
|
+
}
|
|
456
|
+
};
|
|
457
|
+
walk(doc, doc.main, I, 16, doc.main, [], 0);
|
|
458
|
+
return out;
|
|
459
|
+
}
|
|
460
|
+
function parseModel(text, name = "main.ldr", opts) {
|
|
461
|
+
return flatten(parse(text, name), opts);
|
|
462
|
+
}
|
|
463
|
+
function sequenceSteps(insts) {
|
|
464
|
+
const key = (i) => i.steps.map((x) => String(x).padStart(6, "0")).join(".");
|
|
465
|
+
const uniq = [...new Set(insts.map(key))].sort();
|
|
466
|
+
const rank = new Map(uniq.map((k, j) => [k, j]));
|
|
467
|
+
const stepOf = new Int32Array(insts.length);
|
|
468
|
+
insts.forEach((i, k) => stepOf[k] = rank.get(key(i)));
|
|
469
|
+
return { count: uniq.length, stepOf };
|
|
470
|
+
}
|
|
471
|
+
function parseLDConfig(text) {
|
|
472
|
+
const out = new Map;
|
|
473
|
+
for (const l of text.split(/\r?\n/)) {
|
|
474
|
+
const m = /!COLOUR\s+(\S+)\s+CODE\s+(\d+)\s+VALUE\s+(#[0-9A-Fa-f]{6})\s+EDGE\s+(\S+)(?:.*?ALPHA\s+(\d+))?/.exec(l);
|
|
475
|
+
if (m)
|
|
476
|
+
out.set(+m[2], { code: +m[2], name: m[1], hex: m[3].toUpperCase(), edge: m[4], alpha: m[5] ? +m[5] : 255 });
|
|
477
|
+
}
|
|
478
|
+
return out;
|
|
479
|
+
}
|
|
480
|
+
// src/render.ts
|
|
481
|
+
import * as THREE from "three";
|
|
482
|
+
async function loadPack(url) {
|
|
483
|
+
const res = await fetch(url);
|
|
484
|
+
const t0 = performance.now();
|
|
485
|
+
let buf;
|
|
486
|
+
if (url.endsWith(".gz"))
|
|
487
|
+
buf = await new Response(res.body.pipeThrough(new DecompressionStream("gzip"))).arrayBuffer();
|
|
488
|
+
else if (url.endsWith(".br"))
|
|
489
|
+
buf = await new Response(res.body.pipeThrough(new DecompressionStream("brotli"))).arrayBuffer();
|
|
490
|
+
else if (url.endsWith(".zst")) {
|
|
491
|
+
const raw = new Uint8Array(await res.arrayBuffer());
|
|
492
|
+
const t1 = performance.now();
|
|
493
|
+
const { decompress } = await import("fzstd");
|
|
494
|
+
const o = decompress(raw);
|
|
495
|
+
buf = o.buffer.slice(o.byteOffset, o.byteOffset + o.byteLength);
|
|
496
|
+
globalThis.__decodeMs = performance.now() - t1;
|
|
497
|
+
} else
|
|
498
|
+
buf = await res.arrayBuffer();
|
|
499
|
+
if (!url.endsWith(".zst"))
|
|
500
|
+
globalThis.__decodeMs = performance.now() - t0;
|
|
501
|
+
return decodePack(buf);
|
|
502
|
+
}
|
|
503
|
+
function decodePack(buf) {
|
|
504
|
+
const hl = new DataView(buf).getUint32(0, true);
|
|
505
|
+
const header = JSON.parse(new TextDecoder().decode(new Uint8Array(buf, 4, hl)));
|
|
506
|
+
const base = 4 + hl;
|
|
507
|
+
const arr = (r) => r && (r[2] === "f32" ? new Float32Array(buf, base + r[0], r[1]) : r[2] === "u16" ? new Uint16Array(buf, base + r[0], r[1]) : r[2] === "u8" ? new Uint8Array(buf, base + r[0], r[1]) : new Uint32Array(buf, base + r[0], r[1]));
|
|
508
|
+
return { header, arr, bytes: buf.byteLength };
|
|
509
|
+
}
|
|
510
|
+
function geom(b, arr) {
|
|
511
|
+
if (!b)
|
|
512
|
+
return null;
|
|
513
|
+
const g = new THREE.BufferGeometry;
|
|
514
|
+
g.setAttribute("position", new THREE.BufferAttribute(arr(b.pos), 3));
|
|
515
|
+
g.setAttribute("normal", new THREE.BufferAttribute(arr(b.nrm), 3));
|
|
516
|
+
g.setAttribute("ldrColor", new THREE.BufferAttribute(arr(b.col), 4));
|
|
517
|
+
if (b.idx)
|
|
518
|
+
g.setIndex(new THREE.BufferAttribute(arr(b.idx), 1));
|
|
519
|
+
if (b.uv)
|
|
520
|
+
g.setAttribute("ldrUV", new THREE.BufferAttribute(arr(b.uv), 2));
|
|
521
|
+
return g;
|
|
522
|
+
}
|
|
523
|
+
function makeMaterial(side) {
|
|
524
|
+
const m = new THREE.MeshStandardMaterial({ vertexColors: true, roughness: 0.45, side });
|
|
525
|
+
m.onBeforeCompile = (s) => {
|
|
526
|
+
s.vertexShader = `attribute vec4 ldrColor;
|
|
527
|
+
` + s.vertexShader.replace("#include <color_vertex>", `#ifdef USE_INSTANCING_COLOR
|
|
528
|
+
vColor = vec4(ldrColor.a > 0.5 ? pow(ldrColor.rgb, vec3(2.2)) : instanceColor.rgb, 1.0); // baked LDConfig colours are sRGB
|
|
529
|
+
#else
|
|
530
|
+
vColor = vec4(pow(ldrColor.rgb, vec3(2.2)), 1.0);
|
|
531
|
+
#endif`);
|
|
532
|
+
};
|
|
533
|
+
m.customProgramCacheKey = () => "ldrColor";
|
|
534
|
+
return m;
|
|
535
|
+
}
|
|
536
|
+
function makeTexturedMaterial(side, map, wrapU = false) {
|
|
537
|
+
const m = makeMaterial(side);
|
|
538
|
+
const base = m.onBeforeCompile;
|
|
539
|
+
m.onBeforeCompile = (sh, r) => {
|
|
540
|
+
base.call(m, sh, r);
|
|
541
|
+
sh.uniforms.ldrMap = { value: map };
|
|
542
|
+
sh.uniforms.ldrWrapU = { value: wrapU ? 1 : 0 };
|
|
543
|
+
sh.vertexShader = `attribute vec2 ldrUV; varying vec2 vLdrUV;
|
|
544
|
+
` + sh.vertexShader.replace("#include <uv_vertex>", `#include <uv_vertex>
|
|
545
|
+
vLdrUV = ldrUV;`);
|
|
546
|
+
sh.fragmentShader = `uniform sampler2D ldrMap; uniform int ldrWrapU; varying vec2 vLdrUV;
|
|
547
|
+
` + sh.fragmentShader.replace("#include <color_fragment>", `#include <color_fragment>
|
|
548
|
+
{ vec2 uv = vec2(ldrWrapU == 1 ? fract(vLdrUV.x) : vLdrUV.x, vLdrUV.y); vec4 t = texture2D(ldrMap, uv);
|
|
549
|
+
float inside = step(0.0, uv.x) * step(uv.x, 1.0) * step(0.0, uv.y) * step(uv.y, 1.0);
|
|
550
|
+
diffuseColor.rgb = mix(diffuseColor.rgb, t.rgb, t.a * inside); }`);
|
|
551
|
+
};
|
|
552
|
+
m.customProgramCacheKey = () => "ldrColorTex";
|
|
553
|
+
m.userData.ldrMap = map;
|
|
554
|
+
return m;
|
|
555
|
+
}
|
|
556
|
+
function packTexture(pack, i) {
|
|
557
|
+
const t = new THREE.Texture;
|
|
558
|
+
t.flipY = false;
|
|
559
|
+
t.colorSpace = THREE.SRGBColorSpace;
|
|
560
|
+
t.wrapS = t.wrapT = THREE.ClampToEdgeWrapping;
|
|
561
|
+
const e = pack.header.textures?.[i];
|
|
562
|
+
const bytes = e?.data ? pack.arr(e.data) : null;
|
|
563
|
+
if (!bytes || typeof createImageBitmap === "undefined")
|
|
564
|
+
return { texture: t, ready: Promise.resolve() };
|
|
565
|
+
const ready = createImageBitmap(new Blob([bytes], { type: e.mime || "image/png" }), { premultiplyAlpha: "none", colorSpaceConversion: "none" }).then((img) => {
|
|
566
|
+
t.image = img;
|
|
567
|
+
t.needsUpdate = true;
|
|
568
|
+
}).catch(() => {});
|
|
569
|
+
return { texture: t, ready };
|
|
570
|
+
}
|
|
571
|
+
var lineMat = () => new THREE.ShaderMaterial({
|
|
572
|
+
vertexShader: `attribute vec4 im0; attribute vec4 im1; attribute vec4 im2; attribute vec4 im3;
|
|
573
|
+
void main(){ gl_Position = projectionMatrix * modelViewMatrix * mat4(im0,im1,im2,im3) * vec4(position,1.0); }`,
|
|
574
|
+
fragmentShader: `void main(){ gl_FragColor = vec4(0.13,0.13,0.13,1.0); }`
|
|
575
|
+
});
|
|
576
|
+
var sharedLineMat = () => new THREE.ShaderMaterial({
|
|
577
|
+
vertexShader: `attribute mat4 instanceMatrix; void main(){ gl_Position = projectionMatrix * modelViewMatrix * instanceMatrix * vec4(position,1.0); }`,
|
|
578
|
+
fragmentShader: `void main(){ gl_FragColor = vec4(0.13,0.13,0.13,1.0); }`
|
|
579
|
+
});
|
|
580
|
+
var weldLines = (pos) => {
|
|
581
|
+
const map = new Map;
|
|
582
|
+
const P = [];
|
|
583
|
+
const I = [];
|
|
584
|
+
for (let i = 0;i < pos.length; i += 3) {
|
|
585
|
+
const k = pos[i].toFixed(2) + "," + pos[i + 1].toFixed(2) + "," + pos[i + 2].toFixed(2);
|
|
586
|
+
let j = map.get(k);
|
|
587
|
+
if (j === undefined) {
|
|
588
|
+
j = P.length / 3;
|
|
589
|
+
map.set(k, j);
|
|
590
|
+
P.push(pos[i], pos[i + 1], pos[i + 2]);
|
|
591
|
+
}
|
|
592
|
+
I.push(j);
|
|
593
|
+
}
|
|
594
|
+
return { P: new Float32Array(P), I: P.length / 3 < 65536 ? new Uint16Array(I) : new Uint32Array(I) };
|
|
595
|
+
};
|
|
596
|
+
function createInstancedEdges(pos, im, material = sharedLineMat()) {
|
|
597
|
+
return sharedLines(pos, im, material);
|
|
598
|
+
}
|
|
599
|
+
function conditionalLines(pack, entry, which = "all") {
|
|
600
|
+
if (which === "all") {
|
|
601
|
+
const a = conditionalLines(pack, entry, "body"), b = conditionalLines(pack, entry, "under");
|
|
602
|
+
if (!a || !b)
|
|
603
|
+
return a ?? b;
|
|
604
|
+
const o = new Float32Array(a.length + b.length);
|
|
605
|
+
o.set(a);
|
|
606
|
+
o.set(b, a.length);
|
|
607
|
+
return o;
|
|
608
|
+
}
|
|
609
|
+
const c = which === "under" ? entry?.condlu : entry?.condl;
|
|
610
|
+
if (!c)
|
|
611
|
+
return null;
|
|
612
|
+
const { arr } = pack;
|
|
613
|
+
if (c.raw)
|
|
614
|
+
return arr(c.raw);
|
|
615
|
+
const pools = [entry.body ? arr(entry.body.pos) : [], entry.under ? arr(entry.under.pos) : [], entry.edges ? arr(entry.edges) : [], c.x ? arr(c.x) : []];
|
|
616
|
+
const idx = arr(c.idx);
|
|
617
|
+
const out = new Float32Array(idx.length * 3);
|
|
618
|
+
const starts = [];
|
|
619
|
+
let base = 0;
|
|
620
|
+
for (const p of pools) {
|
|
621
|
+
starts.push(base);
|
|
622
|
+
base += p.length / 3;
|
|
623
|
+
}
|
|
624
|
+
let prev = 0;
|
|
625
|
+
for (let i = 0;i < idx.length; i++) {
|
|
626
|
+
const z = idx[i];
|
|
627
|
+
const j = prev + (z & 1 ? -(z + 1) / 2 : z / 2);
|
|
628
|
+
prev = j;
|
|
629
|
+
let k = 3;
|
|
630
|
+
while (k > 0 && j < starts[k])
|
|
631
|
+
k--;
|
|
632
|
+
const p = pools[k], o = (j - starts[k]) * 3;
|
|
633
|
+
out[i * 3] = p[o];
|
|
634
|
+
out[i * 3 + 1] = p[o + 1];
|
|
635
|
+
out[i * 3 + 2] = p[o + 2];
|
|
636
|
+
}
|
|
637
|
+
return out;
|
|
638
|
+
}
|
|
639
|
+
var condLineMat = () => new THREE.ShaderMaterial({
|
|
640
|
+
uniforms: { ldrViewProj: { value: new THREE.Matrix4 } },
|
|
641
|
+
vertexShader: `uniform mat4 ldrViewProj; attribute mat4 instanceMatrix; attribute vec3 ldrP1; attribute vec3 ldrC0; attribute vec3 ldrC1; attribute float ldrSide;
|
|
642
|
+
void main(){
|
|
643
|
+
mat4 mvp = ldrViewProj * instanceMatrix;
|
|
644
|
+
vec4 a = mvp * vec4(position, 1.0), b = mvp * vec4(ldrP1, 1.0), c0 = mvp * vec4(ldrC0, 1.0), c1 = mvp * vec4(ldrC1, 1.0);
|
|
645
|
+
vec2 A = a.xy / a.w, B = b.xy / b.w, C0 = c0.xy / c0.w, C1 = c1.xy / c1.w, d = B - A;
|
|
646
|
+
float s0 = d.x * (C0.y - A.y) - d.y * (C0.x - A.x), s1 = d.x * (C1.y - A.y) - d.y * (C1.x - A.x);
|
|
647
|
+
gl_Position = s0 * s1 >= 0.0 ? (ldrSide > 0.5 ? b : a) : vec4(2.0, 2.0, 2.0, 1.0);
|
|
648
|
+
}`,
|
|
649
|
+
fragmentShader: `void main(){ gl_FragColor = vec4(0.13,0.13,0.13,1.0); }`
|
|
650
|
+
});
|
|
651
|
+
function conditionalLineVisible(cond, i, mvp) {
|
|
652
|
+
const e = mvp.elements;
|
|
653
|
+
const pr = (o) => {
|
|
654
|
+
const x = cond[o], y = cond[o + 1], z = cond[o + 2];
|
|
655
|
+
const w = e[3] * x + e[7] * y + e[11] * z + e[15];
|
|
656
|
+
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];
|
|
657
|
+
};
|
|
658
|
+
const o = i * 12;
|
|
659
|
+
const A = pr(o), B = pr(o + 3), C0 = pr(o + 6), C1 = pr(o + 9);
|
|
660
|
+
const dx = B[0] - A[0], dy = B[1] - A[1];
|
|
661
|
+
const s0 = dx * (C0[1] - A[1]) - dy * (C0[0] - A[0]), s1 = dx * (C1[1] - A[1]) - dy * (C1[0] - A[0]);
|
|
662
|
+
return s0 * s1 >= 0;
|
|
663
|
+
}
|
|
664
|
+
function createInstancedConditionalEdges(cond, im, material = condLineMat()) {
|
|
665
|
+
return condEdges(condBuffers(cond), im, material);
|
|
666
|
+
}
|
|
667
|
+
function condBuffers(cond) {
|
|
668
|
+
const n = Math.floor(cond.length / 12);
|
|
669
|
+
const buf = new Float32Array(n * 24), side = new Float32Array(n * 2);
|
|
670
|
+
for (let l = 0;l < n; l++) {
|
|
671
|
+
for (let k = 0;k < 12; k++) {
|
|
672
|
+
const v = cond[l * 12 + k];
|
|
673
|
+
buf[l * 24 + k] = v;
|
|
674
|
+
buf[l * 24 + 12 + k] = v;
|
|
675
|
+
}
|
|
676
|
+
side[l * 2 + 1] = 1;
|
|
677
|
+
}
|
|
678
|
+
return { ib: new THREE.InterleavedBuffer(buf, 12), side: new THREE.BufferAttribute(side, 1) };
|
|
679
|
+
}
|
|
680
|
+
function condEdges({ ib, side }, im, material) {
|
|
681
|
+
const g = new THREE.InstancedBufferGeometry;
|
|
682
|
+
g.setAttribute("position", new THREE.InterleavedBufferAttribute(ib, 3, 0));
|
|
683
|
+
g.setAttribute("ldrP1", new THREE.InterleavedBufferAttribute(ib, 3, 3));
|
|
684
|
+
g.setAttribute("ldrC0", new THREE.InterleavedBufferAttribute(ib, 3, 6));
|
|
685
|
+
g.setAttribute("ldrC1", new THREE.InterleavedBufferAttribute(ib, 3, 9));
|
|
686
|
+
g.setAttribute("ldrSide", side);
|
|
687
|
+
g.setAttribute("instanceMatrix", im.instanceMatrix);
|
|
688
|
+
g.instanceCount = im.count;
|
|
689
|
+
const l = new THREE.LineSegments(g, material);
|
|
690
|
+
l.frustumCulled = false;
|
|
691
|
+
l.name = "ldrCondLines";
|
|
692
|
+
l.onBeforeRender = (_r, _s, cam, _g, mat) => {
|
|
693
|
+
const u = mat.uniforms?.ldrViewProj;
|
|
694
|
+
if (!u)
|
|
695
|
+
return;
|
|
696
|
+
l.modelViewMatrix.multiplyMatrices(cam.matrixWorldInverse, l.matrixWorld);
|
|
697
|
+
u.value.multiplyMatrices(cam.projectionMatrix, l.modelViewMatrix);
|
|
698
|
+
mat.uniformsNeedUpdate = true;
|
|
699
|
+
};
|
|
700
|
+
return l;
|
|
701
|
+
}
|
|
702
|
+
function sharedLines(pos, im, material = sharedLineMat()) {
|
|
703
|
+
const { P, I } = weldLines(pos);
|
|
704
|
+
const g = new THREE.InstancedBufferGeometry;
|
|
705
|
+
g.setAttribute("position", new THREE.BufferAttribute(P, 3));
|
|
706
|
+
g.setIndex(new THREE.BufferAttribute(I, 1));
|
|
707
|
+
g.setAttribute("instanceMatrix", im.instanceMatrix);
|
|
708
|
+
g.instanceCount = im.count;
|
|
709
|
+
const l = new THREE.LineSegments(g, material);
|
|
710
|
+
l.frustumCulled = false;
|
|
711
|
+
return l;
|
|
712
|
+
}
|
|
713
|
+
function instancedLines(pos, mats, count) {
|
|
714
|
+
const g = new THREE.InstancedBufferGeometry;
|
|
715
|
+
g.setAttribute("position", new THREE.BufferAttribute(pos, 3));
|
|
716
|
+
const ib = new THREE.InstancedInterleavedBuffer(mats, 16);
|
|
717
|
+
for (let i = 0;i < 4; i++)
|
|
718
|
+
g.setAttribute("im" + i, new THREE.InterleavedBufferAttribute(ib, 4, i * 4));
|
|
719
|
+
g.instanceCount = count;
|
|
720
|
+
const l = new THREE.LineSegments(g, lineMat());
|
|
721
|
+
l.frustumCulled = false;
|
|
722
|
+
return l;
|
|
723
|
+
}
|
|
724
|
+
var toM4 = (m, o = 0) => new THREE.Matrix4().set(m[o + 3], m[o + 4], m[o + 5], m[o], m[o + 6], m[o + 7], m[o + 8], m[o + 1], m[o + 9], m[o + 10], m[o + 11], m[o + 2], 0, 0, 0, 1);
|
|
725
|
+
var mul12 = (a, b) => {
|
|
726
|
+
const r = (i, j) => a[3 + i * 3] * b[3 + j] + a[4 + i * 3] * b[6 + j] + a[5 + i * 3] * b[9 + j];
|
|
727
|
+
return [a[0] + a[3] * b[0] + a[4] * b[1] + a[5] * b[2], a[1] + a[6] * b[0] + a[7] * b[1] + a[8] * b[2], a[2] + a[9] * b[0] + a[10] * b[1] + a[11] * b[2], r(0, 0), r(0, 1), r(0, 2), r(1, 0), r(1, 1), r(1, 2), r(2, 0), r(2, 1), r(2, 2)];
|
|
728
|
+
};
|
|
729
|
+
function computeCulling(insts, H) {
|
|
730
|
+
const upright = (m) => Math.abs(m[7] - 1) < 0.001 && Math.abs(m[4]) < 0.001 && Math.abs(m[10]) < 0.001;
|
|
731
|
+
const CELL = 40;
|
|
732
|
+
const rects = new Map;
|
|
733
|
+
const lk = (y) => Math.round(y * 2) / 2;
|
|
734
|
+
insts.forEach((it, idx) => {
|
|
735
|
+
const p = H.parts[it.part];
|
|
736
|
+
if (!p || !p.mask || !upright(it.m))
|
|
737
|
+
return;
|
|
738
|
+
const b = p.bbox;
|
|
739
|
+
const xs = [], zs = [];
|
|
740
|
+
for (const x of [b[0], b[3]])
|
|
741
|
+
for (const z of [b[2], b[5]]) {
|
|
742
|
+
const w = [it.m[0] + it.m[3] * x + it.m[5] * z, it.m[2] + it.m[9] * x + it.m[11] * z];
|
|
743
|
+
xs.push(w[0]);
|
|
744
|
+
zs.push(w[1]);
|
|
745
|
+
}
|
|
746
|
+
const r = [Math.min(...xs), Math.max(...xs), Math.min(...zs), Math.max(...zs), idx];
|
|
747
|
+
const lv = lk(it.m[1] + b[4]);
|
|
748
|
+
for (let cx = Math.floor(r[0] / CELL);cx <= Math.floor(r[1] / CELL); cx++)
|
|
749
|
+
for (let cz = Math.floor(r[2] / CELL);cz <= Math.floor(r[3] / CELL); cz++) {
|
|
750
|
+
const k = lv + "|" + cx + "|" + cz;
|
|
751
|
+
(rects.get(k) || rects.set(k, []).get(k)).push(r);
|
|
752
|
+
}
|
|
753
|
+
});
|
|
754
|
+
const covered = (x, y, z, self) => {
|
|
755
|
+
const list = rects.get(lk(y) + "|" + Math.floor(x / CELL) + "|" + Math.floor(z / CELL));
|
|
756
|
+
if (!list)
|
|
757
|
+
return false;
|
|
758
|
+
for (const r of list) {
|
|
759
|
+
if (r[4] === self || !(x - 6 >= r[0] - 0.5 && x + 6 <= r[1] + 0.5 && z - 6 >= r[2] - 0.5 && z + 6 <= r[3] + 0.5))
|
|
760
|
+
continue;
|
|
761
|
+
const it = insts[r[4]];
|
|
762
|
+
const p = H.parts[it.part];
|
|
763
|
+
const m = it.m;
|
|
764
|
+
const dx = x - m[0], dy = y - m[1], dz = z - m[2];
|
|
765
|
+
const lx = m[3] * dx + m[6] * dy + m[9] * dz, lz = m[5] * dx + m[8] * dy + m[11] * dz;
|
|
766
|
+
const [nx, nz, bits] = p.mask;
|
|
767
|
+
let ok = true;
|
|
768
|
+
for (const [ox, oz] of [[0, 0], [5, 5], [-5, 5], [5, -5], [-5, -5]]) {
|
|
769
|
+
const ca = Math.floor((lx + ox - p.bbox[0]) / 20), cb = Math.floor((lz + oz - p.bbox[2]) / 20);
|
|
770
|
+
if (ca < 0 || cb < 0 || ca >= nx || cb >= nz || bits[ca * nz + cb] !== "1") {
|
|
771
|
+
ok = false;
|
|
772
|
+
break;
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
if (ok)
|
|
776
|
+
return true;
|
|
777
|
+
}
|
|
778
|
+
return false;
|
|
779
|
+
};
|
|
780
|
+
const studs = [];
|
|
781
|
+
const studTops = new Set;
|
|
782
|
+
const key3 = (x, y, z) => `${Math.round(x)},${Math.round(y)},${Math.round(z)}`;
|
|
783
|
+
insts.forEach((it, idx) => {
|
|
784
|
+
const p = H.parts[it.part];
|
|
785
|
+
if (!p)
|
|
786
|
+
return;
|
|
787
|
+
for (const s of p.studs) {
|
|
788
|
+
const w = mul12(it.m, s.slice(2));
|
|
789
|
+
const color = s[1] === "16" ? it.color : s[1];
|
|
790
|
+
const up = upright(w);
|
|
791
|
+
studs.push({ kind: s[0], color, w, hidden: up && covered(w[0], w[1], w[2], idx) });
|
|
792
|
+
if (up)
|
|
793
|
+
studTops.add(key3(w[0], w[1], w[2]));
|
|
794
|
+
}
|
|
795
|
+
});
|
|
796
|
+
const underHidden = insts.map((it) => {
|
|
797
|
+
const p = H.parts[it.part];
|
|
798
|
+
if (!p || !p.under || !upright(it.m))
|
|
799
|
+
return false;
|
|
800
|
+
const b = p.bbox;
|
|
801
|
+
const nx = Math.round((b[3] - b[0]) / 20), nz = Math.round((b[5] - b[2]) / 20);
|
|
802
|
+
if (!nx || !nz || nx * nz > 4096)
|
|
803
|
+
return false;
|
|
804
|
+
for (let a = 0;a < nx; a++)
|
|
805
|
+
for (let c = 0;c < nz; c++) {
|
|
806
|
+
const lx = b[0] + 10 + 20 * a, lz = b[2] + 10 + 20 * c;
|
|
807
|
+
const x = it.m[0] + it.m[3] * lx + it.m[5] * lz, z = it.m[2] + it.m[9] * lx + it.m[11] * lz;
|
|
808
|
+
if (!studTops.has(key3(x, it.m[1] + b[4], z)))
|
|
809
|
+
return false;
|
|
810
|
+
}
|
|
811
|
+
return true;
|
|
812
|
+
});
|
|
813
|
+
return { studs, underHidden };
|
|
814
|
+
}
|
|
815
|
+
function buildLdrScene(insts, pack, P, color, mark) {
|
|
816
|
+
const { header: H, arr } = pack;
|
|
817
|
+
const root = new THREE.Group;
|
|
818
|
+
const matF = makeMaterial(P.side === "front" ? THREE.FrontSide : THREE.DoubleSide), matB = makeMaterial(P.side === "front" ? THREE.BackSide : THREE.DoubleSide);
|
|
819
|
+
const matD = makeMaterial(THREE.DoubleSide);
|
|
820
|
+
let nUncert = 0;
|
|
821
|
+
const det = (m) => m[3] * (m[7] * m[11] - m[8] * m[10]) - m[4] * (m[6] * m[11] - m[8] * m[9]) + m[5] * (m[6] * m[10] - m[7] * m[9]);
|
|
822
|
+
const byPart = new Map;
|
|
823
|
+
insts.forEach((it, i) => {
|
|
824
|
+
if (H.parts[it.part]) {
|
|
825
|
+
const k = it.part + (det(it.m) < 0 ? "#m" : "");
|
|
826
|
+
(byPart.get(k) || byPart.set(k, []).get(k)).push(i);
|
|
827
|
+
}
|
|
828
|
+
});
|
|
829
|
+
const cull = computeCulling(insts, H);
|
|
830
|
+
mark("culling");
|
|
831
|
+
const edgeObjs = [];
|
|
832
|
+
const condObjs = [];
|
|
833
|
+
const wantCond = P.cond !== false && P.edges === "idx";
|
|
834
|
+
let condMat = null;
|
|
835
|
+
const cmat = () => condMat ??= condLineMat();
|
|
836
|
+
const condCache = new Map;
|
|
837
|
+
const addCond = (entry, im, which = "body") => {
|
|
838
|
+
if (!wantCond)
|
|
839
|
+
return null;
|
|
840
|
+
const ck = which === "under" ? entry.condlu : entry.condl;
|
|
841
|
+
if (!ck)
|
|
842
|
+
return null;
|
|
843
|
+
let cb = condCache.get(ck);
|
|
844
|
+
if (cb === undefined) {
|
|
845
|
+
const c = conditionalLines(pack, entry, which);
|
|
846
|
+
cb = c && c.length ? condBuffers(c) : null;
|
|
847
|
+
condCache.set(ck, cb);
|
|
848
|
+
}
|
|
849
|
+
if (!cb)
|
|
850
|
+
return null;
|
|
851
|
+
const l = condEdges(cb, im, cmat());
|
|
852
|
+
edgeObjs.push(l);
|
|
853
|
+
condObjs.push(l);
|
|
854
|
+
root.add(l);
|
|
855
|
+
return l;
|
|
856
|
+
};
|
|
857
|
+
const stats = { studsTotal: cull.studs.length, studsHidden: cull.studs.filter((s) => s.hidden).length, undersTotal: 0, undersHidden: 0 };
|
|
858
|
+
const toggles = [];
|
|
859
|
+
const studMeshes = [];
|
|
860
|
+
const wantTex = P.tex !== false && !!H.textures;
|
|
861
|
+
const texCache = new Map;
|
|
862
|
+
const texMats = new Map;
|
|
863
|
+
const pending = [];
|
|
864
|
+
const texMat = (t, base, wrapU) => {
|
|
865
|
+
const role = base === matF ? "F" : base === matB ? "B" : "D";
|
|
866
|
+
const k = t + ":" + role + ":" + (wrapU ? 1 : 0);
|
|
867
|
+
let m = texMats.get(k);
|
|
868
|
+
if (!m) {
|
|
869
|
+
let tx = texCache.get(t);
|
|
870
|
+
if (!tx) {
|
|
871
|
+
const r = packTexture(pack, t);
|
|
872
|
+
tx = r.texture;
|
|
873
|
+
pending.push(r.ready);
|
|
874
|
+
texCache.set(t, tx);
|
|
875
|
+
}
|
|
876
|
+
texMats.set(k, m = makeTexturedMaterial(base.side, tx, wrapU));
|
|
877
|
+
}
|
|
878
|
+
return m;
|
|
879
|
+
};
|
|
880
|
+
let texBatches = 0;
|
|
881
|
+
for (const [pk, ids] of byPart) {
|
|
882
|
+
const part = pk.replace(/#m$/, "");
|
|
883
|
+
const p = H.parts[part];
|
|
884
|
+
const mat = P.uncertDouble !== false && p.uncert ? (nUncert++, matD) : pk.endsWith("#m") ? matB : matF;
|
|
885
|
+
const textured = wantTex && !!p.tex;
|
|
886
|
+
const body = geom(textured ? p.tbody : p.body, arr);
|
|
887
|
+
let bodyIm = null;
|
|
888
|
+
const place = (im) => {
|
|
889
|
+
ids.forEach((id, k) => {
|
|
890
|
+
im.setMatrixAt(k, toM4(insts[id].m));
|
|
891
|
+
im.setColorAt(k, color(insts[id].color));
|
|
892
|
+
});
|
|
893
|
+
im.computeBoundingSphere();
|
|
894
|
+
root.add(im);
|
|
895
|
+
};
|
|
896
|
+
if (body) {
|
|
897
|
+
const im = bodyIm = new THREE.InstancedMesh(body, mat, ids.length);
|
|
898
|
+
place(im);
|
|
899
|
+
}
|
|
900
|
+
if (textured)
|
|
901
|
+
for (const tb of p.tex) {
|
|
902
|
+
const g = geom(tb, arr);
|
|
903
|
+
const im = new THREE.InstancedMesh(g, texMat(tb.t, mat, !!tb.wrapU), ids.length);
|
|
904
|
+
texBatches++;
|
|
905
|
+
if (bodyIm) {
|
|
906
|
+
im.instanceMatrix = bodyIm.instanceMatrix;
|
|
907
|
+
im.instanceColor = bodyIm.instanceColor;
|
|
908
|
+
im.boundingSphere = bodyIm.boundingSphere;
|
|
909
|
+
root.add(im);
|
|
910
|
+
} else {
|
|
911
|
+
place(im);
|
|
912
|
+
bodyIm = im;
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
if (P.edges === "idx" && p.edges && bodyIm) {
|
|
916
|
+
const l = sharedLines(arr(p.edges), bodyIm);
|
|
917
|
+
edgeObjs.push(l);
|
|
918
|
+
root.add(l);
|
|
919
|
+
}
|
|
920
|
+
if (bodyIm)
|
|
921
|
+
addCond(p, bodyIm);
|
|
922
|
+
const under = geom(p.under, arr);
|
|
923
|
+
if (under) {
|
|
924
|
+
const ord = [...ids.filter((i) => !cull.underHidden[i]), ...ids.filter((i) => cull.underHidden[i])];
|
|
925
|
+
const vis = ids.filter((i) => !cull.underHidden[i]).length;
|
|
926
|
+
stats.undersTotal += ids.length;
|
|
927
|
+
stats.undersHidden += ids.length - vis;
|
|
928
|
+
const im = new THREE.InstancedMesh(under, mat, ids.length);
|
|
929
|
+
ord.forEach((id, k) => {
|
|
930
|
+
im.setMatrixAt(k, toM4(insts[id].m));
|
|
931
|
+
im.setColorAt(k, color(insts[id].color));
|
|
932
|
+
});
|
|
933
|
+
im.computeBoundingSphere();
|
|
934
|
+
im.count = P.cullUnder ? vis : ids.length;
|
|
935
|
+
root.add(im);
|
|
936
|
+
toggles.push({ mesh: im, all: ids.length, visible: vis, flag: "under" });
|
|
937
|
+
{
|
|
938
|
+
const cl = addCond(p, im, "under");
|
|
939
|
+
if (cl) {
|
|
940
|
+
toggles.push({ mesh: { set count(n) {
|
|
941
|
+
cl.geometry.instanceCount = n;
|
|
942
|
+
} }, all: ids.length, visible: vis, flag: "under" });
|
|
943
|
+
cl.geometry.instanceCount = im.count;
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
if (P.edges === true && p.edges) {
|
|
948
|
+
const mats = new Float32Array(ids.length * 16);
|
|
949
|
+
ids.forEach((id, k) => toM4(insts[id].m).toArray(mats, k * 16));
|
|
950
|
+
{
|
|
951
|
+
const l = instancedLines(arr(p.edges), mats, ids.length);
|
|
952
|
+
edgeObjs.push(l);
|
|
953
|
+
root.add(l);
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
const byKind = new Map;
|
|
958
|
+
for (const s of cull.studs) {
|
|
959
|
+
const k = s.kind + (det(s.w) < 0 ? "#m" : "");
|
|
960
|
+
(byKind.get(k) || byKind.set(k, []).get(k)).push(s);
|
|
961
|
+
}
|
|
962
|
+
for (const [kk, list] of byKind) {
|
|
963
|
+
const kind = kk.replace(/#m$/, "");
|
|
964
|
+
const mat = kk.endsWith("#m") ? matB : matF;
|
|
965
|
+
const k = H.studKinds[kind];
|
|
966
|
+
const g = geom(k.body, arr);
|
|
967
|
+
if (!g)
|
|
968
|
+
continue;
|
|
969
|
+
const ord = [...list.filter((s) => !s.hidden), ...list.filter((s) => s.hidden)];
|
|
970
|
+
const vis = list.filter((s) => !s.hidden).length;
|
|
971
|
+
const im = new THREE.InstancedMesh(g, mat, ord.length);
|
|
972
|
+
ord.forEach((s, i) => {
|
|
973
|
+
im.setMatrixAt(i, toM4(s.w));
|
|
974
|
+
im.setColorAt(i, color(s.color));
|
|
975
|
+
});
|
|
976
|
+
im.computeBoundingSphere();
|
|
977
|
+
im.count = P.cullStuds ? vis : ord.length;
|
|
978
|
+
root.add(im);
|
|
979
|
+
toggles.push({ mesh: im, all: ord.length, visible: vis, flag: "studs" });
|
|
980
|
+
studMeshes.push({ im, kind });
|
|
981
|
+
if (P.edges === "idx" && k.edges) {
|
|
982
|
+
const l = sharedLines(arr(k.edges), im);
|
|
983
|
+
edgeObjs.push(l);
|
|
984
|
+
root.add(l);
|
|
985
|
+
toggles.push({ mesh: { set count(n) {
|
|
986
|
+
l.geometry.instanceCount = n;
|
|
987
|
+
} }, all: ord.length, visible: vis, flag: "studs" });
|
|
988
|
+
l.geometry.instanceCount = im.count;
|
|
989
|
+
}
|
|
990
|
+
{
|
|
991
|
+
const cl = P.condStuds === false ? null : addCond(k, im);
|
|
992
|
+
if (cl) {
|
|
993
|
+
toggles.push({ mesh: { set count(n) {
|
|
994
|
+
cl.geometry.instanceCount = n;
|
|
995
|
+
} }, all: ord.length, visible: vis, flag: "studs" });
|
|
996
|
+
cl.geometry.instanceCount = im.count;
|
|
997
|
+
}
|
|
998
|
+
}
|
|
999
|
+
if (P.edges === true && k.edges) {
|
|
1000
|
+
const mats = new Float32Array(ord.length * 16);
|
|
1001
|
+
ord.forEach((s, i) => toM4(s.w).toArray(mats, i * 16));
|
|
1002
|
+
const l = instancedLines(arr(k.edges), mats, P.cullStuds ? vis : ord.length);
|
|
1003
|
+
edgeObjs.push(l);
|
|
1004
|
+
root.add(l);
|
|
1005
|
+
toggles.push({ mesh: { set count(n) {
|
|
1006
|
+
l.geometry.instanceCount = n;
|
|
1007
|
+
} }, all: ord.length, visible: vis, flag: "studs" });
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
stats.uncertParts = Object.values(H.parts).filter((x) => x.uncert).length;
|
|
1011
|
+
stats.uncertBatches = nUncert;
|
|
1012
|
+
let condOn = true, edgesOn = true;
|
|
1013
|
+
const setEdges = (v) => {
|
|
1014
|
+
edgesOn = v;
|
|
1015
|
+
edgeObjs.forEach((o) => o.visible = v && (condOn || !condObjs.includes(o)));
|
|
1016
|
+
};
|
|
1017
|
+
const setCond = (v) => {
|
|
1018
|
+
condOn = v;
|
|
1019
|
+
condObjs.forEach((o) => o.visible = v && edgesOn);
|
|
1020
|
+
};
|
|
1021
|
+
stats.condBatches = condObjs.length;
|
|
1022
|
+
stats.texBatches = texBatches;
|
|
1023
|
+
stats.textures = texCache.size;
|
|
1024
|
+
const texturesReady = Promise.all(pending).then(() => {});
|
|
1025
|
+
const setCull = (studs, under) => {
|
|
1026
|
+
for (const t of toggles)
|
|
1027
|
+
t.mesh.count = (t.flag === "studs" ? studs : under) ? t.visible : t.all;
|
|
1028
|
+
};
|
|
1029
|
+
const box = new THREE.Box3;
|
|
1030
|
+
for (const it of insts) {
|
|
1031
|
+
const p = H.parts[it.part];
|
|
1032
|
+
if (!p)
|
|
1033
|
+
continue;
|
|
1034
|
+
box.expandByPoint(new THREE.Vector3(it.m[0], it.m[1], it.m[2]));
|
|
1035
|
+
}
|
|
1036
|
+
const setSide = (front) => {
|
|
1037
|
+
matF.side = front ? THREE.FrontSide : THREE.DoubleSide;
|
|
1038
|
+
matB.side = front ? THREE.BackSide : THREE.DoubleSide;
|
|
1039
|
+
matF.needsUpdate = matB.needsUpdate = true;
|
|
1040
|
+
for (const [k, m] of texMats) {
|
|
1041
|
+
const role = k.split(":")[1];
|
|
1042
|
+
m.side = role === "D" || !front ? THREE.DoubleSide : role === "B" ? THREE.BackSide : THREE.FrontSide;
|
|
1043
|
+
m.needsUpdate = true;
|
|
1044
|
+
}
|
|
1045
|
+
};
|
|
1046
|
+
const swapStuds = (alt) => {
|
|
1047
|
+
const saved = studMeshes.map((s) => s.im.geometry);
|
|
1048
|
+
for (const s of studMeshes) {
|
|
1049
|
+
const k2 = alt.header.studKinds[s.kind] ? s.kind : Object.keys(alt.header.studKinds).find((k) => k.replace("p/8/", "p/") === s.kind.replace("p/8/", "p/"));
|
|
1050
|
+
if (k2)
|
|
1051
|
+
s.im.geometry = geom(alt.header.studKinds[k2].body, alt.arr);
|
|
1052
|
+
}
|
|
1053
|
+
return () => studMeshes.forEach((s, i) => s.im.geometry = saved[i]);
|
|
1054
|
+
};
|
|
1055
|
+
return { root, stats, setCull, setSide, setEdges, setCond, swapStuds, box, texturesReady };
|
|
1056
|
+
}
|
|
1057
|
+
// src/scene.ts
|
|
1058
|
+
import * as THREE2 from "three";
|
|
1059
|
+
function colorFn(src, pack, fallback) {
|
|
1060
|
+
const table = new Map;
|
|
1061
|
+
const put = (k, v) => table.set(String(k), new THREE2.Color(typeof v === "string" ? v : v.hex));
|
|
1062
|
+
if (typeof src === "function") {
|
|
1063
|
+
return (c) => {
|
|
1064
|
+
let col = table.get(c);
|
|
1065
|
+
if (!col) {
|
|
1066
|
+
const v = src(+c);
|
|
1067
|
+
col = v === undefined ? direct(c) ?? fallback : new THREE2.Color(v);
|
|
1068
|
+
table.set(c, col);
|
|
1069
|
+
}
|
|
1070
|
+
return col;
|
|
1071
|
+
};
|
|
1072
|
+
}
|
|
1073
|
+
const base = src ?? pack.header.colors ?? {};
|
|
1074
|
+
if (base instanceof Map)
|
|
1075
|
+
for (const [k, v] of base)
|
|
1076
|
+
put(k, v);
|
|
1077
|
+
else
|
|
1078
|
+
for (const k in base)
|
|
1079
|
+
put(k, base[k]);
|
|
1080
|
+
return (c) => table.get(c) ?? direct(c) ?? fallback;
|
|
1081
|
+
}
|
|
1082
|
+
var direct = (c) => {
|
|
1083
|
+
const n = +c;
|
|
1084
|
+
return n > 16777215 && n >>> 24 === 2 ? new THREE2.Color(n & 16777215) : null;
|
|
1085
|
+
};
|
|
1086
|
+
var plasticCache = new WeakMap;
|
|
1087
|
+
function toPlastic(old) {
|
|
1088
|
+
let m = plasticCache.get(old);
|
|
1089
|
+
if (!m) {
|
|
1090
|
+
const p = new THREE2.MeshPhysicalMaterial({ vertexColors: true, roughness: 0.3, clearcoat: 0.3, clearcoatRoughness: 0.2, specularIntensity: 0.6, side: old.side });
|
|
1091
|
+
p.onBeforeCompile = old.onBeforeCompile;
|
|
1092
|
+
p.customProgramCacheKey = () => "Phys" + old.customProgramCacheKey() + old.side;
|
|
1093
|
+
p.userData = old.userData;
|
|
1094
|
+
plasticCache.set(old, m = p);
|
|
1095
|
+
}
|
|
1096
|
+
return m;
|
|
1097
|
+
}
|
|
1098
|
+
function createLdrObject(instances, pack, options = {}) {
|
|
1099
|
+
const o = { edges: true, conditionalLines: true, cullStuds: true, cullUndersides: true, side: "front", material: "plastic", ...options };
|
|
1100
|
+
const color = colorFn(o.colors, pack, new THREE2.Color(o.fallbackColor ?? 10528169));
|
|
1101
|
+
const all = instances.map((i) => ({ part: i.part, color: String(i.color), m: i.m, steps: i.steps }));
|
|
1102
|
+
const key = (i) => i.steps.map((x) => String(x).padStart(6, "0")).join(".");
|
|
1103
|
+
const uniq = [...new Set(all.map(key))].sort();
|
|
1104
|
+
const rank = new Map(uniq.map((k, j) => [k, j]));
|
|
1105
|
+
const stepOf = all.map((i) => rank.get(key(i)));
|
|
1106
|
+
const missing = [...new Set(all.filter((i) => !pack.header.parts[i.part]).map((i) => i.part))];
|
|
1107
|
+
const object = new THREE2.Group;
|
|
1108
|
+
object.name = "ldr";
|
|
1109
|
+
if (!o.ldrawSpace)
|
|
1110
|
+
object.rotation.x = Math.PI;
|
|
1111
|
+
if (o.scale && o.scale !== 1)
|
|
1112
|
+
object.scale.setScalar(o.scale);
|
|
1113
|
+
const pairs = new Map;
|
|
1114
|
+
let cur = null;
|
|
1115
|
+
let { edges, conditionalLines: cond, cullStuds: cs, cullUndersides: cu, side } = o;
|
|
1116
|
+
const stats = { instances: all.length, drawn: 0, missing, studsTotal: 0, studsHidden: 0, undersTotal: 0, undersHidden: 0, buildMs: 0 };
|
|
1117
|
+
const disposeCur = () => {
|
|
1118
|
+
if (!cur)
|
|
1119
|
+
return;
|
|
1120
|
+
object.remove(cur.root);
|
|
1121
|
+
const mats = new Set;
|
|
1122
|
+
cur.root.traverse((x) => {
|
|
1123
|
+
x.geometry?.dispose?.();
|
|
1124
|
+
if (x.material)
|
|
1125
|
+
mats.add(x.material);
|
|
1126
|
+
if (x.dispose && x.isInstancedMesh)
|
|
1127
|
+
x.dispose();
|
|
1128
|
+
});
|
|
1129
|
+
for (const m of [...mats, ...pairs.values()]) {
|
|
1130
|
+
m.userData.ldrMap?.dispose();
|
|
1131
|
+
m.dispose();
|
|
1132
|
+
}
|
|
1133
|
+
cur = null;
|
|
1134
|
+
};
|
|
1135
|
+
const build = (step) => {
|
|
1136
|
+
const t0 = performance.now();
|
|
1137
|
+
disposeCur();
|
|
1138
|
+
const insts = step == null ? all : all.filter((_, k) => stepOf[k] <= step);
|
|
1139
|
+
cur = buildLdrScene(insts, pack, { pack: "", cullStuds: cs, cullUnder: cu, edges: edges ? "idx" : false, side, cond, condStuds: o.conditionalStudLines !== false, tex: o.textures !== false }, color, () => {});
|
|
1140
|
+
pairs.clear();
|
|
1141
|
+
if (o.material === "plastic")
|
|
1142
|
+
cur.root.traverse((x) => {
|
|
1143
|
+
if (x.isMesh && x.material?.isMeshStandardMaterial) {
|
|
1144
|
+
const old = x.material;
|
|
1145
|
+
x.material = toPlastic(old);
|
|
1146
|
+
pairs.set(x.material, old);
|
|
1147
|
+
}
|
|
1148
|
+
});
|
|
1149
|
+
if (!edges)
|
|
1150
|
+
cur.setEdges(false);
|
|
1151
|
+
if (!cond)
|
|
1152
|
+
cur.setCond(false);
|
|
1153
|
+
object.add(cur.root);
|
|
1154
|
+
Object.assign(stats, { drawn: insts.filter((i) => pack.header.parts[i.part]).length, studsTotal: cur.stats.studsTotal, studsHidden: cur.stats.studsHidden, undersTotal: cur.stats.undersTotal, undersHidden: cur.stats.undersHidden, condBatches: cur.stats.condBatches ?? 0, texBatches: cur.stats.texBatches ?? 0, textures: cur.stats.textures ?? 0, buildMs: performance.now() - t0 });
|
|
1155
|
+
api.texturesReady = cur.texturesReady;
|
|
1156
|
+
};
|
|
1157
|
+
const api = { texturesReady: Promise.resolve() };
|
|
1158
|
+
build(o.step ?? null);
|
|
1159
|
+
return Object.assign(api, {
|
|
1160
|
+
object,
|
|
1161
|
+
stats,
|
|
1162
|
+
stepCount: uniq.length,
|
|
1163
|
+
setStep: (s) => build(s),
|
|
1164
|
+
setEdges: (v) => {
|
|
1165
|
+
edges = v;
|
|
1166
|
+
cur.setEdges(v);
|
|
1167
|
+
},
|
|
1168
|
+
setConditionalLines: (v) => {
|
|
1169
|
+
cond = v;
|
|
1170
|
+
cur.setCond(v);
|
|
1171
|
+
},
|
|
1172
|
+
setCulling: (a, b) => {
|
|
1173
|
+
cs = a;
|
|
1174
|
+
cu = b;
|
|
1175
|
+
cur.setCull(a, b);
|
|
1176
|
+
},
|
|
1177
|
+
setSide: (s) => {
|
|
1178
|
+
side = s;
|
|
1179
|
+
cur.setSide(s === "front");
|
|
1180
|
+
for (const [p, old] of pairs) {
|
|
1181
|
+
p.side = old.side;
|
|
1182
|
+
p.needsUpdate = true;
|
|
1183
|
+
}
|
|
1184
|
+
},
|
|
1185
|
+
getBox: (target = new THREE2.Box3) => {
|
|
1186
|
+
object.updateMatrixWorld(true);
|
|
1187
|
+
target.makeEmpty();
|
|
1188
|
+
object.traverse((x) => {
|
|
1189
|
+
if (x.isInstancedMesh && x.count) {
|
|
1190
|
+
x.computeBoundingBox();
|
|
1191
|
+
const b = x.boundingBox.clone().applyMatrix4(x.matrixWorld);
|
|
1192
|
+
target.union(b);
|
|
1193
|
+
}
|
|
1194
|
+
});
|
|
1195
|
+
return target;
|
|
1196
|
+
},
|
|
1197
|
+
dispose: disposeCur
|
|
1198
|
+
});
|
|
1199
|
+
}
|
|
1200
|
+
// src/pack-io.ts
|
|
1201
|
+
import { decompress } from "fzstd";
|
|
1202
|
+
async function decompressPack(data) {
|
|
1203
|
+
const u = data instanceof Uint8Array ? data : new Uint8Array(data);
|
|
1204
|
+
const own = (o) => o.buffer.slice(o.byteOffset, o.byteOffset + o.byteLength);
|
|
1205
|
+
if (u[0] === 40 && u[1] === 181 && u[2] === 47 && u[3] === 253)
|
|
1206
|
+
return own(decompress(u));
|
|
1207
|
+
if (u[0] === 31 && u[1] === 139)
|
|
1208
|
+
return new Response(new Blob([u]).stream().pipeThrough(new DecompressionStream("gzip"))).arrayBuffer();
|
|
1209
|
+
return own(u);
|
|
1210
|
+
}
|
|
1211
|
+
// src/node/library.ts
|
|
1212
|
+
import { existsSync, readFileSync } from "fs";
|
|
1213
|
+
import { join } from "path";
|
|
1214
|
+
var xf = (m, x, y, z) => [m[3] * x + m[4] * y + m[5] * z + m[0], m[6] * x + m[7] * y + m[8] * z + m[1], m[9] * x + m[10] * y + m[11] * z + m[2]];
|
|
1215
|
+
var det = (m) => m[3] * (m[7] * m[11] - m[8] * m[10]) - m[4] * (m[6] * m[11] - m[8] * m[9]) + m[5] * (m[6] * m[10] - m[7] * m[9]);
|
|
1216
|
+
|
|
1217
|
+
class Library {
|
|
1218
|
+
root;
|
|
1219
|
+
unofficialRoot;
|
|
1220
|
+
cache = new Map;
|
|
1221
|
+
meshCache = new Map;
|
|
1222
|
+
constructor(root, unofficialRoot) {
|
|
1223
|
+
this.root = root;
|
|
1224
|
+
this.unofficialRoot = unofficialRoot;
|
|
1225
|
+
}
|
|
1226
|
+
where(name, _seen) {
|
|
1227
|
+
name = norm(name);
|
|
1228
|
+
const seen = _seen ?? new Set;
|
|
1229
|
+
if (seen.has(name))
|
|
1230
|
+
return null;
|
|
1231
|
+
seen.add(name);
|
|
1232
|
+
const hit = this.lookup(name);
|
|
1233
|
+
if (!hit)
|
|
1234
|
+
return null;
|
|
1235
|
+
const file = hit.doc.files.get(hit.doc.main);
|
|
1236
|
+
if (file?.movedTo && file.movedTo !== name) {
|
|
1237
|
+
const target = this.where(file.movedTo, seen);
|
|
1238
|
+
if (target)
|
|
1239
|
+
return { ...target, aliasOf: name };
|
|
1240
|
+
}
|
|
1241
|
+
return hit;
|
|
1242
|
+
}
|
|
1243
|
+
lookup(name) {
|
|
1244
|
+
const roots = [
|
|
1245
|
+
{ root: this.root, dirs: [["parts", "part"], ["p", "prim"], ["models", "model"], ["parts/s", "part"]] }
|
|
1246
|
+
];
|
|
1247
|
+
const unoff = this.unofficialRoot ?? (existsSync(join(this.root, "unofficial")) ? join(this.root, "unofficial") : undefined);
|
|
1248
|
+
if (unoff)
|
|
1249
|
+
roots.push({ root: unoff, dirs: [["parts", "part"], ["p", "prim"], ["models", "model"], ["parts/s", "part"]] });
|
|
1250
|
+
for (const { root, dirs } of roots) {
|
|
1251
|
+
for (const [d, kind] of dirs) {
|
|
1252
|
+
if (d === "parts/s" && name.startsWith("s/"))
|
|
1253
|
+
continue;
|
|
1254
|
+
const key = root + "|" + d + "/" + name;
|
|
1255
|
+
if (!this.cache.has(key)) {
|
|
1256
|
+
const f = this.resolveFile(root, d, name);
|
|
1257
|
+
this.cache.set(key, f ? parse(readFileSync(f, "latin1"), name) : null);
|
|
1258
|
+
}
|
|
1259
|
+
const doc = this.cache.get(key);
|
|
1260
|
+
if (doc)
|
|
1261
|
+
return { kind, doc };
|
|
1262
|
+
}
|
|
1263
|
+
}
|
|
1264
|
+
return null;
|
|
1265
|
+
}
|
|
1266
|
+
resolveFile(root, dir, name) {
|
|
1267
|
+
const exact = join(root, dir, name);
|
|
1268
|
+
if (existsSync(exact))
|
|
1269
|
+
return exact;
|
|
1270
|
+
return null;
|
|
1271
|
+
}
|
|
1272
|
+
colors() {
|
|
1273
|
+
const out = new Map;
|
|
1274
|
+
const d = parse(readFileSync(join(this.root, "LDConfig.ldr"), "latin1"));
|
|
1275
|
+
for (const c of d.files.get(d.main).cmds)
|
|
1276
|
+
if (c.t === 0 && c.meta === "!COLOUR") {
|
|
1277
|
+
const t = c.text.split(/\s+/);
|
|
1278
|
+
const g = (k) => t[t.indexOf(k) + 1];
|
|
1279
|
+
out.set(+g("CODE"), { name: t[1], hex: g("VALUE"), edge: g("EDGE"), alpha: t.includes("ALPHA") ? +g("ALPHA") : 255 });
|
|
1280
|
+
}
|
|
1281
|
+
return out;
|
|
1282
|
+
}
|
|
1283
|
+
mesh(name, opts = {}) {
|
|
1284
|
+
name = norm(name);
|
|
1285
|
+
const ck = opts.texmap ? name + "#texmap" : name;
|
|
1286
|
+
const hit = this.meshCache.get(ck);
|
|
1287
|
+
if (hit)
|
|
1288
|
+
return hit;
|
|
1289
|
+
const pos = [], col = [], edges = [], cond = [];
|
|
1290
|
+
const tx = opts.texmap ? { uv: [], tri: [], names: [] } : undefined;
|
|
1291
|
+
const r = this.where(name);
|
|
1292
|
+
if (r)
|
|
1293
|
+
this.emit(r.doc, r.doc.main, I, 16, false, true, pos, col, edges, cond, tx, null);
|
|
1294
|
+
const m = { name: r?.doc.main ?? name, pos: new Float32Array(pos), col: new Uint32Array(col), edges: new Float32Array(edges), cond: new Float32Array(cond), nrm: smoothNormals(pos, edges, cond) };
|
|
1295
|
+
if (tx) {
|
|
1296
|
+
m.uv = new Float32Array(tx.uv);
|
|
1297
|
+
m.texture = new Int32Array(tx.tri);
|
|
1298
|
+
m.textures = tx.names;
|
|
1299
|
+
}
|
|
1300
|
+
this.meshCache.set(ck, m);
|
|
1301
|
+
return m;
|
|
1302
|
+
}
|
|
1303
|
+
texture(name, doc) {
|
|
1304
|
+
if (doc) {
|
|
1305
|
+
const d = resolveTexture(doc, name);
|
|
1306
|
+
if (d)
|
|
1307
|
+
return d.bytes;
|
|
1308
|
+
}
|
|
1309
|
+
const n = norm(name);
|
|
1310
|
+
const roots = [this.root];
|
|
1311
|
+
const unoff = this.unofficialRoot ?? (existsSync(join(this.root, "unofficial")) ? join(this.root, "unofficial") : undefined);
|
|
1312
|
+
if (unoff)
|
|
1313
|
+
roots.push(unoff);
|
|
1314
|
+
for (const cand of ["textures/" + n, n])
|
|
1315
|
+
for (const root of roots)
|
|
1316
|
+
for (const d of ["parts", "p", "models"]) {
|
|
1317
|
+
const f = join(root, d, cand);
|
|
1318
|
+
if (existsSync(f))
|
|
1319
|
+
return new Uint8Array(readFileSync(f));
|
|
1320
|
+
}
|
|
1321
|
+
return null;
|
|
1322
|
+
}
|
|
1323
|
+
emit(doc, fname, m, color, inv, clip, pos, col, edges, cond, tx, texIn = null) {
|
|
1324
|
+
const f = doc.files.get(fname);
|
|
1325
|
+
if (!f)
|
|
1326
|
+
return;
|
|
1327
|
+
let ccw = true, invertNext = false, clipLocal = clip;
|
|
1328
|
+
const flip = inv !== det(m) < 0;
|
|
1329
|
+
const cc = (c) => c === 16 ? color : c;
|
|
1330
|
+
const texIdx = (t) => {
|
|
1331
|
+
let i = tx.names.indexOf(t.texture);
|
|
1332
|
+
if (i < 0) {
|
|
1333
|
+
i = tx.names.length;
|
|
1334
|
+
tx.names.push(t.texture);
|
|
1335
|
+
}
|
|
1336
|
+
return i;
|
|
1337
|
+
};
|
|
1338
|
+
for (const c0 of f.cmds) {
|
|
1339
|
+
let c = c0;
|
|
1340
|
+
if (c.t === 0) {
|
|
1341
|
+
if (tx && c.geom)
|
|
1342
|
+
c = c.geom;
|
|
1343
|
+
else {
|
|
1344
|
+
if (c.bfc) {
|
|
1345
|
+
if (c.bfc.includes("CW") && !c.bfc.includes("CCW"))
|
|
1346
|
+
ccw = false;
|
|
1347
|
+
if (c.bfc.includes("CCW"))
|
|
1348
|
+
ccw = true;
|
|
1349
|
+
if (c.bfc.includes("INVERTNEXT"))
|
|
1350
|
+
invertNext = true;
|
|
1351
|
+
if (/\bNOCLIP\b/.test(c.bfc))
|
|
1352
|
+
clipLocal = false;
|
|
1353
|
+
else if (/\bCLIP\b/.test(c.bfc))
|
|
1354
|
+
clipLocal = true;
|
|
1355
|
+
}
|
|
1356
|
+
continue;
|
|
1357
|
+
}
|
|
1358
|
+
}
|
|
1359
|
+
if (c.t === 0)
|
|
1360
|
+
continue;
|
|
1361
|
+
if (tx && c.fallback)
|
|
1362
|
+
continue;
|
|
1363
|
+
const tex = tx ? c.tex ? transformTexMap(c.tex, m) : texIn : null;
|
|
1364
|
+
if (c.t === 1) {
|
|
1365
|
+
const sub = doc.files.has(c.file) ? doc : this.where(c.file)?.doc;
|
|
1366
|
+
if (sub)
|
|
1367
|
+
this.emit(sub, sub === doc ? c.file : sub.main, mul(m, c.m), cc(c.color), inv !== invertNext, clipLocal, pos, col, edges, cond, tx, tex);
|
|
1368
|
+
invertNext = false;
|
|
1369
|
+
continue;
|
|
1370
|
+
}
|
|
1371
|
+
const p = c.p, v = [];
|
|
1372
|
+
for (let i = 0;i < p.length; i += 3)
|
|
1373
|
+
v.push(xf(m, p[i], p[i + 1], p[i + 2]));
|
|
1374
|
+
if (c.t === 2) {
|
|
1375
|
+
edges.push(...v[0], ...v[1]);
|
|
1376
|
+
continue;
|
|
1377
|
+
}
|
|
1378
|
+
if (c.t === 5) {
|
|
1379
|
+
cond.push(...v[0], ...v[1], ...v[2] ?? [0, 0, 0], ...v[3] ?? [0, 0, 0]);
|
|
1380
|
+
continue;
|
|
1381
|
+
}
|
|
1382
|
+
const ordered = ccw !== flip ? v : [...v].reverse();
|
|
1383
|
+
const ti = tx && tex ? texIdx(tex) : -1;
|
|
1384
|
+
const code = cc(c.color);
|
|
1385
|
+
const tri = (a, b, d) => {
|
|
1386
|
+
pos.push(...a, ...b, ...d);
|
|
1387
|
+
col.push(code);
|
|
1388
|
+
if (tx) {
|
|
1389
|
+
tx.tri.push(ti);
|
|
1390
|
+
if (tex)
|
|
1391
|
+
tx.uv.push(...texmapTriangleUV(tex, [a, b, d]));
|
|
1392
|
+
else
|
|
1393
|
+
tx.uv.push(NaN, NaN, NaN, NaN, NaN, NaN);
|
|
1394
|
+
}
|
|
1395
|
+
};
|
|
1396
|
+
const emitFace = (w) => {
|
|
1397
|
+
tri(w[0], w[1], w[2]);
|
|
1398
|
+
if (c.t === 4)
|
|
1399
|
+
tri(w[0], w[2], w[3]);
|
|
1400
|
+
};
|
|
1401
|
+
emitFace(ordered);
|
|
1402
|
+
if (!clipLocal)
|
|
1403
|
+
emitFace([...ordered].reverse());
|
|
1404
|
+
}
|
|
1405
|
+
}
|
|
1406
|
+
flatten(doc) {
|
|
1407
|
+
const out = [];
|
|
1408
|
+
const walk = (d, fname, m, color, path, stepPath) => {
|
|
1409
|
+
const f = d.files.get(fname);
|
|
1410
|
+
if (!f)
|
|
1411
|
+
return;
|
|
1412
|
+
let s = 0;
|
|
1413
|
+
for (const c of f.cmds) {
|
|
1414
|
+
if (c.t === 0 && c.step)
|
|
1415
|
+
s++;
|
|
1416
|
+
if (c.t !== 1)
|
|
1417
|
+
continue;
|
|
1418
|
+
const col = c.color === 16 ? color : c.color, mm = mul(m, c.m), sp = [...stepPath, s];
|
|
1419
|
+
if (d.files.has(c.file)) {
|
|
1420
|
+
walk(d, c.file, mm, col, path + "/" + c.file, sp);
|
|
1421
|
+
continue;
|
|
1422
|
+
}
|
|
1423
|
+
const r = this.where(c.file);
|
|
1424
|
+
if (r?.kind === "model")
|
|
1425
|
+
walk(r.doc, r.doc.main, mm, col, path + "/" + c.file, sp);
|
|
1426
|
+
else {
|
|
1427
|
+
const part = r ? r.doc.main : c.file;
|
|
1428
|
+
out.push({ part, color: col, m: mm, step: sp[0], localStep: s, steps: sp, path, missing: !r, aliasOf: r?.aliasOf });
|
|
1429
|
+
}
|
|
1430
|
+
}
|
|
1431
|
+
};
|
|
1432
|
+
walk(doc, doc.main, I, 16, doc.main, []);
|
|
1433
|
+
return out;
|
|
1434
|
+
}
|
|
1435
|
+
}
|
|
1436
|
+
// src/node/build-pack.ts
|
|
1437
|
+
import fs3 from "node:fs";
|
|
1438
|
+
import path3 from "node:path";
|
|
1439
|
+
|
|
1440
|
+
// src/node/resolver.ts
|
|
1441
|
+
import fs from "node:fs";
|
|
1442
|
+
import path from "node:path";
|
|
1443
|
+
var TOP_STUD_RE = /^(stud|studa|stud2|stud2a|stud6|stud6a|stud10|stud15|stud2s|stud2s2|stud-logo\d*|stud2-logo\d*)\.dat$/i;
|
|
1444
|
+
var UNDER_STUD_RE = /^(stud3|stud3a|stud4.*|stud12|stud16.*|stud18a)\.dat$/i;
|
|
1445
|
+
var norm2 = (ref) => ref.trim().replace(/\\/g, "/").toLowerCase();
|
|
1446
|
+
var threeName = (ref) => {
|
|
1447
|
+
const n = norm2(ref);
|
|
1448
|
+
if (n.startsWith("s/"))
|
|
1449
|
+
return "parts/" + n;
|
|
1450
|
+
if (n.startsWith("48/"))
|
|
1451
|
+
return "p/" + n;
|
|
1452
|
+
return n;
|
|
1453
|
+
};
|
|
1454
|
+
function parseType1(line) {
|
|
1455
|
+
const t = line.trim().split(/\s+/);
|
|
1456
|
+
if (t[0] !== "1" || t.length < 15)
|
|
1457
|
+
return null;
|
|
1458
|
+
const v = t.slice(2, 14).map(Number);
|
|
1459
|
+
return { color: t[1], m: v, name: t.slice(14).join(" ") };
|
|
1460
|
+
}
|
|
1461
|
+
function xf2(m, p) {
|
|
1462
|
+
return [
|
|
1463
|
+
m[0] + m[3] * p[0] + m[4] * p[1] + m[5] * p[2],
|
|
1464
|
+
m[1] + m[6] * p[0] + m[7] * p[1] + m[8] * p[2],
|
|
1465
|
+
m[2] + m[9] * p[0] + m[10] * p[1] + m[11] * p[2]
|
|
1466
|
+
];
|
|
1467
|
+
}
|
|
1468
|
+
function compose(a, b) {
|
|
1469
|
+
const r = (i, j) => a[3 + i * 3] * b[3 + j] + a[3 + i * 3 + 1] * b[6 + j] + a[3 + i * 3 + 2] * b[9 + j];
|
|
1470
|
+
const t = xf2(a, [b[0], b[1], b[2]]);
|
|
1471
|
+
return [t[0], t[1], t[2], r(0, 0), r(0, 1), r(0, 2), r(1, 0), r(1, 1), r(1, 2), r(2, 0), r(2, 1), r(2, 2)];
|
|
1472
|
+
}
|
|
1473
|
+
var IDENT = [0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1];
|
|
1474
|
+
|
|
1475
|
+
class Library2 {
|
|
1476
|
+
root;
|
|
1477
|
+
index;
|
|
1478
|
+
text;
|
|
1479
|
+
stats;
|
|
1480
|
+
constructor(root) {
|
|
1481
|
+
this.root = root;
|
|
1482
|
+
this.index = new Map;
|
|
1483
|
+
this.text = new Map;
|
|
1484
|
+
this.stats = new Map;
|
|
1485
|
+
for (const d of ["parts", "p", "models"])
|
|
1486
|
+
this.#walk(path.join(root, d), d);
|
|
1487
|
+
}
|
|
1488
|
+
#walk(dir, rel) {
|
|
1489
|
+
if (!fs.existsSync(dir))
|
|
1490
|
+
return;
|
|
1491
|
+
for (const e of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
1492
|
+
const r = rel + "/" + e.name;
|
|
1493
|
+
if (e.isDirectory())
|
|
1494
|
+
this.#walk(path.join(dir, e.name), r);
|
|
1495
|
+
else
|
|
1496
|
+
this.index.set(r.toLowerCase(), path.join(dir, e.name));
|
|
1497
|
+
}
|
|
1498
|
+
}
|
|
1499
|
+
resolve(ref) {
|
|
1500
|
+
const n = norm2(ref);
|
|
1501
|
+
for (const pre of ["parts/", "p/", "models/"])
|
|
1502
|
+
if (this.index.has(pre + n))
|
|
1503
|
+
return pre + n;
|
|
1504
|
+
return null;
|
|
1505
|
+
}
|
|
1506
|
+
read(key) {
|
|
1507
|
+
if (!this.text.has(key))
|
|
1508
|
+
this.text.set(key, fs.readFileSync(this.index.get(key), "latin1"));
|
|
1509
|
+
return this.text.get(key);
|
|
1510
|
+
}
|
|
1511
|
+
isPart(key) {
|
|
1512
|
+
return key.startsWith("parts/") && !key.startsWith("parts/s/");
|
|
1513
|
+
}
|
|
1514
|
+
fileStats(key) {
|
|
1515
|
+
if (this.stats.has(key))
|
|
1516
|
+
return this.stats.get(key);
|
|
1517
|
+
const s = { tris: 0, studTris: 0, underTris: 0, studs: [], min: [Infinity, Infinity, Infinity], max: [-Infinity, -Infinity, -Infinity], missing: new Set };
|
|
1518
|
+
this.stats.set(key, s);
|
|
1519
|
+
const base = path.basename(key);
|
|
1520
|
+
const isTop = TOP_STUD_RE.test(base), isUnder = UNDER_STUD_RE.test(base);
|
|
1521
|
+
const grow = (p) => {
|
|
1522
|
+
for (let i = 0;i < 3; i++) {
|
|
1523
|
+
if (p[i] < s.min[i])
|
|
1524
|
+
s.min[i] = p[i];
|
|
1525
|
+
if (p[i] > s.max[i])
|
|
1526
|
+
s.max[i] = p[i];
|
|
1527
|
+
}
|
|
1528
|
+
};
|
|
1529
|
+
for (const line of this.read(key).split(/\r?\n/)) {
|
|
1530
|
+
const t = line.trim();
|
|
1531
|
+
const c = t[0];
|
|
1532
|
+
if (c === "3" || c === "4") {
|
|
1533
|
+
const v = t.split(/\s+/).slice(2).map(Number);
|
|
1534
|
+
const nt = c === "3" ? 1 : 2;
|
|
1535
|
+
s.tris += nt;
|
|
1536
|
+
for (let i = 0;i + 2 < v.length; i += 3)
|
|
1537
|
+
grow([v[i], v[i + 1], v[i + 2]]);
|
|
1538
|
+
} else if (c === "1") {
|
|
1539
|
+
const r = parseType1(t);
|
|
1540
|
+
if (!r)
|
|
1541
|
+
continue;
|
|
1542
|
+
const ck = this.resolve(r.name);
|
|
1543
|
+
if (!ck) {
|
|
1544
|
+
s.missing.add(norm2(r.name));
|
|
1545
|
+
continue;
|
|
1546
|
+
}
|
|
1547
|
+
const cs = this.fileStats(ck);
|
|
1548
|
+
s.tris += cs.tris;
|
|
1549
|
+
s.studTris += cs.studTris;
|
|
1550
|
+
s.underTris += cs.underTris;
|
|
1551
|
+
for (const m of cs.missing)
|
|
1552
|
+
s.missing.add(m);
|
|
1553
|
+
if (TOP_STUD_RE.test(path.basename(ck)))
|
|
1554
|
+
s.studs.push([r.m[0], r.m[1], r.m[2]]);
|
|
1555
|
+
else
|
|
1556
|
+
for (const p of cs.studs)
|
|
1557
|
+
s.studs.push(xf2(r.m, p));
|
|
1558
|
+
if (isFinite(cs.min[0])) {
|
|
1559
|
+
for (const x of [cs.min[0], cs.max[0]])
|
|
1560
|
+
for (const y of [cs.min[1], cs.max[1]])
|
|
1561
|
+
for (const z of [cs.min[2], cs.max[2]])
|
|
1562
|
+
grow(xf2(r.m, [x, y, z]));
|
|
1563
|
+
}
|
|
1564
|
+
}
|
|
1565
|
+
}
|
|
1566
|
+
if (isTop) {
|
|
1567
|
+
s.studTris = s.tris;
|
|
1568
|
+
}
|
|
1569
|
+
if (isUnder) {
|
|
1570
|
+
s.underTris = s.tris;
|
|
1571
|
+
}
|
|
1572
|
+
return s;
|
|
1573
|
+
}
|
|
1574
|
+
}
|
|
1575
|
+
function splitModel(text, fallbackName = "main.ldr") {
|
|
1576
|
+
const files = new Map;
|
|
1577
|
+
const data = new Map;
|
|
1578
|
+
let dataCur = null;
|
|
1579
|
+
let cur = null, buf = [], main = null;
|
|
1580
|
+
let skip = false, sawBlock = false;
|
|
1581
|
+
const flush = () => {
|
|
1582
|
+
if (cur)
|
|
1583
|
+
files.set(cur, buf.join(`
|
|
1584
|
+
`));
|
|
1585
|
+
cur = null;
|
|
1586
|
+
buf = [];
|
|
1587
|
+
};
|
|
1588
|
+
const firstBlock = () => {
|
|
1589
|
+
if (sawBlock)
|
|
1590
|
+
return;
|
|
1591
|
+
sawBlock = true;
|
|
1592
|
+
files.clear();
|
|
1593
|
+
cur = null;
|
|
1594
|
+
buf = [];
|
|
1595
|
+
main = null;
|
|
1596
|
+
};
|
|
1597
|
+
for (const line of text.split(/\r?\n/)) {
|
|
1598
|
+
const m = /^\s*0\s+FILE\s+(.+)$/i.exec(line);
|
|
1599
|
+
if (m) {
|
|
1600
|
+
firstBlock();
|
|
1601
|
+
flush();
|
|
1602
|
+
skip = false;
|
|
1603
|
+
dataCur = null;
|
|
1604
|
+
cur = norm2(m[1]);
|
|
1605
|
+
if (!main)
|
|
1606
|
+
main = cur;
|
|
1607
|
+
continue;
|
|
1608
|
+
}
|
|
1609
|
+
if (/^\s*0\s+(NOFILE|!DATA)(\s|$)/i.test(line)) {
|
|
1610
|
+
dataCur = null;
|
|
1611
|
+
if (/!DATA/i.test(line)) {
|
|
1612
|
+
firstBlock();
|
|
1613
|
+
const dn = norm2(line.replace(/^\s*0\s+!DATA\s*/i, ""));
|
|
1614
|
+
dataCur = dn;
|
|
1615
|
+
data.set(dn, "");
|
|
1616
|
+
}
|
|
1617
|
+
flush();
|
|
1618
|
+
skip = true;
|
|
1619
|
+
continue;
|
|
1620
|
+
}
|
|
1621
|
+
if (skip) {
|
|
1622
|
+
const dc = dataCur;
|
|
1623
|
+
if (dc) {
|
|
1624
|
+
const d = /^\s*0\s+!:\s*(.*)$/.exec(line);
|
|
1625
|
+
if (d)
|
|
1626
|
+
data.set(dc, data.get(dc) + d[1].trim());
|
|
1627
|
+
}
|
|
1628
|
+
continue;
|
|
1629
|
+
}
|
|
1630
|
+
if (!cur) {
|
|
1631
|
+
cur = norm2(fallbackName);
|
|
1632
|
+
main = cur;
|
|
1633
|
+
}
|
|
1634
|
+
buf.push(line);
|
|
1635
|
+
}
|
|
1636
|
+
flush();
|
|
1637
|
+
return { files, main, data };
|
|
1638
|
+
}
|
|
1639
|
+
function flatten2(model, lib, onInstance) {
|
|
1640
|
+
const missing = new Map;
|
|
1641
|
+
const walk = (name, parentM, parentColor, depth) => {
|
|
1642
|
+
if (depth > 32)
|
|
1643
|
+
return;
|
|
1644
|
+
for (const line of model.files.get(name).split(`
|
|
1645
|
+
`)) {
|
|
1646
|
+
const r = parseType1(line);
|
|
1647
|
+
if (!r)
|
|
1648
|
+
continue;
|
|
1649
|
+
const color = r.color === "16" ? parentColor : r.color;
|
|
1650
|
+
const m = compose(parentM, r.m);
|
|
1651
|
+
const n = norm2(r.name);
|
|
1652
|
+
if (model.files.has(n)) {
|
|
1653
|
+
walk(n, m, color, depth + 1);
|
|
1654
|
+
continue;
|
|
1655
|
+
}
|
|
1656
|
+
const key = lib.resolve(r.name);
|
|
1657
|
+
if (!key) {
|
|
1658
|
+
missing.set(n, (missing.get(n) || 0) + 1);
|
|
1659
|
+
continue;
|
|
1660
|
+
}
|
|
1661
|
+
onInstance({ part: key, ref: n, color, m });
|
|
1662
|
+
}
|
|
1663
|
+
};
|
|
1664
|
+
walk(model.main, IDENT, "16", 0);
|
|
1665
|
+
return { missing };
|
|
1666
|
+
}
|
|
1667
|
+
function collectDeps(lib, refs) {
|
|
1668
|
+
const out = new Map;
|
|
1669
|
+
const stack = [...refs];
|
|
1670
|
+
while (stack.length) {
|
|
1671
|
+
const ref = stack.pop();
|
|
1672
|
+
const tn = threeName(ref);
|
|
1673
|
+
if (out.has(tn))
|
|
1674
|
+
continue;
|
|
1675
|
+
const key = lib.resolve(ref);
|
|
1676
|
+
if (!key)
|
|
1677
|
+
continue;
|
|
1678
|
+
out.set(tn, key);
|
|
1679
|
+
for (const line of lib.read(key).split(/\r?\n/)) {
|
|
1680
|
+
const r = parseType1(line);
|
|
1681
|
+
if (r)
|
|
1682
|
+
stack.push(r.name);
|
|
1683
|
+
}
|
|
1684
|
+
}
|
|
1685
|
+
return out;
|
|
1686
|
+
}
|
|
1687
|
+
|
|
1688
|
+
// src/node/compile.ts
|
|
1689
|
+
import fs2 from "node:fs";
|
|
1690
|
+
import path2 from "node:path";
|
|
1691
|
+
function loadColors(root) {
|
|
1692
|
+
const map = new Map;
|
|
1693
|
+
for (const l of fs2.readFileSync(path2.join(root, "LDConfig.ldr"), "latin1").split(/\r?\n/)) {
|
|
1694
|
+
const m = /!COLOUR\s+\S+\s+CODE\s+(\d+)\s+VALUE\s+#([0-9A-Fa-f]{6})/.exec(l);
|
|
1695
|
+
if (m) {
|
|
1696
|
+
const h = parseInt(m[2], 16);
|
|
1697
|
+
map.set(m[1], [(h >> 16) / 255, (h >> 8 & 255) / 255, (h & 255) / 255]);
|
|
1698
|
+
}
|
|
1699
|
+
}
|
|
1700
|
+
return map;
|
|
1701
|
+
}
|
|
1702
|
+
var det3 = (m) => m[3] * (m[7] * m[11] - m[8] * m[10]) - m[4] * (m[6] * m[11] - m[8] * m[9]) + m[5] * (m[6] * m[10] - m[7] * m[9]);
|
|
1703
|
+
|
|
1704
|
+
class Compiler {
|
|
1705
|
+
lib;
|
|
1706
|
+
opts;
|
|
1707
|
+
colors;
|
|
1708
|
+
cache = new Map;
|
|
1709
|
+
constructor(lib, opts) {
|
|
1710
|
+
this.lib = lib;
|
|
1711
|
+
this.opts = opts;
|
|
1712
|
+
this.colors = loadColors(lib.root);
|
|
1713
|
+
}
|
|
1714
|
+
resolve(ref, isStud) {
|
|
1715
|
+
const key = this.lib.resolve(ref);
|
|
1716
|
+
if (!key)
|
|
1717
|
+
return null;
|
|
1718
|
+
const low = isStud ? this.opts.studs === "low" : this.opts.prims === "low";
|
|
1719
|
+
if (low && key.startsWith("p/") && !key.startsWith("p/8/") && !key.startsWith("p/48/")) {
|
|
1720
|
+
const alt = "p/8/" + key.slice(2);
|
|
1721
|
+
if (this.lib.index.has(alt))
|
|
1722
|
+
return alt;
|
|
1723
|
+
}
|
|
1724
|
+
if (low && key.startsWith("p/48/")) {
|
|
1725
|
+
const alt = "p/8/" + key.slice(5);
|
|
1726
|
+
if (this.lib.index.has(alt))
|
|
1727
|
+
return alt;
|
|
1728
|
+
const a2 = "p/" + key.slice(5);
|
|
1729
|
+
if (this.lib.index.has(a2))
|
|
1730
|
+
return a2;
|
|
1731
|
+
}
|
|
1732
|
+
return key;
|
|
1733
|
+
}
|
|
1734
|
+
compile(key, separateStuds = this.opts.separateStuds) {
|
|
1735
|
+
const ck = key + (separateStuds ? "" : "#nostudsep");
|
|
1736
|
+
if (this.cache.has(ck))
|
|
1737
|
+
return this.cache.get(ck);
|
|
1738
|
+
const out = { body: { pos: [], col: [] }, under: { pos: [], col: [] }, edges: [], condLines: 0, cond: this.opts.cond ? [] : undefined, condUnder: this.opts.cond ? [] : undefined, studs: [], bbox: [Infinity, Infinity, Infinity, -Infinity, -Infinity, -Infinity], fullBottom: false };
|
|
1739
|
+
const colorOf = (code) => code === "16" ? [1, 1, 1, 0] : [...this.colors.get(code) || (code.startsWith("0x2") ? [parseInt(code.slice(3, 5), 16) / 255, parseInt(code.slice(5, 7), 16) / 255, parseInt(code.slice(7, 9), 16) / 255] : [1, 0, 1]), 1];
|
|
1740
|
+
const walk = (fk, M, color, invert, target, depth) => {
|
|
1741
|
+
if (depth > 40)
|
|
1742
|
+
return;
|
|
1743
|
+
let ccw = true, certified = false, invertNext = false;
|
|
1744
|
+
for (const raw of this.lib.read(fk).split(/\r?\n/)) {
|
|
1745
|
+
const t = raw.trim();
|
|
1746
|
+
if (!t)
|
|
1747
|
+
continue;
|
|
1748
|
+
const c = t[0];
|
|
1749
|
+
if (c === "0") {
|
|
1750
|
+
if (/^0\s+BFC/.test(t)) {
|
|
1751
|
+
if (/CERTIFY/.test(t) && !/NOCERTIFY/.test(t))
|
|
1752
|
+
certified = true;
|
|
1753
|
+
if (/\bCW\b/.test(t))
|
|
1754
|
+
ccw = false;
|
|
1755
|
+
if (/\bCCW\b/.test(t))
|
|
1756
|
+
ccw = true;
|
|
1757
|
+
if (/INVERTNEXT/.test(t))
|
|
1758
|
+
invertNext = true;
|
|
1759
|
+
}
|
|
1760
|
+
continue;
|
|
1761
|
+
}
|
|
1762
|
+
if (c === "1") {
|
|
1763
|
+
const r = parseType1(t);
|
|
1764
|
+
if (!r)
|
|
1765
|
+
continue;
|
|
1766
|
+
const base = r.name.trim().replace(/\\/g, "/").split("/").pop().toLowerCase();
|
|
1767
|
+
const isTop = TOP_STUD_RE.test(base), isUnder = UNDER_STUD_RE.test(base);
|
|
1768
|
+
const ck2 = this.resolve(r.name, isTop);
|
|
1769
|
+
if (!ck2) {
|
|
1770
|
+
invertNext = false;
|
|
1771
|
+
continue;
|
|
1772
|
+
}
|
|
1773
|
+
const childColor = r.color === "16" ? color : r.color;
|
|
1774
|
+
const Mc = compose(M, r.m);
|
|
1775
|
+
const inv = invert !== invertNext !== det3(r.m) < 0;
|
|
1776
|
+
invertNext = false;
|
|
1777
|
+
if (isTop && separateStuds) {
|
|
1778
|
+
out.studs.push({ kind: ck2, color: childColor, m: Mc });
|
|
1779
|
+
growBox(out.bbox, Mc, this.compile(ck2, false).bbox);
|
|
1780
|
+
continue;
|
|
1781
|
+
}
|
|
1782
|
+
walk(ck2, Mc, childColor, inv, isUnder ? "under" : target, depth + 1);
|
|
1783
|
+
continue;
|
|
1784
|
+
}
|
|
1785
|
+
const tok = t.split(/\s+/);
|
|
1786
|
+
if (c === "2") {
|
|
1787
|
+
const v = tok.slice(2, 8).map(Number);
|
|
1788
|
+
out.edges.push(...xf2(M, v.slice(0, 3)), ...xf2(M, v.slice(3, 6)));
|
|
1789
|
+
continue;
|
|
1790
|
+
}
|
|
1791
|
+
if (c === "5") {
|
|
1792
|
+
out.condLines++;
|
|
1793
|
+
const dst = target === "under" ? out.condUnder : out.cond;
|
|
1794
|
+
if (dst) {
|
|
1795
|
+
const v = t.split(/\s+/).slice(2, 14).map(Number);
|
|
1796
|
+
if (v.length === 12 && v.every(Number.isFinite))
|
|
1797
|
+
for (let i = 0;i < 12; i += 3)
|
|
1798
|
+
dst.push(...xf2(M, v.slice(i, i + 3)));
|
|
1799
|
+
}
|
|
1800
|
+
continue;
|
|
1801
|
+
}
|
|
1802
|
+
if (c === "3" || c === "4") {
|
|
1803
|
+
const code = tok[1] === "16" ? color : tok[1];
|
|
1804
|
+
const n = c === "3" ? 3 : 4;
|
|
1805
|
+
const v = tok.slice(2, 2 + n * 3).map(Number);
|
|
1806
|
+
const pts = [];
|
|
1807
|
+
for (let i = 0;i < n; i++)
|
|
1808
|
+
pts.push(xf2(M, v.slice(i * 3, i * 3 + 3)));
|
|
1809
|
+
for (const p of pts)
|
|
1810
|
+
grow(out.bbox, p);
|
|
1811
|
+
let order = n === 3 ? [0, 1, 2] : [0, 1, 2, 0, 2, 3];
|
|
1812
|
+
if (!ccw !== invert)
|
|
1813
|
+
order = n === 3 ? [0, 2, 1] : [0, 2, 1, 0, 3, 2];
|
|
1814
|
+
const rgba = colorOf(code);
|
|
1815
|
+
const b = out[target];
|
|
1816
|
+
for (const i of order) {
|
|
1817
|
+
b.pos.push(...pts[i]);
|
|
1818
|
+
b.col.push(...rgba);
|
|
1819
|
+
}
|
|
1820
|
+
}
|
|
1821
|
+
}
|
|
1822
|
+
if (!certified)
|
|
1823
|
+
out.uncertified = true;
|
|
1824
|
+
};
|
|
1825
|
+
walk(key, IDENT, "16", false, "body", 0);
|
|
1826
|
+
if (this.opts.tex && this.hasTex(key))
|
|
1827
|
+
this.texPass(key, out, separateStuds, colorOf);
|
|
1828
|
+
const [x0, y0, z0, x1, y1, z1] = out.bbox;
|
|
1829
|
+
let ax0 = Infinity, ax1 = -Infinity, az0 = Infinity, az1 = -Infinity;
|
|
1830
|
+
for (const b of [out.body, out.under])
|
|
1831
|
+
for (let i = 0;i < b.pos.length; i += 3)
|
|
1832
|
+
if (b.pos[i + 1] > y1 - 0.5) {
|
|
1833
|
+
ax0 = Math.min(ax0, b.pos[i]);
|
|
1834
|
+
ax1 = Math.max(ax1, b.pos[i]);
|
|
1835
|
+
az0 = Math.min(az0, b.pos[i + 2]);
|
|
1836
|
+
az1 = Math.max(az1, b.pos[i + 2]);
|
|
1837
|
+
}
|
|
1838
|
+
out.fullBottom = isFinite(x0) && ax0 <= x0 + 0.5 && ax1 >= x1 - 0.5 && az0 <= z0 + 0.5 && az1 >= z1 - 0.5;
|
|
1839
|
+
if (separateStuds && isFinite(x0))
|
|
1840
|
+
out.bottomMask = enclosureMask(out);
|
|
1841
|
+
this.cache.set(ck, out);
|
|
1842
|
+
return out;
|
|
1843
|
+
}
|
|
1844
|
+
texMemo = new Map;
|
|
1845
|
+
hasTex(key) {
|
|
1846
|
+
const m = this.texMemo.get(key);
|
|
1847
|
+
if (m !== undefined)
|
|
1848
|
+
return m;
|
|
1849
|
+
this.texMemo.set(key, false);
|
|
1850
|
+
const txt = this.lib.read(key);
|
|
1851
|
+
let r = /^\s*0\s+!TEXMAP/im.test(txt);
|
|
1852
|
+
if (!r)
|
|
1853
|
+
for (const l of txt.split(/\r?\n/)) {
|
|
1854
|
+
const t = l.trim();
|
|
1855
|
+
const p = t.startsWith("0 !:") ? parseType1(t.slice(4)) : t[0] === "1" ? parseType1(t) : null;
|
|
1856
|
+
if (p) {
|
|
1857
|
+
const k = this.lib.resolve(p.name);
|
|
1858
|
+
if (k && this.hasTex(k)) {
|
|
1859
|
+
r = true;
|
|
1860
|
+
break;
|
|
1861
|
+
}
|
|
1862
|
+
}
|
|
1863
|
+
}
|
|
1864
|
+
this.texMemo.set(key, r);
|
|
1865
|
+
return r;
|
|
1866
|
+
}
|
|
1867
|
+
texture(name) {
|
|
1868
|
+
const n = norm2(name);
|
|
1869
|
+
for (const pre of ["parts/textures/", "p/textures/", "models/textures/", "parts/", "p/", "models/"]) {
|
|
1870
|
+
const k = pre + n;
|
|
1871
|
+
if (this.lib.index.has(k))
|
|
1872
|
+
return new Uint8Array(fs2.readFileSync(this.lib.index.get(k)));
|
|
1873
|
+
}
|
|
1874
|
+
return null;
|
|
1875
|
+
}
|
|
1876
|
+
texPass(key, out, separateStuds, colorOf) {
|
|
1877
|
+
const tbody = { pos: [], col: [] }, texb = new Map;
|
|
1878
|
+
const walk = (fk, M, color, invert, target, depth, texIn) => {
|
|
1879
|
+
if (depth > 40)
|
|
1880
|
+
return;
|
|
1881
|
+
let ccw = true, invertNext = false;
|
|
1882
|
+
let stack = [];
|
|
1883
|
+
let next = null;
|
|
1884
|
+
for (const raw of this.lib.read(fk).split(/\r?\n/)) {
|
|
1885
|
+
let t = raw.trim();
|
|
1886
|
+
if (!t)
|
|
1887
|
+
continue;
|
|
1888
|
+
let colon = false;
|
|
1889
|
+
if (t[0] === "0") {
|
|
1890
|
+
const m = /^0\s+(\S+)\s*(.*)$/.exec(t);
|
|
1891
|
+
const kw = (m?.[1] ?? "").toUpperCase(), rest = m?.[2] ?? "";
|
|
1892
|
+
if (kw === "!TEXMAP") {
|
|
1893
|
+
const op = (rest.split(/\s+/)[0] || "").toUpperCase();
|
|
1894
|
+
next = null;
|
|
1895
|
+
if (op === "START" || op === "NEXT") {
|
|
1896
|
+
const map = parseTexMap(rest.replace(/^\S+/, ""), op === "NEXT");
|
|
1897
|
+
if (map) {
|
|
1898
|
+
const pm = transformTexMap(map, M);
|
|
1899
|
+
if (op === "START")
|
|
1900
|
+
stack.push({ map: pm, fallback: false });
|
|
1901
|
+
else
|
|
1902
|
+
next = pm;
|
|
1903
|
+
}
|
|
1904
|
+
} else if (op === "FALLBACK") {
|
|
1905
|
+
const top = stack[stack.length - 1];
|
|
1906
|
+
if (top)
|
|
1907
|
+
top.fallback = true;
|
|
1908
|
+
} else if (op === "END")
|
|
1909
|
+
stack.pop();
|
|
1910
|
+
continue;
|
|
1911
|
+
}
|
|
1912
|
+
if (kw === "!:") {
|
|
1913
|
+
next = null;
|
|
1914
|
+
t = rest.trim();
|
|
1915
|
+
colon = true;
|
|
1916
|
+
if (!t || t[0] === "0")
|
|
1917
|
+
continue;
|
|
1918
|
+
} else {
|
|
1919
|
+
if (kw === "BFC") {
|
|
1920
|
+
if (/\bCW\b/.test(t))
|
|
1921
|
+
ccw = false;
|
|
1922
|
+
if (/\bCCW\b/.test(t))
|
|
1923
|
+
ccw = true;
|
|
1924
|
+
if (/INVERTNEXT/.test(t))
|
|
1925
|
+
invertNext = true;
|
|
1926
|
+
}
|
|
1927
|
+
if (kw === "STEP")
|
|
1928
|
+
stack = [];
|
|
1929
|
+
next = null;
|
|
1930
|
+
continue;
|
|
1931
|
+
}
|
|
1932
|
+
}
|
|
1933
|
+
let tex = texIn;
|
|
1934
|
+
if (next) {
|
|
1935
|
+
tex = next;
|
|
1936
|
+
next = null;
|
|
1937
|
+
} else {
|
|
1938
|
+
const top = stack[stack.length - 1];
|
|
1939
|
+
if (top) {
|
|
1940
|
+
if (top.fallback) {
|
|
1941
|
+
if (!colon)
|
|
1942
|
+
continue;
|
|
1943
|
+
} else
|
|
1944
|
+
tex = top.map;
|
|
1945
|
+
}
|
|
1946
|
+
}
|
|
1947
|
+
const c = t[0];
|
|
1948
|
+
if (c === "1") {
|
|
1949
|
+
const r = parseType1(t);
|
|
1950
|
+
if (!r)
|
|
1951
|
+
continue;
|
|
1952
|
+
const base = r.name.trim().replace(/\\/g, "/").split("/").pop().toLowerCase();
|
|
1953
|
+
const isTop = TOP_STUD_RE.test(base), isUnder = UNDER_STUD_RE.test(base);
|
|
1954
|
+
const ck2 = this.resolve(r.name, isTop);
|
|
1955
|
+
if (!ck2) {
|
|
1956
|
+
invertNext = false;
|
|
1957
|
+
continue;
|
|
1958
|
+
}
|
|
1959
|
+
const inv = invert !== invertNext !== det3(r.m) < 0;
|
|
1960
|
+
invertNext = false;
|
|
1961
|
+
if (isTop && separateStuds)
|
|
1962
|
+
continue;
|
|
1963
|
+
walk(ck2, compose(M, r.m), r.color === "16" ? color : r.color, inv, isUnder ? "under" : target, depth + 1, tex);
|
|
1964
|
+
continue;
|
|
1965
|
+
}
|
|
1966
|
+
if (c !== "3" && c !== "4")
|
|
1967
|
+
continue;
|
|
1968
|
+
const tok = t.split(/\s+/);
|
|
1969
|
+
const code = tok[1] === "16" ? color : tok[1];
|
|
1970
|
+
const n = c === "3" ? 3 : 4;
|
|
1971
|
+
const v = tok.slice(2, 2 + n * 3).map(Number);
|
|
1972
|
+
const pts = [];
|
|
1973
|
+
for (let i = 0;i < n; i++)
|
|
1974
|
+
pts.push(xf2(M, v.slice(i * 3, i * 3 + 3)));
|
|
1975
|
+
let order = n === 3 ? [0, 1, 2] : [0, 1, 2, 0, 2, 3];
|
|
1976
|
+
if (!ccw !== invert)
|
|
1977
|
+
order = n === 3 ? [0, 2, 1] : [0, 2, 1, 0, 3, 2];
|
|
1978
|
+
const rgba = colorOf(code);
|
|
1979
|
+
if (tex) {
|
|
1980
|
+
let b = texb.get(tex.texture);
|
|
1981
|
+
if (!b)
|
|
1982
|
+
texb.set(tex.texture, b = { pos: [], col: [], uv: [] });
|
|
1983
|
+
if (Math.abs(tex.a ?? 0) >= 360)
|
|
1984
|
+
b.wrapU = true;
|
|
1985
|
+
for (let k = 0;k < order.length; k += 3) {
|
|
1986
|
+
const tri = [pts[order[k]], pts[order[k + 1]], pts[order[k + 2]]];
|
|
1987
|
+
const w = texmapTriangleUV(tex, tri);
|
|
1988
|
+
for (let j = 0;j < 3; j++) {
|
|
1989
|
+
b.pos.push(...tri[j]);
|
|
1990
|
+
b.col.push(...rgba);
|
|
1991
|
+
b.uv.push(w[j * 2], w[j * 2 + 1]);
|
|
1992
|
+
}
|
|
1993
|
+
}
|
|
1994
|
+
} else if (target === "body")
|
|
1995
|
+
for (const i of order) {
|
|
1996
|
+
tbody.pos.push(...pts[i]);
|
|
1997
|
+
tbody.col.push(...rgba);
|
|
1998
|
+
}
|
|
1999
|
+
}
|
|
2000
|
+
};
|
|
2001
|
+
walk(key, IDENT, "16", false, "body", 0, null);
|
|
2002
|
+
if (texb.size) {
|
|
2003
|
+
out.tbody = tbody;
|
|
2004
|
+
out.texb = texb;
|
|
2005
|
+
}
|
|
2006
|
+
}
|
|
2007
|
+
}
|
|
2008
|
+
function enclosureMask(c) {
|
|
2009
|
+
const [x0, , z0, x1, y1, z1] = c.bbox;
|
|
2010
|
+
const nx = Math.round((x1 - x0) / 20), nz = Math.round((z1 - z0) / 20);
|
|
2011
|
+
if (!nx || !nz || nx * nz > 4096)
|
|
2012
|
+
return;
|
|
2013
|
+
const tris = [];
|
|
2014
|
+
for (const b of [c.body, c.under])
|
|
2015
|
+
for (let i = 0;i < b.pos.length; i += 9) {
|
|
2016
|
+
const t = b.pos.slice(i, i + 9);
|
|
2017
|
+
if (Math.max(t[1], t[4], t[7]) < y1 - 6)
|
|
2018
|
+
continue;
|
|
2019
|
+
tris.push(t);
|
|
2020
|
+
}
|
|
2021
|
+
let yRay = y1 - 2;
|
|
2022
|
+
const bits = [];
|
|
2023
|
+
const BIN = 4;
|
|
2024
|
+
const buckets = [new Map, new Map];
|
|
2025
|
+
for (const t of tris) {
|
|
2026
|
+
if (Math.min(t[1], t[4], t[7]) > y1 - 0.5 || Math.max(t[1], t[4], t[7]) < y1 - 4)
|
|
2027
|
+
continue;
|
|
2028
|
+
for (const [ai, o] of [[0, 2], [1, 0]]) {
|
|
2029
|
+
const lo = Math.floor(Math.min(t[o], t[3 + o], t[6 + o]) / BIN), hi = Math.floor(Math.max(t[o], t[3 + o], t[6 + o]) / BIN);
|
|
2030
|
+
for (let k = lo;k <= hi; k++)
|
|
2031
|
+
(buckets[ai].get(k) || buckets[ai].set(k, []).get(k)).push(t);
|
|
2032
|
+
}
|
|
2033
|
+
}
|
|
2034
|
+
const hit = (px, pz, axis, sign) => {
|
|
2035
|
+
const o = axis === 0 ? 2 : 0;
|
|
2036
|
+
const pv = axis === 0 ? pz : px;
|
|
2037
|
+
for (const t of buckets[axis === 0 ? 0 : 1].get(Math.floor(pv / BIN)) || []) {
|
|
2038
|
+
const a = [t[1], t[o]], b = [t[4], t[3 + o]], d = [t[7], t[6 + o]];
|
|
2039
|
+
const den = (b[1] - d[1]) * (a[0] - d[0]) + (d[0] - b[0]) * (a[1] - d[1]);
|
|
2040
|
+
if (Math.abs(den) < 0.000000001)
|
|
2041
|
+
continue;
|
|
2042
|
+
const y = yRay;
|
|
2043
|
+
const w1 = ((b[1] - d[1]) * (y - d[0]) + (d[0] - b[0]) * (pv - d[1])) / den, w2 = ((d[1] - a[1]) * (y - d[0]) + (a[0] - d[0]) * (pv - d[1])) / den, w3 = 1 - w1 - w2;
|
|
2044
|
+
if (w1 < -0.000001 || w2 < -0.000001 || w3 < -0.000001)
|
|
2045
|
+
continue;
|
|
2046
|
+
const along = w1 * t[axis] + w2 * t[3 + axis] + w3 * t[6 + axis];
|
|
2047
|
+
const start = axis === 0 ? px : pz;
|
|
2048
|
+
if ((along - start) * sign > 0.01)
|
|
2049
|
+
return true;
|
|
2050
|
+
}
|
|
2051
|
+
return false;
|
|
2052
|
+
};
|
|
2053
|
+
for (let a = 0;a < nx; a++)
|
|
2054
|
+
for (let b = 0;b < nz; b++) {
|
|
2055
|
+
const px = x0 + 10 + 20 * a, pz = z0 + 10 + 20 * b;
|
|
2056
|
+
let ok = true;
|
|
2057
|
+
for (const yy of [y1 - 1, y1 - 3.5]) {
|
|
2058
|
+
yRay = yy;
|
|
2059
|
+
for (const [ox, oz] of [[0, 0], [5, 5], [-5, 5], [5, -5], [-5, -5]]) {
|
|
2060
|
+
const qx = px + ox, qz = pz + oz;
|
|
2061
|
+
if (!(hit(qx, qz, 0, 1) && hit(qx, qz, 0, -1) && hit(qx, qz, 2, 1) && hit(qx, qz, 2, -1))) {
|
|
2062
|
+
ok = false;
|
|
2063
|
+
break;
|
|
2064
|
+
}
|
|
2065
|
+
}
|
|
2066
|
+
if (!ok)
|
|
2067
|
+
break;
|
|
2068
|
+
}
|
|
2069
|
+
bits.push(ok ? 1 : 0);
|
|
2070
|
+
}
|
|
2071
|
+
return { nx, nz, bits };
|
|
2072
|
+
}
|
|
2073
|
+
function grow(b, p) {
|
|
2074
|
+
for (let i = 0;i < 3; i++) {
|
|
2075
|
+
if (p[i] < b[i])
|
|
2076
|
+
b[i] = p[i];
|
|
2077
|
+
if (p[i] > b[i + 3])
|
|
2078
|
+
b[i + 3] = p[i];
|
|
2079
|
+
}
|
|
2080
|
+
}
|
|
2081
|
+
function growBox(b, M, cb) {
|
|
2082
|
+
if (!isFinite(cb[0]))
|
|
2083
|
+
return;
|
|
2084
|
+
for (const x of [cb[0], cb[3]])
|
|
2085
|
+
for (const y of [cb[1], cb[4]])
|
|
2086
|
+
for (const z of [cb[2], cb[5]])
|
|
2087
|
+
grow(b, xf2(M, [x, y, z]));
|
|
2088
|
+
}
|
|
2089
|
+
function writePack(file, parts, studKinds, weld, extra, options) {
|
|
2090
|
+
fs2.writeFileSync(file, encodePack(parts, studKinds, weld, extra, options));
|
|
2091
|
+
}
|
|
2092
|
+
function encodePack(parts, studKinds, weld, extra, options = {}) {
|
|
2093
|
+
const chunks = [];
|
|
2094
|
+
let off = 0;
|
|
2095
|
+
const put = (a) => {
|
|
2096
|
+
const pad = (4 - off % 4) % 4;
|
|
2097
|
+
if (pad) {
|
|
2098
|
+
chunks.push(new Uint8Array(pad));
|
|
2099
|
+
off += pad;
|
|
2100
|
+
}
|
|
2101
|
+
const r = [off, a.length, a instanceof Float32Array ? "f32" : a instanceof Uint16Array ? "u16" : a instanceof Uint8Array ? "u8" : "u32"];
|
|
2102
|
+
chunks.push(a);
|
|
2103
|
+
off += a.byteLength;
|
|
2104
|
+
return r;
|
|
2105
|
+
};
|
|
2106
|
+
let lastPos = [];
|
|
2107
|
+
const bucket = (b) => {
|
|
2108
|
+
const n = b.pos.length / 3;
|
|
2109
|
+
lastPos = [];
|
|
2110
|
+
if (!n)
|
|
2111
|
+
return null;
|
|
2112
|
+
const nrm = new Float32Array(n * 3);
|
|
2113
|
+
for (let i = 0;i < n; i += 3) {
|
|
2114
|
+
const p = b.pos;
|
|
2115
|
+
const a = i * 3;
|
|
2116
|
+
const ux = p[a + 3] - p[a], uy = p[a + 4] - p[a + 1], uz = p[a + 5] - p[a + 2], vx = p[a + 6] - p[a], vy = p[a + 7] - p[a + 1], vz = p[a + 8] - p[a + 2];
|
|
2117
|
+
let nx = uy * vz - uz * vy, ny = uz * vx - ux * vz, nz = ux * vy - uy * vx;
|
|
2118
|
+
const l = Math.hypot(nx, ny, nz) || 1;
|
|
2119
|
+
nx /= l;
|
|
2120
|
+
ny /= l;
|
|
2121
|
+
nz /= l;
|
|
2122
|
+
for (let k = 0;k < 3; k++) {
|
|
2123
|
+
nrm[(i + k) * 3] = nx;
|
|
2124
|
+
nrm[(i + k) * 3 + 1] = ny;
|
|
2125
|
+
nrm[(i + k) * 3 + 2] = nz;
|
|
2126
|
+
}
|
|
2127
|
+
}
|
|
2128
|
+
if (!weld) {
|
|
2129
|
+
lastPos = b.pos;
|
|
2130
|
+
return { pos: put(new Float32Array(b.pos)), nrm: put(nrm), col: put(new Float32Array(b.col)), tris: n / 3 };
|
|
2131
|
+
}
|
|
2132
|
+
const map = new Map;
|
|
2133
|
+
const P = [], N = [], C = [], I = [];
|
|
2134
|
+
for (let i = 0;i < n; i++) {
|
|
2135
|
+
const k = `${b.pos[i * 3].toFixed(3)},${b.pos[i * 3 + 1].toFixed(3)},${b.pos[i * 3 + 2].toFixed(3)},${nrm[i * 3].toFixed(3)},${nrm[i * 3 + 1].toFixed(3)},${nrm[i * 3 + 2].toFixed(3)},${b.col.slice(i * 4, i * 4 + 4).join()}`;
|
|
2136
|
+
let j = map.get(k);
|
|
2137
|
+
if (j === undefined) {
|
|
2138
|
+
j = P.length / 3;
|
|
2139
|
+
map.set(k, j);
|
|
2140
|
+
P.push(b.pos[i * 3], b.pos[i * 3 + 1], b.pos[i * 3 + 2]);
|
|
2141
|
+
N.push(nrm[i * 3], nrm[i * 3 + 1], nrm[i * 3 + 2]);
|
|
2142
|
+
C.push(...b.col.slice(i * 4, i * 4 + 4));
|
|
2143
|
+
}
|
|
2144
|
+
I.push(j);
|
|
2145
|
+
}
|
|
2146
|
+
const vcount = P.length / 3;
|
|
2147
|
+
lastPos = P;
|
|
2148
|
+
return { pos: put(new Float32Array(P)), nrm: put(new Float32Array(N)), col: put(new Float32Array(C)), idx: put(vcount < 65536 ? new Uint16Array(I) : new Uint32Array(I)), tris: n / 3 };
|
|
2149
|
+
};
|
|
2150
|
+
let anyCond = false;
|
|
2151
|
+
const condl = (a, pools) => {
|
|
2152
|
+
if (!a || !a.length)
|
|
2153
|
+
return;
|
|
2154
|
+
anyCond = true;
|
|
2155
|
+
const key = (p, i) => Math.round(p[i] * 1000) + "," + Math.round(p[i + 1] * 1000) + "," + Math.round(p[i + 2] * 1000);
|
|
2156
|
+
const need = new Map;
|
|
2157
|
+
for (let i = 0;i < a.length; i += 3)
|
|
2158
|
+
need.set(key(a, i), -1);
|
|
2159
|
+
let base = 0;
|
|
2160
|
+
for (const p of pools) {
|
|
2161
|
+
for (let i = 0;i < p.length; i += 3) {
|
|
2162
|
+
const k = key(p, i);
|
|
2163
|
+
if (need.get(k) === -1)
|
|
2164
|
+
need.set(k, base + i / 3);
|
|
2165
|
+
}
|
|
2166
|
+
base += p.length / 3;
|
|
2167
|
+
}
|
|
2168
|
+
const X = [], I = new Array(a.length / 3);
|
|
2169
|
+
for (let i = 0;i < a.length; i += 3) {
|
|
2170
|
+
const k = key(a, i);
|
|
2171
|
+
let j = need.get(k);
|
|
2172
|
+
if (j === -1) {
|
|
2173
|
+
j = base + X.length / 3;
|
|
2174
|
+
need.set(k, j);
|
|
2175
|
+
X.push(a[i], a[i + 1], a[i + 2]);
|
|
2176
|
+
}
|
|
2177
|
+
I[i / 3] = j;
|
|
2178
|
+
}
|
|
2179
|
+
let prev = 0, max = 0;
|
|
2180
|
+
for (let i = 0;i < I.length; i++) {
|
|
2181
|
+
const d = I[i] - prev;
|
|
2182
|
+
prev = I[i];
|
|
2183
|
+
const z = d >= 0 ? d * 2 : -d * 2 - 1;
|
|
2184
|
+
I[i] = z;
|
|
2185
|
+
if (z > max)
|
|
2186
|
+
max = z;
|
|
2187
|
+
}
|
|
2188
|
+
return { idx: put(max < 65536 ? new Uint16Array(I) : new Uint32Array(I)), ...X.length ? { x: put(new Float32Array(X)) } : {} };
|
|
2189
|
+
};
|
|
2190
|
+
const texNames = [];
|
|
2191
|
+
const texBucket = (b) => {
|
|
2192
|
+
const n = b.pos.length / 3;
|
|
2193
|
+
if (!n)
|
|
2194
|
+
return null;
|
|
2195
|
+
const nrm = new Float32Array(n * 3);
|
|
2196
|
+
for (let i = 0;i < n; i += 3) {
|
|
2197
|
+
const p = b.pos;
|
|
2198
|
+
const a = i * 3;
|
|
2199
|
+
const ux = p[a + 3] - p[a], uy = p[a + 4] - p[a + 1], uz = p[a + 5] - p[a + 2], vx = p[a + 6] - p[a], vy = p[a + 7] - p[a + 1], vz = p[a + 8] - p[a + 2];
|
|
2200
|
+
let nx = uy * vz - uz * vy, ny = uz * vx - ux * vz, nz = ux * vy - uy * vx;
|
|
2201
|
+
const l = Math.hypot(nx, ny, nz) || 1;
|
|
2202
|
+
nx /= l;
|
|
2203
|
+
ny /= l;
|
|
2204
|
+
nz /= l;
|
|
2205
|
+
for (let k = 0;k < 3; k++) {
|
|
2206
|
+
nrm[(i + k) * 3] = nx;
|
|
2207
|
+
nrm[(i + k) * 3 + 1] = ny;
|
|
2208
|
+
nrm[(i + k) * 3 + 2] = nz;
|
|
2209
|
+
}
|
|
2210
|
+
}
|
|
2211
|
+
const map = new Map;
|
|
2212
|
+
const P = [], N = [], C = [], U = [], I = [];
|
|
2213
|
+
for (let i = 0;i < n; i++) {
|
|
2214
|
+
const k = weld ? `${b.pos.slice(i * 3, i * 3 + 3).map((x) => x.toFixed(3))},${[...nrm.subarray(i * 3, i * 3 + 3)].map((x) => x.toFixed(3))},${b.col.slice(i * 4, i * 4 + 4).join()},${b.uv[i * 2].toFixed(5)},${b.uv[i * 2 + 1].toFixed(5)}` : String(i);
|
|
2215
|
+
let j = map.get(k);
|
|
2216
|
+
if (j === undefined) {
|
|
2217
|
+
j = P.length / 3;
|
|
2218
|
+
map.set(k, j);
|
|
2219
|
+
P.push(b.pos[i * 3], b.pos[i * 3 + 1], b.pos[i * 3 + 2]);
|
|
2220
|
+
N.push(nrm[i * 3], nrm[i * 3 + 1], nrm[i * 3 + 2]);
|
|
2221
|
+
C.push(...b.col.slice(i * 4, i * 4 + 4));
|
|
2222
|
+
U.push(b.uv[i * 2], b.uv[i * 2 + 1]);
|
|
2223
|
+
}
|
|
2224
|
+
I.push(j);
|
|
2225
|
+
}
|
|
2226
|
+
return { pos: put(new Float32Array(P)), nrm: put(new Float32Array(N)), col: put(new Float32Array(C)), uv: put(new Float32Array(U)), idx: put(P.length / 3 < 65536 ? new Uint16Array(I) : new Uint32Array(I)), tris: n / 3 };
|
|
2227
|
+
};
|
|
2228
|
+
const encTex = (c) => {
|
|
2229
|
+
if (!c.texb)
|
|
2230
|
+
return {};
|
|
2231
|
+
const tex = [...c.texb].map(([name, b]) => {
|
|
2232
|
+
let t = texNames.indexOf(name);
|
|
2233
|
+
if (t < 0)
|
|
2234
|
+
t = texNames.push(name) - 1;
|
|
2235
|
+
return { t, ...b.wrapU ? { wrapU: true } : {}, ...texBucket(b) };
|
|
2236
|
+
});
|
|
2237
|
+
return { tbody: bucket(c.tbody), tex };
|
|
2238
|
+
};
|
|
2239
|
+
const enc = (c) => {
|
|
2240
|
+
const body = bucket(c.body), bodyP = lastPos, under = bucket(c.under), underP = lastPos;
|
|
2241
|
+
const edges = c.edges.length ? put(new Float32Array(c.edges)) : null;
|
|
2242
|
+
const condlB = condl(c.cond, [bodyP, underP, c.edges]), condlU = condl(c.condUnder, [bodyP, underP, c.edges]);
|
|
2243
|
+
return { body, under, edges, cond: c.condLines, condl: condlB, condlu: condlU, studs: c.studs.map((s) => [s.kind, s.color, ...s.m.map((x) => +x.toFixed(4))]), bbox: c.bbox, fullBottom: c.fullBottom, uncert: !!c.uncertified, mask: c.bottomMask ? [c.bottomMask.nx, c.bottomMask.nz, c.bottomMask.bits.join("")] : null, ...encTex(c) };
|
|
2244
|
+
};
|
|
2245
|
+
const encParts = Object.fromEntries([...parts].map(([k, c]) => [k, enc(c)])), encKinds = Object.fromEntries([...studKinds].map(([k, c]) => [k, enc(c)]));
|
|
2246
|
+
const textures = texNames.map((name) => {
|
|
2247
|
+
const b = options.texture?.(name);
|
|
2248
|
+
return b ? { name, mime: "image/png", data: put(b) } : { name, missing: true };
|
|
2249
|
+
});
|
|
2250
|
+
const features = [...anyCond ? ["cond"] : [], ...texNames.length ? ["tex"] : []];
|
|
2251
|
+
const header = { version: features.length ? 2 : 1, ...features.length ? { features } : {}, weld, parts: encParts, studKinds: encKinds, ...texNames.length ? { textures } : {}, ...extra };
|
|
2252
|
+
const hj = new TextEncoder().encode(JSON.stringify(header));
|
|
2253
|
+
const hpad = (4 - (4 + hj.length) % 4) % 4;
|
|
2254
|
+
const head = new Uint8Array(4 + hj.length + hpad);
|
|
2255
|
+
new DataView(head.buffer).setUint32(0, hj.length + hpad, true);
|
|
2256
|
+
head.set(hj, 4);
|
|
2257
|
+
head.fill(32, 4 + hj.length);
|
|
2258
|
+
return Buffer.concat([head, ...chunks.map((c) => new Uint8Array(c.buffer, c.byteOffset, c.byteLength))]);
|
|
2259
|
+
}
|
|
2260
|
+
|
|
2261
|
+
// src/node/build-pack.ts
|
|
2262
|
+
function buildPack(o) {
|
|
2263
|
+
const lib = new Library2(o.ldrawRoot);
|
|
2264
|
+
const refs = new Map;
|
|
2265
|
+
const missing = new Set;
|
|
2266
|
+
const data = new Map;
|
|
2267
|
+
for (const m of o.models ?? []) {
|
|
2268
|
+
const { name, text } = typeof m === "string" ? { name: path3.basename(m), text: fs3.readFileSync(m, "latin1") } : m;
|
|
2269
|
+
const sm = splitModel(text, name);
|
|
2270
|
+
for (const [k, v] of sm.data)
|
|
2271
|
+
if (!data.has(k))
|
|
2272
|
+
data.set(k, v);
|
|
2273
|
+
const r = flatten2(sm, lib, (i) => refs.set(i.ref, i.part));
|
|
2274
|
+
for (const k of r.missing.keys())
|
|
2275
|
+
missing.add(k);
|
|
2276
|
+
}
|
|
2277
|
+
for (const p of o.parts ?? []) {
|
|
2278
|
+
const k = lib.resolve(p);
|
|
2279
|
+
if (k)
|
|
2280
|
+
refs.set(norm2(p), k);
|
|
2281
|
+
else
|
|
2282
|
+
missing.add(norm2(p));
|
|
2283
|
+
}
|
|
2284
|
+
if (o.allParts) {
|
|
2285
|
+
for (const k of lib.index.keys())
|
|
2286
|
+
if (lib.isPart(k) && /\.dat$/.test(k))
|
|
2287
|
+
refs.set(k.slice(6), k);
|
|
2288
|
+
}
|
|
2289
|
+
const comp = new Compiler(lib, { studs: o.studs ?? "low", prims: o.primitives ?? "full", weld: o.weld ?? true, separateStuds: true, cond: !!o.cond, tex: !!o.textures });
|
|
2290
|
+
const parts = new Map, kinds = new Map;
|
|
2291
|
+
let n = 0;
|
|
2292
|
+
for (const [ref, key] of refs) {
|
|
2293
|
+
try {
|
|
2294
|
+
const c = comp.compile(key);
|
|
2295
|
+
parts.set(ref, c);
|
|
2296
|
+
for (const s of c.studs)
|
|
2297
|
+
if (!kinds.has(s.kind))
|
|
2298
|
+
kinds.set(s.kind, comp.compile(s.kind, false));
|
|
2299
|
+
} catch {
|
|
2300
|
+
missing.add(ref);
|
|
2301
|
+
}
|
|
2302
|
+
o.onProgress?.(++n, refs.size);
|
|
2303
|
+
}
|
|
2304
|
+
let extra;
|
|
2305
|
+
if (o.colors !== false) {
|
|
2306
|
+
const cfg = path3.join(o.ldrawRoot, "LDConfig.ldr");
|
|
2307
|
+
if (fs3.existsSync(cfg))
|
|
2308
|
+
extra = { colors: Object.fromEntries([...parseLDConfig(fs3.readFileSync(cfg, "latin1"))].map(([k, v]) => [k, v.hex])) };
|
|
2309
|
+
}
|
|
2310
|
+
const texture = (n) => {
|
|
2311
|
+
const k = norm2(n), b = data.get(k) ?? data.get(k.replace(/^textures\//, "")) ?? data.get("textures/" + k);
|
|
2312
|
+
return b ? new Uint8Array(Buffer.from(b, "base64")) : comp.texture(n);
|
|
2313
|
+
};
|
|
2314
|
+
return { data: encodePack(parts, kinds, comp.opts.weld, extra, { texture }), parts: parts.size, studKinds: kinds.size, missing: [...missing] };
|
|
2315
|
+
}
|
|
2316
|
+
export {
|
|
2317
|
+
Compiler,
|
|
2318
|
+
I as IDENTITY,
|
|
2319
|
+
Library,
|
|
2320
|
+
Library2 as ResolverLibrary,
|
|
2321
|
+
buildLdrScene,
|
|
2322
|
+
buildPack,
|
|
2323
|
+
collectDeps,
|
|
2324
|
+
computeCulling,
|
|
2325
|
+
conditionalLineVisible,
|
|
2326
|
+
conditionalLines,
|
|
2327
|
+
createInstancedConditionalEdges,
|
|
2328
|
+
createInstancedEdges,
|
|
2329
|
+
createLdrObject,
|
|
2330
|
+
decodePack,
|
|
2331
|
+
decompressPack,
|
|
2332
|
+
encodePack,
|
|
2333
|
+
flatten,
|
|
2334
|
+
flatten2 as flattenWithLibrary,
|
|
2335
|
+
isDirectColor,
|
|
2336
|
+
loadColors,
|
|
2337
|
+
loadPack,
|
|
2338
|
+
makeMaterial,
|
|
2339
|
+
mul,
|
|
2340
|
+
norm,
|
|
2341
|
+
parse,
|
|
2342
|
+
parseColor,
|
|
2343
|
+
parseLDConfig,
|
|
2344
|
+
parseModel,
|
|
2345
|
+
parseTexMap,
|
|
2346
|
+
resolveTexture,
|
|
2347
|
+
sequenceSteps,
|
|
2348
|
+
smoothNormals,
|
|
2349
|
+
splitModel,
|
|
2350
|
+
texmapTriangleUV,
|
|
2351
|
+
texmapUV,
|
|
2352
|
+
transformTexMap,
|
|
2353
|
+
writePack
|
|
2354
|
+
};
|