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

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