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