@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,824 +1,669 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Element = void 0;
4
- var tslib_1 = require("tslib");
5
- var pbrMaterial_1 = require("@babylonjs/core/Materials/PBR/pbrMaterial");
6
- var highlightLayer_1 = require("@babylonjs/core/Layers/highlightLayer");
7
- var material_1 = require("@babylonjs/core/Materials/material");
8
- var dynamicTexture_1 = require("@babylonjs/core/Materials/Textures/dynamicTexture");
9
- var math_color_1 = require("@babylonjs/core/Maths/math.color");
10
- var abstractMesh_1 = require("@babylonjs/core/Meshes/abstractMesh");
11
- var instancedMesh_1 = require("@babylonjs/core/Meshes/instancedMesh");
12
- var mesh_1 = require("@babylonjs/core/Meshes/mesh");
13
- var transformNode_1 = require("@babylonjs/core/Meshes/transformNode");
14
- var lodash_es_1 = require("lodash-es");
15
- var babylonHelper_1 = require("../util/babylonHelper");
16
- var resourceHelper_1 = require("../util/resourceHelper");
17
- var variantParameterizable_1 = require("./../classes/variantParameterizable");
18
- var dottedPath_1 = require("./dottedPath");
19
- var parameter_1 = require("./parameter");
20
- var sceneLoaderHelper_1 = require("../util/sceneLoaderHelper");
21
- /**
22
- * An {@link Element} of a {@link Variant}. Acts as a container for diverse definitions. Lives only in the context of a
23
- * {@link Variant}.
24
- *
25
- * When used in typings, refer to via its alias {@link VariantElement} to prevent name clashes with the web APIs
26
- * [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element) class
27
- */
28
- var Element = /** @class */ (function (_super) {
29
- tslib_1.__extends(Element, _super);
30
- /**
31
- * Constructor.
32
- */
33
- function Element(variant, name) {
34
- var _a;
35
- var _this = _super.call(this, variant, name) || this;
36
- _this.variant = variant;
37
- _this.name = name;
38
- _this.nodes = [];
39
- _this._dottedNodes = new Map();
40
- if ((_a = process.env.NODE_ENV) === null || _a === void 0 ? void 0 : _a.toLowerCase().includes('dev')) {
41
- _this.assertPathDefinitions();
42
- }
43
- var nodes = _this.variant.inheritedNodes.map(function (node) { return babylonHelper_1.cloneTransformNode(node, _this.filterNode.bind(_this)); });
44
- _this.nodes = nodes.filter(function (node) { return !!node; });
45
- _this._dottedNodes = babylonHelper_1.mapToDottedNodes(_this.nodes, function (node) { return node instanceof transformNode_1.TransformNode; });
46
- _this.addMissingMaterialObservers();
47
- _this.addParameterObservers();
48
- return _this;
49
- }
50
- /**
51
- * Creates an {@link Element} with given name.
52
- */
53
- Element.create = function (variant, name) {
54
- return tslib_1.__awaiter(this, void 0, void 0, function () {
55
- return tslib_1.__generator(this, function (_a) {
56
- return [2 /*return*/, new Element(variant, name)];
57
- });
58
- });
59
- };
60
- Object.defineProperty(Element.prototype, "dottedPath", {
61
- /**
62
- * The {@link DottedPath} in the built tree of {@link Element}s.
63
- * E.g. "_.top-1.sub-2.sub-sub-3.el-1"
64
- */
65
- get: function () {
66
- return dottedPath_1.DottedPath.create(this.variant.dottedPath).addPart(this.name);
67
- },
68
- enumerable: false,
69
- configurable: true
70
- });
71
- Object.defineProperty(Element.prototype, "id", {
72
- /**
73
- * The id representing a {@link DottedPath}.
74
- */
75
- get: function () {
76
- var dottedPath = dottedPath_1.DottedPath.create(this.dottedPath);
77
- dottedPath.shiftPart(); // remove root
78
- return dottedPath.path;
79
- },
80
- enumerable: false,
81
- configurable: true
82
- });
83
- Object.defineProperty(Element.prototype, "definition", {
84
- /**
85
- * The {@link ElementDefinition} of the {@link Element}.
86
- */
87
- get: function () {
88
- var definition = this.variant.structureJson.elements[this.name];
89
- if (lodash_es_1.isArray(definition)) {
90
- return {
91
- paths: {
92
- include: definition,
93
- },
94
- };
95
- }
96
- return definition;
97
- },
98
- enumerable: false,
99
- configurable: true
100
- });
101
- Object.defineProperty(Element.prototype, "pathDefinitions", {
102
- /**
103
- * The {@link PathDefinitions} of the {@link Element}.
104
- */
105
- get: function () {
106
- return this.definition.paths;
107
- },
108
- enumerable: false,
109
- configurable: true
110
- });
111
- Object.defineProperty(Element.prototype, "traceableDefinitions", {
112
- /**
113
- * The {@link TraceableDefinitions} of the {@link Element}.
114
- */
115
- get: function () {
116
- return this.definition.traceables || {};
117
- },
118
- enumerable: false,
119
- configurable: true
120
- });
121
- Object.defineProperty(Element.prototype, "paintableDefinitions", {
122
- /**
123
- * The {@link PaintableDefinition} of the {@link Element}.
124
- */
125
- get: function () {
126
- return this.definition.paintables || {};
127
- },
128
- enumerable: false,
129
- configurable: true
130
- });
131
- Object.defineProperty(Element.prototype, "inheritedParameters", {
132
- /**
133
- * The inherited {@link ParameterBag}.
134
- * Merges the {@link Variant}'s parameters and those from the {@link Element}.
135
- */
136
- get: function () {
137
- return lodash_es_1.merge({}, this.variant.inheritedParameters, this.parameters);
138
- },
139
- enumerable: false,
140
- configurable: true
141
- });
142
- Object.defineProperty(Element.prototype, "meshesFlat", {
143
- /**
144
- * The actual {@link Mesh}es that make up this {@link Element}.
145
- * Handy for e.g. creating a bounding box around an entire element.
146
- */
147
- get: function () {
148
- var flatMeshes = this.nodes.reduce(function (accFlatMeshes, curNode) {
149
- var currMeshes = curNode.getChildMeshes(false, function (n) { return n instanceof mesh_1.Mesh; });
150
- return tslib_1.__spreadArrays(accFlatMeshes, currMeshes);
151
- }, []);
152
- return flatMeshes;
153
- },
154
- enumerable: false,
155
- configurable: true
156
- });
157
- Object.defineProperty(Element.prototype, "abstractMeshesFlat", {
158
- /**
159
- * The actual {@link AbstractMesh}es that make up this {@link Element}.
160
- * I.e. (real) Meshes as well as InstancedMeshes.
161
- */
162
- get: function () {
163
- var allAbstractMeshes = this.nodes.reduce(function (acc, currNode) {
164
- var currAbstractMeshes = currNode.getChildMeshes(false, function (n) { return n instanceof abstractMesh_1.AbstractMesh; });
165
- if (currNode instanceof abstractMesh_1.AbstractMesh) {
166
- currAbstractMeshes.unshift(currNode);
167
- }
168
- return tslib_1.__spreadArrays(acc, currAbstractMeshes);
169
- }, []); // initial accumulator value
170
- return allAbstractMeshes;
171
- },
172
- enumerable: false,
173
- configurable: true
174
- });
175
- Object.defineProperty(Element.prototype, "meshes", {
176
- /**
177
- * The meshes of this {@link Element}.
178
- */
179
- get: function () {
180
- return this.nodes.filter(function (node) { return node instanceof mesh_1.Mesh; });
181
- },
182
- enumerable: false,
183
- configurable: true
184
- });
185
- Object.defineProperty(Element.prototype, "highlightLayer", {
186
- get: function () {
187
- return this._highlightLayer;
188
- },
189
- /**
190
- * @protected
191
- */
192
- set: function (layer) {
193
- this._highlightLayer = layer;
194
- },
195
- enumerable: false,
196
- configurable: true
197
- });
198
- /**
199
- * Destroys this {@link Element} and dispose all nodes.
200
- */
201
- Element.prototype.destroy = function () {
202
- this.nodes.forEach(function (node) {
203
- node.dispose();
204
- });
205
- return this;
206
- };
207
- /**
208
- * Gets a node by its {@link DottedPath}.
209
- */
210
- Element.prototype.getNode = function (dottedPath) {
211
- var _dottedPath = dottedPath_1.DottedPath.create(dottedPath);
212
- var keys = Array.from(this._dottedNodes.keys()).map(function (dp) { return dp.path; });
213
- var values = Array.from(this._dottedNodes.values());
214
- var node = values[keys.indexOf(_dottedPath.path)];
215
- if (!node) {
216
- throw new Error("Node with path \"" + _dottedPath.path + "\" does not exist for element \"" + this.id + "\".");
217
- }
218
- return node;
219
- };
220
- /**
221
- * Gets a mesh by its {@link DottedPath}.
222
- */
223
- Element.prototype.getMesh = function (dottedPath) {
224
- return tslib_1.__awaiter(this, void 0, void 0, function () {
225
- var node;
226
- return tslib_1.__generator(this, function (_a) {
227
- switch (_a.label) {
228
- case 0: return [4 /*yield*/, this.getNode(dottedPath)];
229
- case 1:
230
- node = _a.sent();
231
- if (node instanceof mesh_1.Mesh) {
232
- return [2 /*return*/, node];
233
- }
234
- return [2 /*return*/, null];
235
- }
236
- });
237
- });
238
- };
239
- /**
240
- * Gets a {@link PaintableDefinition} by its name.
241
- */
242
- Element.prototype.getPaintableDefinition = function (name) {
243
- if (!this.paintableDefinitions || !this.paintableDefinitions[name]) {
244
- throw new Error("No paintable \"" + name + "\" defined for element \"" + this.id + "\".");
245
- }
246
- return this.paintableDefinitions[name];
247
- };
248
- /**
249
- * Gets a {@link TraceableDefinition} by its name.
250
- */
251
- Element.prototype.getTraceableDefinition = function (name) {
252
- if (!this.traceableDefinitions || !this.traceableDefinitions[name]) {
253
- throw new Error("No traceable \"" + name + "\" defined to add observer in element \"" + this.id + "\".");
254
- }
255
- return this.traceableDefinitions[name];
256
- };
257
- /**
258
- * Gets the paintable Node by its name.
259
- */
260
- Element.prototype.getPaintableNode = function (name) {
261
- return this.getNode(this.getPaintableDefinition(name).path);
262
- };
263
- /**
264
- * Gets the traceable Node by its name.
265
- */
266
- Element.prototype.getTraceableNode = function (name) {
267
- return this.getNode(this.getTraceableDefinition(name).path);
268
- };
269
- /**
270
- * @see {@link VariantParameterizable.commitParameters}
271
- * @emit {@link Event.ELEMENT_PARAMETER_COMMITTED}
272
- */
273
- Element.prototype.commitParameters = function (parameters) {
274
- return tslib_1.__awaiter(this, void 0, void 0, function () {
275
- return tslib_1.__generator(this, function (_a) {
276
- return [2 /*return*/, _super.prototype.commitParameters.call(this, parameters)];
277
- });
278
- });
279
- };
280
- /**
281
- * Adds an observer function for camera matrix changes.
282
- * The `observer` gets 2 parameters: the `AbstractMesh` and a `ClientRect` object.
283
- */
284
- Element.prototype.addTraceableObserver = function (traceable, observer, payload) {
285
- var _this = this;
286
- if (payload && !(payload instanceof HTMLElement)) {
287
- throw new Error("Payload for camera observer must be an instance of \"HTMLElement\" in element \"" + this.id + "\".");
288
- }
289
- if (!this.traceableDefinitions || !this.traceableDefinitions[traceable]) {
290
- throw new Error("No traceable \"" + traceable + "\" defined to add observer in element \"" + this.id + "\".");
291
- }
292
- var node = this.getTraceableNode(traceable);
293
- if (!(node instanceof abstractMesh_1.AbstractMesh)) {
294
- throw new Error("The path must be an instance of \"AbstractMesh\" for camera observer in element \"" + this.id + "\".");
295
- }
296
- this.variant.viewer.sceneManager.activeCamera.onViewMatrixChangedObservable.add(function (eventData, eventState) {
297
- var position = babylonHelper_1.getClientRectFromMesh(node, _this.variant.viewer.scene, _this.variant.viewer.canvas);
298
- if (payload) {
299
- payload.style.top = position.top + "px";
300
- payload.style.left = position.left + "px";
301
- }
302
- observer(node, position);
303
- });
304
- return this;
305
- };
306
- /**
307
- * Draws a `ImageBitmap` or `OffscreenCanvas` onto a `paintable` defined via {@link PaintableDefinition}.
308
- */
309
- Element.prototype.drawPaintable = function (paintable, imageSource) {
310
- // node and material checks and preperation
311
- var node = this.getPaintableNode(paintable);
312
- if (!(node instanceof abstractMesh_1.AbstractMesh)) {
313
- throw new Error("The path must be an instance of \"AbstractMesh\" for paintable \"" + paintable + "\" " + ("in element \"" + this.id + "\"."));
314
- }
315
- if (node.material && this.variant.viewer.cloneMaterialsOnMutation) {
316
- if (node.material && !lodash_es_1.has(node.metadata, 'dirty.material')) {
317
- babylonHelper_1.cloneTransformNodeMaterial(node);
318
- }
319
- if (!lodash_es_1.has(node.metadata, 'dirty.material.texture')) {
320
- // inject initial value and mark as dirty
321
- babylonHelper_1.injectNodeMetadata(node, { dirty: { material: { texture: true } } });
322
- }
323
- }
324
- else if (!node.material) {
325
- // paintables should also work if no material is assigned to the node per default
326
- // in this case a certain fallback material with fitting default values is defined
327
- var pbrMat = new pbrMaterial_1.PBRMaterial(this.id + "." + paintable + ".material", this.variant.viewer.scene);
328
- pbrMat.roughness = 1.0;
329
- pbrMat.metallic = 1.0;
330
- node.material = pbrMat;
331
- }
332
- // TODO: Not sure if this really should be overwritten every time the paintable is redrawn.
333
- // Maybe the 3D artist had something in mind when setting a certain transparency mode, which would get overwritten in this case.
334
- // Applying it as a default if no material is set is fine though (see roughness/metallic some lines above).
335
- // Removing this line will most likely break existing configurators, so be cautios!
336
- node.material.transparencyMode = material_1.Material.MATERIAL_ALPHATESTANDBLEND;
337
- // consider width and height of the paintable
338
- var widthAndHeight = {
339
- width: imageSource.width,
340
- height: imageSource.height,
341
- };
342
- var paintableDefinition = this.getPaintableDefinition(paintable);
343
- if (lodash_es_1.isNumber(paintableDefinition.textureOptions)) {
344
- widthAndHeight.width = paintableDefinition.textureOptions;
345
- widthAndHeight.height = paintableDefinition.textureOptions;
346
- }
347
- else if (lodash_es_1.isPlainObject(paintableDefinition.textureOptions)) {
348
- widthAndHeight.width = paintableDefinition.textureOptions.width;
349
- widthAndHeight.height = paintableDefinition.textureOptions.height;
350
- }
351
- // create dynamic texture on which the image can be drawn
352
- var texture = new dynamicTexture_1.DynamicTexture(this.id + "." + paintable + ".texture", widthAndHeight, this.variant.viewer.scene, false);
353
- // draw image on texture
354
- var ctx = texture.getContext();
355
- ctx.drawImage(imageSource, 0, 0);
356
- texture.update(false);
357
- // finally apply the texture on the desired node material
358
- babylonHelper_1.setMaterialTexture(node, texture, false);
359
- return this;
360
- };
361
- /**
362
- * Draws a SVG string onto a `paintable` defined via {@link PaintableDefinition}.
363
- */
364
- Element.prototype.drawPaintableFromSvg = function (paintable, svgSource) {
365
- return tslib_1.__awaiter(this, void 0, void 0, function () {
366
- var image;
367
- return tslib_1.__generator(this, function (_a) {
368
- switch (_a.label) {
369
- case 0: return [4 /*yield*/, resourceHelper_1.createImageFromSvg(svgSource)];
370
- case 1:
371
- image = _a.sent();
372
- return [2 /*return*/, this.drawPaintable(paintable, image)];
373
- }
374
- });
375
- });
376
- };
377
- /**
378
- * Draws an Image from source (URL/URI) onto a `paintable` defined via {@link PaintableDefinition}.
379
- */
380
- Element.prototype.drawPaintableFromImgSrc = function (paintable, imgSource) {
381
- return tslib_1.__awaiter(this, void 0, void 0, function () {
382
- var image;
383
- return tslib_1.__generator(this, function (_a) {
384
- switch (_a.label) {
385
- case 0: return [4 /*yield*/, resourceHelper_1.createImageFromImgSrc(imgSource)];
386
- case 1:
387
- image = _a.sent();
388
- return [2 /*return*/, this.drawPaintable(paintable, image)];
389
- }
390
- });
391
- });
392
- };
393
- /**
394
- * Adds observers for loading missing materials where needed
395
- */
396
- Element.prototype.addMissingMaterialObservers = function () {
397
- for (var _i = 0, _a = this.abstractMeshesFlat; _i < _a.length; _i++) {
398
- var abstractMesh = _a[_i];
399
- sceneLoaderHelper_1.addMissingMaterialObserver(abstractMesh);
400
- }
401
- };
402
- /**
403
- * Adds the default {@link ParameterObserver}s which are called every time {@link commitParameters} is called.
404
- */
405
- Element.prototype.addParameterObservers = function () {
406
- var _this = this;
407
- this._parameterObservers.set(parameter_1.Parameter.VISIBLE, [
408
- function (element, oldValue, newValue) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
409
- var visible;
410
- return tslib_1.__generator(this, function (_a) {
411
- try {
412
- visible = parameter_1.Parameter.parseBoolean(newValue);
413
- }
414
- catch (e) {
415
- return [2 /*return*/];
416
- }
417
- if (visible === true) {
418
- element.nodes.forEach(function (node) {
419
- babylonHelper_1.injectNodeMetadata(node, { visibility: node.isEnabled() });
420
- babylonHelper_1.activateTransformNode(node);
421
- });
422
- }
423
- else if (visible === false) {
424
- element.nodes.forEach(function (node) {
425
- babylonHelper_1.injectNodeMetadata(node, { visibility: node.isEnabled() });
426
- babylonHelper_1.deactivateTransformNode(node);
427
- });
428
- }
429
- return [2 /*return*/];
430
- });
431
- }); },
432
- ]);
433
- this._parameterObservers.set(parameter_1.Parameter.SCALING, [
434
- function (element, oldValue, newValue) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
435
- return tslib_1.__generator(this, function (_a) {
436
- // we have to deal just with root nodes here due to relative impacts in a node tree
437
- element.nodes.forEach(function (node) {
438
- return babylonHelper_1.transformTransformNode(node, {
439
- scaling: parameter_1.Parameter.parseVector(newValue),
440
- position: parameter_1.Parameter.parseVector(element.inheritedParameters[parameter_1.Parameter.POSITION] || '(0, 0, 0)'),
441
- rotation: parameter_1.Parameter.parseRotation(element.inheritedParameters[parameter_1.Parameter.ROTATION] || '(0, 0, 0)'),
442
- });
443
- });
444
- return [2 /*return*/];
445
- });
446
- }); },
447
- ]);
448
- this._parameterObservers.set(parameter_1.Parameter.POSITION, [
449
- function (element, oldValue, newValue) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
450
- return tslib_1.__generator(this, function (_a) {
451
- // we have to deal just with root nodes here due to relative impacts in a node tree
452
- element.nodes.forEach(function (node) {
453
- return babylonHelper_1.transformTransformNode(node, {
454
- scaling: parameter_1.Parameter.parseVector(element.inheritedParameters[parameter_1.Parameter.SCALING] || '(1, 1, 1)'),
455
- position: parameter_1.Parameter.parseVector(newValue),
456
- rotation: parameter_1.Parameter.parseRotation(element.inheritedParameters[parameter_1.Parameter.ROTATION] || '(0, 0, 0)'),
457
- });
458
- });
459
- return [2 /*return*/];
460
- });
461
- }); },
462
- ]);
463
- this._parameterObservers.set(parameter_1.Parameter.ROTATION, [
464
- function (element, oldValue, newValue) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
465
- return tslib_1.__generator(this, function (_a) {
466
- // we have to deal just with root nodes here due to relative impacts in a node tree
467
- element.nodes.forEach(function (node) {
468
- return babylonHelper_1.transformTransformNode(node, {
469
- scaling: parameter_1.Parameter.parseVector(element.inheritedParameters[parameter_1.Parameter.SCALING] || '(1, 1, 1)'),
470
- position: parameter_1.Parameter.parseVector(element.inheritedParameters[parameter_1.Parameter.POSITION] || '(0, 0, 0)'),
471
- rotation: parameter_1.Parameter.parseRotation(newValue),
472
- });
473
- });
474
- return [2 /*return*/];
475
- });
476
- }); },
477
- ]);
478
- this._parameterObservers.set(parameter_1.Parameter.MATERIAL, [
479
- function (element, oldValue, newValue) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
480
- var materialName;
481
- return tslib_1.__generator(this, function (_a) {
482
- materialName = newValue.toString();
483
- element.nodes.forEach(function (node) {
484
- babylonHelper_1.assertTransformNode(node, function (node) {
485
- if (node instanceof instancedMesh_1.InstancedMesh) {
486
- throw new Error("Changing parameter \"" + parameter_1.Parameter.MATERIAL + "\" " +
487
- "of an InstancedMesh is not supported. " +
488
- ("Tried to change node \"" + node.id + "\" on element \"" + element.id + "\"."));
489
- }
490
- });
491
- babylonHelper_1.setMaterial(element.variant, node, materialName);
492
- });
493
- return [2 /*return*/];
494
- });
495
- }); },
496
- ]);
497
- this._parameterObservers.set(parameter_1.Parameter.MATERIAL_COLOR, [
498
- function (element, oldValue, newValue) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
499
- var color;
500
- var _this = this;
501
- return tslib_1.__generator(this, function (_a) {
502
- color = parameter_1.Parameter.parseColor(newValue);
503
- element.nodes.forEach(function (node) {
504
- babylonHelper_1.assertTransformNode(node, function (node) {
505
- if (node instanceof instancedMesh_1.InstancedMesh) {
506
- throw new Error("Changing parameter \"" + parameter_1.Parameter.MATERIAL_COLOR + "\" " +
507
- "of an InstancedMesh is not supported. " +
508
- ("Tried to change node \"" + node.id + "\" on element \"" + element.id + "\"."));
509
- }
510
- });
511
- if (_this.variant.viewer.cloneMaterialsOnMutation) {
512
- if (!lodash_es_1.has(node.metadata, 'dirty.material')) {
513
- babylonHelper_1.cloneTransformNodeMaterial(node);
514
- }
515
- if (!lodash_es_1.has(node.metadata, 'dirty.material.color')) {
516
- // inject initial value and mark as dirty
517
- babylonHelper_1.injectNodeMetadata(node, { dirty: { material: { color: oldValue } } });
518
- }
519
- }
520
- babylonHelper_1.setMaterialColor(node, color);
521
- });
522
- return [2 /*return*/];
523
- });
524
- }); },
525
- ]);
526
- this._parameterObservers.set(parameter_1.Parameter.MATERIAL_METALLNESS, [
527
- function (element, oldValue, newValue) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
528
- var metallness;
529
- var _this = this;
530
- return tslib_1.__generator(this, function (_a) {
531
- metallness = parameter_1.Parameter.parseNumber(newValue);
532
- element.nodes.forEach(function (node) {
533
- babylonHelper_1.assertTransformNode(node, function (node) {
534
- if (node instanceof instancedMesh_1.InstancedMesh) {
535
- throw new Error("Changing parameter \"" + parameter_1.Parameter.MATERIAL_METALLNESS + "\" " +
536
- "of an InstancedMesh is not supported. " +
537
- ("Tried to change node \"" + node.id + "\" on element \"" + element.id + "\"."));
538
- }
539
- });
540
- if (_this.variant.viewer.cloneMaterialsOnMutation) {
541
- if (!lodash_es_1.has(node.metadata, 'dirty.material')) {
542
- babylonHelper_1.cloneTransformNodeMaterial(node);
543
- }
544
- if (!lodash_es_1.has(node.metadata, 'dirty.material.metallness')) {
545
- // inject initial value and mark as dirty
546
- babylonHelper_1.injectNodeMetadata(node, { dirty: { material: { metallness: oldValue } } });
547
- }
548
- }
549
- babylonHelper_1.setMaterialMetallness(node, metallness);
550
- });
551
- return [2 /*return*/];
552
- });
553
- }); },
554
- ]);
555
- this._parameterObservers.set(parameter_1.Parameter.MATERIAL_ROUGHNESS, [
556
- function (element, oldValue, newValue) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
557
- var roughness;
558
- var _this = this;
559
- return tslib_1.__generator(this, function (_a) {
560
- roughness = parameter_1.Parameter.parseNumber(newValue);
561
- element.nodes.forEach(function (node) {
562
- babylonHelper_1.assertTransformNode(node, function (node) {
563
- if (node instanceof instancedMesh_1.InstancedMesh) {
564
- throw new Error("Changing parameter \"" + parameter_1.Parameter.MATERIAL_ROUGHNESS + "\" " +
565
- "of an InstancedMesh is not supported. " +
566
- ("Tried to change node \"" + node.id + "\" on element \"" + element.id + "\"."));
567
- }
568
- });
569
- if (_this.variant.viewer.cloneMaterialsOnMutation) {
570
- if (!lodash_es_1.has(node.metadata, 'dirty.material')) {
571
- babylonHelper_1.cloneTransformNodeMaterial(node);
572
- }
573
- if (!lodash_es_1.has(node.metadata, 'dirty.material.roughness')) {
574
- // inject initial value and mark as dirty
575
- babylonHelper_1.injectNodeMetadata(node, { dirty: { material: { roughness: oldValue } } });
576
- }
577
- }
578
- babylonHelper_1.setMaterialRoughness(node, roughness);
579
- });
580
- return [2 /*return*/];
581
- });
582
- }); },
583
- ]);
584
- this._parameterObservers.set(parameter_1.Parameter.HIGHLIGHT_COLOR, [
585
- function (element, oldValue, newValue) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
586
- var _a;
587
- return tslib_1.__generator(this, function (_b) {
588
- // trigger Parameter.HIGHLIGHTED observers
589
- (_a = this._parameterObservers.get(parameter_1.Parameter.HIGHLIGHTED)) === null || _a === void 0 ? void 0 : _a.forEach(function (observer) {
590
- observer(element, !!element.highlighted, !!element.highlighted);
591
- });
592
- return [2 /*return*/];
593
- });
594
- }); },
595
- ]);
596
- this._parameterObservers.set(parameter_1.Parameter.HIGHLIGHT_ENABLED, [
597
- function (element, oldValue, newValue) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
598
- var curr;
599
- var _a;
600
- return tslib_1.__generator(this, function (_b) {
601
- try {
602
- curr = parameter_1.Parameter.parseBoolean(newValue);
603
- }
604
- catch (e) {
605
- return [2 /*return*/];
606
- }
607
- switch (curr) {
608
- case true:
609
- if (element._highlightLayer) {
610
- break;
611
- }
612
- element.highlightLayer = new highlightLayer_1.HighlightLayer(dottedPath_1.DottedPath.create(element.id).addPart('highlight').path, element.variant.viewer.scene);
613
- break;
614
- case false:
615
- (_a = element.highlightLayer) === null || _a === void 0 ? void 0 : _a.dispose();
616
- break;
617
- }
618
- return [2 /*return*/];
619
- });
620
- }); },
621
- ]);
622
- this._parameterObservers.set(parameter_1.Parameter.HIGHLIGHTED, [
623
- function (element, oldValue, newValue) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
624
- var highlighted;
625
- var _this = this;
626
- var _a;
627
- return tslib_1.__generator(this, function (_b) {
628
- if (!element.inheritedParameters[parameter_1.Parameter.HIGHLIGHT_ENABLED]) {
629
- return [2 /*return*/];
630
- }
631
- // trigger Parameter.HIGHLIGHT_ENABLED observers to ensure HighlightLayer
632
- (_a = this._parameterObservers.get(parameter_1.Parameter.HIGHLIGHT_ENABLED)) === null || _a === void 0 ? void 0 : _a.forEach(function (observer) {
633
- observer(element, _this.parameters[parameter_1.Parameter.HIGHLIGHT_ENABLED], true);
634
- });
635
- try {
636
- highlighted = parameter_1.Parameter.parseBoolean(newValue);
637
- }
638
- catch (e) {
639
- return [2 /*return*/];
640
- }
641
- // Add/Remove meshes to previously created highlight layers.
642
- if (highlighted === true) {
643
- element.nodes.forEach(function (node) {
644
- babylonHelper_1.assertTransformNode(node, function (node) {
645
- if (node instanceof instancedMesh_1.InstancedMesh) {
646
- throw new Error("Changing parameter \"" + parameter_1.Parameter.HIGHLIGHTED + "\" " +
647
- "of an InstancedMesh is not supported. " +
648
- ("Tried to change node \"" + node.id + "\" on element \"" + element.id + "\"."));
649
- }
650
- });
651
- var color = math_color_1.Color3.Green();
652
- if (element.inheritedParameters[parameter_1.Parameter.HIGHLIGHT_COLOR]) {
653
- color = parameter_1.Parameter.parseColor(element.inheritedParameters[parameter_1.Parameter.HIGHLIGHT_COLOR]);
654
- }
655
- if (typeof element._highlightLayer !== 'undefined') {
656
- babylonHelper_1.addToHighlightLayer(element._highlightLayer, color, node);
657
- }
658
- });
659
- }
660
- else if (highlighted === false) {
661
- element.nodes.forEach(function (node) {
662
- if (typeof element._highlightLayer !== 'undefined') {
663
- babylonHelper_1.removeFromHighlightLayer(element._highlightLayer, node);
664
- }
665
- });
666
- }
667
- return [2 /*return*/];
668
- });
669
- }); },
670
- ]);
671
- this._parameterObservers.set(parameter_1.Parameter.CAST_SHADOW, [
672
- function (element, oldValue, newValue) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
673
- var castShadow, lightCsl;
674
- return tslib_1.__generator(this, function (_a) {
675
- switch (_a.label) {
676
- case 0:
677
- try {
678
- castShadow = parameter_1.Parameter.parseBoolean(newValue);
679
- }
680
- catch (e) {
681
- return [2 /*return*/];
682
- }
683
- lightCsl = element.inheritedParameters[parameter_1.Parameter.CAST_SHADOW_FROM_LIGHTS];
684
- if (!lightCsl) {
685
- lightCsl = element.variant.inheritedViewerLights.map(function (l) { return l.name; }).join(',');
686
- }
687
- if (!(castShadow === true)) return [3 /*break*/, 2];
688
- return [4 /*yield*/, this.castShadowValueHandler(lightCsl, babylonHelper_1.addToShadowGenerator)];
689
- case 1:
690
- _a.sent();
691
- _a.label = 2;
692
- case 2:
693
- if (!(castShadow === false)) return [3 /*break*/, 4];
694
- return [4 /*yield*/, this.castShadowValueHandler(lightCsl, babylonHelper_1.removeFromShadowGenerator)];
695
- case 3:
696
- _a.sent();
697
- _a.label = 4;
698
- case 4: return [2 /*return*/];
699
- }
700
- });
701
- }); },
702
- ]);
703
- this._parameterObservers.set(parameter_1.Parameter.CAST_SHADOW_FROM_LIGHTS, [
704
- function (element, oldValue, newValue) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
705
- var lightCsl;
706
- return tslib_1.__generator(this, function (_a) {
707
- switch (_a.label) {
708
- case 0:
709
- lightCsl = element.variant.inheritedViewerLights.map(function (l) { return l.name; }).join(',');
710
- // cleanup all shadow generators
711
- return [4 /*yield*/, this.castShadowValueHandler(lightCsl, babylonHelper_1.removeFromShadowGenerator)];
712
- case 1:
713
- // cleanup all shadow generators
714
- _a.sent();
715
- if (!(element.castShadow === true)) return [3 /*break*/, 3];
716
- // if newValue is undefined or '' then set newValue to lightCsl (use all lights)
717
- if (!newValue) {
718
- newValue = lightCsl;
719
- }
720
- return [4 /*yield*/, this.castShadowValueHandler(newValue, babylonHelper_1.addToShadowGenerator)];
721
- case 2:
722
- _a.sent();
723
- _a.label = 3;
724
- case 3: return [2 /*return*/];
725
- }
726
- });
727
- }); },
728
- ]);
729
- this._parameterObservers.set(parameter_1.Parameter.RECEIVE_SHADOWS, [
730
- function (element, oldValue, newValue) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
731
- return tslib_1.__generator(this, function (_a) {
732
- element.nodes.forEach(function (node) { return babylonHelper_1.setReceiveShadows(node, parameter_1.Parameter.parseBoolean(newValue)); });
733
- return [2 /*return*/];
734
- });
735
- }); },
736
- ]);
737
- return this;
738
- };
739
- /**
740
- * Filter for nodes that are not defined or excluded in the `pathDefinitions`.
741
- */
742
- Element.prototype.filterNode = function (node) {
743
- var _this = this;
744
- var includeTokens = [];
745
- this.pathDefinitions.include.forEach(function (dottedPath) {
746
- includeTokens = lodash_es_1.union(includeTokens, dottedPath_1.DottedPath.create(dottedPath).leafTokens);
747
- });
748
- var shallKeep = function (dp) {
749
- var isDirectlyIncluded = !!includeTokens.find(function (_dp) { return _dp === dp.path; });
750
- var isSubOfIncluded = !!_this.pathDefinitions.include.find(function (_dp) {
751
- return dp.path.startsWith("" + _dp + dottedPath_1.DottedPath.DELIMITER);
752
- });
753
- return isDirectlyIncluded || isSubOfIncluded;
754
- };
755
- var shallExclude = function (dp) {
756
- if (!_this.pathDefinitions.exclude) {
757
- return false;
758
- }
759
- return !!_this.pathDefinitions.exclude.find(function (_dp) { return dp.path === _dp; });
760
- };
761
- return shallKeep(node.metadata.dottedPath) && !shallExclude(node.metadata.dottedPath);
762
- };
763
- /**
764
- * Asserts whether all {@link pathDefinitions} link to a valid `Node` path.
765
- * @protected
766
- */
767
- Element.prototype.assertPathDefinitions = function () {
768
- var _this = this;
769
- var _a;
770
- var dottedNodes = this.variant.inheritedDottedNodes;
771
- if (dottedNodes.size === 0) {
772
- throw new Error("There are no mapped nodes for element \"" + this.id + "\".");
773
- }
774
- var include = this.pathDefinitions.include.map(function (path) { return dottedPath_1.DottedPath.create(path).path; });
775
- var exclude = (_a = this.pathDefinitions.exclude) === null || _a === void 0 ? void 0 : _a.map(function (path) { return dottedPath_1.DottedPath.create(path).path; });
776
- var keys = Array.from(dottedNodes.keys()).map(function (dottedPath) { return dottedPath.path; });
777
- lodash_es_1.union(include, exclude).forEach(function (path) {
778
- if (keys.indexOf(path) === -1) {
779
- throw new Error("Node with path \"" + path + "\" does not exist for element \"" + _this.id + "\".");
780
- }
781
- });
782
- };
783
- /**
784
- * Handles callback for given light parameter.
785
- */
786
- Element.prototype.castShadowValueHandler = function (lightCsl, mutator) {
787
- return tslib_1.__awaiter(this, void 0, void 0, function () {
788
- var lights, _i, _a, lightName, viewerLight, shadowGenerators;
789
- var _this = this;
790
- return tslib_1.__generator(this, function (_b) {
791
- switch (_b.label) {
792
- case 0:
793
- lights = [];
794
- _i = 0, _a = parameter_1.Parameter.parseCommaSeparatedList(lightCsl);
795
- _b.label = 1;
796
- case 1:
797
- if (!(_i < _a.length)) return [3 /*break*/, 4];
798
- lightName = _a[_i];
799
- return [4 /*yield*/, this.variant.getViewerLight(lightName)];
800
- case 2:
801
- viewerLight = _b.sent();
802
- if (viewerLight) {
803
- lights.push(viewerLight.light);
804
- }
805
- _b.label = 3;
806
- case 3:
807
- _i++;
808
- return [3 /*break*/, 1];
809
- case 4:
810
- shadowGenerators = lights.map(function (light) { return light === null || light === void 0 ? void 0 : light.getShadowGenerator(); }).filter(Boolean);
811
- shadowGenerators.forEach(function (generator) {
812
- _this.nodes.forEach(function (node) {
813
- mutator(generator, node);
814
- });
815
- });
816
- return [2 /*return*/];
817
- }
818
- });
819
- });
820
- };
821
- return Element;
822
- }(variantParameterizable_1.VariantParameterizable));
823
- exports.Element = Element;
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.Element = void 0;
13
+ const babylonHelper_1 = require("../util/babylonHelper");
14
+ const resourceHelper_1 = require("../util/resourceHelper");
15
+ const sceneLoaderHelper_1 = require("../util/sceneLoaderHelper");
16
+ const variantParameterizable_1 = require("./../classes/variantParameterizable");
17
+ const dottedPath_1 = require("./dottedPath");
18
+ const parameter_1 = require("./parameter");
19
+ const highlightLayer_1 = require("@babylonjs/core/Layers/highlightLayer");
20
+ const pbrMaterial_1 = require("@babylonjs/core/Materials/PBR/pbrMaterial");
21
+ const dynamicTexture_1 = require("@babylonjs/core/Materials/Textures/dynamicTexture");
22
+ const material_1 = require("@babylonjs/core/Materials/material");
23
+ const math_color_1 = require("@babylonjs/core/Maths/math.color");
24
+ const abstractMesh_1 = require("@babylonjs/core/Meshes/abstractMesh");
25
+ const instancedMesh_1 = require("@babylonjs/core/Meshes/instancedMesh");
26
+ const mesh_1 = require("@babylonjs/core/Meshes/mesh");
27
+ const transformNode_1 = require("@babylonjs/core/Meshes/transformNode");
28
+ const lodash_es_1 = require("lodash-es");
29
+ /**
30
+ * An {@link Element} of a {@link Variant}. Acts as a container for diverse definitions. Lives only in the context of a
31
+ * {@link Variant}.
32
+ *
33
+ * When used in typings, refer to via its alias {@link VariantElement} to prevent name clashes with the web APIs
34
+ * [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element) class
35
+ */
36
+ class Element extends variantParameterizable_1.VariantParameterizable {
37
+ /**
38
+ * Constructor.
39
+ */
40
+ constructor(variant, name) {
41
+ var _a;
42
+ super(variant, name);
43
+ this.variant = variant;
44
+ this.name = name;
45
+ this.nodes = [];
46
+ this._dottedNodes = new Map();
47
+ if ((_a = process.env.NODE_ENV) === null || _a === void 0 ? void 0 : _a.toLowerCase().includes('dev')) {
48
+ this.assertPathDefinitions();
49
+ }
50
+ const nodes = this.variant.inheritedNodes.map(node => (0, babylonHelper_1.cloneTransformNode)(node, this.filterNode.bind(this)));
51
+ this.nodes = nodes.filter(node => !!node);
52
+ this._dottedNodes = (0, babylonHelper_1.mapToDottedNodes)(this.nodes, node => node instanceof transformNode_1.TransformNode);
53
+ this.addMissingMaterialObservers();
54
+ this.addParameterObservers();
55
+ }
56
+ /**
57
+ * Creates an {@link Element} with given name.
58
+ */
59
+ static create(variant, name) {
60
+ return __awaiter(this, void 0, void 0, function* () {
61
+ return new Element(variant, name);
62
+ });
63
+ }
64
+ /**
65
+ * The {@link DottedPath} in the built tree of {@link Element}s.
66
+ * E.g. "_.top-1.sub-2.sub-sub-3.el-1"
67
+ */
68
+ get dottedPath() {
69
+ return dottedPath_1.DottedPath.create(this.variant.dottedPath).addPart(this.name);
70
+ }
71
+ /**
72
+ * The id representing a {@link DottedPath}.
73
+ */
74
+ get id() {
75
+ const dottedPath = dottedPath_1.DottedPath.create(this.dottedPath);
76
+ dottedPath.shiftPart(); // remove root
77
+ return dottedPath.path;
78
+ }
79
+ /**
80
+ * The {@link ElementDefinition} of the {@link Element}.
81
+ */
82
+ get definition() {
83
+ const definition = this.variant.structureJson.elements[this.name];
84
+ if ((0, lodash_es_1.isArray)(definition)) {
85
+ return {
86
+ paths: {
87
+ include: definition,
88
+ },
89
+ };
90
+ }
91
+ return definition;
92
+ }
93
+ /**
94
+ * The {@link PathDefinitions} of the {@link Element}.
95
+ */
96
+ get pathDefinitions() {
97
+ return this.definition.paths;
98
+ }
99
+ /**
100
+ * The {@link TraceableDefinitions} of the {@link Element}.
101
+ */
102
+ get traceableDefinitions() {
103
+ return this.definition.traceables || {};
104
+ }
105
+ /**
106
+ * The {@link PaintableDefinition} of the {@link Element}.
107
+ */
108
+ get paintableDefinitions() {
109
+ return this.definition.paintables || {};
110
+ }
111
+ /**
112
+ * The inherited {@link ParameterBag}.
113
+ * Merges the {@link Variant}'s parameters and those from the {@link Element}.
114
+ */
115
+ get inheritedParameters() {
116
+ return (0, lodash_es_1.merge)({}, this.variant.inheritedParameters, this.parameters);
117
+ }
118
+ /**
119
+ * The actual {@link Mesh}es that make up this {@link Element}.
120
+ * Handy for e.g. creating a bounding box around an entire element.
121
+ */
122
+ get meshesFlat() {
123
+ const flatMeshes = this.nodes.reduce((accFlatMeshes, curNode) => {
124
+ const currMeshes = curNode.getChildMeshes(false, n => n instanceof mesh_1.Mesh);
125
+ return [...accFlatMeshes, ...currMeshes];
126
+ }, []);
127
+ return flatMeshes;
128
+ }
129
+ /**
130
+ * The actual {@link AbstractMesh}es that make up this {@link Element}.
131
+ * I.e. (real) Meshes as well as InstancedMeshes.
132
+ */
133
+ get abstractMeshesFlat() {
134
+ const allAbstractMeshes = this.nodes.reduce((acc, currNode) => {
135
+ const currAbstractMeshes = currNode.getChildMeshes(false, n => n instanceof abstractMesh_1.AbstractMesh);
136
+ if (currNode instanceof abstractMesh_1.AbstractMesh) {
137
+ currAbstractMeshes.unshift(currNode);
138
+ }
139
+ return [...acc, ...currAbstractMeshes];
140
+ }, []); // initial accumulator value
141
+ return allAbstractMeshes;
142
+ }
143
+ /**
144
+ * The meshes of this {@link Element}.
145
+ */
146
+ get meshes() {
147
+ return this.nodes.filter(node => node instanceof mesh_1.Mesh);
148
+ }
149
+ /**
150
+ * @protected
151
+ */
152
+ set highlightLayer(layer) {
153
+ this._highlightLayer = layer;
154
+ }
155
+ get highlightLayer() {
156
+ return this._highlightLayer;
157
+ }
158
+ /**
159
+ * Destroys this {@link Element} and dispose all nodes.
160
+ */
161
+ destroy() {
162
+ this.nodes.forEach(node => {
163
+ node.dispose();
164
+ });
165
+ return this;
166
+ }
167
+ /**
168
+ * Gets a node by its {@link DottedPath}.
169
+ */
170
+ getNode(dottedPath) {
171
+ const _dottedPath = dottedPath_1.DottedPath.create(dottedPath);
172
+ const keys = Array.from(this._dottedNodes.keys()).map(dp => dp.path);
173
+ const values = Array.from(this._dottedNodes.values());
174
+ const node = values[keys.indexOf(_dottedPath.path)];
175
+ if (!node) {
176
+ throw new Error(`Node with path "${_dottedPath.path}" does not exist for element "${this.id}".`);
177
+ }
178
+ return node;
179
+ }
180
+ /**
181
+ * Gets a mesh by its {@link DottedPath}.
182
+ */
183
+ getMesh(dottedPath) {
184
+ return __awaiter(this, void 0, void 0, function* () {
185
+ const node = yield this.getNode(dottedPath);
186
+ if (node instanceof mesh_1.Mesh) {
187
+ return node;
188
+ }
189
+ return null;
190
+ });
191
+ }
192
+ /**
193
+ * Gets a {@link PaintableDefinition} by its name.
194
+ */
195
+ getPaintableDefinition(name) {
196
+ if (!this.paintableDefinitions || !this.paintableDefinitions[name]) {
197
+ throw new Error(`No paintable "${name}" defined for element "${this.id}".`);
198
+ }
199
+ return this.paintableDefinitions[name];
200
+ }
201
+ /**
202
+ * Gets a {@link TraceableDefinition} by its name.
203
+ */
204
+ getTraceableDefinition(name) {
205
+ if (!this.traceableDefinitions || !this.traceableDefinitions[name]) {
206
+ throw new Error(`No traceable "${name}" defined to add observer in element "${this.id}".`);
207
+ }
208
+ return this.traceableDefinitions[name];
209
+ }
210
+ /**
211
+ * Gets the paintable Node by its name.
212
+ */
213
+ getPaintableNode(name) {
214
+ return this.getNode(this.getPaintableDefinition(name).path);
215
+ }
216
+ /**
217
+ * Gets the traceable Node by its name.
218
+ */
219
+ getTraceableNode(name) {
220
+ return this.getNode(this.getTraceableDefinition(name).path);
221
+ }
222
+ /**
223
+ * @see {@link VariantParameterizable.commitParameters}
224
+ * @emit {@link Event.ELEMENT_PARAMETER_COMMITTED}
225
+ */
226
+ commitParameters(parameters) {
227
+ const _super = Object.create(null, {
228
+ commitParameters: { get: () => super.commitParameters }
229
+ });
230
+ return __awaiter(this, void 0, void 0, function* () {
231
+ return _super.commitParameters.call(this, parameters);
232
+ });
233
+ }
234
+ /**
235
+ * Adds an observer function for camera matrix changes.
236
+ * The `observer` gets 2 parameters: the `AbstractMesh` and a `ClientRect` object.
237
+ */
238
+ addTraceableObserver(traceable, observer, payload) {
239
+ if (payload && !(payload instanceof HTMLElement)) {
240
+ throw new Error(`Payload for camera observer must be an instance of "HTMLElement" in element "${this.id}".`);
241
+ }
242
+ if (!this.traceableDefinitions || !this.traceableDefinitions[traceable]) {
243
+ throw new Error(`No traceable "${traceable}" defined to add observer in element "${this.id}".`);
244
+ }
245
+ const node = this.getTraceableNode(traceable);
246
+ if (!(node instanceof abstractMesh_1.AbstractMesh)) {
247
+ throw new Error(`The path must be an instance of "AbstractMesh" for camera observer in element "${this.id}".`);
248
+ }
249
+ this.variant.viewer.sceneManager.activeCamera.onViewMatrixChangedObservable.add((eventData, eventState) => {
250
+ const position = (0, babylonHelper_1.getClientRectFromMesh)(node, this.variant.viewer.scene, this.variant.viewer.canvas);
251
+ if (payload) {
252
+ payload.style.top = `${position.top}px`;
253
+ payload.style.left = `${position.left}px`;
254
+ }
255
+ observer(node, position);
256
+ });
257
+ return this;
258
+ }
259
+ /**
260
+ * Draws a `ImageBitmap` or `OffscreenCanvas` onto a `paintable` defined via {@link PaintableDefinition}.
261
+ */
262
+ drawPaintable(paintable, imageSource) {
263
+ // node and material checks and preperation
264
+ const node = this.getPaintableNode(paintable);
265
+ if (!(node instanceof abstractMesh_1.AbstractMesh)) {
266
+ throw new Error(`The path must be an instance of "AbstractMesh" for paintable "${paintable}" ` + `in element "${this.id}".`);
267
+ }
268
+ if (node.material && this.variant.viewer.cloneMaterialsOnMutation) {
269
+ if (node.material && !(0, lodash_es_1.has)(node.metadata, 'dirty.material')) {
270
+ (0, babylonHelper_1.cloneTransformNodeMaterial)(node);
271
+ }
272
+ if (!(0, lodash_es_1.has)(node.metadata, 'dirty.material.texture')) {
273
+ // inject initial value and mark as dirty
274
+ (0, babylonHelper_1.injectNodeMetadata)(node, { dirty: { material: { texture: true } } });
275
+ }
276
+ }
277
+ else if (!node.material) {
278
+ // paintables should also work if no material is assigned to the node per default
279
+ // in this case a certain fallback material with fitting default values is defined
280
+ const pbrMat = new pbrMaterial_1.PBRMaterial(`${this.id}.${paintable}.material`, this.variant.viewer.scene);
281
+ pbrMat.roughness = 1.0;
282
+ pbrMat.metallic = 1.0;
283
+ node.material = pbrMat;
284
+ }
285
+ // TODO: Not sure if this really should be overwritten every time the paintable is redrawn.
286
+ // Maybe the 3D artist had something in mind when setting a certain transparency mode, which would get overwritten in this case.
287
+ // Applying it as a default if no material is set is fine though (see roughness/metallic some lines above).
288
+ // Removing this line will most likely break existing configurators, so be cautios!
289
+ node.material.transparencyMode = material_1.Material.MATERIAL_ALPHATESTANDBLEND;
290
+ // consider width and height of the paintable
291
+ const widthAndHeight = {
292
+ width: imageSource.width,
293
+ height: imageSource.height,
294
+ };
295
+ const paintableDefinition = this.getPaintableDefinition(paintable);
296
+ if ((0, lodash_es_1.isNumber)(paintableDefinition.textureOptions)) {
297
+ widthAndHeight.width = paintableDefinition.textureOptions;
298
+ widthAndHeight.height = paintableDefinition.textureOptions;
299
+ }
300
+ else if ((0, lodash_es_1.isPlainObject)(paintableDefinition.textureOptions)) {
301
+ widthAndHeight.width = paintableDefinition.textureOptions.width;
302
+ widthAndHeight.height = paintableDefinition.textureOptions.height;
303
+ }
304
+ // create dynamic texture on which the image can be drawn
305
+ const texture = new dynamicTexture_1.DynamicTexture(`${this.id}.${paintable}.texture`, widthAndHeight, this.variant.viewer.scene, false);
306
+ // draw image on texture
307
+ const ctx = texture.getContext();
308
+ ctx.drawImage(imageSource, 0, 0);
309
+ texture.update(false);
310
+ // finally apply the texture on the desired node material
311
+ (0, babylonHelper_1.setMaterialTexture)(node, texture, false);
312
+ return this;
313
+ }
314
+ /**
315
+ * Draws a SVG string onto a `paintable` defined via {@link PaintableDefinition}.
316
+ */
317
+ drawPaintableFromSvg(paintable, svgSource) {
318
+ return __awaiter(this, void 0, void 0, function* () {
319
+ const image = yield (0, resourceHelper_1.createImageFromSvg)(svgSource);
320
+ return this.drawPaintable(paintable, image);
321
+ });
322
+ }
323
+ /**
324
+ * Draws an Image from source (URL/URI) onto a `paintable` defined via {@link PaintableDefinition}.
325
+ */
326
+ drawPaintableFromImgSrc(paintable, imgSource) {
327
+ return __awaiter(this, void 0, void 0, function* () {
328
+ const image = yield (0, resourceHelper_1.createImageFromImgSrc)(imgSource);
329
+ return this.drawPaintable(paintable, image);
330
+ });
331
+ }
332
+ /**
333
+ * Adds observers for loading missing materials where needed
334
+ */
335
+ addMissingMaterialObservers() {
336
+ for (const abstractMesh of this.abstractMeshesFlat) {
337
+ (0, sceneLoaderHelper_1.addMissingMaterialObserver)(abstractMesh);
338
+ }
339
+ }
340
+ /**
341
+ * Adds the default {@link ParameterObserver}s which are called every time {@link commitParameters} is called.
342
+ */
343
+ addParameterObservers() {
344
+ this._parameterObservers.set(parameter_1.Parameter.VISIBLE, [
345
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
346
+ let visible;
347
+ try {
348
+ visible = parameter_1.Parameter.parseBoolean(newValue);
349
+ }
350
+ catch (e) {
351
+ return;
352
+ }
353
+ if (visible === true) {
354
+ element.nodes.forEach(node => {
355
+ (0, babylonHelper_1.injectNodeMetadata)(node, { visibility: node.isEnabled() });
356
+ (0, babylonHelper_1.activateTransformNode)(node);
357
+ });
358
+ }
359
+ else if (visible === false) {
360
+ element.nodes.forEach(node => {
361
+ (0, babylonHelper_1.injectNodeMetadata)(node, { visibility: node.isEnabled() });
362
+ (0, babylonHelper_1.deactivateTransformNode)(node);
363
+ });
364
+ }
365
+ }),
366
+ ]);
367
+ this._parameterObservers.set(parameter_1.Parameter.SCALING, [
368
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
369
+ // we have to deal just with root nodes here due to relative impacts in a node tree
370
+ element.nodes.forEach(node => (0, babylonHelper_1.transformTransformNode)(node, {
371
+ scaling: parameter_1.Parameter.parseVector(newValue),
372
+ position: parameter_1.Parameter.parseVector(element.inheritedParameters[parameter_1.Parameter.POSITION] || '(0, 0, 0)'),
373
+ rotation: parameter_1.Parameter.parseRotation(element.inheritedParameters[parameter_1.Parameter.ROTATION] || '(0, 0, 0)'),
374
+ }));
375
+ }),
376
+ ]);
377
+ this._parameterObservers.set(parameter_1.Parameter.POSITION, [
378
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
379
+ // we have to deal just with root nodes here due to relative impacts in a node tree
380
+ element.nodes.forEach(node => (0, babylonHelper_1.transformTransformNode)(node, {
381
+ scaling: parameter_1.Parameter.parseVector(element.inheritedParameters[parameter_1.Parameter.SCALING] || '(1, 1, 1)'),
382
+ position: parameter_1.Parameter.parseVector(newValue),
383
+ rotation: parameter_1.Parameter.parseRotation(element.inheritedParameters[parameter_1.Parameter.ROTATION] || '(0, 0, 0)'),
384
+ }));
385
+ }),
386
+ ]);
387
+ this._parameterObservers.set(parameter_1.Parameter.ROTATION, [
388
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
389
+ // we have to deal just with root nodes here due to relative impacts in a node tree
390
+ element.nodes.forEach(node => (0, babylonHelper_1.transformTransformNode)(node, {
391
+ scaling: parameter_1.Parameter.parseVector(element.inheritedParameters[parameter_1.Parameter.SCALING] || '(1, 1, 1)'),
392
+ position: parameter_1.Parameter.parseVector(element.inheritedParameters[parameter_1.Parameter.POSITION] || '(0, 0, 0)'),
393
+ rotation: parameter_1.Parameter.parseRotation(newValue),
394
+ }));
395
+ }),
396
+ ]);
397
+ this._parameterObservers.set(parameter_1.Parameter.MATERIAL, [
398
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
399
+ const materialName = newValue.toString();
400
+ element.nodes.forEach(node => {
401
+ (0, babylonHelper_1.assertTransformNode)(node, (node) => {
402
+ if (node instanceof instancedMesh_1.InstancedMesh) {
403
+ throw new Error(`Changing parameter "${parameter_1.Parameter.MATERIAL}" ` +
404
+ `of an InstancedMesh is not supported. ` +
405
+ `Tried to change node "${node.id}" on element "${element.id}".`);
406
+ }
407
+ });
408
+ (0, babylonHelper_1.setMaterial)(element.variant, node, materialName);
409
+ });
410
+ }),
411
+ ]);
412
+ this._parameterObservers.set(parameter_1.Parameter.MATERIAL_COLOR, [
413
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
414
+ const color = parameter_1.Parameter.parseColor(newValue);
415
+ element.nodes.forEach(node => {
416
+ (0, babylonHelper_1.assertTransformNode)(node, (node) => {
417
+ if (node instanceof instancedMesh_1.InstancedMesh) {
418
+ throw new Error(`Changing parameter "${parameter_1.Parameter.MATERIAL_COLOR}" ` +
419
+ `of an InstancedMesh is not supported. ` +
420
+ `Tried to change node "${node.id}" on element "${element.id}".`);
421
+ }
422
+ });
423
+ if (this.variant.viewer.cloneMaterialsOnMutation) {
424
+ if (!(0, lodash_es_1.has)(node.metadata, 'dirty.material')) {
425
+ (0, babylonHelper_1.cloneTransformNodeMaterial)(node);
426
+ }
427
+ if (!(0, lodash_es_1.has)(node.metadata, 'dirty.material.color')) {
428
+ // inject initial value and mark as dirty
429
+ (0, babylonHelper_1.injectNodeMetadata)(node, { dirty: { material: { color: oldValue } } });
430
+ }
431
+ }
432
+ (0, babylonHelper_1.setMaterialColor)(node, color);
433
+ });
434
+ }),
435
+ ]);
436
+ this._parameterObservers.set(parameter_1.Parameter.MATERIAL_METALLNESS, [
437
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
438
+ const metallness = parameter_1.Parameter.parseNumber(newValue);
439
+ element.nodes.forEach(node => {
440
+ (0, babylonHelper_1.assertTransformNode)(node, (node) => {
441
+ if (node instanceof instancedMesh_1.InstancedMesh) {
442
+ throw new Error(`Changing parameter "${parameter_1.Parameter.MATERIAL_METALLNESS}" ` +
443
+ `of an InstancedMesh is not supported. ` +
444
+ `Tried to change node "${node.id}" on element "${element.id}".`);
445
+ }
446
+ });
447
+ if (this.variant.viewer.cloneMaterialsOnMutation) {
448
+ if (!(0, lodash_es_1.has)(node.metadata, 'dirty.material')) {
449
+ (0, babylonHelper_1.cloneTransformNodeMaterial)(node);
450
+ }
451
+ if (!(0, lodash_es_1.has)(node.metadata, 'dirty.material.metallness')) {
452
+ // inject initial value and mark as dirty
453
+ (0, babylonHelper_1.injectNodeMetadata)(node, { dirty: { material: { metallness: oldValue } } });
454
+ }
455
+ }
456
+ (0, babylonHelper_1.setMaterialMetallness)(node, metallness);
457
+ });
458
+ }),
459
+ ]);
460
+ this._parameterObservers.set(parameter_1.Parameter.MATERIAL_ROUGHNESS, [
461
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
462
+ const roughness = parameter_1.Parameter.parseNumber(newValue);
463
+ element.nodes.forEach(node => {
464
+ (0, babylonHelper_1.assertTransformNode)(node, (node) => {
465
+ if (node instanceof instancedMesh_1.InstancedMesh) {
466
+ throw new Error(`Changing parameter "${parameter_1.Parameter.MATERIAL_ROUGHNESS}" ` +
467
+ `of an InstancedMesh is not supported. ` +
468
+ `Tried to change node "${node.id}" on element "${element.id}".`);
469
+ }
470
+ });
471
+ if (this.variant.viewer.cloneMaterialsOnMutation) {
472
+ if (!(0, lodash_es_1.has)(node.metadata, 'dirty.material')) {
473
+ (0, babylonHelper_1.cloneTransformNodeMaterial)(node);
474
+ }
475
+ if (!(0, lodash_es_1.has)(node.metadata, 'dirty.material.roughness')) {
476
+ // inject initial value and mark as dirty
477
+ (0, babylonHelper_1.injectNodeMetadata)(node, { dirty: { material: { roughness: oldValue } } });
478
+ }
479
+ }
480
+ (0, babylonHelper_1.setMaterialRoughness)(node, roughness);
481
+ });
482
+ }),
483
+ ]);
484
+ this._parameterObservers.set(parameter_1.Parameter.HIGHLIGHT_COLOR, [
485
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
486
+ var _a;
487
+ // trigger Parameter.HIGHLIGHTED observers
488
+ (_a = this._parameterObservers.get(parameter_1.Parameter.HIGHLIGHTED)) === null || _a === void 0 ? void 0 : _a.forEach(observer => {
489
+ observer(element, !!element.highlighted, !!element.highlighted);
490
+ });
491
+ }),
492
+ ]);
493
+ this._parameterObservers.set(parameter_1.Parameter.HIGHLIGHT_ENABLED, [
494
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
495
+ var _b;
496
+ let curr;
497
+ try {
498
+ curr = parameter_1.Parameter.parseBoolean(newValue);
499
+ }
500
+ catch (e) {
501
+ return;
502
+ }
503
+ switch (curr) {
504
+ case true:
505
+ if (element._highlightLayer) {
506
+ break;
507
+ }
508
+ element.highlightLayer = new highlightLayer_1.HighlightLayer(dottedPath_1.DottedPath.create(element.id).addPart('highlight').path, element.variant.viewer.scene);
509
+ break;
510
+ case false:
511
+ (_b = element.highlightLayer) === null || _b === void 0 ? void 0 : _b.dispose();
512
+ break;
513
+ }
514
+ }),
515
+ ]);
516
+ this._parameterObservers.set(parameter_1.Parameter.HIGHLIGHTED, [
517
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
518
+ var _c;
519
+ if (!element.inheritedParameters[parameter_1.Parameter.HIGHLIGHT_ENABLED]) {
520
+ return;
521
+ }
522
+ // trigger Parameter.HIGHLIGHT_ENABLED observers to ensure HighlightLayer
523
+ (_c = this._parameterObservers.get(parameter_1.Parameter.HIGHLIGHT_ENABLED)) === null || _c === void 0 ? void 0 : _c.forEach(observer => {
524
+ observer(element, this.parameters[parameter_1.Parameter.HIGHLIGHT_ENABLED], true);
525
+ });
526
+ let highlighted;
527
+ try {
528
+ highlighted = parameter_1.Parameter.parseBoolean(newValue);
529
+ }
530
+ catch (e) {
531
+ return;
532
+ }
533
+ // Add/Remove meshes to previously created highlight layers.
534
+ if (highlighted === true) {
535
+ element.nodes.forEach(node => {
536
+ (0, babylonHelper_1.assertTransformNode)(node, (node) => {
537
+ if (node instanceof instancedMesh_1.InstancedMesh) {
538
+ throw new Error(`Changing parameter "${parameter_1.Parameter.HIGHLIGHTED}" ` +
539
+ `of an InstancedMesh is not supported. ` +
540
+ `Tried to change node "${node.id}" on element "${element.id}".`);
541
+ }
542
+ });
543
+ let color = math_color_1.Color3.Green();
544
+ if (element.inheritedParameters[parameter_1.Parameter.HIGHLIGHT_COLOR]) {
545
+ color = parameter_1.Parameter.parseColor(element.inheritedParameters[parameter_1.Parameter.HIGHLIGHT_COLOR]);
546
+ }
547
+ if (typeof element._highlightLayer !== 'undefined') {
548
+ (0, babylonHelper_1.addToHighlightLayer)(element._highlightLayer, color, node);
549
+ }
550
+ });
551
+ }
552
+ else if (highlighted === false) {
553
+ element.nodes.forEach(node => {
554
+ if (typeof element._highlightLayer !== 'undefined') {
555
+ (0, babylonHelper_1.removeFromHighlightLayer)(element._highlightLayer, node);
556
+ }
557
+ });
558
+ }
559
+ }),
560
+ ]);
561
+ this._parameterObservers.set(parameter_1.Parameter.CAST_SHADOW, [
562
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
563
+ let castShadow;
564
+ try {
565
+ castShadow = parameter_1.Parameter.parseBoolean(newValue);
566
+ }
567
+ catch (e) {
568
+ return;
569
+ }
570
+ let lightCsl = element.inheritedParameters[parameter_1.Parameter.CAST_SHADOW_FROM_LIGHTS];
571
+ if (!lightCsl) {
572
+ lightCsl = element.variant.inheritedViewerLights.map(l => l.name).join(',');
573
+ }
574
+ if (castShadow === true) {
575
+ yield this.castShadowValueHandler(lightCsl, babylonHelper_1.addToShadowGenerator);
576
+ }
577
+ if (castShadow === false) {
578
+ yield this.castShadowValueHandler(lightCsl, babylonHelper_1.removeFromShadowGenerator);
579
+ }
580
+ }),
581
+ ]);
582
+ this._parameterObservers.set(parameter_1.Parameter.CAST_SHADOW_FROM_LIGHTS, [
583
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
584
+ // TODO: Possible performance issue in combination with CAST_SHADOW, since both observers are initially called
585
+ // when CAST_SHADOW and CAST_SHADOW_FROM_LIGHTS are defined in the spec.
586
+ const lightCsl = element.variant.inheritedViewerLights.map(l => l.name).join(',');
587
+ // cleanup all shadow generators
588
+ yield this.castShadowValueHandler(lightCsl, babylonHelper_1.removeFromShadowGenerator);
589
+ if (element.castShadow === true) {
590
+ // if newValue is undefined or '' then set newValue to lightCsl (use all lights)
591
+ if (!newValue) {
592
+ newValue = lightCsl;
593
+ }
594
+ yield this.castShadowValueHandler(newValue, babylonHelper_1.addToShadowGenerator);
595
+ }
596
+ }),
597
+ ]);
598
+ this._parameterObservers.set(parameter_1.Parameter.RECEIVE_SHADOWS, [
599
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
600
+ element.nodes.forEach(node => (0, babylonHelper_1.setReceiveShadows)(node, parameter_1.Parameter.parseBoolean(newValue)));
601
+ }),
602
+ ]);
603
+ return this;
604
+ }
605
+ /**
606
+ * Filter for nodes that are not defined or excluded in the `pathDefinitions`.
607
+ */
608
+ filterNode(node) {
609
+ let includeTokens = [];
610
+ this.pathDefinitions.include.forEach(dottedPath => {
611
+ includeTokens = (0, lodash_es_1.union)(includeTokens, dottedPath_1.DottedPath.create(dottedPath).leafTokens);
612
+ });
613
+ const shallKeep = (dp) => {
614
+ const isDirectlyIncluded = !!includeTokens.find(_dp => _dp === dp.path);
615
+ const isSubOfIncluded = !!this.pathDefinitions.include.find(_dp => {
616
+ return dp.path.startsWith(`${_dp}${dottedPath_1.DottedPath.DELIMITER}`);
617
+ });
618
+ return isDirectlyIncluded || isSubOfIncluded;
619
+ };
620
+ const shallExclude = (dp) => {
621
+ if (!this.pathDefinitions.exclude) {
622
+ return false;
623
+ }
624
+ return !!this.pathDefinitions.exclude.find(_dp => dp.path === _dp);
625
+ };
626
+ return shallKeep(node.metadata.dottedPath) && !shallExclude(node.metadata.dottedPath);
627
+ }
628
+ /**
629
+ * Asserts whether all {@link pathDefinitions} link to a valid `Node` path.
630
+ * @protected
631
+ */
632
+ assertPathDefinitions() {
633
+ var _a;
634
+ const dottedNodes = this.variant.inheritedDottedNodes;
635
+ if (dottedNodes.size === 0) {
636
+ throw new Error(`There are no mapped nodes for element "${this.id}".`);
637
+ }
638
+ const include = this.pathDefinitions.include.map(path => dottedPath_1.DottedPath.create(path).path);
639
+ const exclude = (_a = this.pathDefinitions.exclude) === null || _a === void 0 ? void 0 : _a.map(path => dottedPath_1.DottedPath.create(path).path);
640
+ const keys = Array.from(dottedNodes.keys()).map(dottedPath => dottedPath.path);
641
+ (0, lodash_es_1.union)(include, exclude).forEach(path => {
642
+ if (keys.indexOf(path) === -1) {
643
+ throw new Error(`Node with path "${path}" does not exist for element "${this.id}".`);
644
+ }
645
+ });
646
+ }
647
+ /**
648
+ * Handles callback for given light parameter.
649
+ */
650
+ castShadowValueHandler(lightCsl, mutator) {
651
+ return __awaiter(this, void 0, void 0, function* () {
652
+ let lights = [];
653
+ for (const lightName of parameter_1.Parameter.parseCommaSeparatedList(lightCsl)) {
654
+ const viewerLight = yield this.variant.getViewerLight(lightName);
655
+ if (viewerLight) {
656
+ lights.push(viewerLight.light);
657
+ }
658
+ }
659
+ const shadowGenerators = lights.map(light => light === null || light === void 0 ? void 0 : light.getShadowGenerator()).filter(Boolean);
660
+ shadowGenerators.forEach(generator => {
661
+ this.nodes.forEach(node => {
662
+ mutator(generator, node);
663
+ });
664
+ });
665
+ });
666
+ }
667
+ }
668
+ exports.Element = Element;
824
669
  //# sourceMappingURL=element.js.map