@inweb/viewer-three 26.5.0 → 26.5.1

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.
Files changed (53) hide show
  1. package/dist/plugins/components/AxesHelperComponent.js +65 -0
  2. package/dist/plugins/components/AxesHelperComponent.js.map +1 -0
  3. package/dist/plugins/components/AxesHelperComponent.min.js +1 -0
  4. package/dist/plugins/components/AxesHelperComponent.module.js +39 -0
  5. package/dist/plugins/components/AxesHelperComponent.module.js.map +1 -0
  6. package/dist/plugins/components/ExtentsHelperComponent.js +55 -0
  7. package/dist/plugins/components/ExtentsHelperComponent.js.map +1 -0
  8. package/dist/plugins/components/ExtentsHelperComponent.min.js +1 -0
  9. package/dist/plugins/components/ExtentsHelperComponent.module.js +29 -0
  10. package/dist/plugins/components/ExtentsHelperComponent.module.js.map +1 -0
  11. package/dist/plugins/components/LightHelperComponent.js +65 -0
  12. package/dist/plugins/components/LightHelperComponent.js.map +1 -0
  13. package/dist/plugins/components/LightHelperComponent.min.js +1 -0
  14. package/dist/plugins/components/LightHelperComponent.module.js +40 -0
  15. package/dist/plugins/components/LightHelperComponent.module.js.map +1 -0
  16. package/dist/plugins/loaders/IFCXLoader.js +887 -0
  17. package/dist/plugins/loaders/IFCXLoader.js.map +1 -0
  18. package/dist/plugins/loaders/IFCXLoader.min.js +1 -0
  19. package/dist/plugins/loaders/IFCXLoader.module.js +726 -0
  20. package/dist/plugins/loaders/IFCXLoader.module.js.map +1 -0
  21. package/dist/viewer-three.js +49075 -32407
  22. package/dist/viewer-three.js.map +1 -1
  23. package/dist/viewer-three.min.js +2 -7
  24. package/dist/viewer-three.module.js +192 -86
  25. package/dist/viewer-three.module.js.map +1 -1
  26. package/lib/Viewer/Viewer.d.ts +51 -68
  27. package/lib/Viewer/loaders/GLTFFileLoader.d.ts +9 -0
  28. package/lib/Viewer/loaders/GLTFLoadingManager.d.ts +9 -3
  29. package/lib/Viewer/loaders/GLTFModelLoader.d.ts +8 -0
  30. package/lib/Viewer/loaders/index.d.ts +67 -0
  31. package/lib/index-umd.d.ts +1 -0
  32. package/lib/index.d.ts +6 -4
  33. package/package.json +10 -7
  34. package/{src/Viewer → plugins}/components/AxesHelperComponent.ts +4 -4
  35. package/{src/Viewer → plugins}/components/ExtentsHelperComponent.ts +4 -4
  36. package/{src/Viewer → plugins}/components/LightHelperComponent.ts +4 -4
  37. package/plugins/loaders/IFCX/IFCXLoader.ts +71 -0
  38. package/plugins/loaders/IFCX/render.js +701 -0
  39. package/plugins/loaders/IFCXFileLoader.ts +76 -0
  40. package/plugins/loaders/IFCXLoader.ts +30 -0
  41. package/plugins/loaders/IFCXModelLoader.ts +75 -0
  42. package/src/Viewer/Viewer.ts +101 -148
  43. package/src/Viewer/commands/Explode.ts +2 -2
  44. package/src/Viewer/components/index.ts +2 -8
  45. package/src/Viewer/loaders/GLTFFileLoader.ts +73 -0
  46. package/src/Viewer/loaders/GLTFLoadingManager.ts +16 -8
  47. package/src/Viewer/loaders/GLTFModelLoader.ts +74 -0
  48. package/src/Viewer/loaders/index.ts +99 -0
  49. package/src/index-umd.ts +30 -0
  50. package/src/index.ts +9 -5
  51. package/lib/Viewer/components/AxesHelperComponent.d.ts +0 -10
  52. package/lib/Viewer/components/ExtentsHelperComponent.d.ts +0 -9
  53. package/lib/Viewer/components/LightHelperComponent.d.ts +0 -9
@@ -0,0 +1,701 @@
1
+ ///////////////////////////////////////////////////////////////////////////////
2
+ // Copyright (C) 2002-2025, Open Design Alliance (the "Alliance").
3
+ // All rights reserved.
4
+ //
5
+ // This software and its documentation and related materials are owned by
6
+ // the Alliance. The software may only be incorporated into application
7
+ // programs owned by members of the Alliance, subject to a signed
8
+ // Membership Agreement and Supplemental Software License Agreement with the
9
+ // Alliance. The structure and organization of this software are the valuable
10
+ // trade secrets of the Alliance and its suppliers. The software is also
11
+ // protected by copyright law and international treaty provisions. Application
12
+ // programs incorporating this software must include the following statement
13
+ // with their copyright notices:
14
+ //
15
+ // This application incorporates Open Design Alliance software pursuant to a
16
+ // license agreement with Open Design Alliance.
17
+ // Open Design Alliance Copyright (C) 2002-2025 by Open Design Alliance.
18
+ // All rights reserved.
19
+ //
20
+ // By use of this software, its documentation or related materials, you
21
+ // acknowledge and accept the above terms.
22
+ ///////////////////////////////////////////////////////////////////////////////
23
+
24
+ // https://github.com/buildingSMART/IFC5-development/docs/viewer/render.mjs
25
+ // Commit Date: 17.04.2025 11:13:04
26
+ // Commit SHA-1: 0a351ab2efe7deabbdd131c338a08d5b5c38179c
27
+
28
+ // (C) buildingSMART International
29
+ // published under MIT license
30
+
31
+ /* eslint-disable prefer-const */
32
+ /* eslint-disable no-useless-catch */
33
+
34
+ import {
35
+ Box3,
36
+ BufferAttribute,
37
+ BufferGeometry,
38
+ Color,
39
+ Group,
40
+ Line,
41
+ LineBasicMaterial,
42
+ Matrix4,
43
+ Mesh,
44
+ MeshBasicMaterial,
45
+ PerspectiveCamera,
46
+ Scene,
47
+ Vector3,
48
+ } from "three";
49
+
50
+ const THREE = {
51
+ Box3,
52
+ BufferAttribute,
53
+ BufferGeometry,
54
+ Color,
55
+ Group,
56
+ Line,
57
+ LineBasicMaterial,
58
+ Matrix4,
59
+ Mesh,
60
+ MeshBasicMaterial,
61
+ PerspectiveCamera,
62
+ Scene,
63
+ Vector3,
64
+ };
65
+
66
+ // composed-object.ts
67
+ function getChildByName(root, childName, skip = 0) {
68
+ let fragments = childName.replace(/^<\/|^\/|>$/g, "").split("/");
69
+ for (let i = 0; i < skip; ++i) {
70
+ fragments.shift();
71
+ }
72
+ let start = root;
73
+ while (fragments.length && start && start.children) {
74
+ // console.log(start, fragments[0]);
75
+ let f = fragments.shift();
76
+ start = start.children.find((i) => i.name.split("/").reverse()[0] === f);
77
+ }
78
+ if (fragments.length == 0) {
79
+ return start;
80
+ }
81
+ }
82
+
83
+ // compose-alpha.ts
84
+ function GetNode(node, path) {
85
+ if (path === "") return node;
86
+ let parts = path.split("/");
87
+ let child = node.children.get(parts[0]);
88
+ if (child) {
89
+ if (parts.length === 1) {
90
+ return child;
91
+ }
92
+ return GetNode(child, GetTail(path));
93
+ } else {
94
+ return null;
95
+ }
96
+ }
97
+ function GetHead(path) {
98
+ return path.split("/")[0];
99
+ }
100
+ function GetTail(path) {
101
+ let parts = path.split("/");
102
+ parts.shift();
103
+ return parts.join("/");
104
+ }
105
+ function MakeNode(node) {
106
+ return {
107
+ node,
108
+ children: /* @__PURE__ */ new Map(),
109
+ attributes: /* @__PURE__ */ new Map(),
110
+ };
111
+ }
112
+ function ConvertToCompositionNode(path, inputNodes) {
113
+ let compositionNode = {
114
+ path,
115
+ children: {},
116
+ inherits: {},
117
+ attributes: {},
118
+ };
119
+ inputNodes.forEach((node) => {
120
+ Object.keys(node.children).forEach((childName) => {
121
+ compositionNode.children[childName] = node.children[childName];
122
+ });
123
+ Object.keys(node.inherits).forEach((inheritName) => {
124
+ let ih = node.inherits[inheritName];
125
+ if (ih === null) {
126
+ delete compositionNode.inherits[inheritName];
127
+ } else {
128
+ compositionNode.inherits[inheritName] = ih;
129
+ }
130
+ });
131
+ Object.keys(node.attributes).forEach((attrName) => {
132
+ compositionNode.attributes[attrName] = node.attributes[attrName];
133
+ });
134
+ });
135
+ return compositionNode;
136
+ }
137
+ function MMSet(map, key, value) {
138
+ if (map.has(key)) {
139
+ map.get(key)?.push(value);
140
+ } else {
141
+ map.set(key, [value]);
142
+ }
143
+ }
144
+ function FindRootsOrCycles(nodes) {
145
+ let dependencies = /* @__PURE__ */ new Map();
146
+ let dependents = /* @__PURE__ */ new Map();
147
+ nodes.forEach((node, path) => {
148
+ Object.keys(node.inherits).forEach((inheritName) => {
149
+ MMSet(dependencies, path, node.inherits[inheritName]);
150
+ MMSet(dependents, node.inherits[inheritName], path);
151
+ });
152
+ Object.keys(node.children).forEach((childName) => {
153
+ MMSet(dependencies, path, node.children[childName]);
154
+ MMSet(dependents, node.children[childName], path);
155
+ });
156
+ });
157
+ let paths = [...nodes.keys()];
158
+ let perm = {};
159
+ let temp = {};
160
+ function visit(path) {
161
+ if (perm[path]) return;
162
+ if (temp[path]) throw new Error(`CYCLE!`);
163
+ temp[path] = true;
164
+ let deps = dependencies.get(path);
165
+ if (deps) {
166
+ deps.forEach((dep) => visit(dep));
167
+ }
168
+ perm[path] = true;
169
+ }
170
+ let roots = /* @__PURE__ */ new Set();
171
+ try {
172
+ paths.forEach((path) => {
173
+ if (!dependents.has(path) && path.indexOf("/") === -1) {
174
+ roots.add(path);
175
+ }
176
+ visit(path);
177
+ });
178
+ } catch {
179
+ return null;
180
+ }
181
+ return roots;
182
+ }
183
+ function ConvertNodes(input) {
184
+ let compositionNodes = /* @__PURE__ */ new Map();
185
+ for (let [path, inputNodes] of input) {
186
+ compositionNodes.set(path, ConvertToCompositionNode(path, inputNodes));
187
+ }
188
+ return compositionNodes;
189
+ }
190
+ var CycleError = class extends Error {};
191
+ function ExpandFirstRootInInput(nodes) {
192
+ let roots = FindRootsOrCycles(nodes);
193
+ if (!roots) {
194
+ throw new CycleError();
195
+ }
196
+ return ExpandNewNode([...roots.values()][0], nodes);
197
+ }
198
+ function CreateArtificialRoot(nodes) {
199
+ let roots = FindRootsOrCycles(nodes);
200
+ if (!roots) {
201
+ throw new CycleError();
202
+ }
203
+ let pseudoRoot = {
204
+ node: "",
205
+ attributes: /* @__PURE__ */ new Map(),
206
+ children: /* @__PURE__ */ new Map(),
207
+ };
208
+ roots.forEach((root) => {
209
+ pseudoRoot.children.set(root, ExpandNewNode(root, nodes));
210
+ });
211
+ return pseudoRoot;
212
+ }
213
+ function ExpandNewNode(node, nodes) {
214
+ return ExpandNode(node, MakeNode(node), nodes);
215
+ }
216
+ function ExpandNode(path, node, nodes) {
217
+ let input = nodes.get(path);
218
+ if (input) {
219
+ AddDataFromInput(input, node, nodes);
220
+ }
221
+ node.children.forEach((child, name) => {
222
+ ExpandNode(`${path}/${name}`, child, nodes);
223
+ });
224
+ return node;
225
+ }
226
+ function AddDataFromInput(input, node, nodes) {
227
+ Object.values(input.inherits).forEach((inherit) => {
228
+ let classNode = ExpandNewNode(GetHead(inherit), nodes);
229
+ let subnode = GetNode(classNode, GetTail(inherit));
230
+ if (!subnode) throw new Error(`Unknown node ${inherit}`);
231
+ subnode.children.forEach((child, childName) => {
232
+ node.children.set(childName, child);
233
+ });
234
+ for (let [attrID, attr] of subnode.attributes) {
235
+ node.attributes.set(attrID, attr);
236
+ }
237
+ });
238
+ Object.entries(input.children).forEach(([childName, child]) => {
239
+ if (child !== null) {
240
+ let classNode = ExpandNewNode(GetHead(child), nodes);
241
+ let subnode = GetNode(classNode, GetTail(child));
242
+ if (!subnode) throw new Error(`Unknown node ${child}`);
243
+ node.children.set(childName, subnode);
244
+ } else {
245
+ node.children.delete(childName);
246
+ }
247
+ });
248
+ Object.entries(input.attributes).forEach(([attrID, attr]) => {
249
+ node.attributes.set(attrID, attr);
250
+ });
251
+ }
252
+
253
+ // workflow-alpha.ts
254
+ function MMSet2(map, key, value) {
255
+ if (map.has(key)) {
256
+ map.get(key)?.push(value);
257
+ } else {
258
+ map.set(key, [value]);
259
+ }
260
+ }
261
+ function ToInputNodes(data) {
262
+ let inputNodes = /* @__PURE__ */ new Map();
263
+ data.forEach((ifcxNode) => {
264
+ let node = {
265
+ path: ifcxNode.identifier,
266
+ children: ifcxNode.children ? ifcxNode.children : {},
267
+ inherits: ifcxNode.inherits ? ifcxNode.inherits : {},
268
+ attributes: ifcxNode.attributes ? ifcxNode.attributes : {},
269
+ };
270
+ MMSet2(inputNodes, node.path, node);
271
+ });
272
+ return inputNodes;
273
+ }
274
+ var SchemaValidationError = class extends Error {};
275
+ function ValidateAttributeValue(desc, value, path, schemas) {
276
+ if (desc.inherits) {
277
+ desc.inherits.forEach((inheritedSchemaID) => {
278
+ let inheritedSchema = schemas[inheritedSchemaID];
279
+ if (!inheritedSchema) {
280
+ throw new SchemaValidationError(`Unknown inherited schema id "${desc.inherits}"`);
281
+ }
282
+ ValidateAttributeValue(inheritedSchema.value, value, path, schemas);
283
+ });
284
+ }
285
+ if (desc.dataType === "Boolean") {
286
+ if (typeof value !== "boolean") {
287
+ throw new SchemaValidationError(`Expected "${value}" to be of type boolean`);
288
+ }
289
+ } else if (desc.dataType === "String") {
290
+ if (typeof value !== "string") {
291
+ throw new SchemaValidationError(`Expected "${value}" to be of type string`);
292
+ }
293
+ } else if (desc.dataType === "DateTime") {
294
+ if (typeof value !== "string") {
295
+ throw new SchemaValidationError(`Expected "${value}" to be of type date`);
296
+ }
297
+ } else if (desc.dataType === "Enum") {
298
+ if (typeof value !== "string") {
299
+ throw new SchemaValidationError(`Expected "${value}" to be of type string`);
300
+ }
301
+ let found = desc.enumRestrictions.options.filter((option) => option === value).length === 1;
302
+ if (!found) {
303
+ throw new SchemaValidationError(`Expected "${value}" to be one of [${desc.enumRestrictions.options.join(",")}]`);
304
+ }
305
+ } else if (desc.dataType === "Integer") {
306
+ if (typeof value !== "number") {
307
+ throw new SchemaValidationError(`Expected "${value}" to be of type int`);
308
+ }
309
+ } else if (desc.dataType === "Real") {
310
+ if (typeof value !== "number") {
311
+ throw new SchemaValidationError(`Expected "${value}" to be of type real`);
312
+ }
313
+ } else if (desc.dataType === "Relation") {
314
+ if (typeof value !== "string") {
315
+ throw new SchemaValidationError(`Expected "${value}" to be of type string`);
316
+ }
317
+ } else if (desc.dataType === "Object") {
318
+ if (typeof value !== "object") {
319
+ throw new SchemaValidationError(`Expected "${value}" to be of type object`);
320
+ }
321
+ if (desc.objectRestrictions) {
322
+ Object.keys(desc.objectRestrictions.values).forEach((key) => {
323
+ if (!Object.hasOwn(value, key)) {
324
+ throw new SchemaValidationError(`Expected "${value}" to have key ${key}`);
325
+ }
326
+ ValidateAttributeValue(desc.objectRestrictions.values[key], value[key], path + "." + key, schemas);
327
+ });
328
+ }
329
+ } else if (desc.dataType === "Array") {
330
+ if (!Array.isArray(value)) {
331
+ throw new SchemaValidationError(`Expected "${value}" to be of type array`);
332
+ }
333
+ value.forEach((entry) => {
334
+ ValidateAttributeValue(desc.arrayRestrictions.value, entry, path + ".<array>.", schemas);
335
+ });
336
+ } else {
337
+ throw new SchemaValidationError(`Unexpected datatype ${desc.dataType}`);
338
+ }
339
+ }
340
+ function Validate(schemas, inputNodes) {
341
+ inputNodes.forEach((node) => {
342
+ Object.keys(node.attributes).forEach((schemaID) => {
343
+ if (!schemas[schemaID]) {
344
+ throw new SchemaValidationError(`Missing schema "${schemaID}" referenced by ["${node.path}"].attributes`);
345
+ }
346
+ let schema = schemas[schemaID];
347
+ let value = node.attributes[schemaID];
348
+ try {
349
+ ValidateAttributeValue(schema.value, value, "", schemas);
350
+ } catch (e) {
351
+ if (e instanceof SchemaValidationError) {
352
+ throw new SchemaValidationError(`Error validating ["${node.path}"].attributes["${schemaID}"]: ${e.message}`);
353
+ } else {
354
+ throw e;
355
+ }
356
+ }
357
+ });
358
+ });
359
+ }
360
+ function LoadIfcxFile(file, checkSchemas = true, createArtificialRoot = false) {
361
+ let inputNodes = ToInputNodes(file.data);
362
+ let compositionNodes = ConvertNodes(inputNodes);
363
+ try {
364
+ if (checkSchemas) {
365
+ Validate(file.schemas, compositionNodes);
366
+ }
367
+ } catch (e) {
368
+ throw e;
369
+ }
370
+ if (createArtificialRoot) {
371
+ return CreateArtificialRoot(compositionNodes);
372
+ } else {
373
+ return ExpandFirstRootInInput(compositionNodes);
374
+ }
375
+ }
376
+ function Federate(files) {
377
+ let result = {
378
+ header: files[0].header,
379
+ schemas: {},
380
+ data: [],
381
+ };
382
+ files.forEach((file) => {
383
+ Object.keys(file.schemas).forEach((schemaID) => (result.schemas[schemaID] = file.schemas[schemaID]));
384
+ });
385
+ files.forEach((file) => {
386
+ file.data.forEach((node) => result.data.push(node));
387
+ });
388
+ return Prune(result);
389
+ }
390
+ function Collapse(nodes, deleteEmpty = false) {
391
+ let result = {
392
+ path: nodes[0].path,
393
+ children: {},
394
+ inherits: {},
395
+ attributes: {},
396
+ };
397
+ nodes.forEach((node) => {
398
+ Object.keys(node.children).forEach((name) => {
399
+ result.children[name] = node.children[name];
400
+ });
401
+ Object.keys(node.inherits).forEach((name) => {
402
+ result.inherits[name] = node.inherits[name];
403
+ });
404
+ Object.keys(node.attributes).forEach((name) => {
405
+ result.attributes[name] = node.attributes[name];
406
+ });
407
+ });
408
+ if (deleteEmpty) {
409
+ let empty = true;
410
+ Object.keys(result.children).forEach((name) => {
411
+ if (result.children[name] !== null) empty = false;
412
+ });
413
+ Object.keys(result.inherits).forEach((name) => {
414
+ if (result.inherits[name] !== null) empty = false;
415
+ });
416
+ Object.keys(result.attributes).forEach((name) => {
417
+ if (result.attributes[name] !== null) empty = false;
418
+ });
419
+ if (empty) return null;
420
+ }
421
+ return result;
422
+ }
423
+ function Prune(file, deleteEmpty = false) {
424
+ let result = {
425
+ header: file.header,
426
+ schemas: file.schemas,
427
+ data: [],
428
+ };
429
+ let inputNodes = ToInputNodes(file.data);
430
+ inputNodes.forEach((nodes) => {
431
+ let collapsed = Collapse(nodes, deleteEmpty);
432
+ if (collapsed)
433
+ result.data.push({
434
+ identifier: collapsed.path,
435
+ children: collapsed.children,
436
+ inherits: collapsed.inherits,
437
+ attributes: collapsed.attributes,
438
+ });
439
+ });
440
+ return result;
441
+ }
442
+
443
+ // compose-flattened.ts
444
+ function TreeNodeToComposedObject(path, node, schemas) {
445
+ let co = {
446
+ name: path,
447
+ attributes: {},
448
+ children: [],
449
+ };
450
+ node.children.forEach((childNode, childName) => {
451
+ co.children?.push(TreeNodeToComposedObject(`${path}/${childName}`, childNode, schemas));
452
+ });
453
+ node.attributes.forEach((attr, attrName) => {
454
+ if (attr && typeof attr === "object" && !Array.isArray(attr)) {
455
+ Object.keys(attr).forEach((compname) => {
456
+ co.attributes[`${attrName}::${compname}`] = attr[compname];
457
+ });
458
+ } else {
459
+ let schema = schemas[attrName];
460
+ if (schema && schema.value.quantityKind) {
461
+ let postfix = "";
462
+ let quantityKind = schema.value.quantityKind;
463
+ if (quantityKind === "Length") {
464
+ postfix = "m";
465
+ } else if (quantityKind === "Volume") {
466
+ postfix = "m" + String.fromCodePoint(179);
467
+ }
468
+ co.attributes[attrName] = `${attr} ${postfix}`;
469
+ } else {
470
+ co.attributes[attrName] = attr;
471
+ }
472
+ }
473
+ });
474
+ if (Object.keys(co.attributes).length === 0) delete co.attributes;
475
+ return co;
476
+ }
477
+ function compose3(files) {
478
+ let federated = Federate(files);
479
+ let tree = LoadIfcxFile(federated, true, true);
480
+ return TreeNodeToComposedObject("", tree, federated.schemas);
481
+ }
482
+
483
+ // render.ts
484
+ // var controls;
485
+ // var renderer;
486
+ var scene;
487
+ var camera;
488
+ var datas = [];
489
+ var autoCamera = true;
490
+ // var THREE = window["THREE"];
491
+ function init() {
492
+ scene = new THREE.Scene();
493
+ camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 100);
494
+ camera.up.set(0, 0, 1);
495
+ camera.position.set(50, 50, 50);
496
+ camera.lookAt(0, 0, 0);
497
+ // const nd = document.querySelector(".viewport");
498
+ // renderer = new THREE.WebGLRenderer({
499
+ // alpha: true,
500
+ // logarithmicDepthBuffer: true
501
+ // });
502
+ // renderer.setSize(nd.offsetWidth, nd.offsetHeight);
503
+ // controls = new THREE.OrbitControls(camera, renderer.domElement);
504
+ // controls.enableDamping = true;
505
+ // controls.dampingFactor = 0.25;
506
+ // nd.appendChild(renderer.domElement);
507
+ scene.add(camera);
508
+ return scene;
509
+ }
510
+ function HasAttr(node, attrName) {
511
+ if (!node || !node.attributes) return false;
512
+ return !!node.attributes[attrName];
513
+ }
514
+ function FindChildWithAttr(node, attrName) {
515
+ if (!node || !node.children) return void 0;
516
+ for (let i = 0; i < node.children.length; i++) {
517
+ if (HasAttr(node.children[i], attrName)) {
518
+ return node.children[i];
519
+ }
520
+ }
521
+ return void 0;
522
+ }
523
+ function createMaterialFromParent(parent, root) {
524
+ let reference = parent.attributes["usd::usdshade::materialbindingapi::material::binding"];
525
+ let material = {
526
+ color: new THREE.Color(0.6, 0.6, 0.6),
527
+ transparent: false,
528
+ opacity: 1,
529
+ };
530
+ if (reference) {
531
+ const materialNode = getChildByName(root, reference.ref);
532
+ let shader = FindChildWithAttr(materialNode, "usd::materials::inputs::diffuseColor");
533
+ if (shader) {
534
+ let color = shader?.attributes["usd::materials::inputs::diffuseColor"];
535
+ material.color = new THREE.Color(...color);
536
+ if (shader?.attributes["usd::materials::inputs::opacity"]) {
537
+ material.transparent = true;
538
+ material.opacity = shader.attributes["usd::materials::inputs::opacity"];
539
+ }
540
+ }
541
+ }
542
+ return material;
543
+ }
544
+ function createCurveFromJson(node, parent, root) {
545
+ let points = new Float32Array(node.attributes["usd::usdgeom::basiscurves::points"].flat());
546
+ const geometry = new THREE.BufferGeometry();
547
+ geometry.setAttribute("position", new THREE.BufferAttribute(points, 3));
548
+ const material = createMaterialFromParent(parent, root);
549
+ let lineMaterial = new THREE.LineBasicMaterial({ ...material });
550
+ lineMaterial.color.multiplyScalar(0.8);
551
+ return new THREE.Line(geometry, lineMaterial);
552
+ }
553
+ function createMeshFromJson(node, parent, root) {
554
+ let points = new Float32Array(node.attributes["usd::usdgeom::mesh::points"].flat());
555
+ let indices = new Uint16Array(node.attributes["usd::usdgeom::mesh::faceVertexIndices"]);
556
+ const geometry = new THREE.BufferGeometry();
557
+ geometry.setAttribute("position", new THREE.BufferAttribute(points, 3));
558
+ geometry.setIndex(new THREE.BufferAttribute(indices, 1));
559
+ geometry.computeVertexNormals();
560
+ const material = createMaterialFromParent(parent, root);
561
+ let meshMaterial = new THREE.MeshBasicMaterial({ ...material });
562
+ return new THREE.Mesh(geometry, meshMaterial);
563
+ }
564
+ function traverseTree(node, parent, root, parentNode = void 0) {
565
+ let elem = new THREE.Group();
566
+ if (HasAttr(node, "usd::usdgeom::visibility::visibility")) {
567
+ if (node.attributes["usd::usdgeom::visibility::visibility"] === "invisible") {
568
+ return;
569
+ }
570
+ } else if (HasAttr(node, "usd::usdgeom::mesh::points")) {
571
+ elem = createMeshFromJson(node, parentNode, root);
572
+ } else if (HasAttr(node, "usd::usdgeom::basiscurves::points")) {
573
+ elem = createCurveFromJson(node, parentNode, root);
574
+ }
575
+ parent.add(elem);
576
+ if (node !== root) {
577
+ elem.matrixAutoUpdate = false;
578
+ let matrixNode =
579
+ node.attributes && node.attributes["usd::xformop::transform"]
580
+ ? node.attributes["usd::xformop::transform"].flat()
581
+ : null;
582
+ if (matrixNode) {
583
+ let matrix = new THREE.Matrix4();
584
+ matrix.set(...matrixNode);
585
+ matrix.transpose();
586
+ elem.matrix = matrix;
587
+ }
588
+ }
589
+ (node.children || []).forEach((child) => traverseTree(child, elem || parent, root, node));
590
+ }
591
+ // function encodeHtmlEntities(str) {
592
+ // const div = document.createElement("div");
593
+ // div.textContent = str;
594
+ // return div.innerHTML;
595
+ // }
596
+ // var icons = {
597
+ // "usd::usdgeom::mesh::points": "deployed_code",
598
+ // "usd::usdgeom::basiscurves::points": "line_curve",
599
+ // "usd::usdshade::material::outputs::surface.connect": "line_style"
600
+ // };
601
+ // function buildDomTree(prim, node) {
602
+ // const elem = document.createElement("div");
603
+ // let span;
604
+ // elem.appendChild(document.createTextNode(prim.name ? prim.name.split("/").reverse()[0] : "root"));
605
+ // elem.appendChild(span = document.createElement("span"));
606
+ // Object.entries(icons).forEach(([k, v]) => span.innerText += (prim.attributes || {})[k] ? v : " ");
607
+ // span.className = "material-symbols-outlined";
608
+ // elem.onclick = (evt) => {
609
+ // let rows = [["name", prim.name]].concat(Object.entries(prim.attributes)).map(([k, v]) => `<tr><td>${encodeHtmlEntities(k)}</td><td>${encodeHtmlEntities(typeof v === "object" ? JSON.stringify(v) : v)}</td>`).join("");
610
+ // document.querySelector(".attributes .table").innerHTML = `<table border="0">${rows}</table>`;
611
+ // evt.stopPropagation();
612
+ // };
613
+ // node.appendChild(elem);
614
+ // (prim.children || []).forEach((p) => buildDomTree(p, elem));
615
+ // }
616
+ function composeAndRender() {
617
+ if (scene) {
618
+ scene.children = [];
619
+ }
620
+ // document.querySelector(".tree").innerHTML = "";
621
+ if (datas.length === 0) {
622
+ return;
623
+ }
624
+ let tree = null;
625
+ let dataArray = datas.map((arr) => arr[1]);
626
+ tree = compose3(dataArray);
627
+ if (!tree) {
628
+ console.error("No result from composition");
629
+ return;
630
+ }
631
+ traverseTree(tree, scene || init(), tree);
632
+ if (autoCamera) {
633
+ const boundingBox = new THREE.Box3();
634
+ boundingBox.setFromObject(scene);
635
+ if (!boundingBox.isEmpty()) {
636
+ let avg = boundingBox.min.clone().add(boundingBox.max).multiplyScalar(0.5);
637
+ let ext = boundingBox.max.clone().sub(boundingBox.min).length();
638
+ camera.position.copy(avg.clone().add(new THREE.Vector3(1, 1, 1).normalize().multiplyScalar(ext)));
639
+ camera.far = ext * 3;
640
+ camera.updateProjectionMatrix();
641
+ // controls.target.copy(avg);
642
+ // controls.update();
643
+ autoCamera = false;
644
+ }
645
+ }
646
+ // buildDomTree(tree, document.querySelector(".tree"));
647
+ // animate();
648
+ }
649
+ // function createLayerDom() {
650
+ // document.querySelector(".layers div").innerHTML = "";
651
+ // datas.forEach(([name, _], index) => {
652
+ // const elem = document.createElement("div");
653
+ // elem.appendChild(document.createTextNode(name));
654
+ // ["\u25B3", "\u25BD", "\xD7"].reverse().forEach((lbl, cmd) => {
655
+ // const btn = document.createElement("span");
656
+ // btn.onclick = (evt) => {
657
+ // evt.stopPropagation();
658
+ // if (cmd === 2) {
659
+ // if (index > 0) {
660
+ // [datas[index], datas[index - 1]] = [datas[index - 1], datas[index]];
661
+ // }
662
+ // } else if (cmd === 1) {
663
+ // if (index < datas.length - 1) {
664
+ // [datas[index], datas[index + 1]] = [datas[index + 1], datas[index]];
665
+ // }
666
+ // } else if (cmd === 0) {
667
+ // datas.splice(index, 1);
668
+ // }
669
+ // composeAndRender();
670
+ // createLayerDom();
671
+ // };
672
+ // btn.appendChild(document.createTextNode(lbl));
673
+ // elem.appendChild(btn);
674
+ // });
675
+ // document.querySelector(".layers div").appendChild(elem);
676
+ // });
677
+ // }
678
+ // function addModel(name, m) {
679
+ // datas.push([name, m]);
680
+ // createLayerDom();
681
+ // composeAndRender();
682
+ // }
683
+ // function animate() {
684
+ // requestAnimationFrame(animate);
685
+ // controls.update();
686
+ // renderer.render(scene, camera);
687
+ // }
688
+ // export {
689
+ // composeAndRender,
690
+ // addModel as default
691
+ // };
692
+ export function parse(m, name) {
693
+ datas.push([name, m]);
694
+ composeAndRender();
695
+ return scene;
696
+ }
697
+ export function clear() {
698
+ scene = undefined;
699
+ datas.length = 0;
700
+ autoCamera = true;
701
+ }