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

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