@combeenation/3d-viewer 12.4.1 → 12.4.3

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 (114) hide show
  1. package/README.md +9 -9
  2. package/dist/lib-cjs/api/classes/animationInterface.d.ts +8 -8
  3. package/dist/lib-cjs/api/classes/animationInterface.js +2 -2
  4. package/dist/lib-cjs/api/classes/dottedPath.d.ts +79 -79
  5. package/dist/lib-cjs/api/classes/dottedPath.js +166 -166
  6. package/dist/lib-cjs/api/classes/element.d.ts +153 -153
  7. package/dist/lib-cjs/api/classes/element.js +702 -702
  8. package/dist/lib-cjs/api/classes/event.d.ts +401 -401
  9. package/dist/lib-cjs/api/classes/event.js +424 -424
  10. package/dist/lib-cjs/api/classes/eventBroadcaster.d.ts +26 -26
  11. package/dist/lib-cjs/api/classes/eventBroadcaster.js +49 -49
  12. package/dist/lib-cjs/api/classes/fuzzyMap.d.ts +7 -7
  13. package/dist/lib-cjs/api/classes/fuzzyMap.js +21 -21
  14. package/dist/lib-cjs/api/classes/parameter.d.ts +410 -410
  15. package/dist/lib-cjs/api/classes/parameter.js +642 -642
  16. package/dist/lib-cjs/api/classes/parameterObservable.d.ts +36 -36
  17. package/dist/lib-cjs/api/classes/parameterObservable.js +72 -72
  18. package/dist/lib-cjs/api/classes/parameterizable.d.ts +15 -15
  19. package/dist/lib-cjs/api/classes/parameterizable.js +102 -102
  20. package/dist/lib-cjs/api/classes/placementAnimation.d.ts +45 -45
  21. package/dist/lib-cjs/api/classes/placementAnimation.js +176 -176
  22. package/dist/lib-cjs/api/classes/variant.d.ts +261 -261
  23. package/dist/lib-cjs/api/classes/variant.js +872 -872
  24. package/dist/lib-cjs/api/classes/variantInstance.d.ts +53 -53
  25. package/dist/lib-cjs/api/classes/variantInstance.js +125 -125
  26. package/dist/lib-cjs/api/classes/variantParameterizable.d.ts +17 -17
  27. package/dist/lib-cjs/api/classes/variantParameterizable.js +86 -86
  28. package/dist/lib-cjs/api/classes/viewer.d.ts +215 -215
  29. package/dist/lib-cjs/api/classes/viewer.js +708 -708
  30. package/dist/lib-cjs/api/classes/viewerError.d.ts +43 -43
  31. package/dist/lib-cjs/api/classes/viewerError.js +55 -55
  32. package/dist/lib-cjs/api/classes/viewerLight.d.ts +66 -66
  33. package/dist/lib-cjs/api/classes/viewerLight.js +344 -344
  34. package/dist/lib-cjs/api/internal/lensRendering.d.ts +8 -8
  35. package/dist/lib-cjs/api/internal/lensRendering.js +11 -11
  36. package/dist/lib-cjs/api/internal/sceneSetup.d.ts +13 -13
  37. package/dist/lib-cjs/api/internal/sceneSetup.js +227 -227
  38. package/dist/lib-cjs/api/manager/animationManager.d.ts +30 -30
  39. package/dist/lib-cjs/api/manager/animationManager.js +126 -126
  40. package/dist/lib-cjs/api/manager/gltfExportManager.d.ts +80 -80
  41. package/dist/lib-cjs/api/manager/gltfExportManager.js +300 -299
  42. package/dist/lib-cjs/api/manager/gltfExportManager.js.map +1 -1
  43. package/dist/lib-cjs/api/manager/sceneManager.d.ts +33 -33
  44. package/dist/lib-cjs/api/manager/sceneManager.js +128 -128
  45. package/dist/lib-cjs/api/manager/tagManager.d.ts +118 -118
  46. package/dist/lib-cjs/api/manager/tagManager.js +530 -530
  47. package/dist/lib-cjs/api/manager/textureLoadManager.d.ts +22 -22
  48. package/dist/lib-cjs/api/manager/textureLoadManager.js +107 -107
  49. package/dist/lib-cjs/api/manager/variantInstanceManager.d.ts +106 -106
  50. package/dist/lib-cjs/api/manager/variantInstanceManager.js +290 -290
  51. package/dist/lib-cjs/api/store/specStorage.d.ts +32 -32
  52. package/dist/lib-cjs/api/store/specStorage.js +65 -65
  53. package/dist/lib-cjs/api/util/babylonHelper.d.ts +238 -238
  54. package/dist/lib-cjs/api/util/babylonHelper.js +825 -825
  55. package/dist/lib-cjs/api/util/debugHelper.d.ts +9 -9
  56. package/dist/lib-cjs/api/util/debugHelper.js +93 -93
  57. package/dist/lib-cjs/api/util/deviceHelper.d.ts +9 -9
  58. package/dist/lib-cjs/api/util/deviceHelper.js +28 -28
  59. package/dist/lib-cjs/api/util/geometryHelper.d.ts +17 -17
  60. package/dist/lib-cjs/api/util/geometryHelper.js +112 -112
  61. package/dist/lib-cjs/api/util/globalTypes.d.ts +490 -490
  62. package/dist/lib-cjs/api/util/globalTypes.js +1 -1
  63. package/dist/lib-cjs/api/util/resourceHelper.d.ts +58 -58
  64. package/dist/lib-cjs/api/util/resourceHelper.js +214 -214
  65. package/dist/lib-cjs/api/util/sceneLoaderHelper.d.ts +58 -58
  66. package/dist/lib-cjs/api/util/sceneLoaderHelper.js +228 -228
  67. package/dist/lib-cjs/api/util/stringHelper.d.ts +13 -13
  68. package/dist/lib-cjs/api/util/stringHelper.js +32 -32
  69. package/dist/lib-cjs/api/util/structureHelper.d.ts +9 -9
  70. package/dist/lib-cjs/api/util/structureHelper.js +57 -57
  71. package/dist/lib-cjs/buildinfo.json +3 -3
  72. package/dist/lib-cjs/commonjs.tsconfig.tsbuildinfo +1 -1
  73. package/dist/lib-cjs/index.d.ts +63 -63
  74. package/dist/lib-cjs/index.js +128 -128
  75. package/package.json +93 -92
  76. package/src/api/classes/animationInterface.ts +10 -10
  77. package/src/api/classes/dottedPath.ts +181 -181
  78. package/src/api/classes/element.ts +766 -766
  79. package/src/api/classes/event.ts +457 -457
  80. package/src/api/classes/eventBroadcaster.ts +52 -52
  81. package/src/api/classes/fuzzyMap.ts +21 -21
  82. package/src/api/classes/parameter.ts +686 -686
  83. package/src/api/classes/parameterObservable.ts +73 -73
  84. package/src/api/classes/parameterizable.ts +87 -87
  85. package/src/api/classes/placementAnimation.ts +162 -162
  86. package/src/api/classes/variant.ts +965 -965
  87. package/src/api/classes/variantInstance.ts +123 -123
  88. package/src/api/classes/variantParameterizable.ts +83 -83
  89. package/src/api/classes/viewer.ts +751 -751
  90. package/src/api/classes/viewerError.ts +63 -63
  91. package/src/api/classes/viewerLight.ts +335 -335
  92. package/src/api/internal/debugViewer.ts +90 -90
  93. package/src/api/internal/lensRendering.ts +9 -9
  94. package/src/api/internal/sceneSetup.ts +208 -208
  95. package/src/api/manager/animationManager.ts +143 -143
  96. package/src/api/manager/gltfExportManager.ts +337 -334
  97. package/src/api/manager/sceneManager.ts +134 -134
  98. package/src/api/manager/tagManager.ts +572 -572
  99. package/src/api/manager/textureLoadManager.ts +107 -107
  100. package/src/api/manager/variantInstanceManager.ts +306 -306
  101. package/src/api/store/specStorage.ts +68 -68
  102. package/src/api/util/babylonHelper.ts +915 -915
  103. package/src/api/util/debugHelper.ts +121 -121
  104. package/src/api/util/deviceHelper.ts +31 -31
  105. package/src/api/util/geometryHelper.ts +142 -142
  106. package/src/api/util/globalTypes.ts +566 -566
  107. package/src/api/util/resourceHelper.ts +201 -201
  108. package/src/api/util/sceneLoaderHelper.ts +247 -247
  109. package/src/api/util/stringHelper.ts +30 -30
  110. package/src/api/util/structureHelper.ts +62 -62
  111. package/src/buildinfo.json +3 -3
  112. package/src/dev.ts +70 -70
  113. package/src/index.ts +116 -116
  114. package/src/types.d.ts +49 -49
@@ -1,703 +1,703 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.Element = void 0;
13
- const babylonHelper_1 = require("../util/babylonHelper");
14
- const resourceHelper_1 = require("../util/resourceHelper");
15
- const sceneLoaderHelper_1 = require("../util/sceneLoaderHelper");
16
- const variantParameterizable_1 = require("./../classes/variantParameterizable");
17
- const dottedPath_1 = require("./dottedPath");
18
- const event_1 = require("./event");
19
- const parameter_1 = require("./parameter");
20
- const highlightLayer_1 = require("@babylonjs/core/Layers/highlightLayer");
21
- const pbrMaterial_1 = require("@babylonjs/core/Materials/PBR/pbrMaterial");
22
- const material_1 = require("@babylonjs/core/Materials/material");
23
- const math_color_1 = require("@babylonjs/core/Maths/math.color");
24
- const abstractMesh_1 = require("@babylonjs/core/Meshes/abstractMesh");
25
- const instancedMesh_1 = require("@babylonjs/core/Meshes/instancedMesh");
26
- const mesh_1 = require("@babylonjs/core/Meshes/mesh");
27
- const transformNode_1 = require("@babylonjs/core/Meshes/transformNode");
28
- const tags_1 = require("@babylonjs/core/Misc/tags");
29
- const lodash_es_1 = require("lodash-es");
30
- /**
31
- * An {@link Element} of a {@link Variant}. Acts as a container for diverse definitions. Lives only in the context of a
32
- * {@link Variant}.
33
- *
34
- * When used in typings, refer to via its alias {@link VariantElement} to prevent name clashes with the web APIs
35
- * [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element) class
36
- */
37
- class Element extends variantParameterizable_1.VariantParameterizable {
38
- /**
39
- * Constructor.
40
- */
41
- constructor(variant, name) {
42
- super(variant, name);
43
- this.variant = variant;
44
- this.name = name;
45
- this.nodes = [];
46
- this._dottedNodes = new Map();
47
- if (process.env.NODE_ENV === 'development') {
48
- this.assertPathDefinitions();
49
- }
50
- const nodes = this.variant.inheritedNodes.map(node => {
51
- const nodeNamingStrategy = {
52
- handler: variant.viewer.nodeNamingStrategyHandler,
53
- payload: {
54
- variantInstance: variant.variantInstance,
55
- variant: variant,
56
- variantParameterizable: this,
57
- },
58
- };
59
- return (0, babylonHelper_1.cloneTransformNode)(node, nodeNamingStrategy, this.filterNode.bind(this), true);
60
- });
61
- // when cloning an instanced mesh the source mesh is still referencing to the source of the original mesh
62
- // this is wrong, as we want to reference the cloned source
63
- // this can only be done after the cloning process of the whole tree is finished, as we can't be sure if all
64
- // instanced meshes or all sources meshes are cloning prior
65
- const reassignSourceMesh = (node) => {
66
- if (node instanceof instancedMesh_1.InstancedMesh) {
67
- // find the cloned instance and source mesh
68
- const clonedInstance = node;
69
- const originalInstance = clonedInstance.metadata.cloneSource;
70
- const originalSource = originalInstance.sourceMesh;
71
- const clonedSource = originalSource.metadata.cloneTarget;
72
- // it's not possible to just exchange the source mesh => readonly property
73
- // we have to create a new clone with the correct source mesh
74
- const exchangedInstance = clonedInstance.clone(clonedInstance.name, clonedInstance.parent, true, clonedSource);
75
- // fix enabled and tags state after cloning
76
- exchangedInstance.setEnabled(clonedInstance.isEnabled(false));
77
- if (tags_1.Tags.HasTags(clonedInstance)) {
78
- tags_1.Tags.AddTagsTo(exchangedInstance, tags_1.Tags.GetTags(clonedInstance, true));
79
- }
80
- // re-reference the childs of the cloned instance as well
81
- const childs = clonedInstance.getChildTransformNodes(true);
82
- childs.forEach(child => (child.parent = exchangedInstance));
83
- // old instance mesh is not needed anymore and can be disposed
84
- clonedInstance.dispose();
85
- }
86
- // recurse into childs
87
- const children = node.getChildTransformNodes(true);
88
- children.forEach(reassignSourceMesh);
89
- };
90
- nodes.forEach(node => {
91
- if (node) {
92
- reassignSourceMesh(node);
93
- }
94
- });
95
- this.nodes = nodes.filter(node => !!node);
96
- this._dottedNodes = (0, babylonHelper_1.mapToDottedNodes)(this.nodes, node => node instanceof transformNode_1.TransformNode);
97
- variant.broadcastEvent(event_1.Event.ELEMENT_NODES_CLONED, this);
98
- this.addMissingMaterialObservers();
99
- this.addParameterObservers();
100
- }
101
- /**
102
- * Creates an {@link Element} with given name.
103
- */
104
- static create(variant, name) {
105
- return __awaiter(this, void 0, void 0, function* () {
106
- const element = new Element(variant, name);
107
- variant.broadcastEvent(event_1.Event.ELEMENT_CREATED, element);
108
- return element;
109
- });
110
- }
111
- /**
112
- * The {@link DottedPath} in the built tree of {@link Element}s.
113
- * E.g. "_.top-1.sub-2.sub-sub-3.el-1"
114
- */
115
- get dottedPath() {
116
- return dottedPath_1.DottedPath.create(this.variant.dottedPath).addPart(this.name);
117
- }
118
- /**
119
- * The id representing a {@link DottedPath}.
120
- */
121
- get id() {
122
- const dottedPath = dottedPath_1.DottedPath.create(this.dottedPath);
123
- dottedPath.shiftPart(); // remove root
124
- return dottedPath.path;
125
- }
126
- /**
127
- * The {@link ElementDefinition} of the {@link Element}.
128
- */
129
- get definition() {
130
- const definition = this.variant.structureJson.elements[this.name];
131
- if ((0, lodash_es_1.isArray)(definition)) {
132
- return {
133
- paths: {
134
- include: definition,
135
- },
136
- };
137
- }
138
- return definition;
139
- }
140
- /**
141
- * The {@link PathDefinitions} of the {@link Element}.
142
- */
143
- get pathDefinitions() {
144
- return this.definition.paths;
145
- }
146
- /**
147
- * The {@link TraceableDefinitions} of the {@link Element}.
148
- */
149
- get traceableDefinitions() {
150
- return this.definition.traceables || {};
151
- }
152
- /**
153
- * The {@link PaintableDefinition} of the {@link Element}.
154
- */
155
- get paintableDefinitions() {
156
- return this.definition.paintables || {};
157
- }
158
- /**
159
- * The inherited {@link ParameterBag}.
160
- * Merges the {@link Variant}'s parameters and those from the {@link Element}.
161
- */
162
- get inheritedParameters() {
163
- return (0, lodash_es_1.merge)({}, this.variant.inheritedParameters, this.parameters);
164
- }
165
- /**
166
- * The actual {@link TransformNode}s that make up this {@link Element}.
167
- */
168
- get nodesFlat() {
169
- return this.nodes.reduce((acc, cur) => [...acc, ...cur.getChildren(n => n instanceof transformNode_1.TransformNode, false)], this.nodes);
170
- }
171
- /**
172
- * The actual {@link Mesh}es that make up this {@link Element}.
173
- * Handy for e.g. creating a bounding box around an entire element.
174
- */
175
- get meshesFlat() {
176
- return this.nodes.reduce((acc, cur) => [...acc, ...cur.getChildren(n => n instanceof mesh_1.Mesh, false)], this.nodes.filter(n => n instanceof mesh_1.Mesh));
177
- }
178
- /**
179
- * The actual {@link AbstractMesh}es that make up this {@link Element}.
180
- * I.e. (real) Meshes as well as InstancedMeshes.
181
- */
182
- get abstractMeshesFlat() {
183
- return this.nodes.reduce((acc, cur) => [...acc, ...cur.getChildren(n => n instanceof abstractMesh_1.AbstractMesh, false)], this.nodes.filter(n => n instanceof abstractMesh_1.AbstractMesh));
184
- }
185
- /**
186
- * The meshes of this {@link Element}.
187
- */
188
- get meshes() {
189
- return this.nodes.filter(node => node instanceof mesh_1.Mesh);
190
- }
191
- /**
192
- * @protected
193
- */
194
- set highlightLayer(layer) {
195
- this._highlightLayer = layer;
196
- }
197
- get highlightLayer() {
198
- return this._highlightLayer;
199
- }
200
- /**
201
- * Destroys this {@link Element} and dispose all nodes.
202
- */
203
- destroy() {
204
- this.nodes.forEach(node => {
205
- node.dispose();
206
- });
207
- return this;
208
- }
209
- /**
210
- * Gets a node by its {@link DottedPath}.
211
- */
212
- getNode(dottedPath) {
213
- const _dottedPath = dottedPath_1.DottedPath.create(dottedPath);
214
- const keys = Array.from(this._dottedNodes.keys()).map(dp => dp.path);
215
- const values = Array.from(this._dottedNodes.values());
216
- const node = values[keys.indexOf(_dottedPath.path)];
217
- if (!node) {
218
- throw new Error(`Node with path "${_dottedPath.path}" does not exist for element "${this.id}".`);
219
- }
220
- return node;
221
- }
222
- /**
223
- * Gets a mesh by its {@link DottedPath}.
224
- */
225
- getMesh(dottedPath) {
226
- return __awaiter(this, void 0, void 0, function* () {
227
- const node = yield this.getNode(dottedPath);
228
- if (node instanceof mesh_1.Mesh) {
229
- return node;
230
- }
231
- return null;
232
- });
233
- }
234
- /**
235
- * Gets a {@link PaintableDefinition} by its name.
236
- */
237
- getPaintableDefinition(name) {
238
- if (!this.paintableDefinitions || !this.paintableDefinitions[name]) {
239
- throw new Error(`No paintable "${name}" defined for element "${this.id}".`);
240
- }
241
- return this.paintableDefinitions[name];
242
- }
243
- /**
244
- * Gets a {@link TraceableDefinition} by its name.
245
- */
246
- getTraceableDefinition(name) {
247
- if (!this.traceableDefinitions || !this.traceableDefinitions[name]) {
248
- throw new Error(`No traceable "${name}" defined to add observer in element "${this.id}".`);
249
- }
250
- return this.traceableDefinitions[name];
251
- }
252
- /**
253
- * Gets the paintable Node by its name.
254
- */
255
- getPaintableNode(name) {
256
- return this.getNode(this.getPaintableDefinition(name).path);
257
- }
258
- /**
259
- * Gets the traceable Node by its name.
260
- */
261
- getTraceableNode(name) {
262
- return this.getNode(this.getTraceableDefinition(name).path);
263
- }
264
- /**
265
- * @see {@link VariantParameterizable.commitParameters}
266
- * @emit {@link Event.ELEMENT_PARAMETER_COMMITTED}
267
- */
268
- commitParameters(parameters) {
269
- const _super = Object.create(null, {
270
- commitParameters: { get: () => super.commitParameters }
271
- });
272
- return __awaiter(this, void 0, void 0, function* () {
273
- return _super.commitParameters.call(this, parameters);
274
- });
275
- }
276
- /**
277
- * Adds an observer function for camera matrix changes.
278
- * The `observer` gets 2 parameters: the `AbstractMesh` and a `ClientRect` object.
279
- */
280
- addTraceableObserver(traceable, observer, payload) {
281
- if (payload && !(payload instanceof HTMLElement)) {
282
- throw new Error(`Payload for camera observer must be an instance of "HTMLElement" in element "${this.id}".`);
283
- }
284
- if (!this.traceableDefinitions || !this.traceableDefinitions[traceable]) {
285
- throw new Error(`No traceable "${traceable}" defined to add observer in element "${this.id}".`);
286
- }
287
- const node = this.getTraceableNode(traceable);
288
- if (!(node instanceof abstractMesh_1.AbstractMesh)) {
289
- throw new Error(`The path must be an instance of "AbstractMesh" for camera observer in element "${this.id}".`);
290
- }
291
- this.variant.viewer.sceneManager.activeCamera.onViewMatrixChangedObservable.add((eventData, eventState) => {
292
- const position = (0, babylonHelper_1.getClientRectFromMesh)(node, this.variant.viewer.scene, this.variant.viewer.canvas);
293
- if (payload) {
294
- payload.style.top = `${position.top}px`;
295
- payload.style.left = `${position.left}px`;
296
- }
297
- observer(node, position);
298
- });
299
- return this;
300
- }
301
- /**
302
- * Draws a `ImageBitmap` or `OffscreenCanvas` onto a `paintable` defined via {@link PaintableDefinition}.
303
- */
304
- drawPaintable(paintable, imageSource) {
305
- // node and material checks and preperation
306
- const node = this.getPaintableNode(paintable);
307
- if (!(node instanceof abstractMesh_1.AbstractMesh)) {
308
- throw new Error(`The path must be an instance of "AbstractMesh" for paintable "${paintable}" ` + `in element "${this.id}".`);
309
- }
310
- if (node.material && this.variant.viewer.cloneMaterialsOnMutation) {
311
- if (node.material && !(0, lodash_es_1.has)(node.metadata, 'dirty.material')) {
312
- (0, babylonHelper_1.cloneTransformNodeMaterial)(node);
313
- }
314
- if (!(0, lodash_es_1.has)(node.metadata, 'dirty.material.texture')) {
315
- // inject initial value and mark as dirty
316
- (0, babylonHelper_1.injectMetadata)(node, { dirty: { material: { texture: true } } });
317
- }
318
- }
319
- else if (!node.material) {
320
- // paintables should also work if no material is assigned to the node per default
321
- // in this case a certain fallback material with fitting default values is defined
322
- const pbrMat = new pbrMaterial_1.PBRMaterial(`${this.id}.${paintable}.material`, this.variant.viewer.scene);
323
- pbrMat.roughness = 1.0;
324
- pbrMat.metallic = 1.0;
325
- node.material = pbrMat;
326
- }
327
- // TODO: Not sure if this really should be overwritten every time the paintable is redrawn.
328
- // Maybe the 3D artist had something in mind when setting a certain transparency mode, which would get overwritten
329
- // in this case.
330
- // Applying it as a default if no material is set is fine though (see roughness/metallic some lines above).
331
- // Removing this line will most likely break existing configurators, so be cautios!
332
- node.material.transparencyMode = material_1.Material.MATERIAL_ALPHATESTANDBLEND;
333
- const paintableDefinition = this.getPaintableDefinition(paintable);
334
- if ((0, lodash_es_1.isNumber)(paintableDefinition.textureOptions)) {
335
- // convert into object to satisfy the interface of `drawPaintableOnMaterial`
336
- paintableDefinition.textureOptions = {
337
- width: paintableDefinition.textureOptions,
338
- height: paintableDefinition.textureOptions,
339
- };
340
- }
341
- // convert texture size options into scale values, as the scaling is now done via uvScale properties
342
- const paintableOptions = {};
343
- if (paintableDefinition.textureOptions.width) {
344
- paintableOptions.uScale = paintableDefinition.textureOptions.width / imageSource.width;
345
- }
346
- if (paintableDefinition.textureOptions.height) {
347
- paintableOptions.vScale = paintableDefinition.textureOptions.height / imageSource.height;
348
- }
349
- // forward to generic "paintable" call
350
- (0, babylonHelper_1.drawPaintableOnMaterial)(node.material, imageSource, this.variant.viewer.scene, paintableOptions);
351
- return this;
352
- }
353
- /**
354
- * Draws a SVG string onto a `paintable` defined via {@link PaintableDefinition}.
355
- */
356
- drawPaintableFromSvg(paintable, svgSource) {
357
- return __awaiter(this, void 0, void 0, function* () {
358
- const image = yield (0, resourceHelper_1.createImageFromSvg)(svgSource);
359
- return this.drawPaintable(paintable, image);
360
- });
361
- }
362
- /**
363
- * Draws an Image from source (URL/URI) onto a `paintable` defined via {@link PaintableDefinition}.
364
- */
365
- drawPaintableFromImgSrc(paintable, imgSource) {
366
- return __awaiter(this, void 0, void 0, function* () {
367
- const image = yield (0, resourceHelper_1.createImageFromImgSrc)(imgSource);
368
- return this.drawPaintable(paintable, image);
369
- });
370
- }
371
- /**
372
- * Adds observers for loading missing materials where needed
373
- */
374
- addMissingMaterialObservers() {
375
- for (const abstractMesh of this.abstractMeshesFlat) {
376
- (0, sceneLoaderHelper_1.addMissingMaterialObserver)(abstractMesh);
377
- }
378
- }
379
- /**
380
- * Adds the default {@link ParameterObserver}s which are called every time {@link commitParameters} is called.
381
- */
382
- addParameterObservers() {
383
- this._parameterObservers.set(parameter_1.Parameter.VISIBLE, [
384
- (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
385
- let visible;
386
- try {
387
- visible = parameter_1.Parameter.parseBoolean(newValue);
388
- }
389
- catch (e) {
390
- return;
391
- }
392
- if (visible === true) {
393
- // Override visible state and materials if tag manager is in use to support texture lazy loading
394
- if (element.variant.viewer.tagManager.parameters.size > 0) {
395
- const hiddenNodeNames = [];
396
- element.variant.viewer.tagManager.parameters.forEach((parameterBag, subject) => {
397
- const nodes = element.variant.viewer.tagManager.getNodesBySubject(subject);
398
- const visibleParamValue = parameterBag[parameter_1.Parameter.VISIBLE];
399
- const isVisible = undefined === visibleParamValue || parameter_1.Parameter.parseBoolean(visibleParamValue);
400
- if (!isVisible) {
401
- hiddenNodeNames.push(...nodes.map(node => node.name));
402
- }
403
- // overwrite materials immediatly, so that original material of node doesn't get loaded, since it could
404
- // be exchanged by the tag manager
405
- if (parameterBag[parameter_1.Parameter.MATERIAL]) {
406
- nodes.forEach(node => (0, babylonHelper_1.setMaterial)(node, parameterBag[parameter_1.Parameter.MATERIAL], false));
407
- }
408
- });
409
- // enable all nodes that are not explicitely hidden by the tag manager
410
- element.nodesFlat.map(node => {
411
- if (!hiddenNodeNames.includes(node.name)) {
412
- node.setEnabled(true);
413
- }
414
- });
415
- return;
416
- }
417
- // The default behaviour.
418
- element.nodes.forEach(node => {
419
- (0, babylonHelper_1.injectMetadata)(node, { visibility: node.isEnabled() });
420
- (0, babylonHelper_1.activateTransformNode)(node);
421
- });
422
- }
423
- else if (visible === false) {
424
- element.nodes.forEach(node => {
425
- (0, babylonHelper_1.injectMetadata)(node, { visibility: node.isEnabled() });
426
- (0, babylonHelper_1.deactivateTransformNode)(node);
427
- });
428
- }
429
- }),
430
- ]);
431
- this._parameterObservers.set(parameter_1.Parameter.SCALING, [
432
- (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
433
- // we have to deal just with root nodes here due to relative impacts in a node tree
434
- element.nodes.forEach(node => (0, babylonHelper_1.transformTransformNode)(node, {
435
- scaling: parameter_1.Parameter.parseVector(newValue),
436
- position: parameter_1.Parameter.parseVector(element.inheritedParameters[parameter_1.Parameter.POSITION] || parameter_1.Parameter.getDefaultValue(parameter_1.Parameter.POSITION)),
437
- rotation: parameter_1.Parameter.parseRotation(element.inheritedParameters[parameter_1.Parameter.ROTATION] || parameter_1.Parameter.getDefaultValue(parameter_1.Parameter.ROTATION)),
438
- }));
439
- }),
440
- ]);
441
- this._parameterObservers.set(parameter_1.Parameter.POSITION, [
442
- (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
443
- // we have to deal just with root nodes here due to relative impacts in a node tree
444
- element.nodes.forEach(node => (0, babylonHelper_1.transformTransformNode)(node, {
445
- scaling: parameter_1.Parameter.parseVector(element.inheritedParameters[parameter_1.Parameter.SCALING] || parameter_1.Parameter.getDefaultValue(parameter_1.Parameter.SCALING)),
446
- position: parameter_1.Parameter.parseVector(newValue),
447
- rotation: parameter_1.Parameter.parseRotation(element.inheritedParameters[parameter_1.Parameter.ROTATION] || parameter_1.Parameter.getDefaultValue(parameter_1.Parameter.ROTATION)),
448
- }));
449
- }),
450
- ]);
451
- this._parameterObservers.set(parameter_1.Parameter.ROTATION, [
452
- (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
453
- // we have to deal just with root nodes here due to relative impacts in a node tree
454
- element.nodes.forEach(node => (0, babylonHelper_1.transformTransformNode)(node, {
455
- scaling: parameter_1.Parameter.parseVector(element.inheritedParameters[parameter_1.Parameter.SCALING] || parameter_1.Parameter.getDefaultValue(parameter_1.Parameter.SCALING)),
456
- position: parameter_1.Parameter.parseVector(element.inheritedParameters[parameter_1.Parameter.POSITION] || parameter_1.Parameter.getDefaultValue(parameter_1.Parameter.POSITION)),
457
- rotation: parameter_1.Parameter.parseRotation(newValue),
458
- }));
459
- }),
460
- ]);
461
- this._parameterObservers.set(parameter_1.Parameter.MATERIAL, [
462
- (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
463
- const materialName = newValue.toString();
464
- for (const node of element.nodes) {
465
- (0, babylonHelper_1.assertTransformNode)(node, node => (0, babylonHelper_1.assertMeshCapability)(node, parameter_1.Parameter.MATERIAL));
466
- (0, babylonHelper_1.setMaterial)(node, materialName, true, element.variant);
467
- }
468
- }),
469
- ]);
470
- this._parameterObservers.set(parameter_1.Parameter.MATERIAL_COLOR, [
471
- (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
472
- const color = parameter_1.Parameter.parseColor(newValue);
473
- element.nodes.forEach(node => {
474
- (0, babylonHelper_1.assertTransformNode)(node, node => (0, babylonHelper_1.assertMeshCapability)(node, parameter_1.Parameter.MATERIAL_COLOR));
475
- if (this.variant.viewer.cloneMaterialsOnMutation) {
476
- if (!(0, lodash_es_1.has)(node.metadata, 'dirty.material')) {
477
- (0, babylonHelper_1.cloneTransformNodeMaterial)(node);
478
- }
479
- if (!(0, lodash_es_1.has)(node.metadata, 'dirty.material.color')) {
480
- // inject initial value and mark as dirty
481
- (0, babylonHelper_1.injectMetadata)(node, { dirty: { material: { color: oldValue } } });
482
- }
483
- }
484
- (0, babylonHelper_1.setMaterialColor)(node, color);
485
- });
486
- }),
487
- ]);
488
- this._parameterObservers.set(parameter_1.Parameter.MATERIAL_METALLNESS, [
489
- (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
490
- const metallness = parameter_1.Parameter.parseNumber(newValue);
491
- element.nodes.forEach(node => {
492
- (0, babylonHelper_1.assertTransformNode)(node, node => (0, babylonHelper_1.assertMeshCapability)(node, parameter_1.Parameter.MATERIAL_METALLNESS));
493
- if (this.variant.viewer.cloneMaterialsOnMutation) {
494
- if (!(0, lodash_es_1.has)(node.metadata, 'dirty.material')) {
495
- (0, babylonHelper_1.cloneTransformNodeMaterial)(node);
496
- }
497
- if (!(0, lodash_es_1.has)(node.metadata, 'dirty.material.metallness')) {
498
- // inject initial value and mark as dirty
499
- (0, babylonHelper_1.injectMetadata)(node, { dirty: { material: { metallness: oldValue } } });
500
- }
501
- }
502
- (0, babylonHelper_1.setMaterialMetallness)(node, metallness);
503
- });
504
- }),
505
- ]);
506
- this._parameterObservers.set(parameter_1.Parameter.MATERIAL_ROUGHNESS, [
507
- (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
508
- const roughness = parameter_1.Parameter.parseNumber(newValue);
509
- element.nodes.forEach(node => {
510
- (0, babylonHelper_1.assertTransformNode)(node, node => (0, babylonHelper_1.assertMeshCapability)(node, parameter_1.Parameter.MATERIAL_ROUGHNESS));
511
- if (this.variant.viewer.cloneMaterialsOnMutation) {
512
- if (!(0, lodash_es_1.has)(node.metadata, 'dirty.material')) {
513
- (0, babylonHelper_1.cloneTransformNodeMaterial)(node);
514
- }
515
- if (!(0, lodash_es_1.has)(node.metadata, 'dirty.material.roughness')) {
516
- // inject initial value and mark as dirty
517
- (0, babylonHelper_1.injectMetadata)(node, { dirty: { material: { roughness: oldValue } } });
518
- }
519
- }
520
- (0, babylonHelper_1.setMaterialRoughness)(node, roughness);
521
- });
522
- }),
523
- ]);
524
- this._parameterObservers.set(parameter_1.Parameter.HIGHLIGHT_COLOR, [
525
- (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
526
- var _a;
527
- // trigger Parameter.HIGHLIGHTED observers
528
- (_a = this._parameterObservers.get(parameter_1.Parameter.HIGHLIGHTED)) === null || _a === void 0 ? void 0 : _a.forEach(observer => {
529
- observer(element, !!element.highlighted, !!element.highlighted);
530
- });
531
- }),
532
- ]);
533
- this._parameterObservers.set(parameter_1.Parameter.HIGHLIGHT_ENABLED, [
534
- (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
535
- var _b;
536
- let curr;
537
- try {
538
- curr = parameter_1.Parameter.parseBoolean(newValue);
539
- }
540
- catch (e) {
541
- return;
542
- }
543
- switch (curr) {
544
- case true:
545
- if (element._highlightLayer) {
546
- break;
547
- }
548
- element.highlightLayer = new highlightLayer_1.HighlightLayer(dottedPath_1.DottedPath.create(element.id).addPart('highlight').path, element.variant.viewer.scene);
549
- break;
550
- case false:
551
- (_b = element.highlightLayer) === null || _b === void 0 ? void 0 : _b.dispose();
552
- break;
553
- }
554
- }),
555
- ]);
556
- this._parameterObservers.set(parameter_1.Parameter.HIGHLIGHTED, [
557
- (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
558
- var _c;
559
- if (!element.inheritedParameters[parameter_1.Parameter.HIGHLIGHT_ENABLED]) {
560
- return;
561
- }
562
- // trigger Parameter.HIGHLIGHT_ENABLED observers to ensure HighlightLayer
563
- (_c = this._parameterObservers.get(parameter_1.Parameter.HIGHLIGHT_ENABLED)) === null || _c === void 0 ? void 0 : _c.forEach(observer => {
564
- observer(element, this.parameters[parameter_1.Parameter.HIGHLIGHT_ENABLED], true);
565
- });
566
- let highlighted;
567
- try {
568
- highlighted = parameter_1.Parameter.parseBoolean(newValue);
569
- }
570
- catch (e) {
571
- return;
572
- }
573
- // Add/Remove meshes to previously created highlight layers.
574
- if (highlighted === true) {
575
- element.nodes.forEach(node => {
576
- (0, babylonHelper_1.assertTransformNode)(node, node => (0, babylonHelper_1.assertMeshCapability)(node, parameter_1.Parameter.HIGHLIGHTED));
577
- let color = math_color_1.Color3.Green();
578
- if (element.inheritedParameters[parameter_1.Parameter.HIGHLIGHT_COLOR]) {
579
- color = parameter_1.Parameter.parseColor(element.inheritedParameters[parameter_1.Parameter.HIGHLIGHT_COLOR]);
580
- }
581
- if (typeof element._highlightLayer !== 'undefined') {
582
- (0, babylonHelper_1.addToHighlightLayer)(element._highlightLayer, color, node);
583
- }
584
- });
585
- }
586
- else if (highlighted === false) {
587
- element.nodes.forEach(node => {
588
- if (typeof element._highlightLayer !== 'undefined') {
589
- (0, babylonHelper_1.removeFromHighlightLayer)(element._highlightLayer, node);
590
- }
591
- });
592
- }
593
- }),
594
- ]);
595
- this._parameterObservers.set(parameter_1.Parameter.CAST_SHADOW, [
596
- (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
597
- let castShadow;
598
- try {
599
- castShadow = parameter_1.Parameter.parseBoolean(newValue);
600
- }
601
- catch (e) {
602
- return;
603
- }
604
- let lightCsl = element.inheritedParameters[parameter_1.Parameter.CAST_SHADOW_FROM_LIGHTS];
605
- if (!lightCsl) {
606
- lightCsl = element.variant.inheritedViewerLights.map(l => l.name).join(',');
607
- }
608
- if (castShadow === true) {
609
- yield this.castShadowValueHandler(lightCsl, babylonHelper_1.addToShadowGenerator);
610
- }
611
- if (castShadow === false) {
612
- yield this.castShadowValueHandler(lightCsl, babylonHelper_1.removeFromShadowGenerator);
613
- }
614
- }),
615
- ]);
616
- this._parameterObservers.set(parameter_1.Parameter.CAST_SHADOW_FROM_LIGHTS, [
617
- (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
618
- // TODO: Possible performance issue in combination with CAST_SHADOW, since both observers are initially called
619
- // when CAST_SHADOW and CAST_SHADOW_FROM_LIGHTS are defined in the spec.
620
- const lightCsl = element.variant.inheritedViewerLights.map(l => l.name).join(',');
621
- // cleanup all shadow generators
622
- yield this.castShadowValueHandler(lightCsl, babylonHelper_1.removeFromShadowGenerator);
623
- if (element.castShadow === true) {
624
- // if newValue is undefined or '' then set newValue to lightCsl (use all lights)
625
- if (!newValue) {
626
- newValue = lightCsl;
627
- }
628
- yield this.castShadowValueHandler(newValue, babylonHelper_1.addToShadowGenerator);
629
- }
630
- }),
631
- ]);
632
- this._parameterObservers.set(parameter_1.Parameter.RECEIVE_SHADOWS, [
633
- (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
634
- element.nodes.forEach(node => (0, babylonHelper_1.setReceiveShadows)(node, parameter_1.Parameter.parseBoolean(newValue)));
635
- }),
636
- ]);
637
- return this;
638
- }
639
- /**
640
- * Filter for nodes that are not defined or excluded in the `pathDefinitions`.
641
- */
642
- filterNode(node) {
643
- let includeTokens = [];
644
- this.pathDefinitions.include.forEach(dottedPath => {
645
- includeTokens = (0, lodash_es_1.union)(includeTokens, dottedPath_1.DottedPath.create(dottedPath).leafTokens);
646
- });
647
- const shallKeep = (dp) => {
648
- const isDirectlyIncluded = !!includeTokens.find(_dp => _dp === dp.path);
649
- const isSubOfIncluded = !!this.pathDefinitions.include.find(_dp => {
650
- return dp.path.startsWith(`${_dp}${dottedPath_1.DottedPath.DELIMITER}`);
651
- });
652
- return isDirectlyIncluded || isSubOfIncluded;
653
- };
654
- const shallExclude = (dp) => {
655
- if (!this.pathDefinitions.exclude) {
656
- return false;
657
- }
658
- return !!this.pathDefinitions.exclude.find(_dp => dp.path === _dp);
659
- };
660
- return shallKeep(node.metadata.dottedPath) && !shallExclude(node.metadata.dottedPath);
661
- }
662
- /**
663
- * Asserts whether all {@link pathDefinitions} link to a valid `Node` path.
664
- * @protected
665
- */
666
- assertPathDefinitions() {
667
- var _a;
668
- const dottedNodes = this.variant.inheritedDottedNodes;
669
- if (dottedNodes.size === 0) {
670
- throw new Error(`There are no mapped nodes for element "${this.id}".`);
671
- }
672
- const include = this.pathDefinitions.include.map(path => dottedPath_1.DottedPath.create(path).path);
673
- const exclude = (_a = this.pathDefinitions.exclude) === null || _a === void 0 ? void 0 : _a.map(path => dottedPath_1.DottedPath.create(path).path);
674
- const keys = Array.from(dottedNodes.keys()).map(dottedPath => dottedPath.path);
675
- (0, lodash_es_1.union)(include, exclude).forEach(path => {
676
- if (keys.indexOf(path) === -1) {
677
- throw new Error(`Node with path "${path}" does not exist for element "${this.id}".`);
678
- }
679
- });
680
- }
681
- /**
682
- * Handles callback for given light parameter.
683
- */
684
- castShadowValueHandler(lightCsl, mutator) {
685
- return __awaiter(this, void 0, void 0, function* () {
686
- const lights = [];
687
- for (const lightName of parameter_1.Parameter.parseCommaSeparatedList(lightCsl)) {
688
- const viewerLight = yield this.variant.getViewerLight(lightName);
689
- if (viewerLight) {
690
- lights.push(viewerLight.light);
691
- }
692
- }
693
- const shadowGenerators = lights.map(light => light === null || light === void 0 ? void 0 : light.getShadowGenerator()).filter(Boolean);
694
- shadowGenerators.forEach(generator => {
695
- this.nodes.forEach(node => {
696
- mutator(generator, node);
697
- });
698
- });
699
- });
700
- }
701
- }
702
- exports.Element = Element;
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.Element = void 0;
13
+ const babylonHelper_1 = require("../util/babylonHelper");
14
+ const resourceHelper_1 = require("../util/resourceHelper");
15
+ const sceneLoaderHelper_1 = require("../util/sceneLoaderHelper");
16
+ const variantParameterizable_1 = require("./../classes/variantParameterizable");
17
+ const dottedPath_1 = require("./dottedPath");
18
+ const event_1 = require("./event");
19
+ const parameter_1 = require("./parameter");
20
+ const highlightLayer_1 = require("@babylonjs/core/Layers/highlightLayer");
21
+ const pbrMaterial_1 = require("@babylonjs/core/Materials/PBR/pbrMaterial");
22
+ const material_1 = require("@babylonjs/core/Materials/material");
23
+ const math_color_1 = require("@babylonjs/core/Maths/math.color");
24
+ const abstractMesh_1 = require("@babylonjs/core/Meshes/abstractMesh");
25
+ const instancedMesh_1 = require("@babylonjs/core/Meshes/instancedMesh");
26
+ const mesh_1 = require("@babylonjs/core/Meshes/mesh");
27
+ const transformNode_1 = require("@babylonjs/core/Meshes/transformNode");
28
+ const tags_1 = require("@babylonjs/core/Misc/tags");
29
+ const lodash_es_1 = require("lodash-es");
30
+ /**
31
+ * An {@link Element} of a {@link Variant}. Acts as a container for diverse definitions. Lives only in the context of a
32
+ * {@link Variant}.
33
+ *
34
+ * When used in typings, refer to via its alias {@link VariantElement} to prevent name clashes with the web APIs
35
+ * [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element) class
36
+ */
37
+ class Element extends variantParameterizable_1.VariantParameterizable {
38
+ /**
39
+ * Constructor.
40
+ */
41
+ constructor(variant, name) {
42
+ super(variant, name);
43
+ this.variant = variant;
44
+ this.name = name;
45
+ this.nodes = [];
46
+ this._dottedNodes = new Map();
47
+ if (process.env.NODE_ENV === 'development') {
48
+ this.assertPathDefinitions();
49
+ }
50
+ const nodes = this.variant.inheritedNodes.map(node => {
51
+ const nodeNamingStrategy = {
52
+ handler: variant.viewer.nodeNamingStrategyHandler,
53
+ payload: {
54
+ variantInstance: variant.variantInstance,
55
+ variant: variant,
56
+ variantParameterizable: this,
57
+ },
58
+ };
59
+ return (0, babylonHelper_1.cloneTransformNode)(node, nodeNamingStrategy, this.filterNode.bind(this), true);
60
+ });
61
+ // when cloning an instanced mesh the source mesh is still referencing to the source of the original mesh
62
+ // this is wrong, as we want to reference the cloned source
63
+ // this can only be done after the cloning process of the whole tree is finished, as we can't be sure if all
64
+ // instanced meshes or all sources meshes are cloning prior
65
+ const reassignSourceMesh = (node) => {
66
+ if (node instanceof instancedMesh_1.InstancedMesh) {
67
+ // find the cloned instance and source mesh
68
+ const clonedInstance = node;
69
+ const originalInstance = clonedInstance.metadata.cloneSource;
70
+ const originalSource = originalInstance.sourceMesh;
71
+ const clonedSource = originalSource.metadata.cloneTarget;
72
+ // it's not possible to just exchange the source mesh => readonly property
73
+ // we have to create a new clone with the correct source mesh
74
+ const exchangedInstance = clonedInstance.clone(clonedInstance.name, clonedInstance.parent, true, clonedSource);
75
+ // fix enabled and tags state after cloning
76
+ exchangedInstance.setEnabled(clonedInstance.isEnabled(false));
77
+ if (tags_1.Tags.HasTags(clonedInstance)) {
78
+ tags_1.Tags.AddTagsTo(exchangedInstance, tags_1.Tags.GetTags(clonedInstance, true));
79
+ }
80
+ // re-reference the childs of the cloned instance as well
81
+ const childs = clonedInstance.getChildTransformNodes(true);
82
+ childs.forEach(child => (child.parent = exchangedInstance));
83
+ // old instance mesh is not needed anymore and can be disposed
84
+ clonedInstance.dispose();
85
+ }
86
+ // recurse into childs
87
+ const children = node.getChildTransformNodes(true);
88
+ children.forEach(reassignSourceMesh);
89
+ };
90
+ nodes.forEach(node => {
91
+ if (node) {
92
+ reassignSourceMesh(node);
93
+ }
94
+ });
95
+ this.nodes = nodes.filter(node => !!node);
96
+ this._dottedNodes = (0, babylonHelper_1.mapToDottedNodes)(this.nodes, node => node instanceof transformNode_1.TransformNode);
97
+ variant.broadcastEvent(event_1.Event.ELEMENT_NODES_CLONED, this);
98
+ this.addMissingMaterialObservers();
99
+ this.addParameterObservers();
100
+ }
101
+ /**
102
+ * Creates an {@link Element} with given name.
103
+ */
104
+ static create(variant, name) {
105
+ return __awaiter(this, void 0, void 0, function* () {
106
+ const element = new Element(variant, name);
107
+ variant.broadcastEvent(event_1.Event.ELEMENT_CREATED, element);
108
+ return element;
109
+ });
110
+ }
111
+ /**
112
+ * The {@link DottedPath} in the built tree of {@link Element}s.
113
+ * E.g. "_.top-1.sub-2.sub-sub-3.el-1"
114
+ */
115
+ get dottedPath() {
116
+ return dottedPath_1.DottedPath.create(this.variant.dottedPath).addPart(this.name);
117
+ }
118
+ /**
119
+ * The id representing a {@link DottedPath}.
120
+ */
121
+ get id() {
122
+ const dottedPath = dottedPath_1.DottedPath.create(this.dottedPath);
123
+ dottedPath.shiftPart(); // remove root
124
+ return dottedPath.path;
125
+ }
126
+ /**
127
+ * The {@link ElementDefinition} of the {@link Element}.
128
+ */
129
+ get definition() {
130
+ const definition = this.variant.structureJson.elements[this.name];
131
+ if ((0, lodash_es_1.isArray)(definition)) {
132
+ return {
133
+ paths: {
134
+ include: definition,
135
+ },
136
+ };
137
+ }
138
+ return definition;
139
+ }
140
+ /**
141
+ * The {@link PathDefinitions} of the {@link Element}.
142
+ */
143
+ get pathDefinitions() {
144
+ return this.definition.paths;
145
+ }
146
+ /**
147
+ * The {@link TraceableDefinitions} of the {@link Element}.
148
+ */
149
+ get traceableDefinitions() {
150
+ return this.definition.traceables || {};
151
+ }
152
+ /**
153
+ * The {@link PaintableDefinition} of the {@link Element}.
154
+ */
155
+ get paintableDefinitions() {
156
+ return this.definition.paintables || {};
157
+ }
158
+ /**
159
+ * The inherited {@link ParameterBag}.
160
+ * Merges the {@link Variant}'s parameters and those from the {@link Element}.
161
+ */
162
+ get inheritedParameters() {
163
+ return (0, lodash_es_1.merge)({}, this.variant.inheritedParameters, this.parameters);
164
+ }
165
+ /**
166
+ * The actual {@link TransformNode}s that make up this {@link Element}.
167
+ */
168
+ get nodesFlat() {
169
+ return this.nodes.reduce((acc, cur) => [...acc, ...cur.getChildren(n => n instanceof transformNode_1.TransformNode, false)], this.nodes);
170
+ }
171
+ /**
172
+ * The actual {@link Mesh}es that make up this {@link Element}.
173
+ * Handy for e.g. creating a bounding box around an entire element.
174
+ */
175
+ get meshesFlat() {
176
+ return this.nodes.reduce((acc, cur) => [...acc, ...cur.getChildren(n => n instanceof mesh_1.Mesh, false)], this.nodes.filter(n => n instanceof mesh_1.Mesh));
177
+ }
178
+ /**
179
+ * The actual {@link AbstractMesh}es that make up this {@link Element}.
180
+ * I.e. (real) Meshes as well as InstancedMeshes.
181
+ */
182
+ get abstractMeshesFlat() {
183
+ return this.nodes.reduce((acc, cur) => [...acc, ...cur.getChildren(n => n instanceof abstractMesh_1.AbstractMesh, false)], this.nodes.filter(n => n instanceof abstractMesh_1.AbstractMesh));
184
+ }
185
+ /**
186
+ * The meshes of this {@link Element}.
187
+ */
188
+ get meshes() {
189
+ return this.nodes.filter(node => node instanceof mesh_1.Mesh);
190
+ }
191
+ /**
192
+ * @protected
193
+ */
194
+ set highlightLayer(layer) {
195
+ this._highlightLayer = layer;
196
+ }
197
+ get highlightLayer() {
198
+ return this._highlightLayer;
199
+ }
200
+ /**
201
+ * Destroys this {@link Element} and dispose all nodes.
202
+ */
203
+ destroy() {
204
+ this.nodes.forEach(node => {
205
+ node.dispose();
206
+ });
207
+ return this;
208
+ }
209
+ /**
210
+ * Gets a node by its {@link DottedPath}.
211
+ */
212
+ getNode(dottedPath) {
213
+ const _dottedPath = dottedPath_1.DottedPath.create(dottedPath);
214
+ const keys = Array.from(this._dottedNodes.keys()).map(dp => dp.path);
215
+ const values = Array.from(this._dottedNodes.values());
216
+ const node = values[keys.indexOf(_dottedPath.path)];
217
+ if (!node) {
218
+ throw new Error(`Node with path "${_dottedPath.path}" does not exist for element "${this.id}".`);
219
+ }
220
+ return node;
221
+ }
222
+ /**
223
+ * Gets a mesh by its {@link DottedPath}.
224
+ */
225
+ getMesh(dottedPath) {
226
+ return __awaiter(this, void 0, void 0, function* () {
227
+ const node = yield this.getNode(dottedPath);
228
+ if (node instanceof mesh_1.Mesh) {
229
+ return node;
230
+ }
231
+ return null;
232
+ });
233
+ }
234
+ /**
235
+ * Gets a {@link PaintableDefinition} by its name.
236
+ */
237
+ getPaintableDefinition(name) {
238
+ if (!this.paintableDefinitions || !this.paintableDefinitions[name]) {
239
+ throw new Error(`No paintable "${name}" defined for element "${this.id}".`);
240
+ }
241
+ return this.paintableDefinitions[name];
242
+ }
243
+ /**
244
+ * Gets a {@link TraceableDefinition} by its name.
245
+ */
246
+ getTraceableDefinition(name) {
247
+ if (!this.traceableDefinitions || !this.traceableDefinitions[name]) {
248
+ throw new Error(`No traceable "${name}" defined to add observer in element "${this.id}".`);
249
+ }
250
+ return this.traceableDefinitions[name];
251
+ }
252
+ /**
253
+ * Gets the paintable Node by its name.
254
+ */
255
+ getPaintableNode(name) {
256
+ return this.getNode(this.getPaintableDefinition(name).path);
257
+ }
258
+ /**
259
+ * Gets the traceable Node by its name.
260
+ */
261
+ getTraceableNode(name) {
262
+ return this.getNode(this.getTraceableDefinition(name).path);
263
+ }
264
+ /**
265
+ * @see {@link VariantParameterizable.commitParameters}
266
+ * @emit {@link Event.ELEMENT_PARAMETER_COMMITTED}
267
+ */
268
+ commitParameters(parameters) {
269
+ const _super = Object.create(null, {
270
+ commitParameters: { get: () => super.commitParameters }
271
+ });
272
+ return __awaiter(this, void 0, void 0, function* () {
273
+ return _super.commitParameters.call(this, parameters);
274
+ });
275
+ }
276
+ /**
277
+ * Adds an observer function for camera matrix changes.
278
+ * The `observer` gets 2 parameters: the `AbstractMesh` and a `ClientRect` object.
279
+ */
280
+ addTraceableObserver(traceable, observer, payload) {
281
+ if (payload && !(payload instanceof HTMLElement)) {
282
+ throw new Error(`Payload for camera observer must be an instance of "HTMLElement" in element "${this.id}".`);
283
+ }
284
+ if (!this.traceableDefinitions || !this.traceableDefinitions[traceable]) {
285
+ throw new Error(`No traceable "${traceable}" defined to add observer in element "${this.id}".`);
286
+ }
287
+ const node = this.getTraceableNode(traceable);
288
+ if (!(node instanceof abstractMesh_1.AbstractMesh)) {
289
+ throw new Error(`The path must be an instance of "AbstractMesh" for camera observer in element "${this.id}".`);
290
+ }
291
+ this.variant.viewer.sceneManager.activeCamera.onViewMatrixChangedObservable.add((eventData, eventState) => {
292
+ const position = (0, babylonHelper_1.getClientRectFromMesh)(node, this.variant.viewer.scene, this.variant.viewer.canvas);
293
+ if (payload) {
294
+ payload.style.top = `${position.top}px`;
295
+ payload.style.left = `${position.left}px`;
296
+ }
297
+ observer(node, position);
298
+ });
299
+ return this;
300
+ }
301
+ /**
302
+ * Draws a `ImageBitmap` or `OffscreenCanvas` onto a `paintable` defined via {@link PaintableDefinition}.
303
+ */
304
+ drawPaintable(paintable, imageSource) {
305
+ // node and material checks and preperation
306
+ const node = this.getPaintableNode(paintable);
307
+ if (!(node instanceof abstractMesh_1.AbstractMesh)) {
308
+ throw new Error(`The path must be an instance of "AbstractMesh" for paintable "${paintable}" ` + `in element "${this.id}".`);
309
+ }
310
+ if (node.material && this.variant.viewer.cloneMaterialsOnMutation) {
311
+ if (node.material && !(0, lodash_es_1.has)(node.metadata, 'dirty.material')) {
312
+ (0, babylonHelper_1.cloneTransformNodeMaterial)(node);
313
+ }
314
+ if (!(0, lodash_es_1.has)(node.metadata, 'dirty.material.texture')) {
315
+ // inject initial value and mark as dirty
316
+ (0, babylonHelper_1.injectMetadata)(node, { dirty: { material: { texture: true } } });
317
+ }
318
+ }
319
+ else if (!node.material) {
320
+ // paintables should also work if no material is assigned to the node per default
321
+ // in this case a certain fallback material with fitting default values is defined
322
+ const pbrMat = new pbrMaterial_1.PBRMaterial(`${this.id}.${paintable}.material`, this.variant.viewer.scene);
323
+ pbrMat.roughness = 1.0;
324
+ pbrMat.metallic = 1.0;
325
+ node.material = pbrMat;
326
+ }
327
+ // TODO: Not sure if this really should be overwritten every time the paintable is redrawn.
328
+ // Maybe the 3D artist had something in mind when setting a certain transparency mode, which would get overwritten
329
+ // in this case.
330
+ // Applying it as a default if no material is set is fine though (see roughness/metallic some lines above).
331
+ // Removing this line will most likely break existing configurators, so be cautios!
332
+ node.material.transparencyMode = material_1.Material.MATERIAL_ALPHATESTANDBLEND;
333
+ const paintableDefinition = this.getPaintableDefinition(paintable);
334
+ if ((0, lodash_es_1.isNumber)(paintableDefinition.textureOptions)) {
335
+ // convert into object to satisfy the interface of `drawPaintableOnMaterial`
336
+ paintableDefinition.textureOptions = {
337
+ width: paintableDefinition.textureOptions,
338
+ height: paintableDefinition.textureOptions,
339
+ };
340
+ }
341
+ // convert texture size options into scale values, as the scaling is now done via uvScale properties
342
+ const paintableOptions = {};
343
+ if (paintableDefinition.textureOptions.width) {
344
+ paintableOptions.uScale = paintableDefinition.textureOptions.width / imageSource.width;
345
+ }
346
+ if (paintableDefinition.textureOptions.height) {
347
+ paintableOptions.vScale = paintableDefinition.textureOptions.height / imageSource.height;
348
+ }
349
+ // forward to generic "paintable" call
350
+ (0, babylonHelper_1.drawPaintableOnMaterial)(node.material, imageSource, this.variant.viewer.scene, paintableOptions);
351
+ return this;
352
+ }
353
+ /**
354
+ * Draws a SVG string onto a `paintable` defined via {@link PaintableDefinition}.
355
+ */
356
+ drawPaintableFromSvg(paintable, svgSource) {
357
+ return __awaiter(this, void 0, void 0, function* () {
358
+ const image = yield (0, resourceHelper_1.createImageFromSvg)(svgSource);
359
+ return this.drawPaintable(paintable, image);
360
+ });
361
+ }
362
+ /**
363
+ * Draws an Image from source (URL/URI) onto a `paintable` defined via {@link PaintableDefinition}.
364
+ */
365
+ drawPaintableFromImgSrc(paintable, imgSource) {
366
+ return __awaiter(this, void 0, void 0, function* () {
367
+ const image = yield (0, resourceHelper_1.createImageFromImgSrc)(imgSource);
368
+ return this.drawPaintable(paintable, image);
369
+ });
370
+ }
371
+ /**
372
+ * Adds observers for loading missing materials where needed
373
+ */
374
+ addMissingMaterialObservers() {
375
+ for (const abstractMesh of this.abstractMeshesFlat) {
376
+ (0, sceneLoaderHelper_1.addMissingMaterialObserver)(abstractMesh);
377
+ }
378
+ }
379
+ /**
380
+ * Adds the default {@link ParameterObserver}s which are called every time {@link commitParameters} is called.
381
+ */
382
+ addParameterObservers() {
383
+ this._parameterObservers.set(parameter_1.Parameter.VISIBLE, [
384
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
385
+ let visible;
386
+ try {
387
+ visible = parameter_1.Parameter.parseBoolean(newValue);
388
+ }
389
+ catch (e) {
390
+ return;
391
+ }
392
+ if (visible === true) {
393
+ // Override visible state and materials if tag manager is in use to support texture lazy loading
394
+ if (element.variant.viewer.tagManager.parameters.size > 0) {
395
+ const hiddenNodeNames = [];
396
+ element.variant.viewer.tagManager.parameters.forEach((parameterBag, subject) => {
397
+ const nodes = element.variant.viewer.tagManager.getNodesBySubject(subject);
398
+ const visibleParamValue = parameterBag[parameter_1.Parameter.VISIBLE];
399
+ const isVisible = undefined === visibleParamValue || parameter_1.Parameter.parseBoolean(visibleParamValue);
400
+ if (!isVisible) {
401
+ hiddenNodeNames.push(...nodes.map(node => node.name));
402
+ }
403
+ // overwrite materials immediatly, so that original material of node doesn't get loaded, since it could
404
+ // be exchanged by the tag manager
405
+ if (parameterBag[parameter_1.Parameter.MATERIAL]) {
406
+ nodes.forEach(node => (0, babylonHelper_1.setMaterial)(node, parameterBag[parameter_1.Parameter.MATERIAL], false));
407
+ }
408
+ });
409
+ // enable all nodes that are not explicitely hidden by the tag manager
410
+ element.nodesFlat.map(node => {
411
+ if (!hiddenNodeNames.includes(node.name)) {
412
+ node.setEnabled(true);
413
+ }
414
+ });
415
+ return;
416
+ }
417
+ // The default behaviour.
418
+ element.nodes.forEach(node => {
419
+ (0, babylonHelper_1.injectMetadata)(node, { visibility: node.isEnabled() });
420
+ (0, babylonHelper_1.activateTransformNode)(node);
421
+ });
422
+ }
423
+ else if (visible === false) {
424
+ element.nodes.forEach(node => {
425
+ (0, babylonHelper_1.injectMetadata)(node, { visibility: node.isEnabled() });
426
+ (0, babylonHelper_1.deactivateTransformNode)(node);
427
+ });
428
+ }
429
+ }),
430
+ ]);
431
+ this._parameterObservers.set(parameter_1.Parameter.SCALING, [
432
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
433
+ // we have to deal just with root nodes here due to relative impacts in a node tree
434
+ element.nodes.forEach(node => (0, babylonHelper_1.transformTransformNode)(node, {
435
+ scaling: parameter_1.Parameter.parseVector(newValue),
436
+ position: parameter_1.Parameter.parseVector(element.inheritedParameters[parameter_1.Parameter.POSITION] || parameter_1.Parameter.getDefaultValue(parameter_1.Parameter.POSITION)),
437
+ rotation: parameter_1.Parameter.parseRotation(element.inheritedParameters[parameter_1.Parameter.ROTATION] || parameter_1.Parameter.getDefaultValue(parameter_1.Parameter.ROTATION)),
438
+ }));
439
+ }),
440
+ ]);
441
+ this._parameterObservers.set(parameter_1.Parameter.POSITION, [
442
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
443
+ // we have to deal just with root nodes here due to relative impacts in a node tree
444
+ element.nodes.forEach(node => (0, babylonHelper_1.transformTransformNode)(node, {
445
+ scaling: parameter_1.Parameter.parseVector(element.inheritedParameters[parameter_1.Parameter.SCALING] || parameter_1.Parameter.getDefaultValue(parameter_1.Parameter.SCALING)),
446
+ position: parameter_1.Parameter.parseVector(newValue),
447
+ rotation: parameter_1.Parameter.parseRotation(element.inheritedParameters[parameter_1.Parameter.ROTATION] || parameter_1.Parameter.getDefaultValue(parameter_1.Parameter.ROTATION)),
448
+ }));
449
+ }),
450
+ ]);
451
+ this._parameterObservers.set(parameter_1.Parameter.ROTATION, [
452
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
453
+ // we have to deal just with root nodes here due to relative impacts in a node tree
454
+ element.nodes.forEach(node => (0, babylonHelper_1.transformTransformNode)(node, {
455
+ scaling: parameter_1.Parameter.parseVector(element.inheritedParameters[parameter_1.Parameter.SCALING] || parameter_1.Parameter.getDefaultValue(parameter_1.Parameter.SCALING)),
456
+ position: parameter_1.Parameter.parseVector(element.inheritedParameters[parameter_1.Parameter.POSITION] || parameter_1.Parameter.getDefaultValue(parameter_1.Parameter.POSITION)),
457
+ rotation: parameter_1.Parameter.parseRotation(newValue),
458
+ }));
459
+ }),
460
+ ]);
461
+ this._parameterObservers.set(parameter_1.Parameter.MATERIAL, [
462
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
463
+ const materialName = newValue.toString();
464
+ for (const node of element.nodes) {
465
+ (0, babylonHelper_1.assertTransformNode)(node, node => (0, babylonHelper_1.assertMeshCapability)(node, parameter_1.Parameter.MATERIAL));
466
+ (0, babylonHelper_1.setMaterial)(node, materialName, true, element.variant);
467
+ }
468
+ }),
469
+ ]);
470
+ this._parameterObservers.set(parameter_1.Parameter.MATERIAL_COLOR, [
471
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
472
+ const color = parameter_1.Parameter.parseColor(newValue);
473
+ element.nodes.forEach(node => {
474
+ (0, babylonHelper_1.assertTransformNode)(node, node => (0, babylonHelper_1.assertMeshCapability)(node, parameter_1.Parameter.MATERIAL_COLOR));
475
+ if (this.variant.viewer.cloneMaterialsOnMutation) {
476
+ if (!(0, lodash_es_1.has)(node.metadata, 'dirty.material')) {
477
+ (0, babylonHelper_1.cloneTransformNodeMaterial)(node);
478
+ }
479
+ if (!(0, lodash_es_1.has)(node.metadata, 'dirty.material.color')) {
480
+ // inject initial value and mark as dirty
481
+ (0, babylonHelper_1.injectMetadata)(node, { dirty: { material: { color: oldValue } } });
482
+ }
483
+ }
484
+ (0, babylonHelper_1.setMaterialColor)(node, color);
485
+ });
486
+ }),
487
+ ]);
488
+ this._parameterObservers.set(parameter_1.Parameter.MATERIAL_METALLNESS, [
489
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
490
+ const metallness = parameter_1.Parameter.parseNumber(newValue);
491
+ element.nodes.forEach(node => {
492
+ (0, babylonHelper_1.assertTransformNode)(node, node => (0, babylonHelper_1.assertMeshCapability)(node, parameter_1.Parameter.MATERIAL_METALLNESS));
493
+ if (this.variant.viewer.cloneMaterialsOnMutation) {
494
+ if (!(0, lodash_es_1.has)(node.metadata, 'dirty.material')) {
495
+ (0, babylonHelper_1.cloneTransformNodeMaterial)(node);
496
+ }
497
+ if (!(0, lodash_es_1.has)(node.metadata, 'dirty.material.metallness')) {
498
+ // inject initial value and mark as dirty
499
+ (0, babylonHelper_1.injectMetadata)(node, { dirty: { material: { metallness: oldValue } } });
500
+ }
501
+ }
502
+ (0, babylonHelper_1.setMaterialMetallness)(node, metallness);
503
+ });
504
+ }),
505
+ ]);
506
+ this._parameterObservers.set(parameter_1.Parameter.MATERIAL_ROUGHNESS, [
507
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
508
+ const roughness = parameter_1.Parameter.parseNumber(newValue);
509
+ element.nodes.forEach(node => {
510
+ (0, babylonHelper_1.assertTransformNode)(node, node => (0, babylonHelper_1.assertMeshCapability)(node, parameter_1.Parameter.MATERIAL_ROUGHNESS));
511
+ if (this.variant.viewer.cloneMaterialsOnMutation) {
512
+ if (!(0, lodash_es_1.has)(node.metadata, 'dirty.material')) {
513
+ (0, babylonHelper_1.cloneTransformNodeMaterial)(node);
514
+ }
515
+ if (!(0, lodash_es_1.has)(node.metadata, 'dirty.material.roughness')) {
516
+ // inject initial value and mark as dirty
517
+ (0, babylonHelper_1.injectMetadata)(node, { dirty: { material: { roughness: oldValue } } });
518
+ }
519
+ }
520
+ (0, babylonHelper_1.setMaterialRoughness)(node, roughness);
521
+ });
522
+ }),
523
+ ]);
524
+ this._parameterObservers.set(parameter_1.Parameter.HIGHLIGHT_COLOR, [
525
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
526
+ var _a;
527
+ // trigger Parameter.HIGHLIGHTED observers
528
+ (_a = this._parameterObservers.get(parameter_1.Parameter.HIGHLIGHTED)) === null || _a === void 0 ? void 0 : _a.forEach(observer => {
529
+ observer(element, !!element.highlighted, !!element.highlighted);
530
+ });
531
+ }),
532
+ ]);
533
+ this._parameterObservers.set(parameter_1.Parameter.HIGHLIGHT_ENABLED, [
534
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
535
+ var _b;
536
+ let curr;
537
+ try {
538
+ curr = parameter_1.Parameter.parseBoolean(newValue);
539
+ }
540
+ catch (e) {
541
+ return;
542
+ }
543
+ switch (curr) {
544
+ case true:
545
+ if (element._highlightLayer) {
546
+ break;
547
+ }
548
+ element.highlightLayer = new highlightLayer_1.HighlightLayer(dottedPath_1.DottedPath.create(element.id).addPart('highlight').path, element.variant.viewer.scene);
549
+ break;
550
+ case false:
551
+ (_b = element.highlightLayer) === null || _b === void 0 ? void 0 : _b.dispose();
552
+ break;
553
+ }
554
+ }),
555
+ ]);
556
+ this._parameterObservers.set(parameter_1.Parameter.HIGHLIGHTED, [
557
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
558
+ var _c;
559
+ if (!element.inheritedParameters[parameter_1.Parameter.HIGHLIGHT_ENABLED]) {
560
+ return;
561
+ }
562
+ // trigger Parameter.HIGHLIGHT_ENABLED observers to ensure HighlightLayer
563
+ (_c = this._parameterObservers.get(parameter_1.Parameter.HIGHLIGHT_ENABLED)) === null || _c === void 0 ? void 0 : _c.forEach(observer => {
564
+ observer(element, this.parameters[parameter_1.Parameter.HIGHLIGHT_ENABLED], true);
565
+ });
566
+ let highlighted;
567
+ try {
568
+ highlighted = parameter_1.Parameter.parseBoolean(newValue);
569
+ }
570
+ catch (e) {
571
+ return;
572
+ }
573
+ // Add/Remove meshes to previously created highlight layers.
574
+ if (highlighted === true) {
575
+ element.nodes.forEach(node => {
576
+ (0, babylonHelper_1.assertTransformNode)(node, node => (0, babylonHelper_1.assertMeshCapability)(node, parameter_1.Parameter.HIGHLIGHTED));
577
+ let color = math_color_1.Color3.Green();
578
+ if (element.inheritedParameters[parameter_1.Parameter.HIGHLIGHT_COLOR]) {
579
+ color = parameter_1.Parameter.parseColor(element.inheritedParameters[parameter_1.Parameter.HIGHLIGHT_COLOR]);
580
+ }
581
+ if (typeof element._highlightLayer !== 'undefined') {
582
+ (0, babylonHelper_1.addToHighlightLayer)(element._highlightLayer, color, node);
583
+ }
584
+ });
585
+ }
586
+ else if (highlighted === false) {
587
+ element.nodes.forEach(node => {
588
+ if (typeof element._highlightLayer !== 'undefined') {
589
+ (0, babylonHelper_1.removeFromHighlightLayer)(element._highlightLayer, node);
590
+ }
591
+ });
592
+ }
593
+ }),
594
+ ]);
595
+ this._parameterObservers.set(parameter_1.Parameter.CAST_SHADOW, [
596
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
597
+ let castShadow;
598
+ try {
599
+ castShadow = parameter_1.Parameter.parseBoolean(newValue);
600
+ }
601
+ catch (e) {
602
+ return;
603
+ }
604
+ let lightCsl = element.inheritedParameters[parameter_1.Parameter.CAST_SHADOW_FROM_LIGHTS];
605
+ if (!lightCsl) {
606
+ lightCsl = element.variant.inheritedViewerLights.map(l => l.name).join(',');
607
+ }
608
+ if (castShadow === true) {
609
+ yield this.castShadowValueHandler(lightCsl, babylonHelper_1.addToShadowGenerator);
610
+ }
611
+ if (castShadow === false) {
612
+ yield this.castShadowValueHandler(lightCsl, babylonHelper_1.removeFromShadowGenerator);
613
+ }
614
+ }),
615
+ ]);
616
+ this._parameterObservers.set(parameter_1.Parameter.CAST_SHADOW_FROM_LIGHTS, [
617
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
618
+ // TODO: Possible performance issue in combination with CAST_SHADOW, since both observers are initially called
619
+ // when CAST_SHADOW and CAST_SHADOW_FROM_LIGHTS are defined in the spec.
620
+ const lightCsl = element.variant.inheritedViewerLights.map(l => l.name).join(',');
621
+ // cleanup all shadow generators
622
+ yield this.castShadowValueHandler(lightCsl, babylonHelper_1.removeFromShadowGenerator);
623
+ if (element.castShadow === true) {
624
+ // if newValue is undefined or '' then set newValue to lightCsl (use all lights)
625
+ if (!newValue) {
626
+ newValue = lightCsl;
627
+ }
628
+ yield this.castShadowValueHandler(newValue, babylonHelper_1.addToShadowGenerator);
629
+ }
630
+ }),
631
+ ]);
632
+ this._parameterObservers.set(parameter_1.Parameter.RECEIVE_SHADOWS, [
633
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
634
+ element.nodes.forEach(node => (0, babylonHelper_1.setReceiveShadows)(node, parameter_1.Parameter.parseBoolean(newValue)));
635
+ }),
636
+ ]);
637
+ return this;
638
+ }
639
+ /**
640
+ * Filter for nodes that are not defined or excluded in the `pathDefinitions`.
641
+ */
642
+ filterNode(node) {
643
+ let includeTokens = [];
644
+ this.pathDefinitions.include.forEach(dottedPath => {
645
+ includeTokens = (0, lodash_es_1.union)(includeTokens, dottedPath_1.DottedPath.create(dottedPath).leafTokens);
646
+ });
647
+ const shallKeep = (dp) => {
648
+ const isDirectlyIncluded = !!includeTokens.find(_dp => _dp === dp.path);
649
+ const isSubOfIncluded = !!this.pathDefinitions.include.find(_dp => {
650
+ return dp.path.startsWith(`${_dp}${dottedPath_1.DottedPath.DELIMITER}`);
651
+ });
652
+ return isDirectlyIncluded || isSubOfIncluded;
653
+ };
654
+ const shallExclude = (dp) => {
655
+ if (!this.pathDefinitions.exclude) {
656
+ return false;
657
+ }
658
+ return !!this.pathDefinitions.exclude.find(_dp => dp.path === _dp);
659
+ };
660
+ return shallKeep(node.metadata.dottedPath) && !shallExclude(node.metadata.dottedPath);
661
+ }
662
+ /**
663
+ * Asserts whether all {@link pathDefinitions} link to a valid `Node` path.
664
+ * @protected
665
+ */
666
+ assertPathDefinitions() {
667
+ var _a;
668
+ const dottedNodes = this.variant.inheritedDottedNodes;
669
+ if (dottedNodes.size === 0) {
670
+ throw new Error(`There are no mapped nodes for element "${this.id}".`);
671
+ }
672
+ const include = this.pathDefinitions.include.map(path => dottedPath_1.DottedPath.create(path).path);
673
+ const exclude = (_a = this.pathDefinitions.exclude) === null || _a === void 0 ? void 0 : _a.map(path => dottedPath_1.DottedPath.create(path).path);
674
+ const keys = Array.from(dottedNodes.keys()).map(dottedPath => dottedPath.path);
675
+ (0, lodash_es_1.union)(include, exclude).forEach(path => {
676
+ if (keys.indexOf(path) === -1) {
677
+ throw new Error(`Node with path "${path}" does not exist for element "${this.id}".`);
678
+ }
679
+ });
680
+ }
681
+ /**
682
+ * Handles callback for given light parameter.
683
+ */
684
+ castShadowValueHandler(lightCsl, mutator) {
685
+ return __awaiter(this, void 0, void 0, function* () {
686
+ const lights = [];
687
+ for (const lightName of parameter_1.Parameter.parseCommaSeparatedList(lightCsl)) {
688
+ const viewerLight = yield this.variant.getViewerLight(lightName);
689
+ if (viewerLight) {
690
+ lights.push(viewerLight.light);
691
+ }
692
+ }
693
+ const shadowGenerators = lights.map(light => light === null || light === void 0 ? void 0 : light.getShadowGenerator()).filter(Boolean);
694
+ shadowGenerators.forEach(generator => {
695
+ this.nodes.forEach(node => {
696
+ mutator(generator, node);
697
+ });
698
+ });
699
+ });
700
+ }
701
+ }
702
+ exports.Element = Element;
703
703
  //# sourceMappingURL=element.js.map