@combeenation/3d-viewer 5.0.1 → 5.0.3-beta2

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