@combeenation/3d-viewer 7.1.0 → 7.1.2

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 (117) 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 +153 -153
  7. package/dist/lib-cjs/api/classes/element.js +672 -670
  8. package/dist/lib-cjs/api/classes/element.js.map +1 -1
  9. package/dist/lib-cjs/api/classes/event.d.ts +401 -396
  10. package/dist/lib-cjs/api/classes/event.js +424 -419
  11. package/dist/lib-cjs/api/classes/event.js.map +1 -1
  12. package/dist/lib-cjs/api/classes/eventBroadcaster.d.ts +26 -26
  13. package/dist/lib-cjs/api/classes/eventBroadcaster.js +49 -49
  14. package/dist/lib-cjs/api/classes/fuzzyMap.d.ts +7 -7
  15. package/dist/lib-cjs/api/classes/fuzzyMap.js +21 -21
  16. package/dist/lib-cjs/api/classes/parameter.d.ts +351 -351
  17. package/dist/lib-cjs/api/classes/parameter.js +524 -517
  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 +72 -72
  21. package/dist/lib-cjs/api/classes/parameterizable.d.ts +15 -15
  22. package/dist/lib-cjs/api/classes/parameterizable.js +102 -102
  23. package/dist/lib-cjs/api/classes/placementAnimation.d.ts +45 -45
  24. package/dist/lib-cjs/api/classes/placementAnimation.js +176 -176
  25. package/dist/lib-cjs/api/classes/variant.d.ts +253 -253
  26. package/dist/lib-cjs/api/classes/variant.js +858 -843
  27. package/dist/lib-cjs/api/classes/variant.js.map +1 -1
  28. package/dist/lib-cjs/api/classes/variantInstance.d.ts +53 -53
  29. package/dist/lib-cjs/api/classes/variantInstance.js +125 -125
  30. package/dist/lib-cjs/api/classes/variantParameterizable.d.ts +17 -17
  31. package/dist/lib-cjs/api/classes/variantParameterizable.js +86 -88
  32. package/dist/lib-cjs/api/classes/variantParameterizable.js.map +1 -1
  33. package/dist/lib-cjs/api/classes/viewer.d.ts +204 -199
  34. package/dist/lib-cjs/api/classes/viewer.js +682 -670
  35. package/dist/lib-cjs/api/classes/viewer.js.map +1 -1
  36. package/dist/lib-cjs/api/classes/viewerError.d.ts +43 -0
  37. package/dist/lib-cjs/api/classes/viewerError.js +56 -0
  38. package/dist/lib-cjs/api/classes/viewerError.js.map +1 -0
  39. package/dist/lib-cjs/api/classes/viewerLight.d.ts +66 -66
  40. package/dist/lib-cjs/api/classes/viewerLight.js +348 -348
  41. package/dist/lib-cjs/api/internal/lensRendering.d.ts +8 -8
  42. package/dist/lib-cjs/api/internal/lensRendering.js +11 -11
  43. package/dist/lib-cjs/api/internal/sceneSetup.d.ts +13 -13
  44. package/dist/lib-cjs/api/internal/sceneSetup.js +226 -226
  45. package/dist/lib-cjs/api/manager/animationManager.d.ts +30 -30
  46. package/dist/lib-cjs/api/manager/animationManager.js +126 -126
  47. package/dist/lib-cjs/api/manager/gltfExportManager.d.ts +79 -78
  48. package/dist/lib-cjs/api/manager/gltfExportManager.js +242 -241
  49. package/dist/lib-cjs/api/manager/gltfExportManager.js.map +1 -1
  50. package/dist/lib-cjs/api/manager/sceneManager.d.ts +33 -33
  51. package/dist/lib-cjs/api/manager/sceneManager.js +128 -130
  52. package/dist/lib-cjs/api/manager/sceneManager.js.map +1 -1
  53. package/dist/lib-cjs/api/manager/tagManager.d.ts +116 -108
  54. package/dist/lib-cjs/api/manager/tagManager.js +444 -419
  55. package/dist/lib-cjs/api/manager/tagManager.js.map +1 -1
  56. package/dist/lib-cjs/api/manager/textureLoadManager.d.ts +22 -22
  57. package/dist/lib-cjs/api/manager/textureLoadManager.js +97 -97
  58. package/dist/lib-cjs/api/manager/variantInstanceManager.d.ts +106 -102
  59. package/dist/lib-cjs/api/manager/variantInstanceManager.js +292 -284
  60. package/dist/lib-cjs/api/manager/variantInstanceManager.js.map +1 -1
  61. package/dist/lib-cjs/api/store/specStorage.d.ts +32 -32
  62. package/dist/lib-cjs/api/store/specStorage.js +65 -65
  63. package/dist/lib-cjs/api/util/babylonHelper.d.ts +235 -235
  64. package/dist/lib-cjs/api/util/babylonHelper.js +753 -753
  65. package/dist/lib-cjs/api/util/globalTypes.d.ts +441 -436
  66. package/dist/lib-cjs/api/util/globalTypes.js +1 -1
  67. package/dist/lib-cjs/api/util/resourceHelper.d.ts +58 -58
  68. package/dist/lib-cjs/api/util/resourceHelper.js +203 -203
  69. package/dist/lib-cjs/api/util/sceneLoaderHelper.d.ts +44 -44
  70. package/dist/lib-cjs/api/util/sceneLoaderHelper.js +175 -175
  71. package/dist/lib-cjs/api/util/stringHelper.d.ts +13 -13
  72. package/dist/lib-cjs/api/util/stringHelper.js +32 -32
  73. package/dist/lib-cjs/api/util/structureHelper.d.ts +9 -9
  74. package/dist/lib-cjs/api/util/structureHelper.js +57 -48
  75. package/dist/lib-cjs/api/util/structureHelper.js.map +1 -1
  76. package/dist/lib-cjs/buildinfo.json +3 -3
  77. package/dist/lib-cjs/commonjs.tsconfig.tsbuildinfo +1 -1
  78. package/dist/lib-cjs/index.d.ts +54 -53
  79. package/dist/lib-cjs/index.js +117 -114
  80. package/dist/lib-cjs/index.js.map +1 -1
  81. package/package.json +81 -81
  82. package/src/api/classes/animationInterface.ts +10 -10
  83. package/src/api/classes/dottedPath.ts +181 -181
  84. package/src/api/classes/element.ts +733 -731
  85. package/src/api/classes/event.ts +457 -452
  86. package/src/api/classes/eventBroadcaster.ts +52 -52
  87. package/src/api/classes/fuzzyMap.ts +21 -21
  88. package/src/api/classes/parameter.ts +561 -554
  89. package/src/api/classes/parameterObservable.ts +73 -73
  90. package/src/api/classes/parameterizable.ts +87 -87
  91. package/src/api/classes/placementAnimation.ts +162 -162
  92. package/src/api/classes/variant.ts +949 -933
  93. package/src/api/classes/variantInstance.ts +123 -123
  94. package/src/api/classes/variantParameterizable.ts +83 -85
  95. package/src/api/classes/viewer.ts +760 -743
  96. package/src/api/classes/viewerError.ts +63 -0
  97. package/src/api/classes/viewerLight.ts +339 -339
  98. package/src/api/internal/debugViewer.ts +90 -90
  99. package/src/api/internal/lensRendering.ts +9 -9
  100. package/src/api/internal/sceneSetup.ts +205 -205
  101. package/src/api/manager/animationManager.ts +143 -143
  102. package/src/api/manager/gltfExportManager.ts +237 -236
  103. package/src/api/manager/sceneManager.ts +134 -136
  104. package/src/api/manager/tagManager.ts +477 -451
  105. package/src/api/manager/textureLoadManager.ts +95 -95
  106. package/src/api/manager/variantInstanceManager.ts +309 -297
  107. package/src/api/store/specStorage.ts +68 -68
  108. package/src/api/util/babylonHelper.ts +823 -823
  109. package/src/api/util/globalTypes.ts +508 -503
  110. package/src/api/util/resourceHelper.ts +191 -191
  111. package/src/api/util/sceneLoaderHelper.ts +170 -170
  112. package/src/api/util/stringHelper.ts +30 -30
  113. package/src/api/util/structureHelper.ts +58 -49
  114. package/src/buildinfo.json +3 -3
  115. package/src/dev.ts +62 -62
  116. package/src/index.ts +103 -100
  117. package/src/types.d.ts +38 -38
@@ -1,754 +1,754 @@
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.reportDuplicateNodeNames = exports.duplicateNodeNames = exports.intersectingNodeNames = exports.mapTags = exports.envHelperMetadataName = exports.backgroundDomeName = exports.changeEnvironment = exports.getClientRectFromMesh = exports.removeFromShadowGenerator = exports.addToShadowGenerator = exports.setReceiveShadows = exports.removeFromHighlightLayer = exports.addToHighlightLayer = exports.setMaterialRoughness = exports.setMaterialMetallness = exports.setMaterialTexture = exports.setMaterialColor = exports.setSourceNodeMaterial = exports.setMaterial = exports.transformTransformNode = exports.disableNodeWithParents = exports.enableNodeWithParents = exports.deactivateTransformNode = exports.activateTransformNode = exports.assertMeshCapability = exports.assertTransformNode = exports.injectNodeMetadata = exports.applyMaterial = exports.getOrCreateMaterial = exports.cloneTransformNodeMaterial = exports.cloneNodeWithParents = exports.cloneTransformNode = exports.getDottedPathForNode = exports.mapToDottedNodes = exports.isTextureWithOnLoadObservable = exports.getRootNode = void 0;
13
- const dottedPath_1 = require("../classes/dottedPath");
14
- const event_1 = require("../classes/event");
15
- const sceneSetup_1 = require("../internal/sceneSetup");
16
- const sceneLoaderHelper_1 = require("./sceneLoaderHelper");
17
- const photoDome_1 = require("@babylonjs/core/Helpers/photoDome");
18
- const light_1 = require("@babylonjs/core/Lights/light");
19
- const baseTexture_1 = require("@babylonjs/core/Materials/Textures/baseTexture");
20
- const cubeTexture_1 = require("@babylonjs/core/Materials/Textures/cubeTexture");
21
- const math_axis_1 = require("@babylonjs/core/Maths/math.axis");
22
- const math_color_1 = require("@babylonjs/core/Maths/math.color");
23
- const math_vector_1 = require("@babylonjs/core/Maths/math.vector");
24
- const abstractMesh_1 = require("@babylonjs/core/Meshes/abstractMesh");
25
- const instancedMesh_1 = require("@babylonjs/core/Meshes/instancedMesh");
26
- const transformNode_1 = require("@babylonjs/core/Meshes/transformNode");
27
- const tags_1 = require("@babylonjs/core/Misc/tags");
28
- const tools_1 = require("@babylonjs/core/Misc/tools");
29
- const lodash_es_1 = require("lodash-es");
30
- const backgroundDomeName = 'BackgroundDome_ViewerGenerated';
31
- exports.backgroundDomeName = backgroundDomeName;
32
- const envHelperMetadataName = 'viewerEnvHelper';
33
- exports.envHelperMetadataName = envHelperMetadataName;
34
- const materialWaitingToBeSetMetadataName = 'materialWaitingToBeSet';
35
- /**
36
- * @param node
37
- * @return Node
38
- */
39
- const getRootNode = function (node) {
40
- let _node = node;
41
- while (_node.parent) {
42
- _node = _node.parent;
43
- }
44
- return _node;
45
- };
46
- exports.getRootNode = getRootNode;
47
- /**
48
- * @param nodes
49
- * @param predicate
50
- * @return Map<DottedPath, T>
51
- */
52
- const mapToDottedNodes = function (nodes, predicate) {
53
- const map = new Map();
54
- const addNodes = function (_node) {
55
- if (predicate && predicate(_node)) {
56
- map.set(_node.metadata.dottedPath, _node);
57
- }
58
- _node.getChildren().forEach(child => {
59
- addNodes(child);
60
- });
61
- };
62
- nodes.forEach(node => {
63
- addNodes(node);
64
- });
65
- return map;
66
- };
67
- exports.mapToDottedNodes = mapToDottedNodes;
68
- /**
69
- * @param node
70
- * @return DottedPath
71
- */
72
- const getDottedPathForNode = function (node) {
73
- const dottedPath = dottedPath_1.DottedPath.create(node.name);
74
- let _parent = node;
75
- while (_parent.parent) {
76
- _parent = _parent.parent;
77
- dottedPath.unshiftPart(_parent.name);
78
- }
79
- return dottedPath;
80
- };
81
- exports.getDottedPathForNode = getDottedPathForNode;
82
- /**
83
- * @param node
84
- * @param nodeNamingStrategy
85
- * @param predicate
86
- * @param deep
87
- * @return TransformNode | null
88
- */
89
- const cloneTransformNode = function (node, nodeNamingStrategy, predicate, deep = true) {
90
- if (predicate && !predicate(node)) {
91
- return null;
92
- }
93
- const newName = nodeNamingStrategy.handler(node, nodeNamingStrategy.payload);
94
- const clone = node.clone(newName, node.parent, true);
95
- if (clone) {
96
- clone.id = newName;
97
- clone.metadata = (0, lodash_es_1.cloneDeep)(node.metadata);
98
- injectNodeMetadata(clone, { cloneSource: node }, false);
99
- // if the cloned node is of type InstancedMesh, due to a bug(?),
100
- // the InstancedMesh.isEnabled state may have changed after cloning.
101
- // in that case, set the clone's enabled state to the original's state
102
- if (node.constructor.name === 'InstancedMesh') {
103
- clone.setEnabled(node.isEnabled(false));
104
- }
105
- // copying tags is a temporary workaround, as it doesn't work for TransformNodes ATM
106
- // see https://forum.babylonjs.com/t/transformnode-clone-doesnt-contain-original-tags/38429/1
107
- if (tags_1.Tags === null || tags_1.Tags === void 0 ? void 0 : tags_1.Tags.HasTags(node)) {
108
- tags_1.Tags.AddTagsTo(clone, tags_1.Tags.GetTags(node, true));
109
- }
110
- }
111
- if (deep) {
112
- const children = node.getChildTransformNodes(true);
113
- children.forEach(child => {
114
- const clonedChild = cloneTransformNode(child, nodeNamingStrategy, predicate, deep);
115
- if (clonedChild) {
116
- clonedChild.parent = clone;
117
- }
118
- });
119
- }
120
- return clone;
121
- };
122
- exports.cloneTransformNode = cloneTransformNode;
123
- /**
124
- * @param node
125
- */
126
- const cloneNodeWithParents = function (node) {
127
- let clone = null;
128
- if (node instanceof transformNode_1.TransformNode) {
129
- clone = node.clone(node.name, cloneNodeWithParents(node.parent), true);
130
- }
131
- else if (node instanceof light_1.Light) {
132
- clone = node.clone(node.name, cloneNodeWithParents(node.parent));
133
- }
134
- else if (node) {
135
- throw new Error(`Cloning of "${node === null || node === void 0 ? void 0 : node.constructor.name}" is not implemented (yet).`);
136
- }
137
- return clone;
138
- };
139
- exports.cloneNodeWithParents = cloneNodeWithParents;
140
- /**
141
- * @param node
142
- * @param deep
143
- * @param prefix
144
- * @return TransformNode
145
- */
146
- const cloneTransformNodeMaterial = function (node, prefix = '', deep = true) {
147
- if (node instanceof abstractMesh_1.AbstractMesh && node.material) {
148
- const newMatName = dottedPath_1.DottedPath.create(prefix).addParts([node.material.name, 'clone', node.uniqueId.toString()]);
149
- node.material = node.material.clone(newMatName.path);
150
- }
151
- if (deep) {
152
- const children = node.getChildTransformNodes(true);
153
- children.forEach(child => cloneTransformNodeMaterial(child, prefix, deep));
154
- }
155
- return node;
156
- };
157
- exports.cloneTransformNodeMaterial = cloneTransformNodeMaterial;
158
- /**
159
- * @param node
160
- * @param deep
161
- * @param metadata
162
- */
163
- const injectNodeMetadata = function (node, metadata, deep = true) {
164
- node.metadata = (0, lodash_es_1.merge)({}, node.metadata, metadata);
165
- if (deep && node instanceof transformNode_1.TransformNode) {
166
- const children = node.getChildTransformNodes(true);
167
- children.forEach(child => injectNodeMetadata(child, metadata, deep));
168
- }
169
- };
170
- exports.injectNodeMetadata = injectNodeMetadata;
171
- /**
172
- * @param node
173
- * @param assertCallable
174
- * @param callableParameters
175
- * @param deep
176
- */
177
- const assertTransformNode = function (node, assertCallable, callableParameters = [], deep = true) {
178
- assertCallable(node, ...callableParameters);
179
- if (deep) {
180
- const children = node.getChildTransformNodes(true);
181
- children.forEach(child => assertTransformNode(child, assertCallable, callableParameters, deep));
182
- }
183
- };
184
- exports.assertTransformNode = assertTransformNode;
185
- /**
186
- * @param node
187
- * @param parameter
188
- */
189
- const assertMeshCapability = function (node, parameter) {
190
- if (!(node instanceof abstractMesh_1.AbstractMesh)) {
191
- const cls = node.getClassName();
192
- throw new Error(`Changing parameter "${parameter}" of a(n) ${cls} is not supported. Tried to change node "${node.id}".`);
193
- }
194
- if (node instanceof instancedMesh_1.InstancedMesh) {
195
- throw new Error(`Changing parameter "${parameter}" of an InstancedMesh is not supported. Tried to change node "${node.id}".`);
196
- }
197
- };
198
- exports.assertMeshCapability = assertMeshCapability;
199
- /**
200
- * @param node
201
- * @param deep
202
- */
203
- const activateTransformNode = function (node, deep = true) {
204
- node.setEnabled(true);
205
- /*
206
- if( node instanceof AbstractMesh ) {
207
- node.visibility = 1;
208
- node.isPickable = true;
209
- }
210
- */
211
- if (deep) {
212
- node.getChildTransformNodes(true).forEach(child => activateTransformNode(child, deep));
213
- }
214
- };
215
- exports.activateTransformNode = activateTransformNode;
216
- /**
217
- * @param node
218
- * @param deep
219
- */
220
- const deactivateTransformNode = function (node, deep = true) {
221
- node.setEnabled(false);
222
- /*
223
- if( node instanceof AbstractMesh ) {
224
- node.visibility = 0;
225
- node.isPickable = false;
226
- }
227
- */
228
- if (deep) {
229
- node.getChildTransformNodes(true).forEach(child => deactivateTransformNode(child, deep));
230
- }
231
- };
232
- exports.deactivateTransformNode = deactivateTransformNode;
233
- /**
234
- * @param node
235
- */
236
- const enableNodeWithParents = function (node) {
237
- node.setEnabled(true);
238
- if (node.parent) {
239
- enableNodeWithParents(node.parent);
240
- }
241
- };
242
- exports.enableNodeWithParents = enableNodeWithParents;
243
- /**
244
- * @param node
245
- */
246
- const disableNodeWithParents = function (node) {
247
- node.setEnabled(false);
248
- if (node.parent) {
249
- disableNodeWithParents(node.parent);
250
- }
251
- };
252
- exports.disableNodeWithParents = disableNodeWithParents;
253
- /**
254
- * Applies a {@link TransformationDefinition} consecutively to ensure dependencies in positioning etc.
255
- * @param node
256
- * @param transformation
257
- */
258
- const transformTransformNode = function (node, transformation) {
259
- // scaling
260
- if (!(0, lodash_es_1.has)(node.metadata, 'scaling.initial')) {
261
- injectNodeMetadata(node, { 'scaling.initial': node.scaling }, false);
262
- }
263
- const initialScaling = (0, lodash_es_1.get)(node.metadata, 'scaling.initial');
264
- node.scaling = initialScaling.multiply(transformation.scaling);
265
- // position
266
- if (!(0, lodash_es_1.has)(node.metadata, 'position.initial')) {
267
- injectNodeMetadata(node, { 'position.initial': node.absolutePosition.clone() }, false);
268
- }
269
- const initialPosition = (0, lodash_es_1.get)(node.metadata, 'position.initial');
270
- node.setAbsolutePosition(initialPosition.add(transformation.position).multiply(transformation.scaling));
271
- // rotation
272
- if (!(0, lodash_es_1.has)(node.metadata, 'rotation.initial')) {
273
- let rotationQuaternion = node.rotationQuaternion;
274
- if (!rotationQuaternion) {
275
- rotationQuaternion = math_vector_1.Quaternion.RotationYawPitchRoll(node.rotation.x, node.rotation.y, node.rotation.z);
276
- }
277
- injectNodeMetadata(node, { 'rotation.initial': rotationQuaternion.asArray() }, false);
278
- }
279
- const initialRotationQuaternion = math_vector_1.Quaternion.FromArray((0, lodash_es_1.get)(node.metadata, 'rotation.initial'));
280
- node.rotationQuaternion = initialRotationQuaternion;
281
- node.rotateAround(math_vector_1.Vector3.Zero(), math_axis_1.Axis.X, transformation.rotation.x);
282
- node.rotateAround(math_vector_1.Vector3.Zero(), math_axis_1.Axis.Y, transformation.rotation.y);
283
- node.rotateAround(math_vector_1.Vector3.Zero(), math_axis_1.Axis.Z, transformation.rotation.z);
284
- node.computeWorldMatrix(true);
285
- };
286
- exports.transformTransformNode = transformTransformNode;
287
- /**
288
- * Apply changes of environment (background texture, etc.) consecutively in order to avoid dependency related issues.
289
- * @param scene
290
- * @param envDef
291
- */
292
- const changeEnvironment = function (scene, envDef) {
293
- var _a;
294
- // issue warning if both background texture and usedefault are set
295
- if (envDef.environmentUseDefault && envDef.environmentBackground) {
296
- console.warn(`!!! Warning !!! Spec parameter 'environment.usedefault' is being overruled by 'environment.background'.`);
297
- }
298
- const useDefaultEnv = envDef.environmentUseDefault && !envDef.environmentBackground;
299
- // 1) set ENVIRONMENT_COLOR
300
- const clearColorBefore = scene.clearColor.toString();
301
- scene.clearColor = envDef.environmentColor
302
- ? math_color_1.Color4.FromColor3(envDef.environmentColor)
303
- : useDefaultEnv
304
- ? sceneSetup_1.defaultEnvHelperColor
305
- : sceneSetup_1.defaultSceneClearColor;
306
- const clearColorChanged = clearColorBefore !== scene.clearColor.toString();
307
- // 2) dispose existing & set new ENVIRONMENT (==texture)
308
- const curEnvTexture = scene.environmentTexture;
309
- const envTextureChanged = envDef.environment !== (curEnvTexture === null || curEnvTexture === void 0 ? void 0 : curEnvTexture.url);
310
- if (curEnvTexture && (!envDef.environment || envTextureChanged)) {
311
- curEnvTexture.dispose();
312
- }
313
- const rotation = envDef.environmentRotation !== undefined ? tools_1.Tools.ToRadians(envDef.environmentRotation) : 0;
314
- if (envDef.environment && envTextureChanged) {
315
- const envTexture = cubeTexture_1.CubeTexture.CreateFromPrefilteredData(envDef.environment, scene);
316
- envTexture.rotationY = rotation;
317
- scene.environmentTexture = envTexture;
318
- }
319
- else if (curEnvTexture && curEnvTexture.rotationY !== rotation) {
320
- curEnvTexture.rotationY = rotation;
321
- }
322
- // 3) dispose existing & set new ENVIRONMENT_BACKGROUND
323
- const curDome = scene.getNodeByName(backgroundDomeName);
324
- const backgroundUrlChanged = envDef.environmentBackground !== (curDome === null || curDome === void 0 ? void 0 : curDome.texture.url);
325
- if (curDome && (!envDef.environmentBackground || backgroundUrlChanged)) {
326
- curDome.dispose();
327
- }
328
- const rotationPhotoDome = -1 * rotation;
329
- if (envDef.environmentBackground && backgroundUrlChanged) {
330
- const textureUrl = envDef.environmentBackground;
331
- const dome = new photoDome_1.PhotoDome(backgroundDomeName, textureUrl, { resolution: 32, size: 450 }, scene);
332
- // Rotate submesh to get them in line with environment texture
333
- dome.getChildMeshes(true)[0].rotation.y = tools_1.Tools.ToRadians(90);
334
- dome.rotation.y = rotationPhotoDome;
335
- }
336
- else if (curDome && curDome.rotation.y !== rotationPhotoDome) {
337
- curDome.rotation.y = rotationPhotoDome;
338
- }
339
- // 4) dispose existing & set new ENVIRONMENT_USEDEFAULT (only if no background is set)
340
- const curEnvHelper = (_a = scene.metadata) === null || _a === void 0 ? void 0 : _a[envHelperMetadataName];
341
- if (curEnvHelper && !useDefaultEnv) {
342
- curEnvHelper.dispose();
343
- delete scene.metadata[envHelperMetadataName];
344
- }
345
- const envHelperMainColor = math_color_1.Color3.FromArray(scene.clearColor.asArray());
346
- if (useDefaultEnv && !curEnvHelper) {
347
- const envHelper = scene.createDefaultEnvironment({ sizeAuto: true });
348
- envHelper === null || envHelper === void 0 ? void 0 : envHelper.setMainColor(envHelperMainColor);
349
- if (envHelper) {
350
- scene.metadata = (0, lodash_es_1.merge)({}, scene.metadata, { [envHelperMetadataName]: envHelper });
351
- }
352
- }
353
- else if (curEnvHelper && useDefaultEnv && clearColorChanged) {
354
- curEnvHelper.setMainColor(envHelperMainColor);
355
- }
356
- // 5) set ENVIRONMENT_INTENSITY
357
- if (envDef.environmentIntensity !== undefined) {
358
- scene.environmentIntensity = envDef.environmentIntensity;
359
- }
360
- };
361
- exports.changeEnvironment = changeEnvironment;
362
- /**
363
- * Sets a material by a given material id as material property on the given node.
364
- *
365
- * If the material is not already available in the scene, the viewer tries to create a material based on a Combeenation
366
- * [material asset](https://docs.combeenation.com/docs/howto-create-and-use-babylon-and-material-asset).
367
- * This of course only works if the viewer is used inside a Combeenation configurator.
368
- *
369
- * Furthermore this function also defers the material creation if the node is not visible yet to improve network &
370
- * viewer bootstrap performance as textures are automatically being lazy loaded only when they are actually visible in
371
- * the scene.
372
- *
373
- * Finally the material will not be applied before all its textures have been loaded. In this way "flickering" effects
374
- * will be avoided, since the material would be incomplete without its loaded textures.
375
- */
376
- const setMaterial = function (node, materialId, deep = true, variant) {
377
- if (node instanceof abstractMesh_1.AbstractMesh) {
378
- const materialExists = node.getScene().getMaterialById(materialId);
379
- const hasMissingMaterial = (0, lodash_es_1.has)(node.metadata, sceneLoaderHelper_1.missingMaterialMetadataName);
380
- const deferMaterialCreation = !materialExists && !node.isEnabled();
381
- if (deferMaterialCreation) {
382
- // do not set the material
383
- injectNodeMetadata(node, { [sceneLoaderHelper_1.missingMaterialMetadataName]: materialId }, false);
384
- // if it already had the missing material flag before, there already exists an observer...
385
- if (!hasMissingMaterial) {
386
- (0, sceneLoaderHelper_1.addMissingMaterialObserver)(node);
387
- }
388
- }
389
- else {
390
- // create material and apply it when textures have been loaded
391
- getOrCreateMaterial(node.getScene(), materialId, variant).then(material => {
392
- applyMaterial(material, node).then(() => event_1.emitter.emit(event_1.Event.MESH_MATERIAL_APPLIED, node, material));
393
- if (hasMissingMaterial) {
394
- delete node.metadata[sceneLoaderHelper_1.missingMaterialMetadataName];
395
- }
396
- });
397
- }
398
- }
399
- // recursively set materials on children (if desired)
400
- if (deep) {
401
- for (const child of node.getChildTransformNodes(true)) {
402
- setMaterial(child, materialId, deep, variant);
403
- }
404
- }
405
- };
406
- exports.setMaterial = setMaterial;
407
- /**
408
- * Gets the Material either from the given {@link Variant}, the given scene or tries to create it from an Combeenation
409
- * material asset when inside a Combeenation configurator.
410
- */
411
- const getOrCreateMaterial = function (scene, materialId, variant) {
412
- return __awaiter(this, void 0, void 0, function* () {
413
- let chosenMaterial;
414
- chosenMaterial = variant === null || variant === void 0 ? void 0 : variant.inheritedMaterials.find(mat => mat.id === materialId);
415
- chosenMaterial = chosenMaterial || scene.materials.find(mat => mat.id === materialId);
416
- chosenMaterial = chosenMaterial || (yield (0, sceneLoaderHelper_1.createMaterialFromCbnAssets)(materialId, scene));
417
- if (chosenMaterial) {
418
- return chosenMaterial;
419
- }
420
- // reject when material was not found
421
- let rejectMessage = `Material with id "${materialId}" does not exist on scene.`;
422
- if (variant) {
423
- rejectMessage = `Material with id "${materialId}" does not exist for variant "${variant.id}".`;
424
- }
425
- throw new Error(rejectMessage);
426
- });
427
- };
428
- exports.getOrCreateMaterial = getOrCreateMaterial;
429
- /**
430
- * Waits until the materials textures are loaded and shaders are compiled.
431
- * Then sets the material on the node if there is no newer "set material" request
432
- */
433
- const applyMaterial = function (material, node) {
434
- var _a;
435
- return __awaiter(this, void 0, void 0, function* () {
436
- // set current material id as last valid id, in this case all previously set materials on the node will be invalidated
437
- injectNodeMetadata(node, { [materialWaitingToBeSetMetadataName]: material.id }, false);
438
- const promTexturesReady = new Promise(resolve => baseTexture_1.BaseTexture.WhenAllReady(material.getActiveTextures(), resolve));
439
- // this promise should only take some time on the first call of the corresponding shader (eg: PBRMaterial shader)
440
- // on each other call of the same material/shader type there should be not be a waiting time, or at maximum one frame
441
- // https://forum.babylonjs.com/t/mesh-flickering-when-setting-material-initially/37312
442
- const promMaterialCompiled = material.forceCompilationAsync(node);
443
- // material needs to fulfill 2 criterias before it's ready to use
444
- // - textures need to be "ready" => downloaded
445
- // - dedicated shader needs to be compiled
446
- // if this would not be the case you can see some "flickering" when setting the material
447
- yield Promise.all([promTexturesReady, promMaterialCompiled]);
448
- // textures ready, now check if the material is still up-to-date
449
- if (material.id === ((_a = node.metadata) === null || _a === void 0 ? void 0 : _a[materialWaitingToBeSetMetadataName])) {
450
- node.material = material;
451
- delete node.metadata[materialWaitingToBeSetMetadataName];
452
- }
453
- });
454
- };
455
- exports.applyMaterial = applyMaterial;
456
- /**
457
- * !!! Warning !!!
458
- * This function is not public API. Whilst it can help solving certain problems, it only works reliably in well defined
459
- * situations and can cause unwanted side effects under some conditions. Use carefully at your own risk!
460
- *
461
- * See https://combeenation.myjetbrains.com/youtrack/issue/CB-5906 for further details regarding this warning.
462
- *
463
- * Set material of an instanced meshes source mesh.
464
- * Changes the material of all instanced meshes which have the same source mesh.
465
- *
466
- * @param node
467
- * @param material
468
- * @param deep
469
- *
470
- * @ignore
471
- */
472
- const setSourceNodeMaterial = function (node, material, deep = true) {
473
- const warn = ` You're using "setSourceNodeMaterial" which is not public API.
474
- Whilst it can help solving certain problems, it only works reliably in well defined situations and can cause unwanted side effects under some conditions.
475
- Use carefully at your own risk!`;
476
- console.warn(`!!! Warning !!!\n${warn}`);
477
- if (node instanceof instancedMesh_1.InstancedMesh) {
478
- node.sourceMesh.material = material;
479
- }
480
- if (deep) {
481
- node.getChildTransformNodes(true).forEach(child => setSourceNodeMaterial(child, material, deep));
482
- }
483
- };
484
- exports.setSourceNodeMaterial = setSourceNodeMaterial;
485
- /**
486
- * @param node
487
- * @param color
488
- * @param deep
489
- */
490
- const setMaterialColor = function (node, color, deep = true) {
491
- if (node instanceof abstractMesh_1.AbstractMesh && node.material) {
492
- const materialCls = node.material.getClassName();
493
- switch (materialCls) {
494
- case 'PBRMaterial':
495
- node.material.albedoColor = color.toLinearSpace();
496
- break;
497
- case 'StandardMaterial':
498
- node.material.diffuseColor = color;
499
- break;
500
- default:
501
- throw new Error(`Setting color for material of instance "${materialCls}" not implemented (yet).`);
502
- }
503
- setMaterial(node, node.material.id, false);
504
- }
505
- if (deep) {
506
- node.getChildTransformNodes(true).forEach(child => setMaterialColor(child, color, deep));
507
- }
508
- };
509
- exports.setMaterialColor = setMaterialColor;
510
- /**
511
- * @param node
512
- * @param texture
513
- * @param deep
514
- */
515
- const setMaterialTexture = function (node, texture, deep = true) {
516
- if (node instanceof abstractMesh_1.AbstractMesh && node.material) {
517
- const materialCls = node.material.getClassName();
518
- switch (materialCls) {
519
- case 'PBRMaterial':
520
- node.material.albedoTexture = texture;
521
- break;
522
- case 'StandardMaterial':
523
- node.material.diffuseTexture = texture;
524
- break;
525
- default:
526
- throw new Error(`Setting texture for material of instance "${materialCls}" not implemented (yet).`);
527
- }
528
- setMaterial(node, node.material.id, false);
529
- }
530
- if (deep) {
531
- node.getChildTransformNodes(true).forEach(child => setMaterialTexture(child, texture, deep));
532
- }
533
- };
534
- exports.setMaterialTexture = setMaterialTexture;
535
- /**
536
- * @param node
537
- * @param metallness
538
- * @param deep
539
- */
540
- const setMaterialMetallness = function (node, metallness, deep = true) {
541
- if (node instanceof abstractMesh_1.AbstractMesh && node.material) {
542
- const materialCls = node.material.getClassName();
543
- switch (materialCls) {
544
- case 'PBRMaterial':
545
- node.material.metallic = metallness;
546
- break;
547
- default:
548
- throw new Error(`Setting metallness for material of instance "${materialCls}" not implemented (yet).`);
549
- }
550
- setMaterial(node, node.material.id, false);
551
- }
552
- if (deep) {
553
- node.getChildTransformNodes(true).forEach(child => setMaterialMetallness(child, metallness, deep));
554
- }
555
- };
556
- exports.setMaterialMetallness = setMaterialMetallness;
557
- /**
558
- * @param node
559
- * @param roughness
560
- * @param deep
561
- */
562
- const setMaterialRoughness = function (node, roughness, deep = true) {
563
- if (node instanceof abstractMesh_1.AbstractMesh && node.material) {
564
- const materialCls = node.material.getClassName();
565
- switch (materialCls) {
566
- case 'PBRMaterial':
567
- node.material.roughness = roughness;
568
- break;
569
- case 'StandardMaterial':
570
- node.material.roughness = roughness;
571
- break;
572
- default:
573
- throw new Error(`Setting roughness for material of instance "${materialCls}" not implemented (yet).`);
574
- }
575
- setMaterial(node, node.material.id, false);
576
- }
577
- if (deep) {
578
- node.getChildTransformNodes(true).forEach(child => setMaterialRoughness(child, roughness, deep));
579
- }
580
- };
581
- exports.setMaterialRoughness = setMaterialRoughness;
582
- /**
583
- * @param node
584
- * @param layer
585
- * @param color
586
- * @param deep
587
- */
588
- const addToHighlightLayer = function (layer, color, node, deep = true) {
589
- if (node instanceof abstractMesh_1.AbstractMesh) {
590
- layer.addMesh(node, color);
591
- }
592
- if (deep) {
593
- node.getChildTransformNodes(true).forEach(child => addToHighlightLayer(layer, color, child, deep));
594
- }
595
- };
596
- exports.addToHighlightLayer = addToHighlightLayer;
597
- /**
598
- * @param node
599
- * @param layer
600
- * @param deep
601
- */
602
- const removeFromHighlightLayer = function (layer, node, deep = true) {
603
- if (node instanceof abstractMesh_1.AbstractMesh) {
604
- layer.removeMesh(node);
605
- }
606
- if (deep) {
607
- node.getChildTransformNodes(true).forEach(child => removeFromHighlightLayer(layer, child, deep));
608
- }
609
- };
610
- exports.removeFromHighlightLayer = removeFromHighlightLayer;
611
- /**
612
- * @param node
613
- * @param receiveShadows
614
- * @param deep
615
- */
616
- const setReceiveShadows = function (node, receiveShadows, deep = true) {
617
- if (node instanceof abstractMesh_1.AbstractMesh) {
618
- node.receiveShadows = receiveShadows;
619
- }
620
- if (deep) {
621
- node.getChildTransformNodes(true).forEach(child => setReceiveShadows(child, receiveShadows, deep));
622
- }
623
- };
624
- exports.setReceiveShadows = setReceiveShadows;
625
- /**
626
- * @param node
627
- * @param generator
628
- * @param deep
629
- */
630
- const addToShadowGenerator = function (generator, node, deep = true) {
631
- if (node instanceof abstractMesh_1.AbstractMesh) {
632
- // We have to remove the node because there's no duplicate check in babylon
633
- generator.removeShadowCaster(node, false);
634
- generator.addShadowCaster(node, false);
635
- }
636
- if (deep) {
637
- node.getChildTransformNodes(true).forEach(child => addToShadowGenerator(generator, child, deep));
638
- }
639
- };
640
- exports.addToShadowGenerator = addToShadowGenerator;
641
- /**
642
- * @param node
643
- * @param generator
644
- * @param deep
645
- */
646
- const removeFromShadowGenerator = function (generator, node, deep = true) {
647
- if (node instanceof abstractMesh_1.AbstractMesh) {
648
- generator.removeShadowCaster(node, false);
649
- }
650
- if (deep) {
651
- node.getChildTransformNodes(true).forEach(child => removeFromShadowGenerator(generator, child, deep));
652
- }
653
- };
654
- exports.removeFromShadowGenerator = removeFromShadowGenerator;
655
- /**
656
- * https://forum.babylonjs.com/t/get-mesh-bounding-box-position-and-size-in-2d-screen-coordinates/1058/3
657
- * @param mesh
658
- * @param scene
659
- * @param canvas
660
- */
661
- const getClientRectFromMesh = function (mesh, scene, canvas) {
662
- // get bounding box of the mesh
663
- const meshVectors = mesh.getBoundingInfo().boundingBox.vectors;
664
- // get the matrix and viewport needed to project the vectors onto the screen
665
- const worldMatrix = mesh.getWorldMatrix();
666
- const transformMatrix = scene.getTransformMatrix();
667
- const viewport = scene.activeCamera.viewport;
668
- // loop though all the vectors and project them against the current camera viewport to get a set of coordinates
669
- const coordinates = meshVectors.map(vector => {
670
- const projection = math_vector_1.Vector3.Project(vector, worldMatrix, transformMatrix, viewport);
671
- projection.x = projection.x * canvas.clientWidth;
672
- projection.y = projection.y * canvas.clientHeight;
673
- return projection;
674
- });
675
- // get the min and max for all the coordinates so we can calculate the largest possible screen size
676
- const maxX = Math.max(...coordinates.map(o => o.x));
677
- const minX = Math.min(...coordinates.map(o => o.x));
678
- const maxY = Math.max(...coordinates.map(o => o.y));
679
- const minY = Math.min(...coordinates.map(o => o.y));
680
- // return a ClientRect from this
681
- return {
682
- width: maxX - minX,
683
- height: maxY - minY,
684
- left: minX,
685
- top: minY,
686
- right: maxX,
687
- bottom: maxY,
688
- };
689
- };
690
- exports.getClientRectFromMesh = getClientRectFromMesh;
691
- /**
692
- * This type guard checks whether the given `BaseTextures` is any of its subtypes which comes with an
693
- * `onLoadObservable`.
694
- *
695
- * !!! Timing of when this function is called is important !!!
696
- * See the following for more details: https://forum.babylonjs.com/t/basetexture-whenallready-returns-too-early/34501/6
697
- */
698
- const isTextureWithOnLoadObservable = function (texture) {
699
- return !!texture.onLoadObservable;
700
- };
701
- exports.isTextureWithOnLoadObservable = isTextureWithOnLoadObservable;
702
- /**
703
- * @param objects
704
- * @param tagMapping
705
- */
706
- const mapTags = function (objects, tagMapping) {
707
- var _a;
708
- let affectedTags = [];
709
- for (const object of objects) {
710
- const oldTags = Object.keys((_a = tags_1.Tags.GetTags(object, false)) !== null && _a !== void 0 ? _a : {});
711
- const mappedTags = oldTags.map(t => tagMapping[t] || t);
712
- tags_1.Tags.RemoveTagsFrom(object, oldTags.join(' '));
713
- tags_1.Tags.AddTagsTo(object, mappedTags.join(' '));
714
- affectedTags = [...affectedTags, ...mappedTags];
715
- }
716
- return (0, lodash_es_1.uniq)(affectedTags);
717
- };
718
- exports.mapTags = mapTags;
719
- /**
720
- * Gets an array of ids for nodeIds that exist in both TransformNode arrays.
721
- * @param nodes1
722
- * @param nodes2
723
- * @param predicate
724
- */
725
- const intersectingNodeNames = function (nodes1, nodes2, predicate) {
726
- const nodeNames1 = nodes1.filter(n => !predicate || predicate(n)).map(n => n.name);
727
- const nodesNames2 = nodes2.filter(n => !predicate || predicate(n)).map(n => n.name);
728
- return nodeNames1.filter(n1 => nodesNames2.includes(n1));
729
- };
730
- exports.intersectingNodeNames = intersectingNodeNames;
731
- /**
732
- * Gets an array of ids for duplicate nodeIds in given node array.
733
- * @param nodes
734
- * @param predicate
735
- */
736
- const duplicateNodeNames = function (nodes, predicate) {
737
- const nodeNames = nodes.filter(n => !predicate || predicate(n)).map(n => n.name);
738
- return nodeNames.filter((v, i, self) => self.indexOf(v) !== i);
739
- };
740
- exports.duplicateNodeNames = duplicateNodeNames;
741
- /**
742
- * Reports duplicate nodeIds.
743
- * @param nodeNames
744
- */
745
- const reportDuplicateNodeNames = function (nodeNames) {
746
- var _a;
747
- if (nodeNames.length > 0) {
748
- const warn = `There are duplicate nodeNames: ${nodeNames.join(', ')}`;
749
- console.warn(`!!! Warning !!!\n${warn}`);
750
- (_a = window.Cbn) === null || _a === void 0 ? void 0 : _a.Assets.reportDuplicateNodeName(nodeNames);
751
- }
752
- };
753
- exports.reportDuplicateNodeNames = reportDuplicateNodeNames;
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.reportDuplicateNodeNames = exports.duplicateNodeNames = exports.intersectingNodeNames = exports.mapTags = exports.envHelperMetadataName = exports.backgroundDomeName = exports.changeEnvironment = exports.getClientRectFromMesh = exports.removeFromShadowGenerator = exports.addToShadowGenerator = exports.setReceiveShadows = exports.removeFromHighlightLayer = exports.addToHighlightLayer = exports.setMaterialRoughness = exports.setMaterialMetallness = exports.setMaterialTexture = exports.setMaterialColor = exports.setSourceNodeMaterial = exports.setMaterial = exports.transformTransformNode = exports.disableNodeWithParents = exports.enableNodeWithParents = exports.deactivateTransformNode = exports.activateTransformNode = exports.assertMeshCapability = exports.assertTransformNode = exports.injectNodeMetadata = exports.applyMaterial = exports.getOrCreateMaterial = exports.cloneTransformNodeMaterial = exports.cloneNodeWithParents = exports.cloneTransformNode = exports.getDottedPathForNode = exports.mapToDottedNodes = exports.isTextureWithOnLoadObservable = exports.getRootNode = void 0;
13
+ const dottedPath_1 = require("../classes/dottedPath");
14
+ const event_1 = require("../classes/event");
15
+ const sceneSetup_1 = require("../internal/sceneSetup");
16
+ const sceneLoaderHelper_1 = require("./sceneLoaderHelper");
17
+ const photoDome_1 = require("@babylonjs/core/Helpers/photoDome");
18
+ const light_1 = require("@babylonjs/core/Lights/light");
19
+ const baseTexture_1 = require("@babylonjs/core/Materials/Textures/baseTexture");
20
+ const cubeTexture_1 = require("@babylonjs/core/Materials/Textures/cubeTexture");
21
+ const math_axis_1 = require("@babylonjs/core/Maths/math.axis");
22
+ const math_color_1 = require("@babylonjs/core/Maths/math.color");
23
+ const math_vector_1 = require("@babylonjs/core/Maths/math.vector");
24
+ const abstractMesh_1 = require("@babylonjs/core/Meshes/abstractMesh");
25
+ const instancedMesh_1 = require("@babylonjs/core/Meshes/instancedMesh");
26
+ const transformNode_1 = require("@babylonjs/core/Meshes/transformNode");
27
+ const tags_1 = require("@babylonjs/core/Misc/tags");
28
+ const tools_1 = require("@babylonjs/core/Misc/tools");
29
+ const lodash_es_1 = require("lodash-es");
30
+ const backgroundDomeName = 'BackgroundDome_ViewerGenerated';
31
+ exports.backgroundDomeName = backgroundDomeName;
32
+ const envHelperMetadataName = 'viewerEnvHelper';
33
+ exports.envHelperMetadataName = envHelperMetadataName;
34
+ const materialWaitingToBeSetMetadataName = 'materialWaitingToBeSet';
35
+ /**
36
+ * @param node
37
+ * @return Node
38
+ */
39
+ const getRootNode = function (node) {
40
+ let _node = node;
41
+ while (_node.parent) {
42
+ _node = _node.parent;
43
+ }
44
+ return _node;
45
+ };
46
+ exports.getRootNode = getRootNode;
47
+ /**
48
+ * @param nodes
49
+ * @param predicate
50
+ * @return Map<DottedPath, T>
51
+ */
52
+ const mapToDottedNodes = function (nodes, predicate) {
53
+ const map = new Map();
54
+ const addNodes = function (_node) {
55
+ if (predicate && predicate(_node)) {
56
+ map.set(_node.metadata.dottedPath, _node);
57
+ }
58
+ _node.getChildren().forEach(child => {
59
+ addNodes(child);
60
+ });
61
+ };
62
+ nodes.forEach(node => {
63
+ addNodes(node);
64
+ });
65
+ return map;
66
+ };
67
+ exports.mapToDottedNodes = mapToDottedNodes;
68
+ /**
69
+ * @param node
70
+ * @return DottedPath
71
+ */
72
+ const getDottedPathForNode = function (node) {
73
+ const dottedPath = dottedPath_1.DottedPath.create(node.name);
74
+ let _parent = node;
75
+ while (_parent.parent) {
76
+ _parent = _parent.parent;
77
+ dottedPath.unshiftPart(_parent.name);
78
+ }
79
+ return dottedPath;
80
+ };
81
+ exports.getDottedPathForNode = getDottedPathForNode;
82
+ /**
83
+ * @param node
84
+ * @param nodeNamingStrategy
85
+ * @param predicate
86
+ * @param deep
87
+ * @return TransformNode | null
88
+ */
89
+ const cloneTransformNode = function (node, nodeNamingStrategy, predicate, deep = true) {
90
+ if (predicate && !predicate(node)) {
91
+ return null;
92
+ }
93
+ const newName = nodeNamingStrategy.handler(node, nodeNamingStrategy.payload);
94
+ const clone = node.clone(newName, node.parent, true);
95
+ if (clone) {
96
+ clone.id = newName;
97
+ clone.metadata = (0, lodash_es_1.cloneDeep)(node.metadata);
98
+ injectNodeMetadata(clone, { cloneSource: node }, false);
99
+ // if the cloned node is of type InstancedMesh, due to a bug(?),
100
+ // the InstancedMesh.isEnabled state may have changed after cloning.
101
+ // in that case, set the clone's enabled state to the original's state
102
+ if (node.constructor.name === 'InstancedMesh') {
103
+ clone.setEnabled(node.isEnabled(false));
104
+ }
105
+ // copying tags is a temporary workaround, as it doesn't work for TransformNodes ATM
106
+ // see https://forum.babylonjs.com/t/transformnode-clone-doesnt-contain-original-tags/38429/1
107
+ if (tags_1.Tags === null || tags_1.Tags === void 0 ? void 0 : tags_1.Tags.HasTags(node)) {
108
+ tags_1.Tags.AddTagsTo(clone, tags_1.Tags.GetTags(node, true));
109
+ }
110
+ }
111
+ if (deep) {
112
+ const children = node.getChildTransformNodes(true);
113
+ children.forEach(child => {
114
+ const clonedChild = cloneTransformNode(child, nodeNamingStrategy, predicate, deep);
115
+ if (clonedChild) {
116
+ clonedChild.parent = clone;
117
+ }
118
+ });
119
+ }
120
+ return clone;
121
+ };
122
+ exports.cloneTransformNode = cloneTransformNode;
123
+ /**
124
+ * @param node
125
+ */
126
+ const cloneNodeWithParents = function (node) {
127
+ let clone = null;
128
+ if (node instanceof transformNode_1.TransformNode) {
129
+ clone = node.clone(node.name, cloneNodeWithParents(node.parent), true);
130
+ }
131
+ else if (node instanceof light_1.Light) {
132
+ clone = node.clone(node.name, cloneNodeWithParents(node.parent));
133
+ }
134
+ else if (node) {
135
+ throw new Error(`Cloning of "${node === null || node === void 0 ? void 0 : node.constructor.name}" is not implemented (yet).`);
136
+ }
137
+ return clone;
138
+ };
139
+ exports.cloneNodeWithParents = cloneNodeWithParents;
140
+ /**
141
+ * @param node
142
+ * @param deep
143
+ * @param prefix
144
+ * @return TransformNode
145
+ */
146
+ const cloneTransformNodeMaterial = function (node, prefix = '', deep = true) {
147
+ if (node instanceof abstractMesh_1.AbstractMesh && node.material) {
148
+ const newMatName = dottedPath_1.DottedPath.create(prefix).addParts([node.material.name, 'clone', node.uniqueId.toString()]);
149
+ node.material = node.material.clone(newMatName.path);
150
+ }
151
+ if (deep) {
152
+ const children = node.getChildTransformNodes(true);
153
+ children.forEach(child => cloneTransformNodeMaterial(child, prefix, deep));
154
+ }
155
+ return node;
156
+ };
157
+ exports.cloneTransformNodeMaterial = cloneTransformNodeMaterial;
158
+ /**
159
+ * @param node
160
+ * @param deep
161
+ * @param metadata
162
+ */
163
+ const injectNodeMetadata = function (node, metadata, deep = true) {
164
+ node.metadata = (0, lodash_es_1.merge)({}, node.metadata, metadata);
165
+ if (deep && node instanceof transformNode_1.TransformNode) {
166
+ const children = node.getChildTransformNodes(true);
167
+ children.forEach(child => injectNodeMetadata(child, metadata, deep));
168
+ }
169
+ };
170
+ exports.injectNodeMetadata = injectNodeMetadata;
171
+ /**
172
+ * @param node
173
+ * @param assertCallable
174
+ * @param callableParameters
175
+ * @param deep
176
+ */
177
+ const assertTransformNode = function (node, assertCallable, callableParameters = [], deep = true) {
178
+ assertCallable(node, ...callableParameters);
179
+ if (deep) {
180
+ const children = node.getChildTransformNodes(true);
181
+ children.forEach(child => assertTransformNode(child, assertCallable, callableParameters, deep));
182
+ }
183
+ };
184
+ exports.assertTransformNode = assertTransformNode;
185
+ /**
186
+ * @param node
187
+ * @param parameter
188
+ */
189
+ const assertMeshCapability = function (node, parameter) {
190
+ if (!(node instanceof abstractMesh_1.AbstractMesh)) {
191
+ const cls = node.getClassName();
192
+ throw new Error(`Changing parameter "${parameter}" of a(n) ${cls} is not supported. Tried to change node "${node.id}".`);
193
+ }
194
+ if (node instanceof instancedMesh_1.InstancedMesh) {
195
+ throw new Error(`Changing parameter "${parameter}" of an InstancedMesh is not supported. Tried to change node "${node.id}".`);
196
+ }
197
+ };
198
+ exports.assertMeshCapability = assertMeshCapability;
199
+ /**
200
+ * @param node
201
+ * @param deep
202
+ */
203
+ const activateTransformNode = function (node, deep = true) {
204
+ node.setEnabled(true);
205
+ /*
206
+ if( node instanceof AbstractMesh ) {
207
+ node.visibility = 1;
208
+ node.isPickable = true;
209
+ }
210
+ */
211
+ if (deep) {
212
+ node.getChildTransformNodes(true).forEach(child => activateTransformNode(child, deep));
213
+ }
214
+ };
215
+ exports.activateTransformNode = activateTransformNode;
216
+ /**
217
+ * @param node
218
+ * @param deep
219
+ */
220
+ const deactivateTransformNode = function (node, deep = true) {
221
+ node.setEnabled(false);
222
+ /*
223
+ if( node instanceof AbstractMesh ) {
224
+ node.visibility = 0;
225
+ node.isPickable = false;
226
+ }
227
+ */
228
+ if (deep) {
229
+ node.getChildTransformNodes(true).forEach(child => deactivateTransformNode(child, deep));
230
+ }
231
+ };
232
+ exports.deactivateTransformNode = deactivateTransformNode;
233
+ /**
234
+ * @param node
235
+ */
236
+ const enableNodeWithParents = function (node) {
237
+ node.setEnabled(true);
238
+ if (node.parent) {
239
+ enableNodeWithParents(node.parent);
240
+ }
241
+ };
242
+ exports.enableNodeWithParents = enableNodeWithParents;
243
+ /**
244
+ * @param node
245
+ */
246
+ const disableNodeWithParents = function (node) {
247
+ node.setEnabled(false);
248
+ if (node.parent) {
249
+ disableNodeWithParents(node.parent);
250
+ }
251
+ };
252
+ exports.disableNodeWithParents = disableNodeWithParents;
253
+ /**
254
+ * Applies a {@link TransformationDefinition} consecutively to ensure dependencies in positioning etc.
255
+ * @param node
256
+ * @param transformation
257
+ */
258
+ const transformTransformNode = function (node, transformation) {
259
+ // scaling
260
+ if (!(0, lodash_es_1.has)(node.metadata, 'scaling.initial')) {
261
+ injectNodeMetadata(node, { 'scaling.initial': node.scaling }, false);
262
+ }
263
+ const initialScaling = (0, lodash_es_1.get)(node.metadata, 'scaling.initial');
264
+ node.scaling = initialScaling.multiply(transformation.scaling);
265
+ // position
266
+ if (!(0, lodash_es_1.has)(node.metadata, 'position.initial')) {
267
+ injectNodeMetadata(node, { 'position.initial': node.absolutePosition.clone() }, false);
268
+ }
269
+ const initialPosition = (0, lodash_es_1.get)(node.metadata, 'position.initial');
270
+ node.setAbsolutePosition(initialPosition.add(transformation.position).multiply(transformation.scaling));
271
+ // rotation
272
+ if (!(0, lodash_es_1.has)(node.metadata, 'rotation.initial')) {
273
+ let rotationQuaternion = node.rotationQuaternion;
274
+ if (!rotationQuaternion) {
275
+ rotationQuaternion = math_vector_1.Quaternion.RotationYawPitchRoll(node.rotation.x, node.rotation.y, node.rotation.z);
276
+ }
277
+ injectNodeMetadata(node, { 'rotation.initial': rotationQuaternion.asArray() }, false);
278
+ }
279
+ const initialRotationQuaternion = math_vector_1.Quaternion.FromArray((0, lodash_es_1.get)(node.metadata, 'rotation.initial'));
280
+ node.rotationQuaternion = initialRotationQuaternion;
281
+ node.rotateAround(math_vector_1.Vector3.Zero(), math_axis_1.Axis.X, transformation.rotation.x);
282
+ node.rotateAround(math_vector_1.Vector3.Zero(), math_axis_1.Axis.Y, transformation.rotation.y);
283
+ node.rotateAround(math_vector_1.Vector3.Zero(), math_axis_1.Axis.Z, transformation.rotation.z);
284
+ node.computeWorldMatrix(true);
285
+ };
286
+ exports.transformTransformNode = transformTransformNode;
287
+ /**
288
+ * Apply changes of environment (background texture, etc.) consecutively in order to avoid dependency related issues.
289
+ * @param scene
290
+ * @param envDef
291
+ */
292
+ const changeEnvironment = function (scene, envDef) {
293
+ var _a;
294
+ // issue warning if both background texture and usedefault are set
295
+ if (envDef.environmentUseDefault && envDef.environmentBackground) {
296
+ console.warn(`!!! Warning !!! Spec parameter 'environment.usedefault' is being overruled by 'environment.background'.`);
297
+ }
298
+ const useDefaultEnv = envDef.environmentUseDefault && !envDef.environmentBackground;
299
+ // 1) set ENVIRONMENT_COLOR
300
+ const clearColorBefore = scene.clearColor.toString();
301
+ scene.clearColor = envDef.environmentColor
302
+ ? math_color_1.Color4.FromColor3(envDef.environmentColor)
303
+ : useDefaultEnv
304
+ ? sceneSetup_1.defaultEnvHelperColor
305
+ : sceneSetup_1.defaultSceneClearColor;
306
+ const clearColorChanged = clearColorBefore !== scene.clearColor.toString();
307
+ // 2) dispose existing & set new ENVIRONMENT (==texture)
308
+ const curEnvTexture = scene.environmentTexture;
309
+ const envTextureChanged = envDef.environment !== (curEnvTexture === null || curEnvTexture === void 0 ? void 0 : curEnvTexture.url);
310
+ if (curEnvTexture && (!envDef.environment || envTextureChanged)) {
311
+ curEnvTexture.dispose();
312
+ }
313
+ const rotation = envDef.environmentRotation !== undefined ? tools_1.Tools.ToRadians(envDef.environmentRotation) : 0;
314
+ if (envDef.environment && envTextureChanged) {
315
+ const envTexture = cubeTexture_1.CubeTexture.CreateFromPrefilteredData(envDef.environment, scene);
316
+ envTexture.rotationY = rotation;
317
+ scene.environmentTexture = envTexture;
318
+ }
319
+ else if (curEnvTexture && curEnvTexture.rotationY !== rotation) {
320
+ curEnvTexture.rotationY = rotation;
321
+ }
322
+ // 3) dispose existing & set new ENVIRONMENT_BACKGROUND
323
+ const curDome = scene.getNodeByName(backgroundDomeName);
324
+ const backgroundUrlChanged = envDef.environmentBackground !== (curDome === null || curDome === void 0 ? void 0 : curDome.texture.url);
325
+ if (curDome && (!envDef.environmentBackground || backgroundUrlChanged)) {
326
+ curDome.dispose();
327
+ }
328
+ const rotationPhotoDome = -1 * rotation;
329
+ if (envDef.environmentBackground && backgroundUrlChanged) {
330
+ const textureUrl = envDef.environmentBackground;
331
+ const dome = new photoDome_1.PhotoDome(backgroundDomeName, textureUrl, { resolution: 32, size: 450 }, scene);
332
+ // Rotate submesh to get them in line with environment texture
333
+ dome.getChildMeshes(true)[0].rotation.y = tools_1.Tools.ToRadians(90);
334
+ dome.rotation.y = rotationPhotoDome;
335
+ }
336
+ else if (curDome && curDome.rotation.y !== rotationPhotoDome) {
337
+ curDome.rotation.y = rotationPhotoDome;
338
+ }
339
+ // 4) dispose existing & set new ENVIRONMENT_USEDEFAULT (only if no background is set)
340
+ const curEnvHelper = (_a = scene.metadata) === null || _a === void 0 ? void 0 : _a[envHelperMetadataName];
341
+ if (curEnvHelper && !useDefaultEnv) {
342
+ curEnvHelper.dispose();
343
+ delete scene.metadata[envHelperMetadataName];
344
+ }
345
+ const envHelperMainColor = math_color_1.Color3.FromArray(scene.clearColor.asArray());
346
+ if (useDefaultEnv && !curEnvHelper) {
347
+ const envHelper = scene.createDefaultEnvironment({ sizeAuto: true });
348
+ envHelper === null || envHelper === void 0 ? void 0 : envHelper.setMainColor(envHelperMainColor);
349
+ if (envHelper) {
350
+ scene.metadata = (0, lodash_es_1.merge)({}, scene.metadata, { [envHelperMetadataName]: envHelper });
351
+ }
352
+ }
353
+ else if (curEnvHelper && useDefaultEnv && clearColorChanged) {
354
+ curEnvHelper.setMainColor(envHelperMainColor);
355
+ }
356
+ // 5) set ENVIRONMENT_INTENSITY
357
+ if (envDef.environmentIntensity !== undefined) {
358
+ scene.environmentIntensity = envDef.environmentIntensity;
359
+ }
360
+ };
361
+ exports.changeEnvironment = changeEnvironment;
362
+ /**
363
+ * Sets a material by a given material id as material property on the given node.
364
+ *
365
+ * If the material is not already available in the scene, the viewer tries to create a material based on a Combeenation
366
+ * [material asset](https://docs.combeenation.com/docs/howto-create-and-use-babylon-and-material-asset).
367
+ * This of course only works if the viewer is used inside a Combeenation configurator.
368
+ *
369
+ * Furthermore this function also defers the material creation if the node is not visible yet to improve network &
370
+ * viewer bootstrap performance as textures are automatically being lazy loaded only when they are actually visible in
371
+ * the scene.
372
+ *
373
+ * Finally the material will not be applied before all its textures have been loaded. In this way "flickering" effects
374
+ * will be avoided, since the material would be incomplete without its loaded textures.
375
+ */
376
+ const setMaterial = function (node, materialId, deep = true, variant) {
377
+ if (node instanceof abstractMesh_1.AbstractMesh) {
378
+ const materialExists = node.getScene().getMaterialById(materialId);
379
+ const hasMissingMaterial = (0, lodash_es_1.has)(node.metadata, sceneLoaderHelper_1.missingMaterialMetadataName);
380
+ const deferMaterialCreation = !materialExists && !node.isEnabled();
381
+ if (deferMaterialCreation) {
382
+ // do not set the material
383
+ injectNodeMetadata(node, { [sceneLoaderHelper_1.missingMaterialMetadataName]: materialId }, false);
384
+ // if it already had the missing material flag before, there already exists an observer...
385
+ if (!hasMissingMaterial) {
386
+ (0, sceneLoaderHelper_1.addMissingMaterialObserver)(node);
387
+ }
388
+ }
389
+ else {
390
+ // create material and apply it when textures have been loaded
391
+ getOrCreateMaterial(node.getScene(), materialId, variant).then(material => {
392
+ applyMaterial(material, node).then(() => event_1.emitter.emit(event_1.Event.MESH_MATERIAL_APPLIED, node, material));
393
+ if (hasMissingMaterial) {
394
+ delete node.metadata[sceneLoaderHelper_1.missingMaterialMetadataName];
395
+ }
396
+ });
397
+ }
398
+ }
399
+ // recursively set materials on children (if desired)
400
+ if (deep) {
401
+ for (const child of node.getChildTransformNodes(true)) {
402
+ setMaterial(child, materialId, deep, variant);
403
+ }
404
+ }
405
+ };
406
+ exports.setMaterial = setMaterial;
407
+ /**
408
+ * Gets the Material either from the given {@link Variant}, the given scene or tries to create it from an Combeenation
409
+ * material asset when inside a Combeenation configurator.
410
+ */
411
+ const getOrCreateMaterial = function (scene, materialId, variant) {
412
+ return __awaiter(this, void 0, void 0, function* () {
413
+ let chosenMaterial;
414
+ chosenMaterial = variant === null || variant === void 0 ? void 0 : variant.inheritedMaterials.find(mat => mat.id === materialId);
415
+ chosenMaterial = chosenMaterial || scene.materials.find(mat => mat.id === materialId);
416
+ chosenMaterial = chosenMaterial || (yield (0, sceneLoaderHelper_1.createMaterialFromCbnAssets)(materialId, scene));
417
+ if (chosenMaterial) {
418
+ return chosenMaterial;
419
+ }
420
+ // reject when material was not found
421
+ let rejectMessage = `Material with id "${materialId}" does not exist on scene.`;
422
+ if (variant) {
423
+ rejectMessage = `Material with id "${materialId}" does not exist for variant "${variant.id}".`;
424
+ }
425
+ throw new Error(rejectMessage);
426
+ });
427
+ };
428
+ exports.getOrCreateMaterial = getOrCreateMaterial;
429
+ /**
430
+ * Waits until the materials textures are loaded and shaders are compiled.
431
+ * Then sets the material on the node if there is no newer "set material" request
432
+ */
433
+ const applyMaterial = function (material, node) {
434
+ var _a;
435
+ return __awaiter(this, void 0, void 0, function* () {
436
+ // set current material id as last valid id, in this case all previously set materials on the node will be invalidated
437
+ injectNodeMetadata(node, { [materialWaitingToBeSetMetadataName]: material.id }, false);
438
+ const promTexturesReady = new Promise(resolve => baseTexture_1.BaseTexture.WhenAllReady(material.getActiveTextures(), resolve));
439
+ // this promise should only take some time on the first call of the corresponding shader (eg: PBRMaterial shader)
440
+ // on each other call of the same material/shader type there should be not be a waiting time, or at maximum one frame
441
+ // https://forum.babylonjs.com/t/mesh-flickering-when-setting-material-initially/37312
442
+ const promMaterialCompiled = material.forceCompilationAsync(node);
443
+ // material needs to fulfill 2 criterias before it's ready to use
444
+ // - textures need to be "ready" => downloaded
445
+ // - dedicated shader needs to be compiled
446
+ // if this would not be the case you can see some "flickering" when setting the material
447
+ yield Promise.all([promTexturesReady, promMaterialCompiled]);
448
+ // textures ready, now check if the material is still up-to-date
449
+ if (material.id === ((_a = node.metadata) === null || _a === void 0 ? void 0 : _a[materialWaitingToBeSetMetadataName])) {
450
+ node.material = material;
451
+ delete node.metadata[materialWaitingToBeSetMetadataName];
452
+ }
453
+ });
454
+ };
455
+ exports.applyMaterial = applyMaterial;
456
+ /**
457
+ * !!! Warning !!!
458
+ * This function is not public API. Whilst it can help solving certain problems, it only works reliably in well defined
459
+ * situations and can cause unwanted side effects under some conditions. Use carefully at your own risk!
460
+ *
461
+ * See https://combeenation.myjetbrains.com/youtrack/issue/CB-5906 for further details regarding this warning.
462
+ *
463
+ * Set material of an instanced meshes source mesh.
464
+ * Changes the material of all instanced meshes which have the same source mesh.
465
+ *
466
+ * @param node
467
+ * @param material
468
+ * @param deep
469
+ *
470
+ * @ignore
471
+ */
472
+ const setSourceNodeMaterial = function (node, material, deep = true) {
473
+ const warn = ` You're using "setSourceNodeMaterial" which is not public API.
474
+ Whilst it can help solving certain problems, it only works reliably in well defined situations and can cause unwanted side effects under some conditions.
475
+ Use carefully at your own risk!`;
476
+ console.warn(`!!! Warning !!!\n${warn}`);
477
+ if (node instanceof instancedMesh_1.InstancedMesh) {
478
+ node.sourceMesh.material = material;
479
+ }
480
+ if (deep) {
481
+ node.getChildTransformNodes(true).forEach(child => setSourceNodeMaterial(child, material, deep));
482
+ }
483
+ };
484
+ exports.setSourceNodeMaterial = setSourceNodeMaterial;
485
+ /**
486
+ * @param node
487
+ * @param color
488
+ * @param deep
489
+ */
490
+ const setMaterialColor = function (node, color, deep = true) {
491
+ if (node instanceof abstractMesh_1.AbstractMesh && node.material) {
492
+ const materialCls = node.material.getClassName();
493
+ switch (materialCls) {
494
+ case 'PBRMaterial':
495
+ node.material.albedoColor = color.toLinearSpace();
496
+ break;
497
+ case 'StandardMaterial':
498
+ node.material.diffuseColor = color;
499
+ break;
500
+ default:
501
+ throw new Error(`Setting color for material of instance "${materialCls}" not implemented (yet).`);
502
+ }
503
+ setMaterial(node, node.material.id, false);
504
+ }
505
+ if (deep) {
506
+ node.getChildTransformNodes(true).forEach(child => setMaterialColor(child, color, deep));
507
+ }
508
+ };
509
+ exports.setMaterialColor = setMaterialColor;
510
+ /**
511
+ * @param node
512
+ * @param texture
513
+ * @param deep
514
+ */
515
+ const setMaterialTexture = function (node, texture, deep = true) {
516
+ if (node instanceof abstractMesh_1.AbstractMesh && node.material) {
517
+ const materialCls = node.material.getClassName();
518
+ switch (materialCls) {
519
+ case 'PBRMaterial':
520
+ node.material.albedoTexture = texture;
521
+ break;
522
+ case 'StandardMaterial':
523
+ node.material.diffuseTexture = texture;
524
+ break;
525
+ default:
526
+ throw new Error(`Setting texture for material of instance "${materialCls}" not implemented (yet).`);
527
+ }
528
+ setMaterial(node, node.material.id, false);
529
+ }
530
+ if (deep) {
531
+ node.getChildTransformNodes(true).forEach(child => setMaterialTexture(child, texture, deep));
532
+ }
533
+ };
534
+ exports.setMaterialTexture = setMaterialTexture;
535
+ /**
536
+ * @param node
537
+ * @param metallness
538
+ * @param deep
539
+ */
540
+ const setMaterialMetallness = function (node, metallness, deep = true) {
541
+ if (node instanceof abstractMesh_1.AbstractMesh && node.material) {
542
+ const materialCls = node.material.getClassName();
543
+ switch (materialCls) {
544
+ case 'PBRMaterial':
545
+ node.material.metallic = metallness;
546
+ break;
547
+ default:
548
+ throw new Error(`Setting metallness for material of instance "${materialCls}" not implemented (yet).`);
549
+ }
550
+ setMaterial(node, node.material.id, false);
551
+ }
552
+ if (deep) {
553
+ node.getChildTransformNodes(true).forEach(child => setMaterialMetallness(child, metallness, deep));
554
+ }
555
+ };
556
+ exports.setMaterialMetallness = setMaterialMetallness;
557
+ /**
558
+ * @param node
559
+ * @param roughness
560
+ * @param deep
561
+ */
562
+ const setMaterialRoughness = function (node, roughness, deep = true) {
563
+ if (node instanceof abstractMesh_1.AbstractMesh && node.material) {
564
+ const materialCls = node.material.getClassName();
565
+ switch (materialCls) {
566
+ case 'PBRMaterial':
567
+ node.material.roughness = roughness;
568
+ break;
569
+ case 'StandardMaterial':
570
+ node.material.roughness = roughness;
571
+ break;
572
+ default:
573
+ throw new Error(`Setting roughness for material of instance "${materialCls}" not implemented (yet).`);
574
+ }
575
+ setMaterial(node, node.material.id, false);
576
+ }
577
+ if (deep) {
578
+ node.getChildTransformNodes(true).forEach(child => setMaterialRoughness(child, roughness, deep));
579
+ }
580
+ };
581
+ exports.setMaterialRoughness = setMaterialRoughness;
582
+ /**
583
+ * @param node
584
+ * @param layer
585
+ * @param color
586
+ * @param deep
587
+ */
588
+ const addToHighlightLayer = function (layer, color, node, deep = true) {
589
+ if (node instanceof abstractMesh_1.AbstractMesh) {
590
+ layer.addMesh(node, color);
591
+ }
592
+ if (deep) {
593
+ node.getChildTransformNodes(true).forEach(child => addToHighlightLayer(layer, color, child, deep));
594
+ }
595
+ };
596
+ exports.addToHighlightLayer = addToHighlightLayer;
597
+ /**
598
+ * @param node
599
+ * @param layer
600
+ * @param deep
601
+ */
602
+ const removeFromHighlightLayer = function (layer, node, deep = true) {
603
+ if (node instanceof abstractMesh_1.AbstractMesh) {
604
+ layer.removeMesh(node);
605
+ }
606
+ if (deep) {
607
+ node.getChildTransformNodes(true).forEach(child => removeFromHighlightLayer(layer, child, deep));
608
+ }
609
+ };
610
+ exports.removeFromHighlightLayer = removeFromHighlightLayer;
611
+ /**
612
+ * @param node
613
+ * @param receiveShadows
614
+ * @param deep
615
+ */
616
+ const setReceiveShadows = function (node, receiveShadows, deep = true) {
617
+ if (node instanceof abstractMesh_1.AbstractMesh) {
618
+ node.receiveShadows = receiveShadows;
619
+ }
620
+ if (deep) {
621
+ node.getChildTransformNodes(true).forEach(child => setReceiveShadows(child, receiveShadows, deep));
622
+ }
623
+ };
624
+ exports.setReceiveShadows = setReceiveShadows;
625
+ /**
626
+ * @param node
627
+ * @param generator
628
+ * @param deep
629
+ */
630
+ const addToShadowGenerator = function (generator, node, deep = true) {
631
+ if (node instanceof abstractMesh_1.AbstractMesh) {
632
+ // We have to remove the node because there's no duplicate check in babylon
633
+ generator.removeShadowCaster(node, false);
634
+ generator.addShadowCaster(node, false);
635
+ }
636
+ if (deep) {
637
+ node.getChildTransformNodes(true).forEach(child => addToShadowGenerator(generator, child, deep));
638
+ }
639
+ };
640
+ exports.addToShadowGenerator = addToShadowGenerator;
641
+ /**
642
+ * @param node
643
+ * @param generator
644
+ * @param deep
645
+ */
646
+ const removeFromShadowGenerator = function (generator, node, deep = true) {
647
+ if (node instanceof abstractMesh_1.AbstractMesh) {
648
+ generator.removeShadowCaster(node, false);
649
+ }
650
+ if (deep) {
651
+ node.getChildTransformNodes(true).forEach(child => removeFromShadowGenerator(generator, child, deep));
652
+ }
653
+ };
654
+ exports.removeFromShadowGenerator = removeFromShadowGenerator;
655
+ /**
656
+ * https://forum.babylonjs.com/t/get-mesh-bounding-box-position-and-size-in-2d-screen-coordinates/1058/3
657
+ * @param mesh
658
+ * @param scene
659
+ * @param canvas
660
+ */
661
+ const getClientRectFromMesh = function (mesh, scene, canvas) {
662
+ // get bounding box of the mesh
663
+ const meshVectors = mesh.getBoundingInfo().boundingBox.vectors;
664
+ // get the matrix and viewport needed to project the vectors onto the screen
665
+ const worldMatrix = mesh.getWorldMatrix();
666
+ const transformMatrix = scene.getTransformMatrix();
667
+ const viewport = scene.activeCamera.viewport;
668
+ // loop though all the vectors and project them against the current camera viewport to get a set of coordinates
669
+ const coordinates = meshVectors.map(vector => {
670
+ const projection = math_vector_1.Vector3.Project(vector, worldMatrix, transformMatrix, viewport);
671
+ projection.x = projection.x * canvas.clientWidth;
672
+ projection.y = projection.y * canvas.clientHeight;
673
+ return projection;
674
+ });
675
+ // get the min and max for all the coordinates so we can calculate the largest possible screen size
676
+ const maxX = Math.max(...coordinates.map(o => o.x));
677
+ const minX = Math.min(...coordinates.map(o => o.x));
678
+ const maxY = Math.max(...coordinates.map(o => o.y));
679
+ const minY = Math.min(...coordinates.map(o => o.y));
680
+ // return a ClientRect from this
681
+ return {
682
+ width: maxX - minX,
683
+ height: maxY - minY,
684
+ left: minX,
685
+ top: minY,
686
+ right: maxX,
687
+ bottom: maxY,
688
+ };
689
+ };
690
+ exports.getClientRectFromMesh = getClientRectFromMesh;
691
+ /**
692
+ * This type guard checks whether the given `BaseTextures` is any of its subtypes which comes with an
693
+ * `onLoadObservable`.
694
+ *
695
+ * !!! Timing of when this function is called is important !!!
696
+ * See the following for more details: https://forum.babylonjs.com/t/basetexture-whenallready-returns-too-early/34501/6
697
+ */
698
+ const isTextureWithOnLoadObservable = function (texture) {
699
+ return !!texture.onLoadObservable;
700
+ };
701
+ exports.isTextureWithOnLoadObservable = isTextureWithOnLoadObservable;
702
+ /**
703
+ * @param objects
704
+ * @param tagMapping
705
+ */
706
+ const mapTags = function (objects, tagMapping) {
707
+ var _a;
708
+ let affectedTags = [];
709
+ for (const object of objects) {
710
+ const oldTags = Object.keys((_a = tags_1.Tags.GetTags(object, false)) !== null && _a !== void 0 ? _a : {});
711
+ const mappedTags = oldTags.map(t => tagMapping[t] || t);
712
+ tags_1.Tags.RemoveTagsFrom(object, oldTags.join(' '));
713
+ tags_1.Tags.AddTagsTo(object, mappedTags.join(' '));
714
+ affectedTags = [...affectedTags, ...mappedTags];
715
+ }
716
+ return (0, lodash_es_1.uniq)(affectedTags);
717
+ };
718
+ exports.mapTags = mapTags;
719
+ /**
720
+ * Gets an array of ids for nodeIds that exist in both TransformNode arrays.
721
+ * @param nodes1
722
+ * @param nodes2
723
+ * @param predicate
724
+ */
725
+ const intersectingNodeNames = function (nodes1, nodes2, predicate) {
726
+ const nodeNames1 = nodes1.filter(n => !predicate || predicate(n)).map(n => n.name);
727
+ const nodesNames2 = nodes2.filter(n => !predicate || predicate(n)).map(n => n.name);
728
+ return nodeNames1.filter(n1 => nodesNames2.includes(n1));
729
+ };
730
+ exports.intersectingNodeNames = intersectingNodeNames;
731
+ /**
732
+ * Gets an array of ids for duplicate nodeIds in given node array.
733
+ * @param nodes
734
+ * @param predicate
735
+ */
736
+ const duplicateNodeNames = function (nodes, predicate) {
737
+ const nodeNames = nodes.filter(n => !predicate || predicate(n)).map(n => n.name);
738
+ return nodeNames.filter((v, i, self) => self.indexOf(v) !== i);
739
+ };
740
+ exports.duplicateNodeNames = duplicateNodeNames;
741
+ /**
742
+ * Reports duplicate nodeIds.
743
+ * @param nodeNames
744
+ */
745
+ const reportDuplicateNodeNames = function (nodeNames) {
746
+ var _a;
747
+ if (nodeNames.length > 0) {
748
+ const warn = `There are duplicate nodeNames: ${nodeNames.join(', ')}`;
749
+ console.warn(`!!! Warning !!!\n${warn}`);
750
+ (_a = window.Cbn) === null || _a === void 0 ? void 0 : _a.Assets.reportDuplicateNodeName(nodeNames);
751
+ }
752
+ };
753
+ exports.reportDuplicateNodeNames = reportDuplicateNodeNames;
754
754
  //# sourceMappingURL=babylonHelper.js.map