@chanmeng666/archlang-3d 0.1.2 → 0.2.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.
@@ -1,14 +1,20 @@
1
1
  import {
2
2
  ARCHLANG_3D_VERSION,
3
3
  validateScene3D
4
- } from "./chunk-PJMKP2ML.js";
4
+ } from "./chunk-VUSBWX7N.js";
5
5
  import {
6
6
  PREVIEW_LABEL,
7
7
  replayPayload
8
- } from "./chunk-54YVSAVZ.js";
8
+ } from "./chunk-2ZINQMUB.js";
9
9
  import {
10
- keyFrames
11
- } from "./chunk-CTFMM64C.js";
10
+ FINISH_PALETTE,
11
+ FLOOR_FINISHES,
12
+ KIND_PALETTE,
13
+ PALETTE,
14
+ keyFrames,
15
+ linearRgb,
16
+ slotGroups
17
+ } from "./chunk-EYEEIS4R.js";
12
18
 
13
19
  // src/node.ts
14
20
  import { readFileSync as readFileSync2 } from "node:fs";
@@ -239,18 +245,16 @@ async function renderReplay(o) {
239
245
  // src/glb/write.ts
240
246
  import { Document, Logger, WebIO } from "@gltf-transform/core";
241
247
  var GLB_GENERATOR = `archlang-3d ${ARCHLANG_3D_VERSION}`;
242
- var COLOUR = {
243
- slab: [0.55, 0.55, 0.55, 1],
244
- floor: [0.8, 0.75, 0.66, 1],
245
- column: [0.7, 0.7, 0.7, 1],
246
- wall: [0.92, 0.91, 0.88, 1],
247
- door: [0.55, 0.4, 0.28, 1],
248
- window: [0.6, 0.75, 0.85, 1],
249
- opening: [0.6, 0.6, 0.6, 1],
250
- furniture: [0.62, 0.64, 0.66, 1],
251
- roof: [0.45, 0.42, 0.4, 1]
252
- };
253
- var DEMOLISH_COLOUR = [0.78, 0.33, 0.24, 0.3];
248
+ function bodyMaterialName(n) {
249
+ if (n.kind === "floor") return `floor:${FLOOR_FINISHES.find((f) => f === n.extras.finish) ?? "neutral"}`;
250
+ if (n.kind === "wall" && n.part === "demolish") return "wall:demolish";
251
+ if (n.kind === "window" && (n.part === "glass" || n.part === "frame")) return `window:${n.part}`;
252
+ return n.kind;
253
+ }
254
+ function bodyMaterial(name) {
255
+ if (name.startsWith("floor:")) return FINISH_PALETTE[name.slice("floor:".length)];
256
+ return KIND_PALETTE[name] ?? KIND_PALETTE.furniture;
257
+ }
254
258
  var glbNodeName = (n) => n.key.slice(n.key.indexOf("/") + 1);
255
259
  var levelNodeName = (level) => `level:L${level}`;
256
260
  function nodeExtras(n) {
@@ -276,12 +280,23 @@ async function writeGlb(mesh, opts = {}) {
276
280
  doc.getRoot().setDefaultScene(scene);
277
281
  const materials = /* @__PURE__ */ new Map();
278
282
  const material = (n) => {
279
- const ghost = n.kind === "wall" && n.part === "demolish";
280
- const key = ghost ? "wall:demolish" : n.kind;
283
+ const key = bodyMaterialName(n);
284
+ let m = materials.get(key);
285
+ if (!m) {
286
+ const p = bodyMaterial(key);
287
+ m = doc.createMaterial(key).setBaseColorFactor([...linearRgb(p.srgb), p.alpha]).setMetallicFactor(p.metallic).setRoughnessFactor(p.roughness);
288
+ if (p.alpha < 1) m.setAlphaMode("BLEND");
289
+ materials.set(key, m);
290
+ }
291
+ return m;
292
+ };
293
+ const slotMaterial = (slot) => {
294
+ const key = `fixture:${slot}`;
281
295
  let m = materials.get(key);
282
296
  if (!m) {
283
- m = doc.createMaterial(key).setBaseColorFactor(ghost ? DEMOLISH_COLOUR : COLOUR[n.kind]).setMetallicFactor(0).setRoughnessFactor(1);
284
- if (ghost) m.setAlphaMode("BLEND");
297
+ const p = PALETTE[slot];
298
+ m = doc.createMaterial(key).setBaseColorFactor([...linearRgb(p.srgb), p.alpha]).setMetallicFactor(p.metallic).setRoughnessFactor(p.roughness);
299
+ if (p.alpha < 1) m.setAlphaMode("BLEND");
285
300
  materials.set(key, m);
286
301
  }
287
302
  return m;
@@ -294,19 +309,23 @@ async function writeGlb(mesh, opts = {}) {
294
309
  scene.addChild(group);
295
310
  levels.set(n.level, group);
296
311
  }
297
- const positions = [];
298
- const indices = [];
299
- for (const s of n.shells) {
300
- const base = positions.length / 3;
301
- for (const v of s.positions) positions.push(v / 1e6);
302
- for (const i of s.indices) indices.push(base + i);
303
- }
304
- const count = positions.length / 3;
305
- const pos = doc.createAccessor().setType("VEC3").setArray(new Float32Array(positions)).setBuffer(buffer);
306
- const idx = doc.createAccessor().setType("SCALAR").setArray(count > 65535 ? new Uint32Array(indices) : new Uint16Array(indices)).setBuffer(buffer);
307
- const prim = doc.createPrimitive().setAttribute("POSITION", pos).setIndices(idx).setMaterial(material(n));
308
312
  const name = glbNodeName(n);
309
- const node = doc.createNode(name).setMesh(doc.createMesh(name).addPrimitive(prim)).setExtras({ archlang: nodeExtras(n) });
313
+ const gmesh = doc.createMesh(name);
314
+ for (const g of slotGroups(n.shells)) {
315
+ const positions = [];
316
+ const indices = [];
317
+ for (const s of g.shells) {
318
+ const base = positions.length / 3;
319
+ for (const v of s.positions) positions.push(v / 1e6);
320
+ for (const i of s.indices) indices.push(base + i);
321
+ }
322
+ const count = positions.length / 3;
323
+ const pos = doc.createAccessor().setType("VEC3").setArray(new Float32Array(positions)).setBuffer(buffer);
324
+ const idx = doc.createAccessor().setType("SCALAR").setArray(count > 65535 ? new Uint32Array(indices) : new Uint16Array(indices)).setBuffer(buffer);
325
+ const mat = g.slot === "body" ? material(n) : slotMaterial(g.slot);
326
+ gmesh.addPrimitive(doc.createPrimitive().setAttribute("POSITION", pos).setIndices(idx).setMaterial(mat));
327
+ }
328
+ const node = doc.createNode(name).setMesh(gmesh).setExtras({ archlang: nodeExtras(n) });
310
329
  group.addChild(node);
311
330
  }
312
331
  return new WebIO().writeBinary(doc);
@@ -477,6 +496,7 @@ export {
477
496
  SWIFTSHADER_ARGS,
478
497
  renderReplay,
479
498
  GLB_GENERATOR,
499
+ bodyMaterialName,
480
500
  glbNodeName,
481
501
  levelNodeName,
482
502
  nodeExtras,