@combeenation/3d-viewer 6.1.0 → 6.2.0

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