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