@combeenation/3d-viewer 5.1.2 → 5.2.0-alpha4

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