@combeenation/3d-viewer 3.1.1-alpha7 → 3.1.2

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