@combeenation/3d-viewer 4.0.0-alpha9 → 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 (105) hide show
  1. package/README.md +112 -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 -130
  7. package/dist/lib-cjs/api/classes/element.js +682 -739
  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 -326
  12. package/dist/lib-cjs/api/classes/event.js +357 -371
  13. package/dist/lib-cjs/api/classes/event.js.map +1 -1
  14. package/dist/lib-cjs/api/classes/eventBroadcaster.d.ts +26 -26
  15. package/dist/lib-cjs/api/classes/eventBroadcaster.js +53 -53
  16. package/dist/lib-cjs/api/classes/parameter.d.ts +165 -259
  17. package/dist/lib-cjs/api/classes/parameter.js +267 -387
  18. package/dist/lib-cjs/api/classes/parameter.js.map +1 -1
  19. package/dist/lib-cjs/api/classes/parameterObservable.d.ts +36 -36
  20. package/dist/lib-cjs/api/classes/parameterObservable.js +126 -101
  21. package/dist/lib-cjs/api/classes/parameterObservable.js.map +1 -1
  22. package/dist/lib-cjs/api/classes/parameterizable.d.ts +15 -15
  23. package/dist/lib-cjs/api/classes/parameterizable.js +149 -149
  24. package/dist/lib-cjs/api/classes/placementAnimation.d.ts +38 -38
  25. package/dist/lib-cjs/api/classes/placementAnimation.js +138 -138
  26. package/dist/lib-cjs/api/classes/variant.d.ts +190 -224
  27. package/dist/lib-cjs/api/classes/variant.js +873 -1137
  28. package/dist/lib-cjs/api/classes/variant.js.map +1 -1
  29. package/dist/lib-cjs/api/classes/variantInstance.d.ts +41 -45
  30. package/dist/lib-cjs/api/classes/variantInstance.js +98 -108
  31. package/dist/lib-cjs/api/classes/variantInstance.js.map +1 -1
  32. package/dist/lib-cjs/api/classes/variantParameterizable.d.ts +17 -17
  33. package/dist/lib-cjs/api/classes/variantParameterizable.js +92 -92
  34. package/dist/lib-cjs/api/classes/viewer.d.ts +128 -131
  35. package/dist/lib-cjs/api/classes/viewer.js +486 -538
  36. package/dist/lib-cjs/api/classes/viewer.js.map +1 -1
  37. package/dist/lib-cjs/api/classes/viewerLight.d.ts +65 -66
  38. package/dist/lib-cjs/api/classes/viewerLight.js +322 -389
  39. package/dist/lib-cjs/api/classes/viewerLight.js.map +1 -1
  40. package/dist/lib-cjs/api/internal/debugViewer.d.ts +13 -13
  41. package/dist/lib-cjs/api/internal/debugViewer.js +87 -87
  42. package/dist/lib-cjs/api/internal/lensRendering.d.ts +8 -8
  43. package/dist/lib-cjs/api/internal/lensRendering.js +11 -11
  44. package/dist/lib-cjs/api/internal/sceneSetup.d.ts +6 -10
  45. package/dist/lib-cjs/api/internal/sceneSetup.js +227 -231
  46. package/dist/lib-cjs/api/internal/sceneSetup.js.map +1 -1
  47. package/dist/lib-cjs/api/manager/animationManager.d.ts +29 -29
  48. package/dist/lib-cjs/api/manager/animationManager.js +121 -121
  49. package/dist/lib-cjs/api/manager/sceneManager.d.ts +32 -32
  50. package/dist/lib-cjs/api/manager/sceneManager.js +132 -132
  51. package/dist/lib-cjs/api/manager/variantInstanceManager.d.ts +90 -90
  52. package/dist/lib-cjs/api/manager/variantInstanceManager.js +321 -321
  53. package/dist/lib-cjs/api/store/specStorage.d.ts +24 -24
  54. package/dist/lib-cjs/api/store/specStorage.js +51 -51
  55. package/dist/lib-cjs/api/util/babylonHelper.d.ts +125 -175
  56. package/dist/lib-cjs/api/util/babylonHelper.js +368 -520
  57. package/dist/lib-cjs/api/util/babylonHelper.js.map +1 -1
  58. package/dist/lib-cjs/api/util/globalTypes.d.ts +279 -321
  59. package/dist/lib-cjs/api/util/globalTypes.js +1 -1
  60. package/dist/lib-cjs/api/util/resourceHelper.d.ts +51 -26
  61. package/dist/lib-cjs/api/util/resourceHelper.js +243 -237
  62. package/dist/lib-cjs/api/util/resourceHelper.js.map +1 -1
  63. package/dist/lib-cjs/api/util/stringHelper.d.ts +9 -9
  64. package/dist/lib-cjs/api/util/stringHelper.js +25 -25
  65. package/dist/lib-cjs/buildinfo.json +3 -3
  66. package/dist/lib-cjs/index.d.ts +46 -48
  67. package/dist/lib-cjs/index.js +82 -86
  68. package/dist/lib-cjs/index.js.map +1 -1
  69. package/dist/webpack-stats.json +0 -0
  70. package/package.json +83 -83
  71. package/src/api/classes/animationInterface.ts +11 -11
  72. package/src/api/classes/dottedPath.ts +189 -189
  73. package/src/api/classes/element.ts +617 -635
  74. package/src/api/classes/{parameterizable.ts → elementParameterizable.ts} +78 -89
  75. package/src/api/classes/event.ts +355 -370
  76. package/src/api/classes/eventBroadcaster.ts +54 -54
  77. package/src/api/classes/parameter.ts +277 -408
  78. package/src/api/classes/parameterObservable.ts +121 -99
  79. package/src/api/classes/placementAnimation.ts +133 -133
  80. package/src/api/classes/variant.ts +670 -806
  81. package/src/api/classes/variantInstance.ts +81 -88
  82. package/src/api/classes/viewer.ts +421 -473
  83. package/src/api/internal/debugViewer.ts +81 -81
  84. package/src/api/internal/lensRendering.ts +10 -10
  85. package/src/api/internal/sceneSetup.ts +204 -194
  86. package/src/api/manager/animationManager.ts +116 -116
  87. package/src/api/manager/sceneManager.ts +105 -105
  88. package/src/api/manager/variantInstanceManager.ts +236 -236
  89. package/src/api/store/specStorage.ts +53 -53
  90. package/src/api/util/babylonHelper.ts +392 -553
  91. package/src/api/util/globalTypes.ts +318 -369
  92. package/src/api/util/resourceHelper.ts +187 -157
  93. package/src/buildinfo.json +2 -2
  94. package/src/commonjs.tsconfig.json +13 -13
  95. package/src/declaration.tsconfig.json +10 -10
  96. package/src/dev.ts +46 -44
  97. package/src/es6.tsconfig.json +13 -13
  98. package/src/index.ts +87 -91
  99. package/src/pagesconfig.json +57 -52
  100. package/src/tsconfig.json +43 -43
  101. package/src/tsconfig.types.json +9 -9
  102. package/src/types.d.ts +4 -4
  103. package/src/api/classes/variantParameterizable.ts +0 -73
  104. package/src/api/classes/viewerLight.ts +0 -330
  105. package/src/api/util/stringHelper.ts +0 -26
@@ -1,1138 +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 event_1 = require("./event");
18
- var parameter_1 = require("./parameter");
19
- var parameterizable_1 = require("./parameterizable");
20
- var viewerLight_1 = require("./viewerLight");
21
- /**
22
- * A concrete "Variant". Most of these are handled by either the {@link Viewer} or {@link VariantInstance}.
23
- */
24
- var Variant = /** @class */ (function (_super) {
25
- tslib_1.__extends(Variant, _super);
26
- /**
27
- * Constructor.
28
- */
29
- function Variant(name, _structureJson, viewer, parent) {
30
- var _this = _super.call(this, lodash_es_1.cloneDeep(_structureJson.parameterDeclaration), lodash_es_1.cloneDeep(_structureJson.parameters)) || this;
31
- _this.name = name;
32
- _this._structureJson = _structureJson;
33
- _this.viewer = viewer;
34
- _this.parent = parent;
35
- _this.elements = [];
36
- _this.viewerLights = [];
37
- _this._children = new Map();
38
- _this._parameterObservers = new Map();
39
- /**
40
- * @internal
41
- */
42
- _this.parametersInitialized = false;
43
- for (var parameter in _structureJson.parameterDeclaration) {
44
- if (!(parameter in (_structureJson.parameters || []))) {
45
- throw new Error("No default value for parameter \"" + parameter + "\" defined.");
46
- }
47
- }
48
- _this.assetContainer = new assetContainer_1.AssetContainer(viewer.scene);
49
- _this.structureJson = lodash_es_1.cloneDeep(_structureJson);
50
- return _this;
51
- }
52
- /**
53
- * Creates a {@link Variant} based on given parameters.
54
- *
55
- * @throws Error if "gltf" property is provided without a filename
56
- */
57
- Variant.create = function (name, structureJson, viewer, parent) {
58
- return tslib_1.__awaiter(this, void 0, void 0, function () {
59
- var variant;
60
- return tslib_1.__generator(this, function (_a) {
61
- switch (_a.label) {
62
- case 0:
63
- variant = new Variant(name, structureJson, viewer, parent);
64
- return [4 /*yield*/, variant.loadAssets()];
65
- case 1:
66
- _a.sent();
67
- return [2 /*return*/, variant];
68
- }
69
- });
70
- });
71
- };
72
- Object.defineProperty(Variant.prototype, "ancestors", {
73
- /**
74
- * The ancestor {@link Variant}s ordered from top to bottom in the built tree.
75
- */
76
- get: function () {
77
- var ancestors = [];
78
- var variant = this;
79
- while (variant.parent) {
80
- ancestors.unshift(variant.parent);
81
- variant = variant.parent;
82
- }
83
- return ancestors;
84
- },
85
- enumerable: false,
86
- configurable: true
87
- });
88
- Object.defineProperty(Variant.prototype, "root", {
89
- /**
90
- * The root {@link Variant}.
91
- */
92
- get: function () {
93
- var _a;
94
- return (_a = this.ancestors[0]) !== null && _a !== void 0 ? _a : this;
95
- },
96
- enumerable: false,
97
- configurable: true
98
- });
99
- Object.defineProperty(Variant.prototype, "dottedPath", {
100
- /**
101
- * The {@link DottedPath} in the built tree of {@link Variant}s.
102
- * E.g. "_.top-1.sub-2.sub-sub-3"
103
- */
104
- get: function () {
105
- var parentIds = this.ancestors.map(function (ancestor) {
106
- return ancestor.name;
107
- });
108
- return dottedPath_1.DottedPath.createFromParts(parentIds).addPart(this.name);
109
- },
110
- enumerable: false,
111
- configurable: true
112
- });
113
- Object.defineProperty(Variant.prototype, "id", {
114
- /**
115
- * The id representing a {@link DottedPath}.
116
- */
117
- get: function () {
118
- var dottedPath = dottedPath_1.DottedPath.create(this.dottedPath);
119
- dottedPath.shiftPart(); // remove root
120
- return dottedPath.path || '_';
121
- },
122
- enumerable: false,
123
- configurable: true
124
- });
125
- Object.defineProperty(Variant.prototype, "glTF", {
126
- /**
127
- * The defined glTF Asset.
128
- */
129
- get: function () {
130
- if (this.structureJson.glTF) {
131
- if (lodash_es_1.isString(this.structureJson.glTF)) {
132
- return {
133
- rootUrl: this.structureJson.glTF,
134
- fileName: undefined
135
- };
136
- }
137
- if (lodash_es_1.isEmpty(this.structureJson.glTF.rootUrl)) {
138
- throw new Error("No \"rootUrl\" defined in \"glTF\" definition for variant \"" + this.id + "\".");
139
- }
140
- return this.structureJson.glTF;
141
- }
142
- },
143
- enumerable: false,
144
- configurable: true
145
- });
146
- Object.defineProperty(Variant.prototype, "glTFUri", {
147
- /**
148
- * The defined glTF URI.
149
- */
150
- get: function () {
151
- if (this.glTF) {
152
- return [this.glTF.rootUrl, this.glTF.fileName].join('');
153
- }
154
- },
155
- enumerable: false,
156
- configurable: true
157
- });
158
- Object.defineProperty(Variant.prototype, "inheritedGlTFUri", {
159
- /**
160
- * The inherited defined glTF URI.
161
- */
162
- get: function () {
163
- if (!this.glTFUri && this.parent) {
164
- return this.parent.inheritedGlTFUri;
165
- }
166
- return this.glTFUri;
167
- },
168
- enumerable: false,
169
- configurable: true
170
- });
171
- Object.defineProperty(Variant.prototype, "nodes", {
172
- /**
173
- * The TransformNodes of the {@link Variant}.
174
- */
175
- get: function () {
176
- var rootNodes = this.assetContainer.getNodes().filter(function (n) { return n instanceof transformNode_1.TransformNode && !n.parent; });
177
- return rootNodes;
178
- },
179
- enumerable: false,
180
- configurable: true
181
- });
182
- Object.defineProperty(Variant.prototype, "lights", {
183
- /**
184
- * The {@link ViewerLight}s of the {@link Variant}.
185
- */
186
- get: function () {
187
- return this.assetContainer.lights;
188
- },
189
- enumerable: false,
190
- configurable: true
191
- });
192
- Object.defineProperty(Variant.prototype, "dottedNodes", {
193
- /**
194
- * All TransformNodes of the {@link Variant} mapped flat with a {@link DottedPath}.
195
- */
196
- get: function () {
197
- if (!this._dottedNodes) {
198
- var nodes = this.assetContainer.getNodes().filter(function (n) { return n instanceof transformNode_1.TransformNode; });
199
- var dottedNodes_1 = new Map();
200
- nodes.forEach(function (node) {
201
- dottedNodes_1.set(node.metadata.dottedPath, node);
202
- });
203
- this._dottedNodes = dottedNodes_1;
204
- }
205
- return this._dottedNodes;
206
- },
207
- enumerable: false,
208
- configurable: true
209
- });
210
- Object.defineProperty(Variant.prototype, "materials", {
211
- /**
212
- * The Materials of the {@link Variant}.
213
- */
214
- get: function () {
215
- return this.assetContainer.materials;
216
- },
217
- enumerable: false,
218
- configurable: true
219
- });
220
- Object.defineProperty(Variant.prototype, "inheritedElements", {
221
- /**
222
- * All {@link Element}s from this {@link Variant}'s parents.
223
- */
224
- get: function () {
225
- var elements = [];
226
- this.ancestors.forEach(function (ancestor) {
227
- elements = lodash_es_1.concat(elements, ancestor.elements);
228
- });
229
- return lodash_es_1.concat(elements, this.elements);
230
- },
231
- enumerable: false,
232
- configurable: true
233
- });
234
- Object.defineProperty(Variant.prototype, "inheritedViewerLights", {
235
- /**
236
- * All {@link ViewerLight}s inherited from this {@link Variant}'s parents.
237
- */
238
- get: function () {
239
- var viewerLights = [];
240
- this.ancestors.forEach(function (ancestor) {
241
- viewerLights = lodash_es_1.concat(viewerLights, ancestor.viewerLights);
242
- });
243
- return lodash_es_1.concat(viewerLights, this.viewerLights);
244
- },
245
- enumerable: false,
246
- configurable: true
247
- });
248
- Object.defineProperty(Variant.prototype, "inheritedNodes", {
249
- /**
250
- * All TransformNodes inherited from this {@link Variant}'s parents.
251
- */
252
- get: function () {
253
- var nodes = [];
254
- this.ancestors.forEach(function (ancestor) {
255
- nodes = lodash_es_1.concat(nodes, ancestor.nodes);
256
- });
257
- return lodash_es_1.concat(nodes, this.nodes);
258
- },
259
- enumerable: false,
260
- configurable: true
261
- });
262
- Object.defineProperty(Variant.prototype, "inheritedDottedNodes", {
263
- /**
264
- * All TransformNodes inherited from this {@link Variant}'s parents mapped flat with a {@link DottedPath}.
265
- */
266
- get: function () {
267
- var dottedNodes = this.dottedNodes;
268
- this.ancestors.forEach(function (ancestor) {
269
- dottedNodes = resourceHelper_1.mergeMaps(dottedNodes, ancestor.dottedNodes);
270
- });
271
- return dottedNodes;
272
- },
273
- enumerable: false,
274
- configurable: true
275
- });
276
- Object.defineProperty(Variant.prototype, "inheritedLights", {
277
- /**
278
- * All Lights inherited from this {@link Variant}'s parents.
279
- */
280
- get: function () {
281
- var lights = [];
282
- this.ancestors.forEach(function (ancestor) {
283
- lights = lodash_es_1.concat(lights, ancestor.lights);
284
- });
285
- return lodash_es_1.concat(lights, this.lights);
286
- },
287
- enumerable: false,
288
- configurable: true
289
- });
290
- Object.defineProperty(Variant.prototype, "inheritedParameterDeclaration", {
291
- /**
292
- * The {@link ParameterDeclarations} inherited from this {@link Variant}'s parents.
293
- */
294
- get: function () {
295
- var declaration = {};
296
- this.ancestors.forEach(function (ancestor) {
297
- lodash_es_1.merge(declaration, ancestor.parameterDeclaration);
298
- });
299
- return lodash_es_1.merge(declaration, this.parameterDeclaration);
300
- },
301
- enumerable: false,
302
- configurable: true
303
- });
304
- Object.defineProperty(Variant.prototype, "inheritedParameters", {
305
- /**
306
- * The {@link ParameterBag} inherited from this {@link Variant}'s parents.
307
- */
308
- get: function () {
309
- var parameters = {};
310
- this.ancestors.forEach(function (ancestor) {
311
- lodash_es_1.merge(parameters, ancestor.parameters);
312
- });
313
- return lodash_es_1.merge(parameters, this.parameters);
314
- },
315
- enumerable: false,
316
- configurable: true
317
- });
318
- Object.defineProperty(Variant.prototype, "inheritedMaterials", {
319
- /**
320
- * All Materials from this {@link Variant}'s parents.
321
- */
322
- get: function () {
323
- var materials = [];
324
- this.ancestors.forEach(function (ancestor) {
325
- materials = lodash_es_1.concat(materials, ancestor.materials);
326
- });
327
- return lodash_es_1.concat(materials, this.materials);
328
- },
329
- enumerable: false,
330
- configurable: true
331
- });
332
- /**
333
- * Gets the direct children of the current {@link Variant}.
334
- */
335
- Variant.prototype.getChildren = function () {
336
- return tslib_1.__awaiter(this, void 0, void 0, function () {
337
- var children, _a, _b, _i, name_1, _c, _d;
338
- return tslib_1.__generator(this, function (_e) {
339
- switch (_e.label) {
340
- case 0:
341
- children = [];
342
- _a = [];
343
- for (_b in this.structureJson.variants)
344
- _a.push(_b);
345
- _i = 0;
346
- _e.label = 1;
347
- case 1:
348
- if (!(_i < _a.length)) return [3 /*break*/, 4];
349
- name_1 = _a[_i];
350
- _d = (_c = children).push;
351
- return [4 /*yield*/, this.getDescendant(name_1)];
352
- case 2:
353
- _d.apply(_c, [_e.sent()]);
354
- _e.label = 3;
355
- case 3:
356
- _i++;
357
- return [3 /*break*/, 1];
358
- case 4: return [2 /*return*/, children];
359
- }
360
- });
361
- });
362
- };
363
- /**
364
- * Gets a descendant {@link Variant} of the current {@link Variant} relative to its {@link DottedPath}.
365
- * If you have the dotted path `_.product_x.variant_blue.with_yellow_highlight` in a tree and you operate on the
366
- * `product_x`, you can call `this.getDescendant('variant_blue.with_yellow_highlight')` to get the lowermost
367
- * {@link Variant}.
368
- */
369
- Variant.prototype.getDescendant = function (dottedPath) {
370
- return tslib_1.__awaiter(this, void 0, void 0, function () {
371
- var _dottedPath, _a, name, descendantParts, variant, file, _b, _c, _d;
372
- return tslib_1.__generator(this, function (_e) {
373
- switch (_e.label) {
374
- case 0:
375
- _dottedPath = dottedPath_1.DottedPath.create(dottedPath);
376
- _a = _dottedPath.parts, name = _a[0], descendantParts = _a.slice(1);
377
- if (!this._children.has(name)) return [3 /*break*/, 1];
378
- variant = this._children.get(name);
379
- return [3 /*break*/, 7];
380
- case 1:
381
- if (!this.structureJson.variants) {
382
- throw new Error("Missing key \"variants\" in JSON structure for variant \"" + this.id + "\".");
383
- }
384
- if (!this.structureJson.variants[name]) {
385
- throw new Error("Variant \"" + _dottedPath.path + "\" not defined in JSON structure for variant \"" + this.id + "\".");
386
- }
387
- if (!this.structureJson.variants[name].file) return [3 /*break*/, 4];
388
- file = this.structureJson.variants[name].file;
389
- _c = (_b = Variant).create;
390
- _d = [name];
391
- return [4 /*yield*/, resourceHelper_1.loadJson(file)];
392
- case 2: return [4 /*yield*/, _c.apply(_b, _d.concat([_e.sent(), this.viewer, this]))];
393
- case 3:
394
- variant = _e.sent();
395
- return [3 /*break*/, 6];
396
- case 4: return [4 /*yield*/, Variant.create(name, this.structureJson.variants[name], this.viewer, this)];
397
- case 5:
398
- variant = _e.sent();
399
- _e.label = 6;
400
- case 6:
401
- this._children.set(name, variant);
402
- _e.label = 7;
403
- case 7:
404
- if (!variant) {
405
- throw new Error("Variant \"" + _dottedPath.path + "\" was not created.");
406
- }
407
- if (!(descendantParts.length > 0)) return [3 /*break*/, 9];
408
- return [4 /*yield*/, variant.getDescendant(dottedPath_1.DottedPath.createFromParts(descendantParts))];
409
- case 8: return [2 /*return*/, _e.sent()];
410
- case 9: return [2 /*return*/, variant];
411
- }
412
- });
413
- });
414
- };
415
- /**
416
- * Gets the desired {@link Element} of the current {@link Variant} relative to its {@link DottedPath}.
417
- * Uses the mechanism of {@link getDescendant} to resolve the appropriate variant in tree.
418
- */
419
- Variant.prototype.getElement = function (dottedPath) {
420
- return tslib_1.__awaiter(this, void 0, void 0, function () {
421
- var _dottedPath, elementName, variant, element;
422
- return tslib_1.__generator(this, function (_a) {
423
- switch (_a.label) {
424
- case 0:
425
- _dottedPath = dottedPath_1.DottedPath.create(dottedPath);
426
- elementName = _dottedPath.popPart();
427
- variant = this;
428
- if (!(_dottedPath.parts.length > 0)) return [3 /*break*/, 2];
429
- return [4 /*yield*/, this.getDescendant(_dottedPath)];
430
- case 1:
431
- variant = _a.sent();
432
- _a.label = 2;
433
- case 2:
434
- if (variant.inheritedElements.length === 0) {
435
- throw new Error("No elements for variant \"" + variant.id + "\" found. " +
436
- "Either none are defined or they are not initialized (are you operating on the appropriate living?).");
437
- }
438
- variant.inheritedElements.forEach(function (_element) {
439
- if (_element.name === elementName) {
440
- element = _element;
441
- }
442
- });
443
- if (!element) {
444
- throw new Error("Element with name \"" + elementName + "\" does not exist for variant \"" + variant.id + "\".");
445
- }
446
- return [2 /*return*/, element];
447
- }
448
- });
449
- });
450
- };
451
- /**
452
- * Gets the desired {@link ViewerLight} of the current {@link Variant} relative to its {@link DottedPath}.
453
- * Uses the mechanism of {@link getDescendant} to resolve the appropriate variant in tree.
454
- */
455
- Variant.prototype.getViewerLight = function (dottedPath) {
456
- return tslib_1.__awaiter(this, void 0, void 0, function () {
457
- var _dottedPath, viewerLightName, variant, viewerLight;
458
- return tslib_1.__generator(this, function (_a) {
459
- switch (_a.label) {
460
- case 0:
461
- _dottedPath = dottedPath_1.DottedPath.create(dottedPath);
462
- viewerLightName = _dottedPath.popPart();
463
- variant = this;
464
- if (!(_dottedPath.parts.length > 0)) return [3 /*break*/, 2];
465
- return [4 /*yield*/, this.getDescendant(_dottedPath)];
466
- case 1:
467
- variant = _a.sent();
468
- _a.label = 2;
469
- case 2:
470
- if (variant.inheritedViewerLights.length === 0) {
471
- throw new Error("No viewerLights for variant \"" + variant.id + "\" found. " +
472
- "Either none are defined or they are not initialized (are you operating on the appropriate living?).");
473
- }
474
- variant.inheritedViewerLights.forEach(function (_viewerLight) {
475
- if (_viewerLight.name === viewerLightName) {
476
- viewerLight = _viewerLight;
477
- }
478
- });
479
- if (!viewerLight) {
480
- throw new Error("ViewerLight with name \"" + viewerLightName + "\" does not exist for variant \"" + variant.id + "\".");
481
- }
482
- return [2 /*return*/, viewerLight];
483
- }
484
- });
485
- });
486
- };
487
- /**
488
- * A proxy for directly getting a Node from an {@link Element} by its {@link DottedPath}s.
489
- */
490
- Variant.prototype.getNode = function (elementDottedPath, nodeDottedPath) {
491
- return tslib_1.__awaiter(this, void 0, void 0, function () {
492
- var element;
493
- return tslib_1.__generator(this, function (_a) {
494
- switch (_a.label) {
495
- case 0: return [4 /*yield*/, this.getElement(elementDottedPath)];
496
- case 1:
497
- element = _a.sent();
498
- return [2 /*return*/, element.getNode(nodeDottedPath)];
499
- }
500
- });
501
- });
502
- };
503
- /**
504
- * A proxy for directly getting a Mesh from an {@link Element} by its {@link DottedPath}s.
505
- */
506
- Variant.prototype.getMesh = function (elementDottedPath, meshDottedPath) {
507
- return tslib_1.__awaiter(this, void 0, void 0, function () {
508
- var element;
509
- return tslib_1.__generator(this, function (_a) {
510
- switch (_a.label) {
511
- case 0: return [4 /*yield*/, this.getElement(elementDottedPath)];
512
- case 1:
513
- element = _a.sent();
514
- return [2 /*return*/, element.getMesh(meshDottedPath)];
515
- }
516
- });
517
- });
518
- };
519
- /**
520
- * Gets the Material defined in one of the variants glTFs by its id.
521
- */
522
- Variant.prototype.getMaterial = function (id) {
523
- for (var _i = 0, _a = this.inheritedMaterials; _i < _a.length; _i++) {
524
- var material = _a[_i];
525
- if (material.id === id) {
526
- return material;
527
- }
528
- }
529
- // fallback to dynamically created materials on scene
530
- for (var _b = 0, _c = this.viewer.scene.materials; _b < _c.length; _b++) {
531
- var material = _c[_b];
532
- if (material.id === id) {
533
- return material;
534
- }
535
- }
536
- throw new Error("Material with id \"" + id + "\" does not exist for variant \"" + this.id + "\".");
537
- };
538
- /**
539
- * Creates a living clone of this {@link Variant}. Will clone all parent {@link Variant}s in tree.
540
- *
541
- * @emit {@link Event.VARIANT_CREATED}
542
- * @ignore
543
- */
544
- Variant.prototype.createLiving = function (parameters) {
545
- var _a;
546
- return tslib_1.__awaiter(this, void 0, void 0, function () {
547
- var parent, variant;
548
- return tslib_1.__generator(this, function (_b) {
549
- switch (_b.label) {
550
- case 0: return [4 /*yield*/, ((_a = this.parent) === null || _a === void 0 ? void 0 : _a.createLiving())];
551
- case 1:
552
- parent = _b.sent();
553
- variant = new Variant(this.name, this._structureJson, this.viewer, parent);
554
- parent === null || parent === void 0 ? void 0 : parent._children.set(variant.name, variant);
555
- variant.assetContainer = this.assetContainer;
556
- variant.parameterObservers = lodash_es_1.cloneDeep(this.parameterObservers);
557
- return [4 /*yield*/, variant.createElements()];
558
- case 2:
559
- _b.sent();
560
- return [4 /*yield*/, variant.createViewerLights()];
561
- case 3:
562
- _b.sent();
563
- variant.addParameterObservers();
564
- return [4 /*yield*/, variant.bootstrapParameters(parameters)];
565
- case 4:
566
- _b.sent();
567
- this.broadcastEvent(event_1.Event.VARIANT_CREATED, variant);
568
- return [2 /*return*/, variant];
569
- }
570
- });
571
- });
572
- };
573
- /**
574
- * Destroys this {@link Variant}, all parents and destroy the {@link Element}s.
575
- */
576
- Variant.prototype.destroy = function () {
577
- this.elements.forEach(function (element) { return element.destroy(); });
578
- if (this.parent) {
579
- this.parent.destroy();
580
- }
581
- this.broadcastEvent(event_1.Event.VARIANT_DESTROYED, this);
582
- return this;
583
- };
584
- /**
585
- * Places the given {@link ParameterBag} in the {@link Variant}'s parameters, replaces all patterns in the
586
- * {@link StructureJson}, broadcasts all {@link ParameterObserver}s and delegates them to its {@link Element}s.
587
- *
588
- * @emit {@link Event.VARIANT_PARAMETER_COMMITTED}
589
- */
590
- Variant.prototype.commitParameters = function (parameters) {
591
- return tslib_1.__awaiter(this, void 0, void 0, function () {
592
- 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;
593
- var _this = this;
594
- return tslib_1.__generator(this, function (_a) {
595
- switch (_a.label) {
596
- case 0:
597
- parameters = lodash_es_1.merge({}, parameters);
598
- oldParameters = lodash_es_1.cloneDeep(this.inheritedParameters);
599
- _parameters = JSON.stringify(parameters);
600
- for (parameter in this.inheritedParameters) {
601
- value = this.inheritedParameters[parameter];
602
- search = new RegExp("\\$\\{" + parameter + "\\}", 'g');
603
- _parameters = _parameters.replace(search, value.toString());
604
- }
605
- lodash_es_1.merge(parameters, JSON.parse(_parameters));
606
- mergedParameters = lodash_es_1.merge({}, this.inheritedParameters, parameters);
607
- structureParameters = this._structureJson.parameters || {};
608
- _structureParameters = JSON.stringify(structureParameters);
609
- for (parameter in mergedParameters) {
610
- value = mergedParameters[parameter];
611
- search = new RegExp("\\$\\{" + parameter + "\\}", 'g');
612
- _structureParameters = _structureParameters.replace(search, value.toString());
613
- }
614
- replacedStructureParameters = JSON.parse(_structureParameters);
615
- differentStructureParameters = {};
616
- for (parameter in replacedStructureParameters) {
617
- if (!lodash_es_1.isEqual(structureParameters[parameter], replacedStructureParameters[parameter])) {
618
- differentStructureParameters[parameter] = replacedStructureParameters[parameter];
619
- }
620
- }
621
- // merge replaced structure parameters and given inherited parameters to structure parameters
622
- lodash_es_1.merge(this.parameters, mergedParameters, differentStructureParameters);
623
- newParameters = this.inheritedParameters;
624
- structure = JSON.stringify(this._structureJson);
625
- for (parameter in newParameters) {
626
- value = newParameters[parameter];
627
- search = new RegExp("\\$\\{" + parameter + "\\}", 'g');
628
- structure = structure.replace(search, value.toString());
629
- }
630
- this.structureJson = JSON.parse(structure);
631
- observerPromises = [];
632
- _loop_1 = function (parameter) {
633
- var oldParameterValue = oldParameters[parameter];
634
- var newParameterValue = newParameters[parameter];
635
- this_1.assertParameter(this_1.inheritedParameterDeclaration, parameter, newParameterValue);
636
- if (oldParameterValue === newParameterValue && this_1.parametersInitialized) {
637
- return "continue";
638
- }
639
- // parameter changed
640
- var parameterObservers = resourceHelper_1.mergeMaps(this_1._parameterObservers, this_1.parameterObservers);
641
- if (parameterObservers.has(parameter)) {
642
- var observers = parameterObservers.get(parameter);
643
- observerPromises = lodash_es_1.concat(observerPromises, observers.map(function (observer) {
644
- var observerResult = observer(_this, oldParameterValue, newParameterValue);
645
- return Promise.resolve(observerResult).then(function () {
646
- if (_this.parametersInitialized) {
647
- _this.broadcastEvent(event_1.Event.VARIANT_PARAMETER_COMMITTED, _this, parameter, oldParameterValue, newParameterValue);
648
- }
649
- });
650
- }));
651
- }
652
- };
653
- this_1 = this;
654
- for (parameter in newParameters) {
655
- _loop_1(parameter);
656
- }
657
- return [4 /*yield*/, Promise.all(observerPromises)];
658
- case 1:
659
- _a.sent();
660
- // broadcast that bag has been committed
661
- this.broadcastEvent(event_1.Event.VARIANT_PARAMETER_BAG_COMMITTED, this, oldParameters, newParameters);
662
- // commit parameters to elements
663
- return [4 /*yield*/, this.commitParametersToElements(newParameters)];
664
- case 2:
665
- // commit parameters to elements
666
- _a.sent();
667
- // commit parameters to lights
668
- return [4 /*yield*/, this.commitParametersToViewerLights(newParameters)];
669
- case 3:
670
- // commit parameters to lights
671
- _a.sent();
672
- if (!this.parent) return [3 /*break*/, 5];
673
- return [4 /*yield*/, this.parent.commitParameters(this.parameters)];
674
- case 4:
675
- _a.sent();
676
- _a.label = 5;
677
- case 5: return [2 /*return*/, this];
678
- }
679
- });
680
- });
681
- };
682
- /**
683
- * Adds an observer function for camera matrix changes for given `dottedPath` representing the {@link Element}
684
- * and the `traceable`. The `observer` gets 2 parameters: the `AbstractMesh` and a `ClientRect` object.
685
- */
686
- Variant.prototype.addTraceableObserver = function (dottedPath, observer, payload) {
687
- return tslib_1.__awaiter(this, void 0, void 0, function () {
688
- var _dottedPath, traceableName, element;
689
- return tslib_1.__generator(this, function (_a) {
690
- switch (_a.label) {
691
- case 0:
692
- _dottedPath = dottedPath_1.DottedPath.create(dottedPath);
693
- traceableName = _dottedPath.popPart();
694
- if (!traceableName) {
695
- throw new Error("The dottedPath must consist of the element and the name of the defined corresponding " +
696
- ("traceable (\"" + _dottedPath.path + "\" given)."));
697
- }
698
- return [4 /*yield*/, this.getElement(_dottedPath)];
699
- case 1:
700
- element = _a.sent();
701
- return [2 /*return*/, element.addTraceableObserver(traceableName, observer, payload)];
702
- }
703
- });
704
- });
705
- };
706
- /**
707
- * Loads {@link glTFUri} with assets, adds them to the {@link Variant}'s `assetContainer` and deactivates the meshes.
708
- * (for further processing).
709
- * @emits {@link Event.ASSET_LOADING_START}
710
- * @emits {@link Event.ASSET_LOADING_END}
711
- */
712
- Variant.prototype.loadAssets = function () {
713
- return tslib_1.__awaiter(this, void 0, void 0, function () {
714
- var _this = this;
715
- return tslib_1.__generator(this, function (_a) {
716
- this.broadcastEvent(event_1.Event.ASSET_LOADING_START, this);
717
- return [2 /*return*/, new Promise(function (resolve) {
718
- if (!_this.structureJson) {
719
- _this.broadcastEvent(event_1.Event.ASSET_LOADING_END, _this);
720
- return resolve(_this);
721
- }
722
- if (!_this.glTF) {
723
- _this.broadcastEvent(event_1.Event.ASSET_LOADING_END, _this);
724
- return resolve(_this);
725
- }
726
- sceneLoader_1.SceneLoader.LoadAssetContainerAsync(_this.glTF.rootUrl, _this.glTF.fileName, _this.viewer.scene).then(function (container) {
727
- _this.assetContainer = container;
728
- var nodes = _this.assetContainer.getNodes().filter(function (n) { return n instanceof transformNode_1.TransformNode; });
729
- nodes.forEach(function (node) {
730
- babylonHelper_1.deactivateTransformNode(node, false);
731
- babylonHelper_1.injectNodeMetadata(node, { dottedPath: babylonHelper_1.getDottedPathForNode(node) }, false);
732
- });
733
- _this.assetContainer.lights.forEach(function (light) {
734
- light.setEnabled(false);
735
- babylonHelper_1.injectNodeMetadata(light, { dottedPath: babylonHelper_1.getDottedPathForNode(light) }, false);
736
- _this.viewer.scene.addLight(light);
737
- });
738
- _this.assetContainer.cameras.forEach(function (camera) {
739
- camera.setEnabled(false);
740
- babylonHelper_1.injectNodeMetadata(camera, { dottedPath: babylonHelper_1.getDottedPathForNode(camera) }, false);
741
- _this.viewer.scene.addCamera(camera);
742
- });
743
- _this.broadcastEvent(event_1.Event.ASSET_LOADING_END, _this);
744
- resolve(_this);
745
- }).catch(function (reason) {
746
- _this.broadcastEvent(event_1.Event.ASSET_LOADING_END, _this);
747
- throw new Error("Error loading assets for variant \"" + _this.id + "\": " + reason + ".");
748
- });
749
- })];
750
- });
751
- });
752
- };
753
- /**
754
- * Commits given parameters to all {@link Element}s.
755
- */
756
- Variant.prototype.commitParametersToElements = function (parameters) {
757
- return tslib_1.__awaiter(this, void 0, void 0, function () {
758
- var _this = this;
759
- return tslib_1.__generator(this, function (_a) {
760
- switch (_a.label) {
761
- case 0: return [4 /*yield*/, Promise.all(this.elements.map(function (element) { return _this.commitParametersToVariantParameterizable(parameters, element, 'elements'); }))];
762
- case 1:
763
- _a.sent();
764
- return [2 /*return*/];
765
- }
766
- });
767
- });
768
- };
769
- /**
770
- * Commits given parameters to all {@link ViewerLight}s.
771
- */
772
- Variant.prototype.commitParametersToViewerLights = function (parameters) {
773
- return tslib_1.__awaiter(this, void 0, void 0, function () {
774
- var _this = this;
775
- return tslib_1.__generator(this, function (_a) {
776
- switch (_a.label) {
777
- case 0: return [4 /*yield*/, Promise.all(this.viewerLights.map(function (viewerLight) { return _this.commitParametersToVariantParameterizable(parameters, viewerLight, 'lights'); }))];
778
- case 1:
779
- _a.sent();
780
- return [2 /*return*/];
781
- }
782
- });
783
- });
784
- };
785
- /**
786
- * Commits given parameters to a {@link VariantParameterizable} and updates the according definition with given
787
- * key in the {@link StructureJson}. The `definitionKey` "elements" for example will update the definition in
788
- * `this.structureJson.elements`.
789
- */
790
- Variant.prototype.commitParametersToVariantParameterizable = function (parameters, parameterizable, definitionKey) {
791
- return tslib_1.__awaiter(this, void 0, void 0, function () {
792
- var initialDefinition, initialDefinitionStr, _parameters, parameter, dpp, parameterValue, parameterizableParameter, search, definition;
793
- return tslib_1.__generator(this, function (_a) {
794
- switch (_a.label) {
795
- case 0:
796
- initialDefinition = lodash_es_1.get(this._structureJson, definitionKey)[parameterizable.name];
797
- initialDefinitionStr = JSON.stringify(initialDefinition);
798
- _parameters = {};
799
- for (parameter in parameters) {
800
- dpp = dottedPath_1.DottedPath.create(parameter);
801
- if (dpp.shiftPart() !== parameterizable.name) {
802
- continue;
803
- }
804
- parameterValue = parameters[parameter];
805
- parameterizableParameter = dpp.path;
806
- // If the variant is explicitly hidden, we must not override the visibility with element parameters. We need
807
- // an exception for visibility to avoid overloading already applied element parameters with element parameters
808
- // defined in the variant spec ("dotted parameters").
809
- // @see https://github.com/Combeenation/3d-viewer/issues/44
810
- if (parameterizableParameter === parameter_1.Parameter.VISIBLE && parameters[parameter_1.Parameter.VISIBLE] === false) {
811
- parameterValue = false;
812
- }
813
- _parameters[parameterizableParameter] = parameterValue;
814
- search = new RegExp("\\$\\{" + parameterizableParameter + "\\}", 'g');
815
- initialDefinitionStr = initialDefinitionStr.replace(search, parameterValue.toString());
816
- }
817
- definition = lodash_es_1.get(this.structureJson, definitionKey);
818
- definition[this.name] = JSON.parse(initialDefinitionStr);
819
- lodash_es_1.set(this.structureJson, definitionKey, definition);
820
- return [4 /*yield*/, parameterizable.commitParameters(_parameters)];
821
- case 1: return [2 /*return*/, _a.sent()];
822
- }
823
- });
824
- });
825
- };
826
- /**
827
- * Commits given {@link Parameter} to the {@link Variant}'s {@link Element}s.
828
- */
829
- Variant.prototype.commitParameterToElements = function (parameter, value) {
830
- return tslib_1.__awaiter(this, void 0, void 0, function () {
831
- var promises, _i, _a, element;
832
- return tslib_1.__generator(this, function (_b) {
833
- switch (_b.label) {
834
- case 0:
835
- promises = [];
836
- for (_i = 0, _a = this.elements; _i < _a.length; _i++) {
837
- element = _a[_i];
838
- promises.push(element.commitParameter(parameter, value));
839
- }
840
- return [4 /*yield*/, Promise.all(promises)];
841
- case 1:
842
- _b.sent();
843
- return [2 /*return*/, this];
844
- }
845
- });
846
- });
847
- };
848
- /**
849
- * Commits given {@link Parameter} to the {@link Variant}'s {@link Element}s.
850
- */
851
- Variant.prototype.commitParameterToViewerLights = function (parameter, value) {
852
- return tslib_1.__awaiter(this, void 0, void 0, function () {
853
- var promises, _i, _a, viewerLight;
854
- return tslib_1.__generator(this, function (_b) {
855
- switch (_b.label) {
856
- case 0:
857
- promises = [];
858
- for (_i = 0, _a = this.viewerLights; _i < _a.length; _i++) {
859
- viewerLight = _a[_i];
860
- promises.push(viewerLight.commitParameter(parameter, value));
861
- }
862
- return [4 /*yield*/, Promise.all(promises)];
863
- case 1:
864
- _b.sent();
865
- return [2 /*return*/, this];
866
- }
867
- });
868
- });
869
- };
870
- /**
871
- * Adds the default {@link ParameterObserver}s which are called every time {@link commitParameters} is called.
872
- */
873
- Variant.prototype.addParameterObservers = function () {
874
- var _this = this;
875
- this._parameterObservers.set(parameter_1.Parameter.VISIBLE, [
876
- function (variant, oldValue, newValue) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
877
- return tslib_1.__generator(this, function (_a) {
878
- switch (_a.label) {
879
- case 0: return [4 /*yield*/, variant.commitParameterToElements(parameter_1.Parameter.VISIBLE, newValue)];
880
- case 1:
881
- _a.sent();
882
- return [4 /*yield*/, variant.commitParameterToViewerLights(parameter_1.Parameter.VISIBLE, newValue)];
883
- case 2:
884
- _a.sent();
885
- return [2 /*return*/];
886
- }
887
- });
888
- }); }
889
- ]);
890
- this._parameterObservers.set(parameter_1.Parameter.SCALING, [
891
- function (variant, oldValue, newValue) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
892
- return tslib_1.__generator(this, function (_a) {
893
- switch (_a.label) {
894
- case 0: return [4 /*yield*/, variant.commitParameterToElements(parameter_1.Parameter.SCALING, newValue)];
895
- case 1:
896
- _a.sent();
897
- return [4 /*yield*/, variant.commitParameterToViewerLights(parameter_1.Parameter.SCALING, newValue)];
898
- case 2:
899
- _a.sent();
900
- return [2 /*return*/];
901
- }
902
- });
903
- }); }
904
- ]);
905
- this._parameterObservers.set(parameter_1.Parameter.MATERIAL, [
906
- function (variant, oldValue, newValue) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
907
- return tslib_1.__generator(this, function (_a) {
908
- switch (_a.label) {
909
- case 0: return [4 /*yield*/, variant.commitParameterToElements(parameter_1.Parameter.MATERIAL, newValue)];
910
- case 1:
911
- _a.sent();
912
- return [2 /*return*/];
913
- }
914
- });
915
- }); }
916
- ]);
917
- this._parameterObservers.set(parameter_1.Parameter.MATERIAL_COLOR, [
918
- function (variant, oldValue, newValue) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
919
- return tslib_1.__generator(this, function (_a) {
920
- switch (_a.label) {
921
- case 0: return [4 /*yield*/, variant.commitParameterToElements(parameter_1.Parameter.MATERIAL_COLOR, newValue)];
922
- case 1:
923
- _a.sent();
924
- return [2 /*return*/];
925
- }
926
- });
927
- }); }
928
- ]);
929
- this._parameterObservers.set(parameter_1.Parameter.MATERIAL_METALLNESS, [
930
- function (variant, oldValue, newValue) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
931
- return tslib_1.__generator(this, function (_a) {
932
- switch (_a.label) {
933
- case 0: return [4 /*yield*/, variant.commitParameterToElements(parameter_1.Parameter.MATERIAL_METALLNESS, newValue)];
934
- case 1:
935
- _a.sent();
936
- return [2 /*return*/];
937
- }
938
- });
939
- }); }
940
- ]);
941
- this._parameterObservers.set(parameter_1.Parameter.MATERIAL_ROUGHNESS, [
942
- function (variant, oldValue, newValue) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
943
- return tslib_1.__generator(this, function (_a) {
944
- switch (_a.label) {
945
- case 0: return [4 /*yield*/, variant.commitParameterToElements(parameter_1.Parameter.MATERIAL_ROUGHNESS, newValue)];
946
- case 1:
947
- _a.sent();
948
- return [2 /*return*/];
949
- }
950
- });
951
- }); }
952
- ]);
953
- this._parameterObservers.set(parameter_1.Parameter.HIGHLIGHT_COLOR, [
954
- function (variant, oldValue, newValue) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
955
- return tslib_1.__generator(this, function (_a) {
956
- switch (_a.label) {
957
- case 0: return [4 /*yield*/, variant.commitParameterToElements(parameter_1.Parameter.HIGHLIGHT_COLOR, newValue)];
958
- case 1:
959
- _a.sent();
960
- return [2 /*return*/];
961
- }
962
- });
963
- }); }
964
- ]);
965
- this._parameterObservers.set(parameter_1.Parameter.HIGHLIGHTED, [
966
- function (variant, oldValue, newValue) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
967
- return tslib_1.__generator(this, function (_a) {
968
- switch (_a.label) {
969
- case 0: return [4 /*yield*/, variant.commitParameterToElements(parameter_1.Parameter.HIGHLIGHTED, newValue)];
970
- case 1:
971
- _a.sent();
972
- return [2 /*return*/];
973
- }
974
- });
975
- }); }
976
- ]);
977
- this._parameterObservers.set(parameter_1.Parameter.POSITION, [
978
- function (variant, oldValue, newValue) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
979
- return tslib_1.__generator(this, function (_a) {
980
- switch (_a.label) {
981
- case 0: return [4 /*yield*/, variant.commitParameterToElements(parameter_1.Parameter.POSITION, newValue)];
982
- case 1:
983
- _a.sent();
984
- return [4 /*yield*/, variant.commitParameterToViewerLights(parameter_1.Parameter.POSITION, newValue)];
985
- case 2:
986
- _a.sent();
987
- return [2 /*return*/];
988
- }
989
- });
990
- }); }
991
- ]);
992
- this._parameterObservers.set(parameter_1.Parameter.ROTATION, [
993
- function (variant, oldValue, newValue) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
994
- return tslib_1.__generator(this, function (_a) {
995
- switch (_a.label) {
996
- case 0: return [4 /*yield*/, variant.commitParameterToElements(parameter_1.Parameter.ROTATION, newValue)];
997
- case 1:
998
- _a.sent();
999
- return [4 /*yield*/, variant.commitParameterToViewerLights(parameter_1.Parameter.ROTATION, newValue)];
1000
- case 2:
1001
- _a.sent();
1002
- return [2 /*return*/];
1003
- }
1004
- });
1005
- }); }
1006
- ]);
1007
- this._parameterObservers.set(parameter_1.Parameter.CAST_SHADOW, [
1008
- function (variant, oldValue, newValue) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
1009
- return tslib_1.__generator(this, function (_a) {
1010
- switch (_a.label) {
1011
- case 0: return [4 /*yield*/, variant.commitParameterToElements(parameter_1.Parameter.CAST_SHADOW, newValue)];
1012
- case 1:
1013
- _a.sent();
1014
- return [2 /*return*/];
1015
- }
1016
- });
1017
- }); }
1018
- ]);
1019
- this._parameterObservers.set(parameter_1.Parameter.CAST_SHADOW_FROM_LIGHTS, [
1020
- function (variant, oldValue, newValue) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
1021
- return tslib_1.__generator(this, function (_a) {
1022
- switch (_a.label) {
1023
- case 0: return [4 /*yield*/, variant.commitParameterToElements(parameter_1.Parameter.CAST_SHADOW_FROM_LIGHTS, newValue)];
1024
- case 1:
1025
- _a.sent();
1026
- return [2 /*return*/];
1027
- }
1028
- });
1029
- }); }
1030
- ]);
1031
- this._parameterObservers.set(parameter_1.Parameter.RECEIVE_SHADOWS, [
1032
- function (variant, oldValue, newValue) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
1033
- return tslib_1.__generator(this, function (_a) {
1034
- switch (_a.label) {
1035
- case 0: return [4 /*yield*/, variant.commitParameterToElements(parameter_1.Parameter.RECEIVE_SHADOWS, newValue)];
1036
- case 1:
1037
- _a.sent();
1038
- return [2 /*return*/];
1039
- }
1040
- });
1041
- }); }
1042
- ]);
1043
- return this;
1044
- };
1045
- /**
1046
- * Creates {@link Element}s and clones nodes into them.
1047
- */
1048
- Variant.prototype.createElements = function () {
1049
- return tslib_1.__awaiter(this, void 0, void 0, function () {
1050
- var _a, _b, _i, name_2, _c, _d;
1051
- var _this = this;
1052
- return tslib_1.__generator(this, function (_e) {
1053
- switch (_e.label) {
1054
- case 0:
1055
- _a = [];
1056
- for (_b in this.structureJson.elements || {})
1057
- _a.push(_b);
1058
- _i = 0;
1059
- _e.label = 1;
1060
- case 1:
1061
- if (!(_i < _a.length)) return [3 /*break*/, 4];
1062
- name_2 = _a[_i];
1063
- _d = (_c = this.elements).push;
1064
- return [4 /*yield*/, element_1.Element.create(this, name_2)];
1065
- case 2:
1066
- _d.apply(_c, [_e.sent()]);
1067
- _e.label = 3;
1068
- case 3:
1069
- _i++;
1070
- return [3 /*break*/, 1];
1071
- case 4:
1072
- // inject node meta to all inherited elements
1073
- // we do this to inject the deepest and most concrete variant information to all cloned nodes in the tree
1074
- this.inheritedElements.forEach(function (element) {
1075
- element.nodes.forEach(function (node) {
1076
- babylonHelper_1.injectNodeMetadata(node, { variant: _this, variantParameterizable: element });
1077
- });
1078
- });
1079
- return [2 /*return*/, this];
1080
- }
1081
- });
1082
- });
1083
- };
1084
- /**
1085
- * Creates {@link ViewerLight}s.
1086
- */
1087
- Variant.prototype.createViewerLights = function () {
1088
- return tslib_1.__awaiter(this, void 0, void 0, function () {
1089
- var _a, _b, _i, name_3, _c, _d;
1090
- var _this = this;
1091
- return tslib_1.__generator(this, function (_e) {
1092
- switch (_e.label) {
1093
- case 0:
1094
- _a = [];
1095
- for (_b in this.structureJson.lights || {})
1096
- _a.push(_b);
1097
- _i = 0;
1098
- _e.label = 1;
1099
- case 1:
1100
- if (!(_i < _a.length)) return [3 /*break*/, 4];
1101
- name_3 = _a[_i];
1102
- _d = (_c = this.viewerLights).push;
1103
- return [4 /*yield*/, viewerLight_1.ViewerLight.create(this, name_3)];
1104
- case 2:
1105
- _d.apply(_c, [_e.sent()]);
1106
- _e.label = 3;
1107
- case 3:
1108
- _i++;
1109
- return [3 /*break*/, 1];
1110
- case 4:
1111
- this.inheritedViewerLights.forEach(function (viewerLight) {
1112
- babylonHelper_1.injectNodeMetadata(viewerLight.light, { variant: _this, variantParameterizable: viewerLight });
1113
- });
1114
- return [2 /*return*/, this];
1115
- }
1116
- });
1117
- });
1118
- };
1119
- /**
1120
- * Bootstrapping for parameters. It sets the `parametersInitialized` to true for all ancestors.
1121
- */
1122
- Variant.prototype.bootstrapParameters = function (parameters) {
1123
- return tslib_1.__awaiter(this, void 0, void 0, function () {
1124
- return tslib_1.__generator(this, function (_a) {
1125
- switch (_a.label) {
1126
- case 0: return [4 /*yield*/, this.commitParameters(lodash_es_1.merge(lodash_es_1.cloneDeep(this.parameters), parameters))];
1127
- case 1:
1128
- _a.sent();
1129
- lodash_es_1.concat(this.ancestors, this).forEach(function (ancestor) { return ancestor.parametersInitialized = true; });
1130
- return [2 /*return*/, this];
1131
- }
1132
- });
1133
- });
1134
- };
1135
- return Variant;
1136
- }(parameterizable_1.Parameterizable));
1137
- 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;
1138
874
  //# sourceMappingURL=variant.js.map