@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,829 +1,842 @@
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.Variant = 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 dottedPath_1 = require("./dottedPath");
17
- const element_1 = require("./element");
18
- const event_1 = require("./event");
19
- const parameter_1 = require("./parameter");
20
- const parameterizable_1 = require("./parameterizable");
21
- const viewerLight_1 = require("./viewerLight");
22
- require("@babylonjs/core/Loading/Plugins/babylonFileLoader");
23
- const sceneLoader_1 = require("@babylonjs/core/Loading/sceneLoader");
24
- const transformNode_1 = require("@babylonjs/core/Meshes/transformNode");
25
- const assetContainer_1 = require("@babylonjs/core/assetContainer");
26
- require("@babylonjs/loaders/glTF/2.0/Extensions/KHR_draco_mesh_compression");
27
- require("@babylonjs/loaders/glTF/2.0/Extensions/KHR_materials_clearcoat");
28
- require("@babylonjs/loaders/glTF/2.0/Extensions/KHR_materials_ior");
29
- require("@babylonjs/loaders/glTF/2.0/Extensions/KHR_materials_pbrSpecularGlossiness");
30
- require("@babylonjs/loaders/glTF/2.0/Extensions/KHR_materials_sheen");
31
- require("@babylonjs/loaders/glTF/2.0/Extensions/KHR_materials_specular");
32
- require("@babylonjs/loaders/glTF/2.0/Extensions/KHR_materials_translucency");
33
- require("@babylonjs/loaders/glTF/2.0/Extensions/KHR_materials_transmission");
34
- require("@babylonjs/loaders/glTF/2.0/Extensions/KHR_materials_unlit");
35
- require("@babylonjs/loaders/glTF/2.0/Extensions/KHR_materials_variants");
36
- require("@babylonjs/loaders/glTF/2.0/Extensions/KHR_texture_basisu");
37
- require("@babylonjs/loaders/glTF/2.0/Extensions/KHR_texture_transform");
38
- require("@babylonjs/loaders/glTF/2.0/glTFLoader");
39
- const lodash_es_1 = require("lodash-es");
40
- /**
41
- * A concrete "Variant". Most of these are handled by either the {@link Viewer} or {@link VariantInstance}.
42
- */
43
- class Variant extends parameterizable_1.Parameterizable {
44
- /**
45
- * Constructor.
46
- */
47
- constructor(name, _structureJson, viewer, parent) {
48
- super((0, lodash_es_1.cloneDeep)(_structureJson.parameterDeclaration), (0, lodash_es_1.cloneDeep)(_structureJson.parameters));
49
- this.name = name;
50
- this._structureJson = _structureJson;
51
- this.viewer = viewer;
52
- this.parent = parent;
53
- this.elements = [];
54
- this.viewerLights = [];
55
- this._children = new Map();
56
- this._parameterObservers = new Map();
57
- /**
58
- * @internal
59
- */
60
- this.parametersInitialized = false;
61
- for (const parameter in _structureJson.parameterDeclaration) {
62
- if (!(parameter in (_structureJson.parameters || []))) {
63
- throw new Error(`No default value for parameter "${parameter}" defined.`);
64
- }
65
- }
66
- this.assetContainer = new assetContainer_1.AssetContainer(viewer.scene);
67
- this.structureJson = (0, lodash_es_1.cloneDeep)(_structureJson);
68
- }
69
- /**
70
- * Creates a {@link Variant} based on given parameters.
71
- *
72
- * @throws Error if "gltf" property is provided without a filename
73
- */
74
- static create(name, structureJson, viewer, parent) {
75
- return __awaiter(this, void 0, void 0, function* () {
76
- const variant = new Variant(name, structureJson, viewer, parent);
77
- yield variant.loadAssets();
78
- return variant;
79
- });
80
- }
81
- /**
82
- * The ancestor {@link Variant}s ordered from top to bottom in the built tree.
83
- */
84
- get ancestors() {
85
- const ancestors = [];
86
- let variant = this;
87
- while (variant.parent) {
88
- ancestors.unshift(variant.parent);
89
- variant = variant.parent;
90
- }
91
- return ancestors;
92
- }
93
- /**
94
- * The root {@link Variant}.
95
- */
96
- get root() {
97
- var _a;
98
- return (_a = this.ancestors[0]) !== null && _a !== void 0 ? _a : this;
99
- }
100
- /**
101
- * The {@link DottedPath} in the built tree of {@link Variant}s.
102
- * E.g. "_.top-1.sub-2.sub-sub-3"
103
- */
104
- get dottedPath() {
105
- const parentIds = this.ancestors.map(ancestor => {
106
- return ancestor.name;
107
- });
108
- return dottedPath_1.DottedPath.createFromParts(parentIds).addPart(this.name);
109
- }
110
- /**
111
- * The id representing a {@link DottedPath}.
112
- */
113
- get id() {
114
- const dottedPath = dottedPath_1.DottedPath.create(this.dottedPath);
115
- dottedPath.shiftPart(); // remove root
116
- return dottedPath.path || '_';
117
- }
118
- /**
119
- * The defined glTF Asset.
120
- */
121
- get glTF() {
122
- if (this.structureJson.glTF) {
123
- if ((0, lodash_es_1.isString)(this.structureJson.glTF)) {
124
- return {
125
- rootUrl: this.structureJson.glTF,
126
- fileName: undefined,
127
- };
128
- }
129
- if ((0, lodash_es_1.isEmpty)(this.structureJson.glTF.rootUrl)) {
130
- throw new Error(`No "rootUrl" defined in "glTF" definition for variant "${this.id}".`);
131
- }
132
- return this.structureJson.glTF;
133
- }
134
- }
135
- /**
136
- * The defined glTF URI.
137
- */
138
- get glTFUri() {
139
- if (this.glTF) {
140
- return [this.glTF.rootUrl, this.glTF.fileName].join('');
141
- }
142
- }
143
- /**
144
- * The inherited defined glTF URI.
145
- */
146
- get inheritedGlTFUri() {
147
- if (!this.glTFUri && this.parent) {
148
- return this.parent.inheritedGlTFUri;
149
- }
150
- return this.glTFUri;
151
- }
152
- /**
153
- * The TransformNodes of the {@link Variant}.
154
- */
155
- get nodes() {
156
- const rootNodes = this.assetContainer
157
- .getNodes()
158
- .filter(n => n instanceof transformNode_1.TransformNode && !n.parent);
159
- return rootNodes;
160
- }
161
- /**
162
- * The {@link ViewerLight}s of the {@link Variant}.
163
- */
164
- get lights() {
165
- return this.assetContainer.lights;
166
- }
167
- /**
168
- * All TransformNodes of the {@link Variant} mapped flat with a {@link DottedPath}.
169
- */
170
- get dottedNodes() {
171
- if (!this._dottedNodes) {
172
- const nodes = this.assetContainer.getNodes().filter(n => n instanceof transformNode_1.TransformNode);
173
- const dottedNodes = new Map();
174
- nodes.forEach(node => {
175
- dottedNodes.set(node.metadata.dottedPath, node);
176
- });
177
- this._dottedNodes = dottedNodes;
178
- }
179
- return this._dottedNodes;
180
- }
181
- /**
182
- * The Materials of the {@link Variant}.
183
- */
184
- get materials() {
185
- return this.assetContainer.materials;
186
- }
187
- /**
188
- * All {@link Element}s from this {@link Variant}'s parents.
189
- */
190
- get inheritedElements() {
191
- let elements = [];
192
- this.ancestors.forEach(ancestor => {
193
- elements = (0, lodash_es_1.concat)(elements, ancestor.elements);
194
- });
195
- return (0, lodash_es_1.concat)(elements, this.elements);
196
- }
197
- /**
198
- * All {@link ViewerLight}s inherited from this {@link Variant}'s parents.
199
- */
200
- get inheritedViewerLights() {
201
- let viewerLights = [];
202
- this.ancestors.forEach(ancestor => {
203
- viewerLights = (0, lodash_es_1.concat)(viewerLights, ancestor.viewerLights);
204
- });
205
- return (0, lodash_es_1.concat)(viewerLights, this.viewerLights);
206
- }
207
- /**
208
- * All TransformNodes inherited from this {@link Variant}'s parents.
209
- */
210
- get inheritedNodes() {
211
- let nodes = [];
212
- this.ancestors.forEach(ancestor => {
213
- nodes = (0, lodash_es_1.concat)(nodes, ancestor.nodes);
214
- });
215
- return (0, lodash_es_1.concat)(nodes, this.nodes);
216
- }
217
- /**
218
- * All TransformNodes inherited from this {@link Variant}'s parents mapped flat with a {@link DottedPath}.
219
- */
220
- get inheritedDottedNodes() {
221
- let dottedNodes = this.dottedNodes;
222
- this.ancestors.forEach(ancestor => {
223
- dottedNodes = (0, resourceHelper_1.mergeMaps)(dottedNodes, ancestor.dottedNodes);
224
- });
225
- return dottedNodes;
226
- }
227
- /**
228
- * All Lights inherited from this {@link Variant}'s parents.
229
- */
230
- get inheritedLights() {
231
- let lights = [];
232
- this.ancestors.forEach(ancestor => {
233
- lights = (0, lodash_es_1.concat)(lights, ancestor.lights);
234
- });
235
- return (0, lodash_es_1.concat)(lights, this.lights);
236
- }
237
- /**
238
- * The {@link ParameterDeclarations} inherited from this {@link Variant}'s parents.
239
- */
240
- get inheritedParameterDeclaration() {
241
- const declaration = {};
242
- this.ancestors.forEach(ancestor => {
243
- (0, lodash_es_1.merge)(declaration, ancestor.parameterDeclaration);
244
- });
245
- return (0, lodash_es_1.merge)(declaration, this.parameterDeclaration);
246
- }
247
- /**
248
- * The {@link ParameterBag} inherited from this {@link Variant}'s parents.
249
- */
250
- get inheritedParameters() {
251
- const parameters = {};
252
- this.ancestors.forEach(ancestor => {
253
- (0, lodash_es_1.merge)(parameters, ancestor.parameters);
254
- });
255
- return (0, lodash_es_1.merge)(parameters, this.parameters);
256
- }
257
- /**
258
- * All Materials from this {@link Variant}'s parents.
259
- */
260
- get inheritedMaterials() {
261
- let materials = [];
262
- this.ancestors.forEach(ancestor => {
263
- materials = (0, lodash_es_1.concat)(materials, ancestor.materials);
264
- });
265
- return (0, lodash_es_1.concat)(materials, this.materials);
266
- }
267
- /**
268
- * Gets the direct children of the current {@link Variant}.
269
- */
270
- getChildren() {
271
- return __awaiter(this, void 0, void 0, function* () {
272
- const children = [];
273
- for (const name in this.structureJson.variants) {
274
- children.push(yield this.getDescendant(name));
275
- }
276
- return children;
277
- });
278
- }
279
- /**
280
- * Gets a descendant {@link Variant} of the current {@link Variant} relative to its {@link DottedPath}.
281
- * If you have the dotted path `_.product_x.variant_blue.with_yellow_highlight` in a tree and you operate on the
282
- * `product_x`, you can call `this.getDescendant('variant_blue.with_yellow_highlight')` to get the lowermost
283
- * {@link Variant}.
284
- */
285
- getDescendant(dottedPath) {
286
- return __awaiter(this, void 0, void 0, function* () {
287
- const _dottedPath = dottedPath_1.DottedPath.create(dottedPath);
288
- const [name, ...descendantParts] = _dottedPath.parts;
289
- let variant;
290
- if (this._children.has(name)) {
291
- variant = this._children.get(name);
292
- }
293
- else {
294
- if (!this.structureJson.variants) {
295
- throw new Error(`Missing key "variants" in JSON structure for variant "${this.id}".`);
296
- }
297
- if (!this.structureJson.variants[name]) {
298
- throw new Error(`Variant "${_dottedPath.path}" not defined in JSON structure for variant "${this.id}".`);
299
- }
300
- if (this.structureJson.variants[name].file) {
301
- const file = this.structureJson.variants[name].file;
302
- variant = yield Variant.create(name, yield (0, resourceHelper_1.loadJson)(file), this.viewer, this);
303
- }
304
- else {
305
- variant = yield Variant.create(name, this.structureJson.variants[name], this.viewer, this);
306
- }
307
- this._children.set(name, variant);
308
- }
309
- if (!variant) {
310
- throw new Error(`Variant "${_dottedPath.path}" was not created.`);
311
- }
312
- if (descendantParts.length > 0) {
313
- return yield variant.getDescendant(dottedPath_1.DottedPath.createFromParts(descendantParts));
314
- }
315
- return variant;
316
- });
317
- }
318
- /**
319
- * Gets the desired {@link Element} of the current {@link Variant} relative to its {@link DottedPath}.
320
- * Uses the mechanism of {@link getDescendant} to resolve the appropriate variant in tree.
321
- */
322
- getElement(dottedPath) {
323
- return __awaiter(this, void 0, void 0, function* () {
324
- const _dottedPath = dottedPath_1.DottedPath.create(dottedPath);
325
- const elementName = _dottedPath.popPart();
326
- let variant = this;
327
- if (_dottedPath.parts.length > 0) {
328
- variant = yield this.getDescendant(_dottedPath);
329
- }
330
- if (variant.inheritedElements.length === 0) {
331
- throw new Error(`No elements for variant "${variant.id}" found. ` +
332
- `Either none are defined or they are not initialized (are you operating on the appropriate living?).`);
333
- }
334
- let element;
335
- variant.inheritedElements.forEach(_element => {
336
- if (_element.name === elementName) {
337
- element = _element;
338
- }
339
- });
340
- if (!element) {
341
- throw new Error(`Element with name "${elementName}" does not exist for variant "${variant.id}".`);
342
- }
343
- return element;
344
- });
345
- }
346
- /**
347
- * Gets the desired {@link ViewerLight} of the current {@link Variant} relative to its {@link DottedPath}.
348
- * Uses the mechanism of {@link getDescendant} to resolve the appropriate variant in tree.
349
- */
350
- getViewerLight(dottedPath) {
351
- return __awaiter(this, void 0, void 0, function* () {
352
- const _dottedPath = dottedPath_1.DottedPath.create(dottedPath);
353
- const viewerLightName = _dottedPath.popPart();
354
- let variant = this;
355
- if (_dottedPath.parts.length > 0) {
356
- variant = yield this.getDescendant(_dottedPath);
357
- }
358
- if (variant.inheritedViewerLights.length === 0) {
359
- throw new Error(`No viewerLights for variant "${variant.id}" found. ` +
360
- `Either none are defined or they are not initialized (are you operating on the appropriate living?).`);
361
- }
362
- let viewerLight;
363
- variant.inheritedViewerLights.forEach(_viewerLight => {
364
- if (_viewerLight.name === viewerLightName) {
365
- viewerLight = _viewerLight;
366
- }
367
- });
368
- if (!viewerLight) {
369
- throw new Error(`ViewerLight with name "${viewerLightName}" does not exist for variant "${variant.id}".`);
370
- }
371
- return viewerLight;
372
- });
373
- }
374
- /**
375
- * A proxy for directly getting a Node from an {@link Element} by its {@link DottedPath}s.
376
- */
377
- getNode(elementDottedPath, nodeDottedPath) {
378
- return __awaiter(this, void 0, void 0, function* () {
379
- const element = yield this.getElement(elementDottedPath);
380
- return element.getNode(nodeDottedPath);
381
- });
382
- }
383
- /**
384
- * A proxy for directly getting a Mesh from an {@link Element} by its {@link DottedPath}s.
385
- */
386
- getMesh(elementDottedPath, meshDottedPath) {
387
- return __awaiter(this, void 0, void 0, function* () {
388
- const element = yield this.getElement(elementDottedPath);
389
- return element.getMesh(meshDottedPath);
390
- });
391
- }
392
- /**
393
- * Gets the Material defined in one of the variants glTFs by its id.
394
- */
395
- getOrCreateMaterial(id) {
396
- const scene = this.viewer.scene;
397
- for (const material of this.inheritedMaterials) {
398
- if (material.id === id) {
399
- return material;
400
- }
401
- }
402
- // fallback to dynamically created materials on scene
403
- for (const material of scene.materials) {
404
- if (material.id === id) {
405
- return material;
406
- }
407
- }
408
- const cbnAssetMaterial = (0, sceneLoaderHelper_1.createMaterialFromCbnAssets)(id, scene);
409
- if (cbnAssetMaterial) {
410
- return cbnAssetMaterial;
411
- }
412
- throw new Error(`Material with id "${id}" does not exist for variant "${this.id}".`);
413
- }
414
- /**
415
- * Creates a living clone of this {@link Variant}. Will clone all parent {@link Variant}s in tree.
416
- *
417
- * @emit {@link Event.VARIANT_CREATED}
418
- * @ignore
419
- */
420
- createLiving(parameters) {
421
- var _a;
422
- return __awaiter(this, void 0, void 0, function* () {
423
- const parent = yield ((_a = this.parent) === null || _a === void 0 ? void 0 : _a.createLiving());
424
- const variant = new Variant(this.name, this._structureJson, this.viewer, parent);
425
- parent === null || parent === void 0 ? void 0 : parent._children.set(variant.name, variant);
426
- variant.assetContainer = this.assetContainer;
427
- variant.parameterObservers = (0, lodash_es_1.cloneDeep)(this.parameterObservers);
428
- yield variant.createElements();
429
- yield variant.createViewerLights();
430
- variant.addParameterObservers();
431
- yield variant.bootstrapParameters(parameters);
432
- this.broadcastEvent(event_1.Event.VARIANT_CREATED, variant);
433
- return variant;
434
- });
435
- }
436
- /**
437
- * Destroys this {@link Variant}, all parents and destroy the {@link Element}s.
438
- */
439
- destroy() {
440
- this.elements.forEach(element => element.destroy());
441
- if (this.parent) {
442
- this.parent.destroy();
443
- }
444
- this.broadcastEvent(event_1.Event.VARIANT_DESTROYED, this);
445
- return this;
446
- }
447
- /**
448
- * Places the given {@link ParameterBag} in the {@link Variant}'s parameters, replaces all patterns in the
449
- * {@link StructureJson}, broadcasts all {@link ParameterObserver}s and delegates them to its {@link Element}s.
450
- *
451
- * @emit {@link Event.VARIANT_PARAMETER_COMMITTED}
452
- */
453
- commitParameters(parameters) {
454
- return __awaiter(this, void 0, void 0, function* () {
455
- parameters = (0, lodash_es_1.merge)({}, parameters);
456
- // remember old parameter values for later comparison
457
- const oldParameters = (0, lodash_es_1.cloneDeep)(this.inheritedParameters);
458
- // replace patterns in given parameters
459
- let _parameters = JSON.stringify(parameters);
460
- for (const parameter in this.inheritedParameters) {
461
- const value = this.inheritedParameters[parameter];
462
- const search = new RegExp(`\\$\\{${parameter}\\}`, 'g');
463
- _parameters = _parameters.replace(search, value.toString());
464
- }
465
- (0, lodash_es_1.merge)(parameters, JSON.parse(_parameters));
466
- // merge inherited parameters and replaced given parameters
467
- const mergedParameters = (0, lodash_es_1.merge)({}, this.inheritedParameters, parameters);
468
- // replace patterns in structure parameters
469
- const structureParameters = this._structureJson.parameters || {};
470
- let _structureParameters = JSON.stringify(structureParameters);
471
- for (const parameter in mergedParameters) {
472
- const value = mergedParameters[parameter];
473
- const search = new RegExp(`\\$\\{${parameter}\\}`, 'g');
474
- _structureParameters = _structureParameters.replace(search, value.toString());
475
- }
476
- const replacedStructureParameters = JSON.parse(_structureParameters);
477
- // calculate which replaced structure parameters have changed and should overload given parameters
478
- const differentStructureParameters = {};
479
- for (const parameter in replacedStructureParameters) {
480
- if (!(0, lodash_es_1.isEqual)(structureParameters[parameter], replacedStructureParameters[parameter])) {
481
- differentStructureParameters[parameter] = replacedStructureParameters[parameter];
482
- }
483
- }
484
- // merge replaced structure parameters and given inherited parameters to structure parameters
485
- (0, lodash_es_1.merge)(this.parameters, mergedParameters, differentStructureParameters);
486
- // inherited parameters are now the new parameters to process
487
- const newParameters = this.inheritedParameters;
488
- // replace all parameter patterns in structure json
489
- let structure = JSON.stringify(this._structureJson);
490
- for (const parameter in newParameters) {
491
- const value = newParameters[parameter];
492
- const search = new RegExp(`\\$\\{${parameter}\\}`, 'g');
493
- structure = structure.replace(search, value.toString());
494
- }
495
- this.structureJson = JSON.parse(structure);
496
- // handle parameter observers
497
- let observerPromises = [];
498
- for (const parameter in newParameters) {
499
- const oldParameterValue = oldParameters[parameter];
500
- const newParameterValue = newParameters[parameter];
501
- this.assertParameter(this.inheritedParameterDeclaration, parameter, newParameterValue);
502
- if (oldParameterValue === newParameterValue && this.parametersInitialized) {
503
- continue;
504
- }
505
- // parameter changed
506
- const parameterObservers = (0, resourceHelper_1.mergeMaps)(this._parameterObservers, this.parameterObservers);
507
- if (parameterObservers.has(parameter)) {
508
- const observers = parameterObservers.get(parameter);
509
- observerPromises = (0, lodash_es_1.concat)(observerPromises, observers.map(observer => {
510
- const observerResult = observer(this, oldParameterValue, newParameterValue);
511
- return Promise.resolve(observerResult).then(() => {
512
- if (this.parametersInitialized) {
513
- this.broadcastEvent(event_1.Event.VARIANT_PARAMETER_COMMITTED, this, parameter, oldParameterValue, newParameterValue);
514
- }
515
- });
516
- }));
517
- }
518
- }
519
- yield Promise.all(observerPromises);
520
- // broadcast that bag has been committed
521
- this.broadcastEvent(event_1.Event.VARIANT_PARAMETER_BAG_COMMITTED, this, oldParameters, newParameters);
522
- // commit parameters to elements
523
- yield this.commitParametersToElements(newParameters);
524
- // commit parameters to lights
525
- yield this.commitParametersToViewerLights(newParameters);
526
- // propagate parameters to parent
527
- if (this.parent) {
528
- yield this.parent.commitParameters(this.parameters);
529
- }
530
- return this;
531
- });
532
- }
533
- /**
534
- * Adds an observer function for camera matrix changes for given `dottedPath` representing the {@link Element}
535
- * and the `traceable`. The `observer` gets 2 parameters: the `AbstractMesh` and a `ClientRect` object.
536
- */
537
- addTraceableObserver(dottedPath, observer, payload) {
538
- return __awaiter(this, void 0, void 0, function* () {
539
- const _dottedPath = dottedPath_1.DottedPath.create(dottedPath);
540
- const traceableName = _dottedPath.popPart();
541
- if (!traceableName) {
542
- throw new Error(`The dottedPath must consist of the element and the name of the defined corresponding ` +
543
- `traceable ("${_dottedPath.path}" given).`);
544
- }
545
- const element = yield this.getElement(_dottedPath);
546
- return element.addTraceableObserver(traceableName, observer, payload);
547
- });
548
- }
549
- /**
550
- * Loads {@link glTFUri} with assets, adds them to the {@link Variant}'s `assetContainer` and deactivates the meshes.
551
- * (for further processing).
552
- * @emits {@link Event.ASSET_LOADING_START}
553
- * @emits {@link Event.ASSET_LOADING_END}
554
- */
555
- loadAssets() {
556
- return __awaiter(this, void 0, void 0, function* () {
557
- this.broadcastEvent(event_1.Event.ASSET_LOADING_START, this);
558
- const promisifiedLoader = new Promise((resolve, reject) => {
559
- if (!this.structureJson) {
560
- this.broadcastEvent(event_1.Event.ASSET_LOADING_END, this);
561
- return resolve(this);
562
- }
563
- if (!this.glTF) {
564
- this.broadcastEvent(event_1.Event.ASSET_LOADING_END, this);
565
- return resolve(this);
566
- }
567
- sceneLoader_1.SceneLoader.LoadAssetContainer(this.glTF.rootUrl, this.glTF.fileName, this.viewer.scene,
568
- // on success
569
- container => {
570
- this.assetContainer = container;
571
- const nodes = this.assetContainer.getNodes().filter(n => n instanceof transformNode_1.TransformNode);
572
- nodes.forEach(node => {
573
- (0, babylonHelper_1.deactivateTransformNode)(node, false);
574
- (0, babylonHelper_1.injectNodeMetadata)(node, { dottedPath: (0, babylonHelper_1.getDottedPathForNode)(node) }, false);
575
- });
576
- this.assetContainer.lights.forEach(light => {
577
- light.setEnabled(false);
578
- (0, babylonHelper_1.injectNodeMetadata)(light, { dottedPath: (0, babylonHelper_1.getDottedPathForNode)(light) }, false);
579
- this.viewer.scene.addLight(light);
580
- });
581
- this.assetContainer.cameras.forEach(camera => {
582
- camera.setEnabled(false);
583
- (0, babylonHelper_1.injectNodeMetadata)(camera, { dottedPath: (0, babylonHelper_1.getDottedPathForNode)(camera) }, false);
584
- this.viewer.scene.addCamera(camera);
585
- });
586
- this.assetContainer.materials.forEach(material => this.viewer.scene.materials.push(material));
587
- this.broadcastEvent(event_1.Event.ASSET_LOADING_END, this);
588
- resolve(this);
589
- },
590
- // on progress
591
- undefined,
592
- // on error
593
- reason => {
594
- this.broadcastEvent(event_1.Event.ASSET_LOADING_END, this);
595
- throw new Error(`Error loading assets for variant "${this.id}": ${reason}.`);
596
- });
597
- });
598
- return yield promisifiedLoader;
599
- });
600
- }
601
- /**
602
- * Commits given parameters to all {@link Element}s.
603
- */
604
- commitParametersToElements(parameters) {
605
- return __awaiter(this, void 0, void 0, function* () {
606
- yield Promise.all(this.elements.map(element => this.commitParametersToVariantParameterizable(parameters, element, 'elements')));
607
- });
608
- }
609
- /**
610
- * Commits given parameters to all {@link ViewerLight}s.
611
- */
612
- commitParametersToViewerLights(parameters) {
613
- return __awaiter(this, void 0, void 0, function* () {
614
- yield Promise.all(this.viewerLights.map(viewerLight => this.commitParametersToVariantParameterizable(parameters, viewerLight, 'lights')));
615
- });
616
- }
617
- /**
618
- * Commits given parameters to a {@link VariantParameterizable} and updates the according definition with given
619
- * key in the {@link StructureJson}. The `definitionKey` "elements" for example will update the definition in
620
- * `this.structureJson.elements`.
621
- */
622
- commitParametersToVariantParameterizable(parameters, parameterizable, definitionKey) {
623
- return __awaiter(this, void 0, void 0, function* () {
624
- const initialDefinition = (0, lodash_es_1.get)(this._structureJson, definitionKey)[parameterizable.name];
625
- let initialDefinitionStr = JSON.stringify(initialDefinition);
626
- const _parameters = {};
627
- for (const parameter in parameters) {
628
- const dpp = dottedPath_1.DottedPath.create(parameter);
629
- if (dpp.shiftPart() !== parameterizable.name) {
630
- continue;
631
- }
632
- // we got a parameterizable ("element") parameter
633
- let parameterValue = parameters[parameter];
634
- const parameterizableParameter = dpp.path;
635
- // If the variant is explicitly hidden, we must not override the visibility with element parameters. We need
636
- // an exception for visibility to avoid overloading already applied element parameters with element parameters
637
- // defined in the variant spec ("dotted parameters").
638
- // @see https://github.com/Combeenation/3d-viewer/issues/44
639
- if (parameterizableParameter === parameter_1.Parameter.VISIBLE && parameters[parameter_1.Parameter.VISIBLE] === false) {
640
- parameterValue = false;
641
- }
642
- _parameters[parameterizableParameter] = parameterValue;
643
- const search = new RegExp(`\\$\\{${parameterizableParameter}\\}`, 'g');
644
- initialDefinitionStr = initialDefinitionStr.replace(search, parameterValue.toString());
645
- }
646
- const definition = (0, lodash_es_1.get)(this.structureJson, definitionKey);
647
- definition[this.name] = JSON.parse(initialDefinitionStr);
648
- (0, lodash_es_1.set)(this.structureJson, definitionKey, definition);
649
- return yield parameterizable.commitParameters(_parameters);
650
- });
651
- }
652
- /**
653
- * Commits given {@link Parameter} to the {@link Variant}'s {@link Element}s.
654
- */
655
- commitParameterToElements(parameter, value) {
656
- return __awaiter(this, void 0, void 0, function* () {
657
- const promises = [];
658
- for (const element of this.elements) {
659
- const paramShowsVariant = parameter_1.Parameter.VISIBLE === parameter && value;
660
- // Fixes https://combeenation.youtrack.cloud/issue/CB-7773
661
- // Don't enable/show the variants element if it is explicitly hidden via its element parameters.
662
- // E.g. via spec:
663
- // ```
664
- // variants: {
665
- // theVariant: {
666
- // elements: {
667
- // Main: ['root.main'],
668
- // Secondary: ['root.secondary'],
669
- // },
670
- // parameters: {
671
- // // !!! The element `Secondary` should **not** be enabled when its containing variant is enabled !!!
672
- // 'Secondary.visible': false,
673
- // }
674
- // }
675
- // }
676
- // ```
677
- const elVisibleParamPath = dottedPath_1.DottedPath.create([element.name, parameter_1.Parameter.VISIBLE]).toString();
678
- const elVisibleParamValue = this.inheritedParameters[elVisibleParamPath];
679
- const elVisibleParamValueParsed = elVisibleParamValue && parameter_1.Parameter.parseBoolean(elVisibleParamValue);
680
- const elIsHiddenViaParams = elVisibleParamValueParsed === false;
681
- if (paramShowsVariant && elIsHiddenViaParams)
682
- continue;
683
- // Fixes https://combeenation.youtrack.cloud/issue/CB-7809
684
- // Apply element material before showing the element to prevent loading of the elements "original" material which
685
- // is never shown when "overwritten" by elements material param.
686
- const elMaterialParamPath = dottedPath_1.DottedPath.create([element.name, parameter_1.Parameter.MATERIAL]).toString();
687
- const elMaterialParamValue = this.inheritedParameters[elMaterialParamPath];
688
- if (paramShowsVariant && elMaterialParamValue) {
689
- yield element.commitParameter(parameter_1.Parameter.MATERIAL, elMaterialParamValue);
690
- }
691
- promises.push(element.commitParameter(parameter, value));
692
- }
693
- yield Promise.all(promises);
694
- return this;
695
- });
696
- }
697
- /**
698
- * Commits given {@link Parameter} to the {@link Variant}'s {@link Element}s.
699
- */
700
- commitParameterToViewerLights(parameter, value) {
701
- return __awaiter(this, void 0, void 0, function* () {
702
- const promises = [];
703
- for (const viewerLight of this.viewerLights) {
704
- promises.push(viewerLight.commitParameter(parameter, value));
705
- }
706
- yield Promise.all(promises);
707
- return this;
708
- });
709
- }
710
- /**
711
- * Adds the default {@link ParameterObserver}s which are called every time {@link commitParameters} is called.
712
- */
713
- addParameterObservers() {
714
- this._parameterObservers.set(parameter_1.Parameter.VISIBLE, [
715
- (variant, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
716
- yield variant.commitParameterToElements(parameter_1.Parameter.VISIBLE, newValue);
717
- yield variant.commitParameterToViewerLights(parameter_1.Parameter.VISIBLE, newValue);
718
- }),
719
- ]);
720
- this._parameterObservers.set(parameter_1.Parameter.SCALING, [
721
- (variant, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
722
- yield variant.commitParameterToElements(parameter_1.Parameter.SCALING, newValue);
723
- yield variant.commitParameterToViewerLights(parameter_1.Parameter.SCALING, newValue);
724
- }),
725
- ]);
726
- this._parameterObservers.set(parameter_1.Parameter.MATERIAL, [
727
- (variant, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
728
- yield variant.commitParameterToElements(parameter_1.Parameter.MATERIAL, newValue);
729
- }),
730
- ]);
731
- this._parameterObservers.set(parameter_1.Parameter.MATERIAL_COLOR, [
732
- (variant, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
733
- yield variant.commitParameterToElements(parameter_1.Parameter.MATERIAL_COLOR, newValue);
734
- }),
735
- ]);
736
- this._parameterObservers.set(parameter_1.Parameter.MATERIAL_METALLNESS, [
737
- (variant, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
738
- yield variant.commitParameterToElements(parameter_1.Parameter.MATERIAL_METALLNESS, newValue);
739
- }),
740
- ]);
741
- this._parameterObservers.set(parameter_1.Parameter.MATERIAL_ROUGHNESS, [
742
- (variant, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
743
- yield variant.commitParameterToElements(parameter_1.Parameter.MATERIAL_ROUGHNESS, newValue);
744
- }),
745
- ]);
746
- this._parameterObservers.set(parameter_1.Parameter.HIGHLIGHT_COLOR, [
747
- (variant, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
748
- yield variant.commitParameterToElements(parameter_1.Parameter.HIGHLIGHT_COLOR, newValue);
749
- }),
750
- ]);
751
- this._parameterObservers.set(parameter_1.Parameter.HIGHLIGHTED, [
752
- (variant, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
753
- yield variant.commitParameterToElements(parameter_1.Parameter.HIGHLIGHTED, newValue);
754
- }),
755
- ]);
756
- this._parameterObservers.set(parameter_1.Parameter.POSITION, [
757
- (variant, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
758
- yield variant.commitParameterToElements(parameter_1.Parameter.POSITION, newValue);
759
- yield variant.commitParameterToViewerLights(parameter_1.Parameter.POSITION, newValue);
760
- }),
761
- ]);
762
- this._parameterObservers.set(parameter_1.Parameter.ROTATION, [
763
- (variant, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
764
- yield variant.commitParameterToElements(parameter_1.Parameter.ROTATION, newValue);
765
- yield variant.commitParameterToViewerLights(parameter_1.Parameter.ROTATION, newValue);
766
- }),
767
- ]);
768
- this._parameterObservers.set(parameter_1.Parameter.CAST_SHADOW, [
769
- (variant, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
770
- yield variant.commitParameterToElements(parameter_1.Parameter.CAST_SHADOW, newValue);
771
- }),
772
- ]);
773
- this._parameterObservers.set(parameter_1.Parameter.CAST_SHADOW_FROM_LIGHTS, [
774
- (variant, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
775
- yield variant.commitParameterToElements(parameter_1.Parameter.CAST_SHADOW_FROM_LIGHTS, newValue);
776
- }),
777
- ]);
778
- this._parameterObservers.set(parameter_1.Parameter.RECEIVE_SHADOWS, [
779
- (variant, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
780
- yield variant.commitParameterToElements(parameter_1.Parameter.RECEIVE_SHADOWS, newValue);
781
- }),
782
- ]);
783
- return this;
784
- }
785
- /**
786
- * Creates {@link Element}s and clones nodes into them.
787
- */
788
- createElements() {
789
- return __awaiter(this, void 0, void 0, function* () {
790
- for (const name in this.structureJson.elements || {}) {
791
- this.elements.push(yield element_1.Element.create(this, name));
792
- }
793
- // inject node meta to all inherited elements
794
- // we do this to inject the deepest and most concrete variant information to all cloned nodes in the tree
795
- this.inheritedElements.forEach(element => {
796
- element.nodes.forEach(node => {
797
- (0, babylonHelper_1.injectNodeMetadata)(node, { variant: this, variantParameterizable: element });
798
- });
799
- });
800
- return this;
801
- });
802
- }
803
- /**
804
- * Creates {@link ViewerLight}s.
805
- */
806
- createViewerLights() {
807
- return __awaiter(this, void 0, void 0, function* () {
808
- for (const name in this.structureJson.lights || {}) {
809
- this.viewerLights.push(yield viewerLight_1.ViewerLight.create(this, name));
810
- }
811
- this.inheritedViewerLights.forEach(viewerLight => {
812
- (0, babylonHelper_1.injectNodeMetadata)(viewerLight.light, { variant: this, variantParameterizable: viewerLight });
813
- });
814
- return this;
815
- });
816
- }
817
- /**
818
- * Bootstrapping for parameters. It sets the `parametersInitialized` to true for all ancestors.
819
- */
820
- bootstrapParameters(parameters) {
821
- return __awaiter(this, void 0, void 0, function* () {
822
- yield this.commitParameters((0, lodash_es_1.merge)((0, lodash_es_1.cloneDeep)(this.parameters), parameters));
823
- (0, lodash_es_1.concat)(this.ancestors, this).forEach(ancestor => (ancestor.parametersInitialized = true));
824
- return this;
825
- });
826
- }
827
- }
828
- exports.Variant = Variant;
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.Variant = 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 dottedPath_1 = require("./dottedPath");
17
+ const element_1 = require("./element");
18
+ const event_1 = require("./event");
19
+ const parameter_1 = require("./parameter");
20
+ const parameterizable_1 = require("./parameterizable");
21
+ const viewerLight_1 = require("./viewerLight");
22
+ require("@babylonjs/core/Loading/Plugins/babylonFileLoader");
23
+ const sceneLoader_1 = require("@babylonjs/core/Loading/sceneLoader");
24
+ const transformNode_1 = require("@babylonjs/core/Meshes/transformNode");
25
+ const assetContainer_1 = require("@babylonjs/core/assetContainer");
26
+ require("@babylonjs/loaders/glTF/2.0/Extensions/KHR_draco_mesh_compression");
27
+ require("@babylonjs/loaders/glTF/2.0/Extensions/KHR_materials_clearcoat");
28
+ require("@babylonjs/loaders/glTF/2.0/Extensions/KHR_materials_ior");
29
+ require("@babylonjs/loaders/glTF/2.0/Extensions/KHR_materials_pbrSpecularGlossiness");
30
+ require("@babylonjs/loaders/glTF/2.0/Extensions/KHR_materials_sheen");
31
+ require("@babylonjs/loaders/glTF/2.0/Extensions/KHR_materials_specular");
32
+ require("@babylonjs/loaders/glTF/2.0/Extensions/KHR_materials_translucency");
33
+ require("@babylonjs/loaders/glTF/2.0/Extensions/KHR_materials_transmission");
34
+ require("@babylonjs/loaders/glTF/2.0/Extensions/KHR_materials_unlit");
35
+ require("@babylonjs/loaders/glTF/2.0/Extensions/KHR_materials_variants");
36
+ require("@babylonjs/loaders/glTF/2.0/Extensions/KHR_texture_basisu");
37
+ require("@babylonjs/loaders/glTF/2.0/Extensions/KHR_texture_transform");
38
+ require("@babylonjs/loaders/glTF/2.0/glTFLoader");
39
+ const lodash_es_1 = require("lodash-es");
40
+ /**
41
+ * A concrete "Variant". Most of these are handled by either the {@link Viewer} or {@link VariantInstance}.
42
+ */
43
+ class Variant extends parameterizable_1.Parameterizable {
44
+ /**
45
+ * Constructor.
46
+ */
47
+ constructor(name, _structureJson, viewer, parent) {
48
+ super((0, lodash_es_1.cloneDeep)(_structureJson.parameterDeclaration), (0, lodash_es_1.cloneDeep)(_structureJson.parameters));
49
+ this.name = name;
50
+ this._structureJson = _structureJson;
51
+ this.viewer = viewer;
52
+ this.parent = parent;
53
+ this.elements = [];
54
+ this.viewerLights = [];
55
+ this._children = new Map();
56
+ this._parameterObservers = new Map();
57
+ /**
58
+ * @internal
59
+ */
60
+ this.parametersInitialized = false;
61
+ for (const parameter in _structureJson.parameterDeclaration) {
62
+ if (!(parameter in (_structureJson.parameters || []))) {
63
+ throw new Error(`No default value for parameter "${parameter}" defined.`);
64
+ }
65
+ }
66
+ this.assetContainer = new assetContainer_1.AssetContainer(viewer.scene);
67
+ this.structureJson = (0, lodash_es_1.cloneDeep)(_structureJson);
68
+ }
69
+ /**
70
+ * Creates a {@link Variant} based on given parameters.
71
+ *
72
+ * @throws Error if "gltf" property is provided without a filename
73
+ */
74
+ static create(name, structureJson, viewer, parent) {
75
+ return __awaiter(this, void 0, void 0, function* () {
76
+ const variant = new Variant(name, structureJson, viewer, parent);
77
+ yield variant.loadAssets();
78
+ return variant;
79
+ });
80
+ }
81
+ /**
82
+ * The ancestor {@link Variant}s ordered from top to bottom in the built tree.
83
+ */
84
+ get ancestors() {
85
+ const ancestors = [];
86
+ let variant = this;
87
+ while (variant.parent) {
88
+ ancestors.unshift(variant.parent);
89
+ variant = variant.parent;
90
+ }
91
+ return ancestors;
92
+ }
93
+ /**
94
+ * The root {@link Variant}.
95
+ */
96
+ get root() {
97
+ var _a;
98
+ return (_a = this.ancestors[0]) !== null && _a !== void 0 ? _a : this;
99
+ }
100
+ /**
101
+ * The {@link DottedPath} in the built tree of {@link Variant}s.
102
+ * E.g. "_.top-1.sub-2.sub-sub-3"
103
+ */
104
+ get dottedPath() {
105
+ const parentIds = this.ancestors.map(ancestor => {
106
+ return ancestor.name;
107
+ });
108
+ return dottedPath_1.DottedPath.createFromParts(parentIds).addPart(this.name);
109
+ }
110
+ /**
111
+ * The id representing a {@link DottedPath}.
112
+ */
113
+ get id() {
114
+ const dottedPath = dottedPath_1.DottedPath.create(this.dottedPath);
115
+ dottedPath.shiftPart(); // remove root
116
+ return dottedPath.path || '_';
117
+ }
118
+ /**
119
+ * The defined glTF Asset.
120
+ */
121
+ get glTF() {
122
+ if (this.structureJson.glTF) {
123
+ if ((0, lodash_es_1.isString)(this.structureJson.glTF)) {
124
+ return {
125
+ rootUrl: this.structureJson.glTF,
126
+ fileName: undefined,
127
+ };
128
+ }
129
+ if ((0, lodash_es_1.isEmpty)(this.structureJson.glTF.rootUrl)) {
130
+ throw new Error(`No "rootUrl" defined in "glTF" definition for variant "${this.id}".`);
131
+ }
132
+ return this.structureJson.glTF;
133
+ }
134
+ }
135
+ /**
136
+ * The defined glTF URI.
137
+ */
138
+ get glTFUri() {
139
+ if (this.glTF) {
140
+ return [this.glTF.rootUrl, this.glTF.fileName].join('');
141
+ }
142
+ }
143
+ /**
144
+ * The inherited defined glTF URI.
145
+ */
146
+ get inheritedGlTFUri() {
147
+ if (!this.glTFUri && this.parent) {
148
+ return this.parent.inheritedGlTFUri;
149
+ }
150
+ return this.glTFUri;
151
+ }
152
+ /**
153
+ * The TransformNodes of the {@link Variant}.
154
+ */
155
+ get nodes() {
156
+ const rootNodes = this.assetContainer
157
+ .getNodes()
158
+ .filter(n => n instanceof transformNode_1.TransformNode && !n.parent);
159
+ return rootNodes;
160
+ }
161
+ /**
162
+ * The {@link ViewerLight}s of the {@link Variant}.
163
+ */
164
+ get lights() {
165
+ return this.assetContainer.lights;
166
+ }
167
+ /**
168
+ * All TransformNodes of the {@link Variant} mapped flat with a {@link DottedPath}.
169
+ */
170
+ get dottedNodes() {
171
+ if (!this._dottedNodes) {
172
+ const nodes = this.assetContainer.getNodes().filter(n => n instanceof transformNode_1.TransformNode);
173
+ const dottedNodes = new Map();
174
+ nodes.forEach(node => {
175
+ dottedNodes.set(node.metadata.dottedPath, node);
176
+ });
177
+ this._dottedNodes = dottedNodes;
178
+ }
179
+ return this._dottedNodes;
180
+ }
181
+ /**
182
+ * The Materials of the {@link Variant}.
183
+ */
184
+ get materials() {
185
+ return this.assetContainer.materials;
186
+ }
187
+ /**
188
+ * All {@link Element}s from this {@link Variant}'s parents.
189
+ */
190
+ get inheritedElements() {
191
+ let elements = [];
192
+ this.ancestors.forEach(ancestor => {
193
+ elements = (0, lodash_es_1.concat)(elements, ancestor.elements);
194
+ });
195
+ return (0, lodash_es_1.concat)(elements, this.elements);
196
+ }
197
+ /**
198
+ * All {@link ViewerLight}s inherited from this {@link Variant}'s parents.
199
+ */
200
+ get inheritedViewerLights() {
201
+ let viewerLights = [];
202
+ this.ancestors.forEach(ancestor => {
203
+ viewerLights = (0, lodash_es_1.concat)(viewerLights, ancestor.viewerLights);
204
+ });
205
+ return (0, lodash_es_1.concat)(viewerLights, this.viewerLights);
206
+ }
207
+ /**
208
+ * All TransformNodes inherited from this {@link Variant}'s parents.
209
+ */
210
+ get inheritedNodes() {
211
+ let nodes = [];
212
+ this.ancestors.forEach(ancestor => {
213
+ nodes = (0, lodash_es_1.concat)(nodes, ancestor.nodes);
214
+ });
215
+ return (0, lodash_es_1.concat)(nodes, this.nodes);
216
+ }
217
+ /**
218
+ * All TransformNodes inherited from this {@link Variant}'s parents mapped flat with a {@link DottedPath}.
219
+ */
220
+ get inheritedDottedNodes() {
221
+ let dottedNodes = this.dottedNodes;
222
+ this.ancestors.forEach(ancestor => {
223
+ dottedNodes = (0, resourceHelper_1.mergeMaps)(dottedNodes, ancestor.dottedNodes);
224
+ });
225
+ return dottedNodes;
226
+ }
227
+ /**
228
+ * All Lights inherited from this {@link Variant}'s parents.
229
+ */
230
+ get inheritedLights() {
231
+ let lights = [];
232
+ this.ancestors.forEach(ancestor => {
233
+ lights = (0, lodash_es_1.concat)(lights, ancestor.lights);
234
+ });
235
+ return (0, lodash_es_1.concat)(lights, this.lights);
236
+ }
237
+ /**
238
+ * The {@link ParameterDeclarations} inherited from this {@link Variant}'s parents.
239
+ */
240
+ get inheritedParameterDeclaration() {
241
+ const declaration = {};
242
+ this.ancestors.forEach(ancestor => {
243
+ (0, lodash_es_1.merge)(declaration, ancestor.parameterDeclaration);
244
+ });
245
+ return (0, lodash_es_1.merge)(declaration, this.parameterDeclaration);
246
+ }
247
+ /**
248
+ * The {@link ParameterBag} inherited from this {@link Variant}'s parents.
249
+ */
250
+ get inheritedParameters() {
251
+ const parameters = {};
252
+ this.ancestors.forEach(ancestor => {
253
+ (0, lodash_es_1.merge)(parameters, ancestor.parameters);
254
+ });
255
+ return (0, lodash_es_1.merge)(parameters, this.parameters);
256
+ }
257
+ /**
258
+ * All Materials from this {@link Variant}'s parents.
259
+ */
260
+ get inheritedMaterials() {
261
+ let materials = [];
262
+ this.ancestors.forEach(ancestor => {
263
+ materials = (0, lodash_es_1.concat)(materials, ancestor.materials);
264
+ });
265
+ return (0, lodash_es_1.concat)(materials, this.materials);
266
+ }
267
+ /**
268
+ * Gets the direct children of the current {@link Variant}.
269
+ */
270
+ getChildren() {
271
+ return __awaiter(this, void 0, void 0, function* () {
272
+ const children = [];
273
+ for (const name in this.structureJson.variants) {
274
+ children.push(yield this.getDescendant(name));
275
+ }
276
+ return children;
277
+ });
278
+ }
279
+ /**
280
+ * Gets a descendant {@link Variant} of the current {@link Variant} relative to its {@link DottedPath}.
281
+ * If you have the dotted path `_.product_x.variant_blue.with_yellow_highlight` in a tree and you operate on the
282
+ * `product_x`, you can call `this.getDescendant('variant_blue.with_yellow_highlight')` to get the lowermost
283
+ * {@link Variant}.
284
+ */
285
+ getDescendant(dottedPath) {
286
+ return __awaiter(this, void 0, void 0, function* () {
287
+ const _dottedPath = dottedPath_1.DottedPath.create(dottedPath);
288
+ const [name, ...descendantParts] = _dottedPath.parts;
289
+ let variant;
290
+ if (this._children.has(name)) {
291
+ variant = this._children.get(name);
292
+ }
293
+ else {
294
+ if (!this.structureJson.variants) {
295
+ throw new Error(`Missing key "variants" in JSON structure for variant "${this.id}".`);
296
+ }
297
+ if (!this.structureJson.variants[name]) {
298
+ throw new Error(`Variant "${_dottedPath.path}" not defined in JSON structure for variant "${this.id}".`);
299
+ }
300
+ if (this.structureJson.variants[name].file) {
301
+ const file = this.structureJson.variants[name].file;
302
+ variant = yield Variant.create(name, yield (0, resourceHelper_1.loadJson)(file), this.viewer, this);
303
+ }
304
+ else {
305
+ variant = yield Variant.create(name, this.structureJson.variants[name], this.viewer, this);
306
+ }
307
+ this._children.set(name, variant);
308
+ }
309
+ if (!variant) {
310
+ throw new Error(`Variant "${_dottedPath.path}" was not created.`);
311
+ }
312
+ if (descendantParts.length > 0) {
313
+ return yield variant.getDescendant(dottedPath_1.DottedPath.createFromParts(descendantParts));
314
+ }
315
+ return variant;
316
+ });
317
+ }
318
+ /**
319
+ * Gets the desired {@link Element} of the current {@link Variant} relative to its {@link DottedPath}.
320
+ * Uses the mechanism of {@link getDescendant} to resolve the appropriate variant in tree.
321
+ */
322
+ getElement(dottedPath) {
323
+ return __awaiter(this, void 0, void 0, function* () {
324
+ const _dottedPath = dottedPath_1.DottedPath.create(dottedPath);
325
+ const elementName = _dottedPath.popPart();
326
+ let variant = this;
327
+ if (_dottedPath.parts.length > 0) {
328
+ variant = yield this.getDescendant(_dottedPath);
329
+ }
330
+ if (variant.inheritedElements.length === 0) {
331
+ throw new Error(`No elements for variant "${variant.id}" found. ` +
332
+ `Either none are defined or they are not initialized (are you operating on the appropriate living?).`);
333
+ }
334
+ let element;
335
+ variant.inheritedElements.forEach(_element => {
336
+ if (_element.name === elementName) {
337
+ element = _element;
338
+ }
339
+ });
340
+ if (!element) {
341
+ throw new Error(`Element with name "${elementName}" does not exist for variant "${variant.id}".`);
342
+ }
343
+ return element;
344
+ });
345
+ }
346
+ /**
347
+ * Gets the desired {@link ViewerLight} of the current {@link Variant} relative to its {@link DottedPath}.
348
+ * Uses the mechanism of {@link getDescendant} to resolve the appropriate variant in tree.
349
+ */
350
+ getViewerLight(dottedPath) {
351
+ return __awaiter(this, void 0, void 0, function* () {
352
+ const _dottedPath = dottedPath_1.DottedPath.create(dottedPath);
353
+ const viewerLightName = _dottedPath.popPart();
354
+ let variant = this;
355
+ if (_dottedPath.parts.length > 0) {
356
+ variant = yield this.getDescendant(_dottedPath);
357
+ }
358
+ if (variant.inheritedViewerLights.length === 0) {
359
+ throw new Error(`No viewerLights for variant "${variant.id}" found. ` +
360
+ `Either none are defined or they are not initialized (are you operating on the appropriate living?).`);
361
+ }
362
+ let viewerLight;
363
+ variant.inheritedViewerLights.forEach(_viewerLight => {
364
+ if (_viewerLight.name === viewerLightName) {
365
+ viewerLight = _viewerLight;
366
+ }
367
+ });
368
+ if (!viewerLight) {
369
+ throw new Error(`ViewerLight with name "${viewerLightName}" does not exist for variant "${variant.id}".`);
370
+ }
371
+ return viewerLight;
372
+ });
373
+ }
374
+ /**
375
+ * A proxy for directly getting a Node from an {@link Element} by its {@link DottedPath}s.
376
+ */
377
+ getNode(elementDottedPath, nodeDottedPath) {
378
+ return __awaiter(this, void 0, void 0, function* () {
379
+ const element = yield this.getElement(elementDottedPath);
380
+ return element.getNode(nodeDottedPath);
381
+ });
382
+ }
383
+ /**
384
+ * A proxy for directly getting a Mesh from an {@link Element} by its {@link DottedPath}s.
385
+ */
386
+ getMesh(elementDottedPath, meshDottedPath) {
387
+ return __awaiter(this, void 0, void 0, function* () {
388
+ const element = yield this.getElement(elementDottedPath);
389
+ return element.getMesh(meshDottedPath);
390
+ });
391
+ }
392
+ /**
393
+ * Gets the Material defined in one of the variants glTFs by its id.
394
+ */
395
+ getOrCreateMaterial(id) {
396
+ const scene = this.viewer.scene;
397
+ for (const material of this.inheritedMaterials) {
398
+ if (material.id === id) {
399
+ return material;
400
+ }
401
+ }
402
+ // fallback to dynamically created materials on scene
403
+ for (const material of scene.materials) {
404
+ if (material.id === id) {
405
+ return material;
406
+ }
407
+ }
408
+ const cbnAssetMaterial = (0, sceneLoaderHelper_1.createMaterialFromCbnAssets)(id, scene);
409
+ if (cbnAssetMaterial) {
410
+ return cbnAssetMaterial;
411
+ }
412
+ throw new Error(`Material with id "${id}" does not exist for variant "${this.id}".`);
413
+ }
414
+ /**
415
+ * Creates a living clone of this {@link Variant}. Will clone all parent {@link Variant}s in tree.
416
+ *
417
+ * @emit {@link Event.VARIANT_CREATED}
418
+ * @ignore
419
+ */
420
+ createLiving(parameters) {
421
+ var _a;
422
+ return __awaiter(this, void 0, void 0, function* () {
423
+ const parent = yield ((_a = this.parent) === null || _a === void 0 ? void 0 : _a.createLiving());
424
+ const variant = new Variant(this.name, this._structureJson, this.viewer, parent);
425
+ parent === null || parent === void 0 ? void 0 : parent._children.set(variant.name, variant);
426
+ variant.assetContainer = this.assetContainer;
427
+ variant.parameterObservers = (0, lodash_es_1.cloneDeep)(this.parameterObservers);
428
+ yield variant.createElements();
429
+ yield variant.createViewerLights();
430
+ variant.addParameterObservers();
431
+ yield variant.bootstrapParameters(parameters);
432
+ this.broadcastEvent(event_1.Event.VARIANT_CREATED, variant);
433
+ return variant;
434
+ });
435
+ }
436
+ /**
437
+ * Destroys this {@link Variant}, all parents and destroy the {@link Element}s.
438
+ */
439
+ destroy() {
440
+ this.elements.forEach(element => element.destroy());
441
+ if (this.parent) {
442
+ this.parent.destroy();
443
+ }
444
+ this.broadcastEvent(event_1.Event.VARIANT_DESTROYED, this);
445
+ return this;
446
+ }
447
+ /**
448
+ * Places the given {@link ParameterBag} in the {@link Variant}'s parameters, replaces all patterns in the
449
+ * {@link StructureJson}, broadcasts all {@link ParameterObserver}s and delegates them to its {@link Element}s.
450
+ *
451
+ * @emit {@link Event.VARIANT_PARAMETER_COMMITTED}
452
+ */
453
+ commitParameters(parameters) {
454
+ return __awaiter(this, void 0, void 0, function* () {
455
+ parameters = (0, lodash_es_1.merge)({}, parameters);
456
+ // remember old parameter values for later comparison
457
+ const oldParameters = (0, lodash_es_1.cloneDeep)(this.inheritedParameters);
458
+ // replace patterns in given parameters
459
+ let _parameters = JSON.stringify(parameters);
460
+ for (const parameter in this.inheritedParameters) {
461
+ const value = this.inheritedParameters[parameter];
462
+ const search = new RegExp(`\\$\\{${parameter}\\}`, 'g');
463
+ _parameters = _parameters.replace(search, value.toString());
464
+ }
465
+ (0, lodash_es_1.merge)(parameters, JSON.parse(_parameters));
466
+ // merge inherited parameters and replaced given parameters
467
+ const mergedParameters = (0, lodash_es_1.merge)({}, this.inheritedParameters, parameters);
468
+ // replace patterns in structure parameters
469
+ const structureParameters = this._structureJson.parameters || {};
470
+ let _structureParameters = JSON.stringify(structureParameters);
471
+ for (const parameter in mergedParameters) {
472
+ const value = mergedParameters[parameter];
473
+ const search = new RegExp(`\\$\\{${parameter}\\}`, 'g');
474
+ _structureParameters = _structureParameters.replace(search, value.toString());
475
+ }
476
+ const replacedStructureParameters = JSON.parse(_structureParameters);
477
+ // calculate which replaced structure parameters have changed and should overload given parameters
478
+ const differentStructureParameters = {};
479
+ for (const parameter in replacedStructureParameters) {
480
+ if (!(0, lodash_es_1.isEqual)(structureParameters[parameter], replacedStructureParameters[parameter])) {
481
+ differentStructureParameters[parameter] = replacedStructureParameters[parameter];
482
+ }
483
+ }
484
+ // merge replaced structure parameters and given inherited parameters to structure parameters
485
+ (0, lodash_es_1.merge)(this.parameters, mergedParameters, differentStructureParameters);
486
+ // inherited parameters are now the new parameters to process
487
+ const newParameters = this.inheritedParameters;
488
+ // replace all parameter patterns in structure json
489
+ let structure = JSON.stringify(this._structureJson);
490
+ for (const parameter in newParameters) {
491
+ const value = newParameters[parameter];
492
+ const search = new RegExp(`\\$\\{${parameter}\\}`, 'g');
493
+ structure = structure.replace(search, value.toString());
494
+ }
495
+ this.structureJson = JSON.parse(structure);
496
+ // handle parameter observers
497
+ let observerPromises = [];
498
+ for (const parameter in newParameters) {
499
+ const oldParameterValue = oldParameters[parameter];
500
+ const newParameterValue = newParameters[parameter];
501
+ this.assertParameter(this.inheritedParameterDeclaration, parameter, newParameterValue);
502
+ if (oldParameterValue === newParameterValue && this.parametersInitialized) {
503
+ continue;
504
+ }
505
+ // parameter changed
506
+ const parameterObservers = (0, resourceHelper_1.mergeMaps)(this._parameterObservers, this.parameterObservers);
507
+ if (parameterObservers.has(parameter)) {
508
+ const observers = parameterObservers.get(parameter);
509
+ observerPromises = (0, lodash_es_1.concat)(observerPromises, observers.map(observer => {
510
+ const observerResult = observer(this, oldParameterValue, newParameterValue);
511
+ return Promise.resolve(observerResult).then(() => {
512
+ if (this.parametersInitialized) {
513
+ this.broadcastEvent(event_1.Event.VARIANT_PARAMETER_COMMITTED, this, parameter, oldParameterValue, newParameterValue);
514
+ }
515
+ });
516
+ }));
517
+ }
518
+ }
519
+ yield Promise.all(observerPromises);
520
+ // broadcast that bag has been committed
521
+ this.broadcastEvent(event_1.Event.VARIANT_PARAMETER_BAG_COMMITTED, this, oldParameters, newParameters);
522
+ // commit parameters to elements
523
+ yield this.commitParametersToElements(newParameters);
524
+ // commit parameters to lights
525
+ yield this.commitParametersToViewerLights(newParameters);
526
+ // propagate parameters to parent
527
+ if (this.parent) {
528
+ yield this.parent.commitParameters(this.parameters);
529
+ }
530
+ return this;
531
+ });
532
+ }
533
+ /**
534
+ * Adds an observer function for camera matrix changes for given `dottedPath` representing the {@link Element}
535
+ * and the `traceable`. The `observer` gets 2 parameters: the `AbstractMesh` and a `ClientRect` object.
536
+ */
537
+ addTraceableObserver(dottedPath, observer, payload) {
538
+ return __awaiter(this, void 0, void 0, function* () {
539
+ const _dottedPath = dottedPath_1.DottedPath.create(dottedPath);
540
+ const traceableName = _dottedPath.popPart();
541
+ if (!traceableName) {
542
+ throw new Error(`The dottedPath must consist of the element and the name of the defined corresponding ` +
543
+ `traceable ("${_dottedPath.path}" given).`);
544
+ }
545
+ const element = yield this.getElement(_dottedPath);
546
+ return element.addTraceableObserver(traceableName, observer, payload);
547
+ });
548
+ }
549
+ /**
550
+ * Loads {@link glTFUri} with assets, adds them to the {@link Variant}'s `assetContainer` and deactivates the meshes.
551
+ * (for further processing).
552
+ * @emits {@link Event.ASSET_LOADING_START}
553
+ * @emits {@link Event.ASSET_LOADING_END}
554
+ */
555
+ loadAssets() {
556
+ return __awaiter(this, void 0, void 0, function* () {
557
+ this.broadcastEvent(event_1.Event.ASSET_LOADING_START, this);
558
+ const promisifiedLoader = new Promise((resolve, reject) => {
559
+ if (!this.structureJson) {
560
+ this.broadcastEvent(event_1.Event.ASSET_LOADING_END, this);
561
+ return resolve(this);
562
+ }
563
+ if (!this.glTF) {
564
+ this.broadcastEvent(event_1.Event.ASSET_LOADING_END, this);
565
+ return resolve(this);
566
+ }
567
+ sceneLoader_1.SceneLoader.LoadAssetContainer(this.glTF.rootUrl, this.glTF.fileName, this.viewer.scene,
568
+ // on success
569
+ container => {
570
+ this.assetContainer = container;
571
+ const nodes = this.assetContainer.getNodes().filter(n => n instanceof transformNode_1.TransformNode);
572
+ nodes.forEach(node => {
573
+ (0, babylonHelper_1.deactivateTransformNode)(node, false);
574
+ (0, babylonHelper_1.injectNodeMetadata)(node, { dottedPath: (0, babylonHelper_1.getDottedPathForNode)(node) }, false);
575
+ });
576
+ this.assetContainer.lights.forEach(light => {
577
+ light.setEnabled(false);
578
+ (0, babylonHelper_1.injectNodeMetadata)(light, { dottedPath: (0, babylonHelper_1.getDottedPathForNode)(light) }, false);
579
+ this.viewer.scene.addLight(light);
580
+ });
581
+ this.assetContainer.cameras.forEach(camera => {
582
+ camera.setEnabled(false);
583
+ (0, babylonHelper_1.injectNodeMetadata)(camera, { dottedPath: (0, babylonHelper_1.getDottedPathForNode)(camera) }, false);
584
+ this.viewer.scene.addCamera(camera);
585
+ });
586
+ this.assetContainer.materials.forEach(material => this.viewer.scene.materials.push(material));
587
+ this.broadcastEvent(event_1.Event.ASSET_LOADING_END, this);
588
+ resolve(this);
589
+ },
590
+ // on progress
591
+ undefined,
592
+ // on error
593
+ reason => {
594
+ this.broadcastEvent(event_1.Event.ASSET_LOADING_END, this);
595
+ throw new Error(`Error loading assets for variant "${this.id}": ${reason}.`);
596
+ });
597
+ });
598
+ return yield promisifiedLoader;
599
+ });
600
+ }
601
+ /**
602
+ * Commits given parameters to all {@link Element}s.
603
+ */
604
+ commitParametersToElements(parameters) {
605
+ return __awaiter(this, void 0, void 0, function* () {
606
+ yield Promise.all(this.elements.map(element => this.commitParametersToVariantParameterizable(parameters, element, 'elements')));
607
+ });
608
+ }
609
+ /**
610
+ * Commits given parameters to all {@link ViewerLight}s.
611
+ */
612
+ commitParametersToViewerLights(parameters) {
613
+ return __awaiter(this, void 0, void 0, function* () {
614
+ yield Promise.all(this.viewerLights.map(viewerLight => this.commitParametersToVariantParameterizable(parameters, viewerLight, 'lights')));
615
+ });
616
+ }
617
+ /**
618
+ * Commits given parameters to a {@link VariantParameterizable} and updates the according definition with given
619
+ * key in the {@link StructureJson}. The `definitionKey` "elements" for example will update the definition in
620
+ * `this.structureJson.elements`.
621
+ */
622
+ commitParametersToVariantParameterizable(parameters, parameterizable, definitionKey) {
623
+ return __awaiter(this, void 0, void 0, function* () {
624
+ const initialDefinition = (0, lodash_es_1.get)(this._structureJson, definitionKey)[parameterizable.name];
625
+ let initialDefinitionStr = JSON.stringify(initialDefinition);
626
+ const _parameters = {};
627
+ for (const parameter in parameters) {
628
+ const dpp = dottedPath_1.DottedPath.create(parameter);
629
+ if (dpp.shiftPart() !== parameterizable.name) {
630
+ continue;
631
+ }
632
+ // we got a parameterizable ("element") parameter
633
+ let parameterValue = parameters[parameter];
634
+ const parameterizableParameter = dpp.path;
635
+ // If the variant is explicitly hidden, we must not override the visibility with element parameters. We need
636
+ // an exception for visibility to avoid overloading already applied element parameters with element parameters
637
+ // defined in the variant spec ("dotted parameters").
638
+ // @see https://github.com/Combeenation/3d-viewer/issues/44
639
+ if (parameterizableParameter === parameter_1.Parameter.VISIBLE && parameters[parameter_1.Parameter.VISIBLE] === false) {
640
+ parameterValue = false;
641
+ }
642
+ _parameters[parameterizableParameter] = parameterValue;
643
+ const search = new RegExp(`\\$\\{${parameterizableParameter}\\}`, 'g');
644
+ initialDefinitionStr = initialDefinitionStr.replace(search, parameterValue.toString());
645
+ }
646
+ const definition = (0, lodash_es_1.get)(this.structureJson, definitionKey);
647
+ definition[this.name] = JSON.parse(initialDefinitionStr);
648
+ (0, lodash_es_1.set)(this.structureJson, definitionKey, definition);
649
+ return yield parameterizable.commitParameters(_parameters);
650
+ });
651
+ }
652
+ /**
653
+ * Commits given {@link Parameter} to the {@link Variant}'s {@link Element}s.
654
+ */
655
+ commitParameterToElements(parameter, value) {
656
+ return __awaiter(this, void 0, void 0, function* () {
657
+ const promises = [];
658
+ for (const element of this.elements) {
659
+ const paramShowsVariant = parameter_1.Parameter.VISIBLE === parameter && value;
660
+ // Fixes https://combeenation.youtrack.cloud/issue/CB-7773
661
+ // Don't enable/show the variants element if it is explicitly hidden via its element parameters.
662
+ // E.g. via spec:
663
+ // ```
664
+ // variants: {
665
+ // theVariant: {
666
+ // elements: {
667
+ // Main: ['root.main'],
668
+ // Secondary: ['root.secondary'],
669
+ // },
670
+ // parameters: {
671
+ // // !!! The element `Secondary` should **not** be enabled when its containing variant is enabled !!!
672
+ // 'Secondary.visible': false,
673
+ // }
674
+ // }
675
+ // }
676
+ // ```
677
+ const elVisibleParamPath = dottedPath_1.DottedPath.create([element.name, parameter_1.Parameter.VISIBLE]).toString();
678
+ const elVisibleParamValue = this.inheritedParameters[elVisibleParamPath];
679
+ const elVisibleParamValueParsed = elVisibleParamValue && parameter_1.Parameter.parseBoolean(elVisibleParamValue);
680
+ const elIsHiddenViaParams = elVisibleParamValueParsed === false;
681
+ if (paramShowsVariant && elIsHiddenViaParams)
682
+ continue;
683
+ // Fixes https://combeenation.youtrack.cloud/issue/CB-7809
684
+ // Apply element material before showing the element to prevent loading of the elements "original" material which
685
+ // is never shown when "overwritten" by elements material param.
686
+ const elMaterialParamPath = dottedPath_1.DottedPath.create([element.name, parameter_1.Parameter.MATERIAL]).toString();
687
+ const elMaterialParamValue = this.inheritedParameters[elMaterialParamPath];
688
+ if (paramShowsVariant && elMaterialParamValue) {
689
+ yield element.commitParameter(parameter_1.Parameter.MATERIAL, elMaterialParamValue);
690
+ }
691
+ promises.push(element.commitParameter(parameter, value));
692
+ }
693
+ yield Promise.all(promises);
694
+ return this;
695
+ });
696
+ }
697
+ /**
698
+ * Commits given {@link Parameter} to the {@link Variant}'s {@link Element}s.
699
+ */
700
+ commitParameterToViewerLights(parameter, value) {
701
+ return __awaiter(this, void 0, void 0, function* () {
702
+ const promises = [];
703
+ for (const viewerLight of this.viewerLights) {
704
+ promises.push(viewerLight.commitParameter(parameter, value));
705
+ }
706
+ yield Promise.all(promises);
707
+ return this;
708
+ });
709
+ }
710
+ /**
711
+ * Adds the default {@link ParameterObserver}s which are called every time {@link commitParameters} is called.
712
+ */
713
+ addParameterObservers() {
714
+ this._parameterObservers.set(parameter_1.Parameter.VISIBLE, [
715
+ (variant, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
716
+ yield variant.commitParameterToElements(parameter_1.Parameter.VISIBLE, newValue);
717
+ yield variant.commitParameterToViewerLights(parameter_1.Parameter.VISIBLE, newValue);
718
+ }),
719
+ ]);
720
+ this._parameterObservers.set(parameter_1.Parameter.SCALING, [
721
+ (variant, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
722
+ yield variant.commitParameterToElements(parameter_1.Parameter.SCALING, newValue);
723
+ yield variant.commitParameterToViewerLights(parameter_1.Parameter.SCALING, newValue);
724
+ }),
725
+ ]);
726
+ this._parameterObservers.set(parameter_1.Parameter.MATERIAL, [
727
+ (variant, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
728
+ yield variant.commitParameterToElements(parameter_1.Parameter.MATERIAL, newValue);
729
+ }),
730
+ ]);
731
+ this._parameterObservers.set(parameter_1.Parameter.MATERIAL_COLOR, [
732
+ (variant, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
733
+ yield variant.commitParameterToElements(parameter_1.Parameter.MATERIAL_COLOR, newValue);
734
+ }),
735
+ ]);
736
+ this._parameterObservers.set(parameter_1.Parameter.MATERIAL_METALLNESS, [
737
+ (variant, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
738
+ yield variant.commitParameterToElements(parameter_1.Parameter.MATERIAL_METALLNESS, newValue);
739
+ }),
740
+ ]);
741
+ this._parameterObservers.set(parameter_1.Parameter.MATERIAL_ROUGHNESS, [
742
+ (variant, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
743
+ yield variant.commitParameterToElements(parameter_1.Parameter.MATERIAL_ROUGHNESS, newValue);
744
+ }),
745
+ ]);
746
+ this._parameterObservers.set(parameter_1.Parameter.HIGHLIGHT_COLOR, [
747
+ (variant, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
748
+ yield variant.commitParameterToElements(parameter_1.Parameter.HIGHLIGHT_COLOR, newValue);
749
+ }),
750
+ ]);
751
+ this._parameterObservers.set(parameter_1.Parameter.HIGHLIGHTED, [
752
+ (variant, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
753
+ yield variant.commitParameterToElements(parameter_1.Parameter.HIGHLIGHTED, newValue);
754
+ }),
755
+ ]);
756
+ this._parameterObservers.set(parameter_1.Parameter.POSITION, [
757
+ (variant, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
758
+ yield variant.commitParameterToElements(parameter_1.Parameter.POSITION, newValue);
759
+ yield variant.commitParameterToViewerLights(parameter_1.Parameter.POSITION, newValue);
760
+ }),
761
+ ]);
762
+ this._parameterObservers.set(parameter_1.Parameter.ROTATION, [
763
+ (variant, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
764
+ yield variant.commitParameterToElements(parameter_1.Parameter.ROTATION, newValue);
765
+ yield variant.commitParameterToViewerLights(parameter_1.Parameter.ROTATION, newValue);
766
+ }),
767
+ ]);
768
+ this._parameterObservers.set(parameter_1.Parameter.CAST_SHADOW, [
769
+ (variant, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
770
+ yield variant.commitParameterToElements(parameter_1.Parameter.CAST_SHADOW, newValue);
771
+ }),
772
+ ]);
773
+ this._parameterObservers.set(parameter_1.Parameter.CAST_SHADOW_FROM_LIGHTS, [
774
+ (variant, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
775
+ yield variant.commitParameterToElements(parameter_1.Parameter.CAST_SHADOW_FROM_LIGHTS, newValue);
776
+ }),
777
+ ]);
778
+ this._parameterObservers.set(parameter_1.Parameter.RECEIVE_SHADOWS, [
779
+ (variant, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
780
+ yield variant.commitParameterToElements(parameter_1.Parameter.RECEIVE_SHADOWS, newValue);
781
+ }),
782
+ ]);
783
+ return this;
784
+ }
785
+ /**
786
+ * Creates {@link Element}s and clones nodes into them.
787
+ */
788
+ createElements() {
789
+ return __awaiter(this, void 0, void 0, function* () {
790
+ this.createElementDefinitionsIfNotExist();
791
+ for (const name in this.structureJson.elements || {}) {
792
+ this.elements.push(yield element_1.Element.create(this, name));
793
+ }
794
+ // inject node meta to all inherited elements
795
+ // we do this to inject the deepest and most concrete variant information to all cloned nodes in the tree
796
+ this.inheritedElements.forEach(element => {
797
+ element.nodes.forEach(node => {
798
+ (0, babylonHelper_1.injectNodeMetadata)(node, { variant: this, variantParameterizable: element });
799
+ });
800
+ });
801
+ return this;
802
+ });
803
+ }
804
+ /**
805
+ * Creates {@link ViewerLight}s.
806
+ */
807
+ createViewerLights() {
808
+ return __awaiter(this, void 0, void 0, function* () {
809
+ for (const name in this.structureJson.lights || {}) {
810
+ this.viewerLights.push(yield viewerLight_1.ViewerLight.create(this, name));
811
+ }
812
+ this.inheritedViewerLights.forEach(viewerLight => {
813
+ (0, babylonHelper_1.injectNodeMetadata)(viewerLight.light, { variant: this, variantParameterizable: viewerLight });
814
+ });
815
+ return this;
816
+ });
817
+ }
818
+ /**
819
+ * Bootstrapping for parameters. It sets the `parametersInitialized` to true for all ancestors.
820
+ */
821
+ bootstrapParameters(parameters) {
822
+ return __awaiter(this, void 0, void 0, function* () {
823
+ yield this.commitParameters((0, lodash_es_1.merge)((0, lodash_es_1.cloneDeep)(this.parameters), parameters));
824
+ (0, lodash_es_1.concat)(this.ancestors, this).forEach(ancestor => (ancestor.parametersInitialized = true));
825
+ return this;
826
+ });
827
+ }
828
+ /**
829
+ * Ensures there is at least one "Main" {@link Element} for the {@link Variant} with all "root nodes" defined in path.
830
+ */
831
+ createElementDefinitionsIfNotExist() {
832
+ if (this._structureJson.elements || this.inheritedNodes.length === 0) {
833
+ return;
834
+ }
835
+ this._structureJson.elements = {
836
+ Main: { paths: { include: this.inheritedNodes.map(node => node.metadata.dottedPath.path) } },
837
+ };
838
+ this.structureJson.elements = (0, lodash_es_1.cloneDeep)(this._structureJson.elements);
839
+ }
840
+ }
841
+ exports.Variant = Variant;
829
842
  //# sourceMappingURL=variant.js.map