@combeenation/3d-viewer 4.0.0-beta3 → 4.0.0-beta4

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