@combeenation/3d-viewer 6.0.0 → 6.1.0-beta2

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 (189) 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 -2
  4. package/dist/lib-cjs/api/classes/dottedPath.d.ts +79 -79
  5. package/dist/lib-cjs/api/classes/dottedPath.js +166 -166
  6. package/dist/lib-cjs/api/classes/element.d.ts +149 -149
  7. package/dist/lib-cjs/api/classes/element.js +669 -669
  8. package/dist/lib-cjs/api/classes/event.d.ts +342 -326
  9. package/dist/lib-cjs/api/classes/event.js +365 -349
  10. package/dist/lib-cjs/api/classes/event.js.map +1 -1
  11. package/dist/lib-cjs/api/classes/eventBroadcaster.d.ts +26 -26
  12. package/dist/lib-cjs/api/classes/eventBroadcaster.js +49 -49
  13. package/dist/lib-cjs/api/classes/parameter.d.ts +339 -339
  14. package/dist/lib-cjs/api/classes/parameter.js +464 -464
  15. package/dist/lib-cjs/api/classes/parameterObservable.d.ts +36 -36
  16. package/dist/lib-cjs/api/classes/parameterObservable.js +97 -97
  17. package/dist/lib-cjs/api/classes/parameterizable.d.ts +15 -15
  18. package/dist/lib-cjs/api/classes/parameterizable.js +102 -102
  19. package/dist/lib-cjs/api/classes/placementAnimation.d.ts +45 -45
  20. package/dist/lib-cjs/api/classes/placementAnimation.js +176 -176
  21. package/dist/lib-cjs/api/classes/variant.d.ts +234 -234
  22. package/dist/lib-cjs/api/classes/variant.js +836 -836
  23. package/dist/lib-cjs/api/classes/variantInstance.d.ts +44 -44
  24. package/dist/lib-cjs/api/classes/variantInstance.js +105 -105
  25. package/dist/lib-cjs/api/classes/variantParameterizable.d.ts +17 -17
  26. package/dist/lib-cjs/api/classes/variantParameterizable.js +88 -88
  27. package/dist/lib-cjs/api/classes/viewer.d.ts +187 -189
  28. package/dist/lib-cjs/api/classes/viewer.js +594 -594
  29. package/dist/lib-cjs/api/classes/viewer.js.map +1 -1
  30. package/dist/lib-cjs/api/classes/viewerLight.d.ts +66 -66
  31. package/dist/lib-cjs/api/classes/viewerLight.js +348 -348
  32. package/dist/lib-cjs/api/internal/lensRendering.d.ts +8 -8
  33. package/dist/lib-cjs/api/internal/lensRendering.js +11 -11
  34. package/dist/lib-cjs/api/internal/sceneSetup.d.ts +13 -13
  35. package/dist/lib-cjs/api/internal/sceneSetup.js +226 -226
  36. package/dist/lib-cjs/api/manager/animationManager.d.ts +30 -30
  37. package/dist/lib-cjs/api/manager/animationManager.js +126 -126
  38. package/dist/lib-cjs/api/manager/gltfExportManager.d.ts +78 -65
  39. package/dist/lib-cjs/api/manager/gltfExportManager.js +241 -197
  40. package/dist/lib-cjs/api/manager/gltfExportManager.js.map +1 -1
  41. package/dist/lib-cjs/api/manager/sceneManager.d.ts +33 -31
  42. package/dist/lib-cjs/api/manager/sceneManager.js +130 -127
  43. package/dist/lib-cjs/api/manager/sceneManager.js.map +1 -1
  44. package/dist/lib-cjs/api/manager/textureLoadManager.d.ts +22 -0
  45. package/dist/lib-cjs/api/manager/textureLoadManager.js +98 -0
  46. package/dist/lib-cjs/api/manager/textureLoadManager.js.map +1 -0
  47. package/dist/lib-cjs/api/manager/variantInstanceManager.d.ts +92 -92
  48. package/dist/lib-cjs/api/manager/variantInstanceManager.js +260 -260
  49. package/dist/lib-cjs/api/store/specStorage.d.ts +24 -24
  50. package/dist/lib-cjs/api/store/specStorage.js +50 -50
  51. package/dist/lib-cjs/api/util/babylonHelper.d.ts +183 -174
  52. package/dist/lib-cjs/api/util/babylonHelper.js +596 -585
  53. package/dist/lib-cjs/api/util/babylonHelper.js.map +1 -1
  54. package/dist/lib-cjs/api/util/globalTypes.d.ts +383 -370
  55. package/dist/lib-cjs/api/util/globalTypes.js +1 -1
  56. package/dist/lib-cjs/api/util/resourceHelper.d.ts +58 -58
  57. package/dist/lib-cjs/api/util/resourceHelper.js +203 -203
  58. package/dist/lib-cjs/api/util/sceneLoaderHelper.d.ts +42 -35
  59. package/dist/lib-cjs/api/util/sceneLoaderHelper.js +139 -139
  60. package/dist/lib-cjs/api/util/sceneLoaderHelper.js.map +1 -1
  61. package/dist/lib-cjs/api/util/stringHelper.d.ts +9 -9
  62. package/dist/lib-cjs/api/util/stringHelper.js +25 -25
  63. package/dist/lib-cjs/api/util/structureHelper.d.ts +9 -9
  64. package/dist/lib-cjs/api/util/structureHelper.js +48 -48
  65. package/dist/lib-cjs/buildinfo.json +3 -3
  66. package/dist/lib-cjs/commonjs.tsconfig.tsbuildinfo +1 -1
  67. package/dist/lib-cjs/index.d.ts +51 -51
  68. package/dist/lib-cjs/index.js +110 -110
  69. package/dist/webpack-stats.json +0 -0
  70. package/package.json +79 -79
  71. package/src/api/classes/animationInterface.ts +10 -10
  72. package/src/api/classes/dottedPath.ts +181 -181
  73. package/src/api/classes/element.ts +717 -717
  74. package/src/api/classes/event.ts +385 -367
  75. package/src/api/classes/eventBroadcaster.ts +52 -52
  76. package/src/api/classes/parameter.ts +497 -497
  77. package/src/api/classes/parameterObservable.ts +100 -100
  78. package/src/api/classes/parameterizable.ts +87 -87
  79. package/src/api/classes/placementAnimation.ts +162 -162
  80. package/src/api/classes/variant.ts +904 -904
  81. package/src/api/classes/variantInstance.ts +97 -97
  82. package/src/api/classes/variantParameterizable.ts +85 -85
  83. package/src/api/classes/viewer.ts +672 -670
  84. package/src/api/classes/viewerLight.ts +339 -339
  85. package/src/api/internal/debugViewer.ts +90 -90
  86. package/src/api/internal/lensRendering.ts +9 -9
  87. package/src/api/internal/sceneSetup.ts +205 -205
  88. package/src/api/manager/animationManager.ts +143 -143
  89. package/src/api/manager/gltfExportManager.ts +236 -192
  90. package/src/api/manager/sceneManager.ts +132 -127
  91. package/src/api/manager/textureLoadManager.ts +95 -0
  92. package/src/api/manager/variantInstanceManager.ts +265 -265
  93. package/src/api/store/specStorage.ts +51 -51
  94. package/src/api/util/babylonHelper.ts +658 -645
  95. package/src/api/util/globalTypes.ts +432 -417
  96. package/src/api/util/resourceHelper.ts +191 -191
  97. package/src/api/util/sceneLoaderHelper.ts +137 -144
  98. package/src/api/util/stringHelper.ts +23 -23
  99. package/src/api/util/structureHelper.ts +49 -49
  100. package/src/buildinfo.json +3 -3
  101. package/src/dev.ts +61 -61
  102. package/src/index.ts +96 -96
  103. package/src/types.d.ts +28 -28
  104. package/dist/lib-es6/api/classes/animationInterface.d.ts +0 -8
  105. package/dist/lib-es6/api/classes/animationInterface.js +0 -2
  106. package/dist/lib-es6/api/classes/animationInterface.js.map +0 -1
  107. package/dist/lib-es6/api/classes/dottedPath.d.ts +0 -79
  108. package/dist/lib-es6/api/classes/dottedPath.js +0 -163
  109. package/dist/lib-es6/api/classes/dottedPath.js.map +0 -1
  110. package/dist/lib-es6/api/classes/element.d.ts +0 -149
  111. package/dist/lib-es6/api/classes/element.js +0 -666
  112. package/dist/lib-es6/api/classes/element.js.map +0 -1
  113. package/dist/lib-es6/api/classes/event.d.ts +0 -326
  114. package/dist/lib-es6/api/classes/event.js +0 -346
  115. package/dist/lib-es6/api/classes/event.js.map +0 -1
  116. package/dist/lib-es6/api/classes/eventBroadcaster.d.ts +0 -26
  117. package/dist/lib-es6/api/classes/eventBroadcaster.js +0 -43
  118. package/dist/lib-es6/api/classes/eventBroadcaster.js.map +0 -1
  119. package/dist/lib-es6/api/classes/parameter.d.ts +0 -339
  120. package/dist/lib-es6/api/classes/parameter.js +0 -461
  121. package/dist/lib-es6/api/classes/parameter.js.map +0 -1
  122. package/dist/lib-es6/api/classes/parameterObservable.d.ts +0 -36
  123. package/dist/lib-es6/api/classes/parameterObservable.js +0 -94
  124. package/dist/lib-es6/api/classes/parameterObservable.js.map +0 -1
  125. package/dist/lib-es6/api/classes/parameterizable.d.ts +0 -15
  126. package/dist/lib-es6/api/classes/parameterizable.js +0 -99
  127. package/dist/lib-es6/api/classes/parameterizable.js.map +0 -1
  128. package/dist/lib-es6/api/classes/placementAnimation.d.ts +0 -45
  129. package/dist/lib-es6/api/classes/placementAnimation.js +0 -173
  130. package/dist/lib-es6/api/classes/placementAnimation.js.map +0 -1
  131. package/dist/lib-es6/api/classes/variant.d.ts +0 -234
  132. package/dist/lib-es6/api/classes/variant.js +0 -833
  133. package/dist/lib-es6/api/classes/variant.js.map +0 -1
  134. package/dist/lib-es6/api/classes/variantInstance.d.ts +0 -44
  135. package/dist/lib-es6/api/classes/variantInstance.js +0 -102
  136. package/dist/lib-es6/api/classes/variantInstance.js.map +0 -1
  137. package/dist/lib-es6/api/classes/variantParameterizable.d.ts +0 -17
  138. package/dist/lib-es6/api/classes/variantParameterizable.js +0 -85
  139. package/dist/lib-es6/api/classes/variantParameterizable.js.map +0 -1
  140. package/dist/lib-es6/api/classes/viewer.d.ts +0 -189
  141. package/dist/lib-es6/api/classes/viewer.js +0 -588
  142. package/dist/lib-es6/api/classes/viewer.js.map +0 -1
  143. package/dist/lib-es6/api/classes/viewerLight.d.ts +0 -66
  144. package/dist/lib-es6/api/classes/viewerLight.js +0 -322
  145. package/dist/lib-es6/api/classes/viewerLight.js.map +0 -1
  146. package/dist/lib-es6/api/internal/lensRendering.d.ts +0 -8
  147. package/dist/lib-es6/api/internal/lensRendering.js +0 -9
  148. package/dist/lib-es6/api/internal/lensRendering.js.map +0 -1
  149. package/dist/lib-es6/api/internal/sceneSetup.d.ts +0 -13
  150. package/dist/lib-es6/api/internal/sceneSetup.js +0 -199
  151. package/dist/lib-es6/api/internal/sceneSetup.js.map +0 -1
  152. package/dist/lib-es6/api/manager/animationManager.d.ts +0 -30
  153. package/dist/lib-es6/api/manager/animationManager.js +0 -123
  154. package/dist/lib-es6/api/manager/animationManager.js.map +0 -1
  155. package/dist/lib-es6/api/manager/gltfExportManager.d.ts +0 -65
  156. package/dist/lib-es6/api/manager/gltfExportManager.js +0 -194
  157. package/dist/lib-es6/api/manager/gltfExportManager.js.map +0 -1
  158. package/dist/lib-es6/api/manager/sceneManager.d.ts +0 -31
  159. package/dist/lib-es6/api/manager/sceneManager.js +0 -124
  160. package/dist/lib-es6/api/manager/sceneManager.js.map +0 -1
  161. package/dist/lib-es6/api/manager/variantInstanceManager.d.ts +0 -92
  162. package/dist/lib-es6/api/manager/variantInstanceManager.js +0 -257
  163. package/dist/lib-es6/api/manager/variantInstanceManager.js.map +0 -1
  164. package/dist/lib-es6/api/store/specStorage.d.ts +0 -24
  165. package/dist/lib-es6/api/store/specStorage.js +0 -47
  166. package/dist/lib-es6/api/store/specStorage.js.map +0 -1
  167. package/dist/lib-es6/api/util/babylonHelper.d.ts +0 -174
  168. package/dist/lib-es6/api/util/babylonHelper.js +0 -556
  169. package/dist/lib-es6/api/util/babylonHelper.js.map +0 -1
  170. package/dist/lib-es6/api/util/globalTypes.d.ts +0 -370
  171. package/dist/lib-es6/api/util/globalTypes.js +0 -2
  172. package/dist/lib-es6/api/util/globalTypes.js.map +0 -1
  173. package/dist/lib-es6/api/util/resourceHelper.d.ts +0 -58
  174. package/dist/lib-es6/api/util/resourceHelper.js +0 -194
  175. package/dist/lib-es6/api/util/resourceHelper.js.map +0 -1
  176. package/dist/lib-es6/api/util/sceneLoaderHelper.d.ts +0 -35
  177. package/dist/lib-es6/api/util/sceneLoaderHelper.js +0 -130
  178. package/dist/lib-es6/api/util/sceneLoaderHelper.js.map +0 -1
  179. package/dist/lib-es6/api/util/stringHelper.d.ts +0 -9
  180. package/dist/lib-es6/api/util/stringHelper.js +0 -22
  181. package/dist/lib-es6/api/util/stringHelper.js.map +0 -1
  182. package/dist/lib-es6/api/util/structureHelper.d.ts +0 -9
  183. package/dist/lib-es6/api/util/structureHelper.js +0 -46
  184. package/dist/lib-es6/api/util/structureHelper.js.map +0 -1
  185. package/dist/lib-es6/buildinfo.json +0 -3
  186. package/dist/lib-es6/es6.tsconfig.tsbuildinfo +0 -1
  187. package/dist/lib-es6/index.d.ts +0 -51
  188. package/dist/lib-es6/index.js +0 -49
  189. package/dist/lib-es6/index.js.map +0 -1
@@ -1,670 +1,670 @@
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
287
- // in this case.
288
- // Applying it as a default if no material is set is fine though (see roughness/metallic some lines above).
289
- // Removing this line will most likely break existing configurators, so be cautios!
290
- node.material.transparencyMode = material_1.Material.MATERIAL_ALPHATESTANDBLEND;
291
- // consider width and height of the paintable
292
- const widthAndHeight = {
293
- width: imageSource.width,
294
- height: imageSource.height,
295
- };
296
- const paintableDefinition = this.getPaintableDefinition(paintable);
297
- if ((0, lodash_es_1.isNumber)(paintableDefinition.textureOptions)) {
298
- widthAndHeight.width = paintableDefinition.textureOptions;
299
- widthAndHeight.height = paintableDefinition.textureOptions;
300
- }
301
- else if ((0, lodash_es_1.isPlainObject)(paintableDefinition.textureOptions)) {
302
- widthAndHeight.width = paintableDefinition.textureOptions.width;
303
- widthAndHeight.height = paintableDefinition.textureOptions.height;
304
- }
305
- // create dynamic texture on which the image can be drawn
306
- const texture = new dynamicTexture_1.DynamicTexture(`${this.id}.${paintable}.texture`, widthAndHeight, this.variant.viewer.scene, false);
307
- // draw image on texture
308
- const ctx = texture.getContext();
309
- ctx.drawImage(imageSource, 0, 0);
310
- texture.update(false);
311
- // finally apply the texture on the desired node material
312
- (0, babylonHelper_1.setMaterialTexture)(node, texture, false);
313
- return this;
314
- }
315
- /**
316
- * Draws a SVG string onto a `paintable` defined via {@link PaintableDefinition}.
317
- */
318
- drawPaintableFromSvg(paintable, svgSource) {
319
- return __awaiter(this, void 0, void 0, function* () {
320
- const image = yield (0, resourceHelper_1.createImageFromSvg)(svgSource);
321
- return this.drawPaintable(paintable, image);
322
- });
323
- }
324
- /**
325
- * Draws an Image from source (URL/URI) onto a `paintable` defined via {@link PaintableDefinition}.
326
- */
327
- drawPaintableFromImgSrc(paintable, imgSource) {
328
- return __awaiter(this, void 0, void 0, function* () {
329
- const image = yield (0, resourceHelper_1.createImageFromImgSrc)(imgSource);
330
- return this.drawPaintable(paintable, image);
331
- });
332
- }
333
- /**
334
- * Adds observers for loading missing materials where needed
335
- */
336
- addMissingMaterialObservers() {
337
- for (const abstractMesh of this.abstractMeshesFlat) {
338
- (0, sceneLoaderHelper_1.addMissingMaterialObserver)(abstractMesh);
339
- }
340
- }
341
- /**
342
- * Adds the default {@link ParameterObserver}s which are called every time {@link commitParameters} is called.
343
- */
344
- addParameterObservers() {
345
- this._parameterObservers.set(parameter_1.Parameter.VISIBLE, [
346
- (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
347
- let visible;
348
- try {
349
- visible = parameter_1.Parameter.parseBoolean(newValue);
350
- }
351
- catch (e) {
352
- return;
353
- }
354
- if (visible === true) {
355
- element.nodes.forEach(node => {
356
- (0, babylonHelper_1.injectNodeMetadata)(node, { visibility: node.isEnabled() });
357
- (0, babylonHelper_1.activateTransformNode)(node);
358
- });
359
- }
360
- else if (visible === false) {
361
- element.nodes.forEach(node => {
362
- (0, babylonHelper_1.injectNodeMetadata)(node, { visibility: node.isEnabled() });
363
- (0, babylonHelper_1.deactivateTransformNode)(node);
364
- });
365
- }
366
- }),
367
- ]);
368
- this._parameterObservers.set(parameter_1.Parameter.SCALING, [
369
- (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
370
- // we have to deal just with root nodes here due to relative impacts in a node tree
371
- element.nodes.forEach(node => (0, babylonHelper_1.transformTransformNode)(node, {
372
- scaling: parameter_1.Parameter.parseVector(newValue),
373
- position: parameter_1.Parameter.parseVector(element.inheritedParameters[parameter_1.Parameter.POSITION] || '(0, 0, 0)'),
374
- rotation: parameter_1.Parameter.parseRotation(element.inheritedParameters[parameter_1.Parameter.ROTATION] || '(0, 0, 0)'),
375
- }));
376
- }),
377
- ]);
378
- this._parameterObservers.set(parameter_1.Parameter.POSITION, [
379
- (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
380
- // we have to deal just with root nodes here due to relative impacts in a node tree
381
- element.nodes.forEach(node => (0, babylonHelper_1.transformTransformNode)(node, {
382
- scaling: parameter_1.Parameter.parseVector(element.inheritedParameters[parameter_1.Parameter.SCALING] || '(1, 1, 1)'),
383
- position: parameter_1.Parameter.parseVector(newValue),
384
- rotation: parameter_1.Parameter.parseRotation(element.inheritedParameters[parameter_1.Parameter.ROTATION] || '(0, 0, 0)'),
385
- }));
386
- }),
387
- ]);
388
- this._parameterObservers.set(parameter_1.Parameter.ROTATION, [
389
- (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
390
- // we have to deal just with root nodes here due to relative impacts in a node tree
391
- element.nodes.forEach(node => (0, babylonHelper_1.transformTransformNode)(node, {
392
- scaling: parameter_1.Parameter.parseVector(element.inheritedParameters[parameter_1.Parameter.SCALING] || '(1, 1, 1)'),
393
- position: parameter_1.Parameter.parseVector(element.inheritedParameters[parameter_1.Parameter.POSITION] || '(0, 0, 0)'),
394
- rotation: parameter_1.Parameter.parseRotation(newValue),
395
- }));
396
- }),
397
- ]);
398
- this._parameterObservers.set(parameter_1.Parameter.MATERIAL, [
399
- (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
400
- const materialName = newValue.toString();
401
- element.nodes.forEach(node => {
402
- (0, babylonHelper_1.assertTransformNode)(node, (node) => {
403
- if (node instanceof instancedMesh_1.InstancedMesh) {
404
- throw new Error(`Changing parameter "${parameter_1.Parameter.MATERIAL}" ` +
405
- `of an InstancedMesh is not supported. ` +
406
- `Tried to change node "${node.id}" on element "${element.id}".`);
407
- }
408
- });
409
- (0, babylonHelper_1.setMaterial)(element.variant, node, materialName);
410
- });
411
- }),
412
- ]);
413
- this._parameterObservers.set(parameter_1.Parameter.MATERIAL_COLOR, [
414
- (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
415
- const color = parameter_1.Parameter.parseColor(newValue);
416
- element.nodes.forEach(node => {
417
- (0, babylonHelper_1.assertTransformNode)(node, (node) => {
418
- if (node instanceof instancedMesh_1.InstancedMesh) {
419
- throw new Error(`Changing parameter "${parameter_1.Parameter.MATERIAL_COLOR}" ` +
420
- `of an InstancedMesh is not supported. ` +
421
- `Tried to change node "${node.id}" on element "${element.id}".`);
422
- }
423
- });
424
- if (this.variant.viewer.cloneMaterialsOnMutation) {
425
- if (!(0, lodash_es_1.has)(node.metadata, 'dirty.material')) {
426
- (0, babylonHelper_1.cloneTransformNodeMaterial)(node);
427
- }
428
- if (!(0, lodash_es_1.has)(node.metadata, 'dirty.material.color')) {
429
- // inject initial value and mark as dirty
430
- (0, babylonHelper_1.injectNodeMetadata)(node, { dirty: { material: { color: oldValue } } });
431
- }
432
- }
433
- (0, babylonHelper_1.setMaterialColor)(node, color);
434
- });
435
- }),
436
- ]);
437
- this._parameterObservers.set(parameter_1.Parameter.MATERIAL_METALLNESS, [
438
- (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
439
- const metallness = parameter_1.Parameter.parseNumber(newValue);
440
- element.nodes.forEach(node => {
441
- (0, babylonHelper_1.assertTransformNode)(node, (node) => {
442
- if (node instanceof instancedMesh_1.InstancedMesh) {
443
- throw new Error(`Changing parameter "${parameter_1.Parameter.MATERIAL_METALLNESS}" ` +
444
- `of an InstancedMesh is not supported. ` +
445
- `Tried to change node "${node.id}" on element "${element.id}".`);
446
- }
447
- });
448
- if (this.variant.viewer.cloneMaterialsOnMutation) {
449
- if (!(0, lodash_es_1.has)(node.metadata, 'dirty.material')) {
450
- (0, babylonHelper_1.cloneTransformNodeMaterial)(node);
451
- }
452
- if (!(0, lodash_es_1.has)(node.metadata, 'dirty.material.metallness')) {
453
- // inject initial value and mark as dirty
454
- (0, babylonHelper_1.injectNodeMetadata)(node, { dirty: { material: { metallness: oldValue } } });
455
- }
456
- }
457
- (0, babylonHelper_1.setMaterialMetallness)(node, metallness);
458
- });
459
- }),
460
- ]);
461
- this._parameterObservers.set(parameter_1.Parameter.MATERIAL_ROUGHNESS, [
462
- (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
463
- const roughness = parameter_1.Parameter.parseNumber(newValue);
464
- element.nodes.forEach(node => {
465
- (0, babylonHelper_1.assertTransformNode)(node, (node) => {
466
- if (node instanceof instancedMesh_1.InstancedMesh) {
467
- throw new Error(`Changing parameter "${parameter_1.Parameter.MATERIAL_ROUGHNESS}" ` +
468
- `of an InstancedMesh is not supported. ` +
469
- `Tried to change node "${node.id}" on element "${element.id}".`);
470
- }
471
- });
472
- if (this.variant.viewer.cloneMaterialsOnMutation) {
473
- if (!(0, lodash_es_1.has)(node.metadata, 'dirty.material')) {
474
- (0, babylonHelper_1.cloneTransformNodeMaterial)(node);
475
- }
476
- if (!(0, lodash_es_1.has)(node.metadata, 'dirty.material.roughness')) {
477
- // inject initial value and mark as dirty
478
- (0, babylonHelper_1.injectNodeMetadata)(node, { dirty: { material: { roughness: oldValue } } });
479
- }
480
- }
481
- (0, babylonHelper_1.setMaterialRoughness)(node, roughness);
482
- });
483
- }),
484
- ]);
485
- this._parameterObservers.set(parameter_1.Parameter.HIGHLIGHT_COLOR, [
486
- (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
487
- var _a;
488
- // trigger Parameter.HIGHLIGHTED observers
489
- (_a = this._parameterObservers.get(parameter_1.Parameter.HIGHLIGHTED)) === null || _a === void 0 ? void 0 : _a.forEach(observer => {
490
- observer(element, !!element.highlighted, !!element.highlighted);
491
- });
492
- }),
493
- ]);
494
- this._parameterObservers.set(parameter_1.Parameter.HIGHLIGHT_ENABLED, [
495
- (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
496
- var _b;
497
- let curr;
498
- try {
499
- curr = parameter_1.Parameter.parseBoolean(newValue);
500
- }
501
- catch (e) {
502
- return;
503
- }
504
- switch (curr) {
505
- case true:
506
- if (element._highlightLayer) {
507
- break;
508
- }
509
- element.highlightLayer = new highlightLayer_1.HighlightLayer(dottedPath_1.DottedPath.create(element.id).addPart('highlight').path, element.variant.viewer.scene);
510
- break;
511
- case false:
512
- (_b = element.highlightLayer) === null || _b === void 0 ? void 0 : _b.dispose();
513
- break;
514
- }
515
- }),
516
- ]);
517
- this._parameterObservers.set(parameter_1.Parameter.HIGHLIGHTED, [
518
- (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
519
- var _c;
520
- if (!element.inheritedParameters[parameter_1.Parameter.HIGHLIGHT_ENABLED]) {
521
- return;
522
- }
523
- // trigger Parameter.HIGHLIGHT_ENABLED observers to ensure HighlightLayer
524
- (_c = this._parameterObservers.get(parameter_1.Parameter.HIGHLIGHT_ENABLED)) === null || _c === void 0 ? void 0 : _c.forEach(observer => {
525
- observer(element, this.parameters[parameter_1.Parameter.HIGHLIGHT_ENABLED], true);
526
- });
527
- let highlighted;
528
- try {
529
- highlighted = parameter_1.Parameter.parseBoolean(newValue);
530
- }
531
- catch (e) {
532
- return;
533
- }
534
- // Add/Remove meshes to previously created highlight layers.
535
- if (highlighted === true) {
536
- element.nodes.forEach(node => {
537
- (0, babylonHelper_1.assertTransformNode)(node, (node) => {
538
- if (node instanceof instancedMesh_1.InstancedMesh) {
539
- throw new Error(`Changing parameter "${parameter_1.Parameter.HIGHLIGHTED}" ` +
540
- `of an InstancedMesh is not supported. ` +
541
- `Tried to change node "${node.id}" on element "${element.id}".`);
542
- }
543
- });
544
- let color = math_color_1.Color3.Green();
545
- if (element.inheritedParameters[parameter_1.Parameter.HIGHLIGHT_COLOR]) {
546
- color = parameter_1.Parameter.parseColor(element.inheritedParameters[parameter_1.Parameter.HIGHLIGHT_COLOR]);
547
- }
548
- if (typeof element._highlightLayer !== 'undefined') {
549
- (0, babylonHelper_1.addToHighlightLayer)(element._highlightLayer, color, node);
550
- }
551
- });
552
- }
553
- else if (highlighted === false) {
554
- element.nodes.forEach(node => {
555
- if (typeof element._highlightLayer !== 'undefined') {
556
- (0, babylonHelper_1.removeFromHighlightLayer)(element._highlightLayer, node);
557
- }
558
- });
559
- }
560
- }),
561
- ]);
562
- this._parameterObservers.set(parameter_1.Parameter.CAST_SHADOW, [
563
- (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
564
- let castShadow;
565
- try {
566
- castShadow = parameter_1.Parameter.parseBoolean(newValue);
567
- }
568
- catch (e) {
569
- return;
570
- }
571
- let lightCsl = element.inheritedParameters[parameter_1.Parameter.CAST_SHADOW_FROM_LIGHTS];
572
- if (!lightCsl) {
573
- lightCsl = element.variant.inheritedViewerLights.map(l => l.name).join(',');
574
- }
575
- if (castShadow === true) {
576
- yield this.castShadowValueHandler(lightCsl, babylonHelper_1.addToShadowGenerator);
577
- }
578
- if (castShadow === false) {
579
- yield this.castShadowValueHandler(lightCsl, babylonHelper_1.removeFromShadowGenerator);
580
- }
581
- }),
582
- ]);
583
- this._parameterObservers.set(parameter_1.Parameter.CAST_SHADOW_FROM_LIGHTS, [
584
- (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
585
- // TODO: Possible performance issue in combination with CAST_SHADOW, since both observers are initially called
586
- // when CAST_SHADOW and CAST_SHADOW_FROM_LIGHTS are defined in the spec.
587
- const lightCsl = element.variant.inheritedViewerLights.map(l => l.name).join(',');
588
- // cleanup all shadow generators
589
- yield this.castShadowValueHandler(lightCsl, babylonHelper_1.removeFromShadowGenerator);
590
- if (element.castShadow === true) {
591
- // if newValue is undefined or '' then set newValue to lightCsl (use all lights)
592
- if (!newValue) {
593
- newValue = lightCsl;
594
- }
595
- yield this.castShadowValueHandler(newValue, babylonHelper_1.addToShadowGenerator);
596
- }
597
- }),
598
- ]);
599
- this._parameterObservers.set(parameter_1.Parameter.RECEIVE_SHADOWS, [
600
- (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
601
- element.nodes.forEach(node => (0, babylonHelper_1.setReceiveShadows)(node, parameter_1.Parameter.parseBoolean(newValue)));
602
- }),
603
- ]);
604
- return this;
605
- }
606
- /**
607
- * Filter for nodes that are not defined or excluded in the `pathDefinitions`.
608
- */
609
- filterNode(node) {
610
- let includeTokens = [];
611
- this.pathDefinitions.include.forEach(dottedPath => {
612
- includeTokens = (0, lodash_es_1.union)(includeTokens, dottedPath_1.DottedPath.create(dottedPath).leafTokens);
613
- });
614
- const shallKeep = (dp) => {
615
- const isDirectlyIncluded = !!includeTokens.find(_dp => _dp === dp.path);
616
- const isSubOfIncluded = !!this.pathDefinitions.include.find(_dp => {
617
- return dp.path.startsWith(`${_dp}${dottedPath_1.DottedPath.DELIMITER}`);
618
- });
619
- return isDirectlyIncluded || isSubOfIncluded;
620
- };
621
- const shallExclude = (dp) => {
622
- if (!this.pathDefinitions.exclude) {
623
- return false;
624
- }
625
- return !!this.pathDefinitions.exclude.find(_dp => dp.path === _dp);
626
- };
627
- return shallKeep(node.metadata.dottedPath) && !shallExclude(node.metadata.dottedPath);
628
- }
629
- /**
630
- * Asserts whether all {@link pathDefinitions} link to a valid `Node` path.
631
- * @protected
632
- */
633
- assertPathDefinitions() {
634
- var _a;
635
- const dottedNodes = this.variant.inheritedDottedNodes;
636
- if (dottedNodes.size === 0) {
637
- throw new Error(`There are no mapped nodes for element "${this.id}".`);
638
- }
639
- const include = this.pathDefinitions.include.map(path => dottedPath_1.DottedPath.create(path).path);
640
- const exclude = (_a = this.pathDefinitions.exclude) === null || _a === void 0 ? void 0 : _a.map(path => dottedPath_1.DottedPath.create(path).path);
641
- const keys = Array.from(dottedNodes.keys()).map(dottedPath => dottedPath.path);
642
- (0, lodash_es_1.union)(include, exclude).forEach(path => {
643
- if (keys.indexOf(path) === -1) {
644
- throw new Error(`Node with path "${path}" does not exist for element "${this.id}".`);
645
- }
646
- });
647
- }
648
- /**
649
- * Handles callback for given light parameter.
650
- */
651
- castShadowValueHandler(lightCsl, mutator) {
652
- return __awaiter(this, void 0, void 0, function* () {
653
- const lights = [];
654
- for (const lightName of parameter_1.Parameter.parseCommaSeparatedList(lightCsl)) {
655
- const viewerLight = yield this.variant.getViewerLight(lightName);
656
- if (viewerLight) {
657
- lights.push(viewerLight.light);
658
- }
659
- }
660
- const shadowGenerators = lights.map(light => light === null || light === void 0 ? void 0 : light.getShadowGenerator()).filter(Boolean);
661
- shadowGenerators.forEach(generator => {
662
- this.nodes.forEach(node => {
663
- mutator(generator, node);
664
- });
665
- });
666
- });
667
- }
668
- }
669
- 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
287
+ // in this case.
288
+ // Applying it as a default if no material is set is fine though (see roughness/metallic some lines above).
289
+ // Removing this line will most likely break existing configurators, so be cautios!
290
+ node.material.transparencyMode = material_1.Material.MATERIAL_ALPHATESTANDBLEND;
291
+ // consider width and height of the paintable
292
+ const widthAndHeight = {
293
+ width: imageSource.width,
294
+ height: imageSource.height,
295
+ };
296
+ const paintableDefinition = this.getPaintableDefinition(paintable);
297
+ if ((0, lodash_es_1.isNumber)(paintableDefinition.textureOptions)) {
298
+ widthAndHeight.width = paintableDefinition.textureOptions;
299
+ widthAndHeight.height = paintableDefinition.textureOptions;
300
+ }
301
+ else if ((0, lodash_es_1.isPlainObject)(paintableDefinition.textureOptions)) {
302
+ widthAndHeight.width = paintableDefinition.textureOptions.width;
303
+ widthAndHeight.height = paintableDefinition.textureOptions.height;
304
+ }
305
+ // create dynamic texture on which the image can be drawn
306
+ const texture = new dynamicTexture_1.DynamicTexture(`${this.id}.${paintable}.texture`, widthAndHeight, this.variant.viewer.scene, false);
307
+ // draw image on texture
308
+ const ctx = texture.getContext();
309
+ ctx.drawImage(imageSource, 0, 0);
310
+ texture.update(false);
311
+ // finally apply the texture on the desired node material
312
+ (0, babylonHelper_1.setMaterialTexture)(node, texture, false);
313
+ return this;
314
+ }
315
+ /**
316
+ * Draws a SVG string onto a `paintable` defined via {@link PaintableDefinition}.
317
+ */
318
+ drawPaintableFromSvg(paintable, svgSource) {
319
+ return __awaiter(this, void 0, void 0, function* () {
320
+ const image = yield (0, resourceHelper_1.createImageFromSvg)(svgSource);
321
+ return this.drawPaintable(paintable, image);
322
+ });
323
+ }
324
+ /**
325
+ * Draws an Image from source (URL/URI) onto a `paintable` defined via {@link PaintableDefinition}.
326
+ */
327
+ drawPaintableFromImgSrc(paintable, imgSource) {
328
+ return __awaiter(this, void 0, void 0, function* () {
329
+ const image = yield (0, resourceHelper_1.createImageFromImgSrc)(imgSource);
330
+ return this.drawPaintable(paintable, image);
331
+ });
332
+ }
333
+ /**
334
+ * Adds observers for loading missing materials where needed
335
+ */
336
+ addMissingMaterialObservers() {
337
+ for (const abstractMesh of this.abstractMeshesFlat) {
338
+ (0, sceneLoaderHelper_1.addMissingMaterialObserver)(abstractMesh);
339
+ }
340
+ }
341
+ /**
342
+ * Adds the default {@link ParameterObserver}s which are called every time {@link commitParameters} is called.
343
+ */
344
+ addParameterObservers() {
345
+ this._parameterObservers.set(parameter_1.Parameter.VISIBLE, [
346
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
347
+ let visible;
348
+ try {
349
+ visible = parameter_1.Parameter.parseBoolean(newValue);
350
+ }
351
+ catch (e) {
352
+ return;
353
+ }
354
+ if (visible === true) {
355
+ element.nodes.forEach(node => {
356
+ (0, babylonHelper_1.injectNodeMetadata)(node, { visibility: node.isEnabled() });
357
+ (0, babylonHelper_1.activateTransformNode)(node);
358
+ });
359
+ }
360
+ else if (visible === false) {
361
+ element.nodes.forEach(node => {
362
+ (0, babylonHelper_1.injectNodeMetadata)(node, { visibility: node.isEnabled() });
363
+ (0, babylonHelper_1.deactivateTransformNode)(node);
364
+ });
365
+ }
366
+ }),
367
+ ]);
368
+ this._parameterObservers.set(parameter_1.Parameter.SCALING, [
369
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
370
+ // we have to deal just with root nodes here due to relative impacts in a node tree
371
+ element.nodes.forEach(node => (0, babylonHelper_1.transformTransformNode)(node, {
372
+ scaling: parameter_1.Parameter.parseVector(newValue),
373
+ position: parameter_1.Parameter.parseVector(element.inheritedParameters[parameter_1.Parameter.POSITION] || '(0, 0, 0)'),
374
+ rotation: parameter_1.Parameter.parseRotation(element.inheritedParameters[parameter_1.Parameter.ROTATION] || '(0, 0, 0)'),
375
+ }));
376
+ }),
377
+ ]);
378
+ this._parameterObservers.set(parameter_1.Parameter.POSITION, [
379
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
380
+ // we have to deal just with root nodes here due to relative impacts in a node tree
381
+ element.nodes.forEach(node => (0, babylonHelper_1.transformTransformNode)(node, {
382
+ scaling: parameter_1.Parameter.parseVector(element.inheritedParameters[parameter_1.Parameter.SCALING] || '(1, 1, 1)'),
383
+ position: parameter_1.Parameter.parseVector(newValue),
384
+ rotation: parameter_1.Parameter.parseRotation(element.inheritedParameters[parameter_1.Parameter.ROTATION] || '(0, 0, 0)'),
385
+ }));
386
+ }),
387
+ ]);
388
+ this._parameterObservers.set(parameter_1.Parameter.ROTATION, [
389
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
390
+ // we have to deal just with root nodes here due to relative impacts in a node tree
391
+ element.nodes.forEach(node => (0, babylonHelper_1.transformTransformNode)(node, {
392
+ scaling: parameter_1.Parameter.parseVector(element.inheritedParameters[parameter_1.Parameter.SCALING] || '(1, 1, 1)'),
393
+ position: parameter_1.Parameter.parseVector(element.inheritedParameters[parameter_1.Parameter.POSITION] || '(0, 0, 0)'),
394
+ rotation: parameter_1.Parameter.parseRotation(newValue),
395
+ }));
396
+ }),
397
+ ]);
398
+ this._parameterObservers.set(parameter_1.Parameter.MATERIAL, [
399
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
400
+ const materialName = newValue.toString();
401
+ element.nodes.forEach(node => {
402
+ (0, babylonHelper_1.assertTransformNode)(node, (node) => {
403
+ if (node instanceof instancedMesh_1.InstancedMesh) {
404
+ throw new Error(`Changing parameter "${parameter_1.Parameter.MATERIAL}" ` +
405
+ `of an InstancedMesh is not supported. ` +
406
+ `Tried to change node "${node.id}" on element "${element.id}".`);
407
+ }
408
+ });
409
+ (0, babylonHelper_1.setMaterial)(element.variant, node, materialName);
410
+ });
411
+ }),
412
+ ]);
413
+ this._parameterObservers.set(parameter_1.Parameter.MATERIAL_COLOR, [
414
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
415
+ const color = parameter_1.Parameter.parseColor(newValue);
416
+ element.nodes.forEach(node => {
417
+ (0, babylonHelper_1.assertTransformNode)(node, (node) => {
418
+ if (node instanceof instancedMesh_1.InstancedMesh) {
419
+ throw new Error(`Changing parameter "${parameter_1.Parameter.MATERIAL_COLOR}" ` +
420
+ `of an InstancedMesh is not supported. ` +
421
+ `Tried to change node "${node.id}" on element "${element.id}".`);
422
+ }
423
+ });
424
+ if (this.variant.viewer.cloneMaterialsOnMutation) {
425
+ if (!(0, lodash_es_1.has)(node.metadata, 'dirty.material')) {
426
+ (0, babylonHelper_1.cloneTransformNodeMaterial)(node);
427
+ }
428
+ if (!(0, lodash_es_1.has)(node.metadata, 'dirty.material.color')) {
429
+ // inject initial value and mark as dirty
430
+ (0, babylonHelper_1.injectNodeMetadata)(node, { dirty: { material: { color: oldValue } } });
431
+ }
432
+ }
433
+ (0, babylonHelper_1.setMaterialColor)(node, color);
434
+ });
435
+ }),
436
+ ]);
437
+ this._parameterObservers.set(parameter_1.Parameter.MATERIAL_METALLNESS, [
438
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
439
+ const metallness = parameter_1.Parameter.parseNumber(newValue);
440
+ element.nodes.forEach(node => {
441
+ (0, babylonHelper_1.assertTransformNode)(node, (node) => {
442
+ if (node instanceof instancedMesh_1.InstancedMesh) {
443
+ throw new Error(`Changing parameter "${parameter_1.Parameter.MATERIAL_METALLNESS}" ` +
444
+ `of an InstancedMesh is not supported. ` +
445
+ `Tried to change node "${node.id}" on element "${element.id}".`);
446
+ }
447
+ });
448
+ if (this.variant.viewer.cloneMaterialsOnMutation) {
449
+ if (!(0, lodash_es_1.has)(node.metadata, 'dirty.material')) {
450
+ (0, babylonHelper_1.cloneTransformNodeMaterial)(node);
451
+ }
452
+ if (!(0, lodash_es_1.has)(node.metadata, 'dirty.material.metallness')) {
453
+ // inject initial value and mark as dirty
454
+ (0, babylonHelper_1.injectNodeMetadata)(node, { dirty: { material: { metallness: oldValue } } });
455
+ }
456
+ }
457
+ (0, babylonHelper_1.setMaterialMetallness)(node, metallness);
458
+ });
459
+ }),
460
+ ]);
461
+ this._parameterObservers.set(parameter_1.Parameter.MATERIAL_ROUGHNESS, [
462
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
463
+ const roughness = parameter_1.Parameter.parseNumber(newValue);
464
+ element.nodes.forEach(node => {
465
+ (0, babylonHelper_1.assertTransformNode)(node, (node) => {
466
+ if (node instanceof instancedMesh_1.InstancedMesh) {
467
+ throw new Error(`Changing parameter "${parameter_1.Parameter.MATERIAL_ROUGHNESS}" ` +
468
+ `of an InstancedMesh is not supported. ` +
469
+ `Tried to change node "${node.id}" on element "${element.id}".`);
470
+ }
471
+ });
472
+ if (this.variant.viewer.cloneMaterialsOnMutation) {
473
+ if (!(0, lodash_es_1.has)(node.metadata, 'dirty.material')) {
474
+ (0, babylonHelper_1.cloneTransformNodeMaterial)(node);
475
+ }
476
+ if (!(0, lodash_es_1.has)(node.metadata, 'dirty.material.roughness')) {
477
+ // inject initial value and mark as dirty
478
+ (0, babylonHelper_1.injectNodeMetadata)(node, { dirty: { material: { roughness: oldValue } } });
479
+ }
480
+ }
481
+ (0, babylonHelper_1.setMaterialRoughness)(node, roughness);
482
+ });
483
+ }),
484
+ ]);
485
+ this._parameterObservers.set(parameter_1.Parameter.HIGHLIGHT_COLOR, [
486
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
487
+ var _a;
488
+ // trigger Parameter.HIGHLIGHTED observers
489
+ (_a = this._parameterObservers.get(parameter_1.Parameter.HIGHLIGHTED)) === null || _a === void 0 ? void 0 : _a.forEach(observer => {
490
+ observer(element, !!element.highlighted, !!element.highlighted);
491
+ });
492
+ }),
493
+ ]);
494
+ this._parameterObservers.set(parameter_1.Parameter.HIGHLIGHT_ENABLED, [
495
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
496
+ var _b;
497
+ let curr;
498
+ try {
499
+ curr = parameter_1.Parameter.parseBoolean(newValue);
500
+ }
501
+ catch (e) {
502
+ return;
503
+ }
504
+ switch (curr) {
505
+ case true:
506
+ if (element._highlightLayer) {
507
+ break;
508
+ }
509
+ element.highlightLayer = new highlightLayer_1.HighlightLayer(dottedPath_1.DottedPath.create(element.id).addPart('highlight').path, element.variant.viewer.scene);
510
+ break;
511
+ case false:
512
+ (_b = element.highlightLayer) === null || _b === void 0 ? void 0 : _b.dispose();
513
+ break;
514
+ }
515
+ }),
516
+ ]);
517
+ this._parameterObservers.set(parameter_1.Parameter.HIGHLIGHTED, [
518
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
519
+ var _c;
520
+ if (!element.inheritedParameters[parameter_1.Parameter.HIGHLIGHT_ENABLED]) {
521
+ return;
522
+ }
523
+ // trigger Parameter.HIGHLIGHT_ENABLED observers to ensure HighlightLayer
524
+ (_c = this._parameterObservers.get(parameter_1.Parameter.HIGHLIGHT_ENABLED)) === null || _c === void 0 ? void 0 : _c.forEach(observer => {
525
+ observer(element, this.parameters[parameter_1.Parameter.HIGHLIGHT_ENABLED], true);
526
+ });
527
+ let highlighted;
528
+ try {
529
+ highlighted = parameter_1.Parameter.parseBoolean(newValue);
530
+ }
531
+ catch (e) {
532
+ return;
533
+ }
534
+ // Add/Remove meshes to previously created highlight layers.
535
+ if (highlighted === true) {
536
+ element.nodes.forEach(node => {
537
+ (0, babylonHelper_1.assertTransformNode)(node, (node) => {
538
+ if (node instanceof instancedMesh_1.InstancedMesh) {
539
+ throw new Error(`Changing parameter "${parameter_1.Parameter.HIGHLIGHTED}" ` +
540
+ `of an InstancedMesh is not supported. ` +
541
+ `Tried to change node "${node.id}" on element "${element.id}".`);
542
+ }
543
+ });
544
+ let color = math_color_1.Color3.Green();
545
+ if (element.inheritedParameters[parameter_1.Parameter.HIGHLIGHT_COLOR]) {
546
+ color = parameter_1.Parameter.parseColor(element.inheritedParameters[parameter_1.Parameter.HIGHLIGHT_COLOR]);
547
+ }
548
+ if (typeof element._highlightLayer !== 'undefined') {
549
+ (0, babylonHelper_1.addToHighlightLayer)(element._highlightLayer, color, node);
550
+ }
551
+ });
552
+ }
553
+ else if (highlighted === false) {
554
+ element.nodes.forEach(node => {
555
+ if (typeof element._highlightLayer !== 'undefined') {
556
+ (0, babylonHelper_1.removeFromHighlightLayer)(element._highlightLayer, node);
557
+ }
558
+ });
559
+ }
560
+ }),
561
+ ]);
562
+ this._parameterObservers.set(parameter_1.Parameter.CAST_SHADOW, [
563
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
564
+ let castShadow;
565
+ try {
566
+ castShadow = parameter_1.Parameter.parseBoolean(newValue);
567
+ }
568
+ catch (e) {
569
+ return;
570
+ }
571
+ let lightCsl = element.inheritedParameters[parameter_1.Parameter.CAST_SHADOW_FROM_LIGHTS];
572
+ if (!lightCsl) {
573
+ lightCsl = element.variant.inheritedViewerLights.map(l => l.name).join(',');
574
+ }
575
+ if (castShadow === true) {
576
+ yield this.castShadowValueHandler(lightCsl, babylonHelper_1.addToShadowGenerator);
577
+ }
578
+ if (castShadow === false) {
579
+ yield this.castShadowValueHandler(lightCsl, babylonHelper_1.removeFromShadowGenerator);
580
+ }
581
+ }),
582
+ ]);
583
+ this._parameterObservers.set(parameter_1.Parameter.CAST_SHADOW_FROM_LIGHTS, [
584
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
585
+ // TODO: Possible performance issue in combination with CAST_SHADOW, since both observers are initially called
586
+ // when CAST_SHADOW and CAST_SHADOW_FROM_LIGHTS are defined in the spec.
587
+ const lightCsl = element.variant.inheritedViewerLights.map(l => l.name).join(',');
588
+ // cleanup all shadow generators
589
+ yield this.castShadowValueHandler(lightCsl, babylonHelper_1.removeFromShadowGenerator);
590
+ if (element.castShadow === true) {
591
+ // if newValue is undefined or '' then set newValue to lightCsl (use all lights)
592
+ if (!newValue) {
593
+ newValue = lightCsl;
594
+ }
595
+ yield this.castShadowValueHandler(newValue, babylonHelper_1.addToShadowGenerator);
596
+ }
597
+ }),
598
+ ]);
599
+ this._parameterObservers.set(parameter_1.Parameter.RECEIVE_SHADOWS, [
600
+ (element, oldValue, newValue) => __awaiter(this, void 0, void 0, function* () {
601
+ element.nodes.forEach(node => (0, babylonHelper_1.setReceiveShadows)(node, parameter_1.Parameter.parseBoolean(newValue)));
602
+ }),
603
+ ]);
604
+ return this;
605
+ }
606
+ /**
607
+ * Filter for nodes that are not defined or excluded in the `pathDefinitions`.
608
+ */
609
+ filterNode(node) {
610
+ let includeTokens = [];
611
+ this.pathDefinitions.include.forEach(dottedPath => {
612
+ includeTokens = (0, lodash_es_1.union)(includeTokens, dottedPath_1.DottedPath.create(dottedPath).leafTokens);
613
+ });
614
+ const shallKeep = (dp) => {
615
+ const isDirectlyIncluded = !!includeTokens.find(_dp => _dp === dp.path);
616
+ const isSubOfIncluded = !!this.pathDefinitions.include.find(_dp => {
617
+ return dp.path.startsWith(`${_dp}${dottedPath_1.DottedPath.DELIMITER}`);
618
+ });
619
+ return isDirectlyIncluded || isSubOfIncluded;
620
+ };
621
+ const shallExclude = (dp) => {
622
+ if (!this.pathDefinitions.exclude) {
623
+ return false;
624
+ }
625
+ return !!this.pathDefinitions.exclude.find(_dp => dp.path === _dp);
626
+ };
627
+ return shallKeep(node.metadata.dottedPath) && !shallExclude(node.metadata.dottedPath);
628
+ }
629
+ /**
630
+ * Asserts whether all {@link pathDefinitions} link to a valid `Node` path.
631
+ * @protected
632
+ */
633
+ assertPathDefinitions() {
634
+ var _a;
635
+ const dottedNodes = this.variant.inheritedDottedNodes;
636
+ if (dottedNodes.size === 0) {
637
+ throw new Error(`There are no mapped nodes for element "${this.id}".`);
638
+ }
639
+ const include = this.pathDefinitions.include.map(path => dottedPath_1.DottedPath.create(path).path);
640
+ const exclude = (_a = this.pathDefinitions.exclude) === null || _a === void 0 ? void 0 : _a.map(path => dottedPath_1.DottedPath.create(path).path);
641
+ const keys = Array.from(dottedNodes.keys()).map(dottedPath => dottedPath.path);
642
+ (0, lodash_es_1.union)(include, exclude).forEach(path => {
643
+ if (keys.indexOf(path) === -1) {
644
+ throw new Error(`Node with path "${path}" does not exist for element "${this.id}".`);
645
+ }
646
+ });
647
+ }
648
+ /**
649
+ * Handles callback for given light parameter.
650
+ */
651
+ castShadowValueHandler(lightCsl, mutator) {
652
+ return __awaiter(this, void 0, void 0, function* () {
653
+ const lights = [];
654
+ for (const lightName of parameter_1.Parameter.parseCommaSeparatedList(lightCsl)) {
655
+ const viewerLight = yield this.variant.getViewerLight(lightName);
656
+ if (viewerLight) {
657
+ lights.push(viewerLight.light);
658
+ }
659
+ }
660
+ const shadowGenerators = lights.map(light => light === null || light === void 0 ? void 0 : light.getShadowGenerator()).filter(Boolean);
661
+ shadowGenerators.forEach(generator => {
662
+ this.nodes.forEach(node => {
663
+ mutator(generator, node);
664
+ });
665
+ });
666
+ });
667
+ }
668
+ }
669
+ exports.Element = Element;
670
670
  //# sourceMappingURL=element.js.map