@combeenation/3d-viewer 6.1.0 → 6.2.0

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 (97) hide show
  1. package/README.md +111 -111
  2. package/dist/lib-cjs/api/classes/animationInterface.d.ts +8 -8
  3. package/dist/lib-cjs/api/classes/animationInterface.js +2 -2
  4. package/dist/lib-cjs/api/classes/dottedPath.d.ts +79 -79
  5. package/dist/lib-cjs/api/classes/dottedPath.js +166 -166
  6. package/dist/lib-cjs/api/classes/element.d.ts +149 -149
  7. package/dist/lib-cjs/api/classes/element.js +669 -669
  8. package/dist/lib-cjs/api/classes/event.d.ts +342 -342
  9. package/dist/lib-cjs/api/classes/event.js +365 -365
  10. package/dist/lib-cjs/api/classes/eventBroadcaster.d.ts +26 -26
  11. package/dist/lib-cjs/api/classes/eventBroadcaster.js +49 -49
  12. package/dist/lib-cjs/api/classes/parameter.d.ts +339 -339
  13. package/dist/lib-cjs/api/classes/parameter.js +464 -464
  14. package/dist/lib-cjs/api/classes/parameterObservable.d.ts +36 -36
  15. package/dist/lib-cjs/api/classes/parameterObservable.js +97 -97
  16. package/dist/lib-cjs/api/classes/parameterizable.d.ts +15 -15
  17. package/dist/lib-cjs/api/classes/parameterizable.js +102 -102
  18. package/dist/lib-cjs/api/classes/placementAnimation.d.ts +45 -45
  19. package/dist/lib-cjs/api/classes/placementAnimation.js +176 -176
  20. package/dist/lib-cjs/api/classes/variant.d.ts +238 -234
  21. package/dist/lib-cjs/api/classes/variant.js +841 -828
  22. package/dist/lib-cjs/api/classes/variant.js.map +1 -1
  23. package/dist/lib-cjs/api/classes/variantInstance.d.ts +44 -44
  24. package/dist/lib-cjs/api/classes/variantInstance.js +105 -105
  25. package/dist/lib-cjs/api/classes/variantParameterizable.d.ts +17 -17
  26. package/dist/lib-cjs/api/classes/variantParameterizable.js +88 -88
  27. package/dist/lib-cjs/api/classes/viewer.d.ts +192 -187
  28. package/dist/lib-cjs/api/classes/viewer.js +639 -594
  29. package/dist/lib-cjs/api/classes/viewer.js.map +1 -1
  30. package/dist/lib-cjs/api/classes/viewerLight.d.ts +66 -66
  31. package/dist/lib-cjs/api/classes/viewerLight.js +348 -348
  32. package/dist/lib-cjs/api/internal/lensRendering.d.ts +8 -8
  33. package/dist/lib-cjs/api/internal/lensRendering.js +11 -11
  34. package/dist/lib-cjs/api/internal/sceneSetup.d.ts +13 -13
  35. package/dist/lib-cjs/api/internal/sceneSetup.js +226 -226
  36. package/dist/lib-cjs/api/manager/animationManager.d.ts +30 -30
  37. package/dist/lib-cjs/api/manager/animationManager.js +126 -126
  38. package/dist/lib-cjs/api/manager/gltfExportManager.d.ts +78 -78
  39. package/dist/lib-cjs/api/manager/gltfExportManager.js +241 -241
  40. package/dist/lib-cjs/api/manager/sceneManager.d.ts +33 -33
  41. package/dist/lib-cjs/api/manager/sceneManager.js +130 -130
  42. package/dist/lib-cjs/api/manager/textureLoadManager.d.ts +22 -22
  43. package/dist/lib-cjs/api/manager/textureLoadManager.js +97 -97
  44. package/dist/lib-cjs/api/manager/variantInstanceManager.d.ts +92 -92
  45. package/dist/lib-cjs/api/manager/variantInstanceManager.js +260 -260
  46. package/dist/lib-cjs/api/store/specStorage.d.ts +24 -24
  47. package/dist/lib-cjs/api/store/specStorage.js +50 -50
  48. package/dist/lib-cjs/api/util/babylonHelper.d.ts +187 -187
  49. package/dist/lib-cjs/api/util/babylonHelper.js +596 -596
  50. package/dist/lib-cjs/api/util/globalTypes.d.ts +387 -383
  51. package/dist/lib-cjs/api/util/globalTypes.js +1 -1
  52. package/dist/lib-cjs/api/util/resourceHelper.d.ts +58 -58
  53. package/dist/lib-cjs/api/util/resourceHelper.js +203 -203
  54. package/dist/lib-cjs/api/util/sceneLoaderHelper.d.ts +42 -42
  55. package/dist/lib-cjs/api/util/sceneLoaderHelper.js +139 -139
  56. package/dist/lib-cjs/api/util/stringHelper.d.ts +9 -9
  57. package/dist/lib-cjs/api/util/stringHelper.js +25 -25
  58. package/dist/lib-cjs/api/util/structureHelper.d.ts +9 -9
  59. package/dist/lib-cjs/api/util/structureHelper.js +48 -48
  60. package/dist/lib-cjs/buildinfo.json +3 -3
  61. package/dist/lib-cjs/commonjs.tsconfig.tsbuildinfo +1 -1
  62. package/dist/lib-cjs/index.d.ts +51 -51
  63. package/dist/lib-cjs/index.js +110 -110
  64. package/package.json +81 -81
  65. package/src/api/classes/animationInterface.ts +10 -10
  66. package/src/api/classes/dottedPath.ts +181 -181
  67. package/src/api/classes/element.ts +717 -717
  68. package/src/api/classes/event.ts +385 -385
  69. package/src/api/classes/eventBroadcaster.ts +52 -52
  70. package/src/api/classes/parameter.ts +497 -497
  71. package/src/api/classes/parameterObservable.ts +100 -100
  72. package/src/api/classes/parameterizable.ts +87 -87
  73. package/src/api/classes/placementAnimation.ts +162 -162
  74. package/src/api/classes/variant.ts +910 -896
  75. package/src/api/classes/variantInstance.ts +97 -97
  76. package/src/api/classes/variantParameterizable.ts +85 -85
  77. package/src/api/classes/viewer.ts +720 -672
  78. package/src/api/classes/viewerLight.ts +339 -339
  79. package/src/api/internal/debugViewer.ts +90 -90
  80. package/src/api/internal/lensRendering.ts +9 -9
  81. package/src/api/internal/sceneSetup.ts +205 -205
  82. package/src/api/manager/animationManager.ts +143 -143
  83. package/src/api/manager/gltfExportManager.ts +236 -236
  84. package/src/api/manager/sceneManager.ts +132 -132
  85. package/src/api/manager/textureLoadManager.ts +95 -95
  86. package/src/api/manager/variantInstanceManager.ts +265 -265
  87. package/src/api/store/specStorage.ts +51 -51
  88. package/src/api/util/babylonHelper.ts +663 -663
  89. package/src/api/util/globalTypes.ts +437 -432
  90. package/src/api/util/resourceHelper.ts +191 -191
  91. package/src/api/util/sceneLoaderHelper.ts +137 -137
  92. package/src/api/util/stringHelper.ts +23 -23
  93. package/src/api/util/structureHelper.ts +49 -49
  94. package/src/buildinfo.json +3 -3
  95. package/src/dev.ts +61 -61
  96. package/src/index.ts +96 -96
  97. package/src/types.d.ts +28 -28
@@ -1,597 +1,597 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- 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.assertTransformNode = exports.injectNodeMetadata = exports.cloneTransformNodeMaterial = exports.cloneNodeWithParents = exports.cloneTransformNode = exports.getDottedPathForNode = exports.mapToDottedNodes = exports.isTextureWithOnLoadObservable = exports.getRootNode = void 0;
4
- const dottedPath_1 = require("../classes/dottedPath");
5
- const sceneSetup_1 = require("../internal/sceneSetup");
6
- const sceneLoaderHelper_1 = require("./sceneLoaderHelper");
7
- const photoDome_1 = require("@babylonjs/core/Helpers/photoDome");
8
- const light_1 = require("@babylonjs/core/Lights/light");
9
- const cubeTexture_1 = require("@babylonjs/core/Materials/Textures/cubeTexture");
10
- const math_axis_1 = require("@babylonjs/core/Maths/math.axis");
11
- const math_color_1 = require("@babylonjs/core/Maths/math.color");
12
- const math_vector_1 = require("@babylonjs/core/Maths/math.vector");
13
- const abstractMesh_1 = require("@babylonjs/core/Meshes/abstractMesh");
14
- const instancedMesh_1 = require("@babylonjs/core/Meshes/instancedMesh");
15
- const transformNode_1 = require("@babylonjs/core/Meshes/transformNode");
16
- const tools_1 = require("@babylonjs/core/Misc/tools");
17
- const lodash_es_1 = require("lodash-es");
18
- const backgroundDomeName = 'BackgroundDome_ViewerGenerated';
19
- exports.backgroundDomeName = backgroundDomeName;
20
- const envHelperMetadataName = 'viewerEnvHelper';
21
- exports.envHelperMetadataName = envHelperMetadataName;
22
- /**
23
- * @param node
24
- * @return Node
25
- */
26
- const getRootNode = function (node) {
27
- let _node = node;
28
- while (_node.parent) {
29
- _node = _node.parent;
30
- }
31
- return _node;
32
- };
33
- exports.getRootNode = getRootNode;
34
- /**
35
- * @param nodes
36
- * @param predicate
37
- * @return Map<DottedPath, T>
38
- */
39
- const mapToDottedNodes = function (nodes, predicate) {
40
- const map = new Map();
41
- const addNodes = function (_node) {
42
- if (predicate && predicate(_node)) {
43
- map.set(_node.metadata.dottedPath, _node);
44
- }
45
- _node.getChildren().forEach(child => {
46
- addNodes(child);
47
- });
48
- };
49
- nodes.forEach(node => {
50
- addNodes(node);
51
- });
52
- return map;
53
- };
54
- exports.mapToDottedNodes = mapToDottedNodes;
55
- /**
56
- * @param node
57
- * @return DottedPath
58
- */
59
- const getDottedPathForNode = function (node) {
60
- const dottedPath = dottedPath_1.DottedPath.create(node.name);
61
- let _parent = node;
62
- while (_parent.parent) {
63
- _parent = _parent.parent;
64
- dottedPath.unshiftPart(_parent.name);
65
- }
66
- return dottedPath;
67
- };
68
- exports.getDottedPathForNode = getDottedPathForNode;
69
- /**
70
- * @param node
71
- * @param predicate
72
- * @param deep
73
- * @return TransformNode | null
74
- */
75
- const cloneTransformNode = function (node, predicate, deep = true) {
76
- if (predicate && !predicate(node)) {
77
- return null;
78
- }
79
- const clone = node.clone(node.name, node.parent, true);
80
- if (clone) {
81
- clone.metadata = (0, lodash_es_1.cloneDeep)(node.metadata);
82
- // if the cloned node is of type InstancedMesh, due to a bug(?),
83
- // the InstancedMesh.isEnabled state may have changed after cloning.
84
- // in that case, set the clone's enabled state to the original's state
85
- if (node.constructor.name === 'InstancedMesh') {
86
- clone.setEnabled(node.isEnabled(false));
87
- }
88
- }
89
- if (deep) {
90
- const children = node.getChildTransformNodes(true);
91
- children.forEach(child => {
92
- const clonedChild = cloneTransformNode(child, predicate, deep);
93
- if (clonedChild) {
94
- clonedChild.parent = clone;
95
- }
96
- });
97
- }
98
- return clone;
99
- };
100
- exports.cloneTransformNode = cloneTransformNode;
101
- /**
102
- * @param node
103
- */
104
- const cloneNodeWithParents = function (node) {
105
- let clone = null;
106
- if (node instanceof transformNode_1.TransformNode) {
107
- clone = node.clone(node.name, cloneNodeWithParents(node.parent), true);
108
- }
109
- else if (node instanceof light_1.Light) {
110
- clone = node.clone(node.name, cloneNodeWithParents(node.parent));
111
- }
112
- else if (node) {
113
- throw new Error(`Cloning of "${node === null || node === void 0 ? void 0 : node.constructor.name}" is not implemented (yet).`);
114
- }
115
- return clone;
116
- };
117
- exports.cloneNodeWithParents = cloneNodeWithParents;
118
- /**
119
- * @param node
120
- * @param deep
121
- * @param prefix
122
- * @return TransformNode
123
- */
124
- const cloneTransformNodeMaterial = function (node, prefix = '', deep = true) {
125
- if (node instanceof abstractMesh_1.AbstractMesh && node.material) {
126
- const newMatName = dottedPath_1.DottedPath.create(prefix).addParts([node.material.name, 'clone', node.uniqueId.toString()]);
127
- node.material = node.material.clone(newMatName.path);
128
- }
129
- if (deep) {
130
- const children = node.getChildTransformNodes(true);
131
- children.forEach(child => cloneTransformNodeMaterial(child, prefix, deep));
132
- }
133
- return node;
134
- };
135
- exports.cloneTransformNodeMaterial = cloneTransformNodeMaterial;
136
- /**
137
- * @param node
138
- * @param deep
139
- * @param metadata
140
- */
141
- const injectNodeMetadata = function (node, metadata, deep = true) {
142
- node.metadata = (0, lodash_es_1.merge)({}, node.metadata, metadata);
143
- if (deep && node instanceof transformNode_1.TransformNode) {
144
- const children = node.getChildTransformNodes(true);
145
- children.forEach(child => injectNodeMetadata(child, metadata, deep));
146
- }
147
- };
148
- exports.injectNodeMetadata = injectNodeMetadata;
149
- /**
150
- * @param node
151
- * @param assertCallable
152
- * @param callableParameters
153
- * @param deep
154
- */
155
- const assertTransformNode = function (node, assertCallable, callableParameters = [], deep = true) {
156
- assertCallable(node, ...callableParameters);
157
- if (deep) {
158
- const children = node.getChildTransformNodes(true);
159
- children.forEach(child => assertTransformNode(child, assertCallable, callableParameters, deep));
160
- }
161
- };
162
- exports.assertTransformNode = assertTransformNode;
163
- /**
164
- * @param node
165
- * @param deep
166
- */
167
- const activateTransformNode = function (node, deep = true) {
168
- node.setEnabled(true);
169
- /*
170
- if( node instanceof AbstractMesh ) {
171
- node.visibility = 1;
172
- node.isPickable = true;
173
- }
174
- */
175
- if (deep) {
176
- node.getChildTransformNodes(true).forEach(child => activateTransformNode(child, deep));
177
- }
178
- };
179
- exports.activateTransformNode = activateTransformNode;
180
- /**
181
- * @param node
182
- * @param deep
183
- */
184
- const deactivateTransformNode = function (node, deep = true) {
185
- node.setEnabled(false);
186
- /*
187
- if( node instanceof AbstractMesh ) {
188
- node.visibility = 0;
189
- node.isPickable = false;
190
- }
191
- */
192
- if (deep) {
193
- node.getChildTransformNodes(true).forEach(child => deactivateTransformNode(child, deep));
194
- }
195
- };
196
- exports.deactivateTransformNode = deactivateTransformNode;
197
- /**
198
- * @param node
199
- */
200
- const enableNodeWithParents = function (node) {
201
- node.setEnabled(true);
202
- if (node.parent) {
203
- enableNodeWithParents(node.parent);
204
- }
205
- };
206
- exports.enableNodeWithParents = enableNodeWithParents;
207
- /**
208
- * @param node
209
- */
210
- const disableNodeWithParents = function (node) {
211
- node.setEnabled(false);
212
- if (node.parent) {
213
- disableNodeWithParents(node.parent);
214
- }
215
- };
216
- exports.disableNodeWithParents = disableNodeWithParents;
217
- /**
218
- * Applies a {@link TransformationDefinition} consecutively to ensure dependencies in positioning etc.
219
- * @param node
220
- * @param transformation
221
- */
222
- const transformTransformNode = function (node, transformation) {
223
- // scaling
224
- if (!(0, lodash_es_1.has)(node.metadata, 'scaling.initial')) {
225
- injectNodeMetadata(node, { 'scaling.initial': node.scaling }, false);
226
- }
227
- const initialScaling = (0, lodash_es_1.get)(node.metadata, 'scaling.initial');
228
- node.scaling = initialScaling.multiply(transformation.scaling);
229
- // position
230
- if (!(0, lodash_es_1.has)(node.metadata, 'position.initial')) {
231
- injectNodeMetadata(node, { 'position.initial': node.absolutePosition.clone() }, false);
232
- }
233
- const initialPosition = (0, lodash_es_1.get)(node.metadata, 'position.initial');
234
- node.setAbsolutePosition(initialPosition.add(transformation.position).multiply(transformation.scaling));
235
- // rotation
236
- if (!(0, lodash_es_1.has)(node.metadata, 'rotation.initial')) {
237
- let rotationQuaternion = node.rotationQuaternion;
238
- if (!rotationQuaternion) {
239
- rotationQuaternion = math_vector_1.Quaternion.RotationYawPitchRoll(node.rotation.x, node.rotation.y, node.rotation.z);
240
- }
241
- injectNodeMetadata(node, { 'rotation.initial': rotationQuaternion.asArray() }, false);
242
- }
243
- const initialRotationQuaternion = math_vector_1.Quaternion.FromArray((0, lodash_es_1.get)(node.metadata, 'rotation.initial'));
244
- node.rotationQuaternion = initialRotationQuaternion;
245
- node.rotateAround(math_vector_1.Vector3.Zero(), math_axis_1.Axis.X, transformation.rotation.x);
246
- node.rotateAround(math_vector_1.Vector3.Zero(), math_axis_1.Axis.Y, transformation.rotation.y);
247
- node.rotateAround(math_vector_1.Vector3.Zero(), math_axis_1.Axis.Z, transformation.rotation.z);
248
- node.computeWorldMatrix(true);
249
- };
250
- exports.transformTransformNode = transformTransformNode;
251
- /**
252
- * Apply changes of environment (background texture, etc.) consecutively in order to avoid dependency related issues.
253
- * @param scene
254
- * @param envDef
255
- */
256
- const changeEnvironment = function (scene, envDef) {
257
- var _a;
258
- // issue warning if both background texture and usedefault are set
259
- if (envDef.environmentUseDefault && envDef.environmentBackground) {
260
- console.warn(`!!! Warning !!! Spec parameter 'environment.usedefault' is being overruled by 'environment.background'.`);
261
- }
262
- const useDefaultEnv = envDef.environmentUseDefault && !envDef.environmentBackground;
263
- // 1) set ENVIRONMENT_COLOR
264
- const clearColorBefore = scene.clearColor.toString();
265
- scene.clearColor = envDef.environmentColor
266
- ? math_color_1.Color4.FromColor3(envDef.environmentColor)
267
- : useDefaultEnv
268
- ? sceneSetup_1.defaultEnvHelperColor
269
- : sceneSetup_1.defaultSceneClearColor;
270
- const clearColorChanged = clearColorBefore !== scene.clearColor.toString();
271
- // 2) dispose existing & set new ENVIRONMENT (==texture)
272
- const curEnvTexture = scene.environmentTexture;
273
- const envTextureChanged = envDef.environment !== (curEnvTexture === null || curEnvTexture === void 0 ? void 0 : curEnvTexture.url);
274
- if (curEnvTexture && (!envDef.environment || envTextureChanged)) {
275
- curEnvTexture.dispose();
276
- }
277
- const rotation = envDef.environmentRotation !== undefined ? tools_1.Tools.ToRadians(envDef.environmentRotation) : 0;
278
- if (envDef.environment && envTextureChanged) {
279
- const envTexture = cubeTexture_1.CubeTexture.CreateFromPrefilteredData(envDef.environment, scene);
280
- envTexture.rotationY = rotation;
281
- scene.environmentTexture = envTexture;
282
- }
283
- else if (curEnvTexture && curEnvTexture.rotationY !== rotation) {
284
- curEnvTexture.rotationY = rotation;
285
- }
286
- // 3) dispose existing & set new ENVIRONMENT_BACKGROUND
287
- const curDome = scene.getNodeByName(backgroundDomeName);
288
- const backgroundUrlChanged = envDef.environmentBackground !== (curDome === null || curDome === void 0 ? void 0 : curDome.texture.url);
289
- if (curDome && (!envDef.environmentBackground || backgroundUrlChanged)) {
290
- curDome.dispose();
291
- }
292
- const rotationPhotoDome = -1 * rotation;
293
- if (envDef.environmentBackground && backgroundUrlChanged) {
294
- const textureUrl = envDef.environmentBackground;
295
- const dome = new photoDome_1.PhotoDome(backgroundDomeName, textureUrl, { resolution: 32, size: 450 }, scene);
296
- // Rotate submesh to get them in line with environment texture
297
- dome.getChildMeshes(true)[0].rotation.y = tools_1.Tools.ToRadians(90);
298
- dome.rotation.y = rotationPhotoDome;
299
- }
300
- else if (curDome && curDome.rotation.y !== rotationPhotoDome) {
301
- curDome.rotation.y = rotationPhotoDome;
302
- }
303
- // 4) dispose existing & set new ENVIRONMENT_USEDEFAULT (only if no background is set)
304
- const curEnvHelper = (_a = scene.metadata) === null || _a === void 0 ? void 0 : _a[envHelperMetadataName];
305
- if (curEnvHelper && !useDefaultEnv) {
306
- curEnvHelper.dispose();
307
- delete scene.metadata[envHelperMetadataName];
308
- }
309
- const envHelperMainColor = math_color_1.Color3.FromArray(scene.clearColor.asArray());
310
- if (useDefaultEnv && !curEnvHelper) {
311
- const envHelper = scene.createDefaultEnvironment({ sizeAuto: true });
312
- envHelper === null || envHelper === void 0 ? void 0 : envHelper.setMainColor(envHelperMainColor);
313
- if (envHelper) {
314
- scene.metadata = (0, lodash_es_1.merge)({}, scene.metadata, { [envHelperMetadataName]: envHelper });
315
- }
316
- }
317
- else if (curEnvHelper && useDefaultEnv && clearColorChanged) {
318
- curEnvHelper.setMainColor(envHelperMainColor);
319
- }
320
- // 5) set ENVIRONMENT_INTENSITY
321
- if (envDef.environmentIntensity !== undefined) {
322
- scene.environmentIntensity = envDef.environmentIntensity;
323
- }
324
- };
325
- exports.changeEnvironment = changeEnvironment;
326
- /**
327
- * @param node
328
- * @param materialName
329
- * @param deep
330
- */
331
- const setMaterial = function (variant, node, materialName, deep = true) {
332
- if (node instanceof abstractMesh_1.AbstractMesh) {
333
- const materialExists = variant.viewer.scene.getMaterialById(materialName);
334
- const hasMissingMaterial = (0, lodash_es_1.has)(node.metadata, sceneLoaderHelper_1.missingMaterialMetadataName);
335
- const deferMaterialCreation = !materialExists && !node.isEnabled();
336
- if (deferMaterialCreation) {
337
- injectNodeMetadata(node, { [sceneLoaderHelper_1.missingMaterialMetadataName]: materialName }, false);
338
- // If it already had the missing material flag before, there already exists an observer...
339
- if (!hasMissingMaterial) {
340
- (0, sceneLoaderHelper_1.addMissingMaterialObserver)(node);
341
- }
342
- }
343
- else {
344
- node.material = variant.getOrCreateMaterial(materialName);
345
- if (hasMissingMaterial) {
346
- delete node.metadata[sceneLoaderHelper_1.missingMaterialMetadataName];
347
- }
348
- }
349
- }
350
- if (deep) {
351
- node.getChildTransformNodes(true).forEach(child => setMaterial(variant, child, materialName, deep));
352
- }
353
- };
354
- exports.setMaterial = setMaterial;
355
- /**
356
- * !!! Warning !!!
357
- * This function is not public API. Whilst it can help solving certain problems, it only works reliably in well defined
358
- * situations and can cause unwanted side effects under some conditions. Use carefully at your own risk!
359
- *
360
- * See https://combeenation.myjetbrains.com/youtrack/issue/CB-5906 for further details regarding this warning.
361
- *
362
- * Set material of an instanced meshes source mesh.
363
- * Changes the material of all instanced meshes which have the same source mesh.
364
- *
365
- * @param node
366
- * @param material
367
- * @param deep
368
- *
369
- * @ignore
370
- */
371
- const setSourceNodeMaterial = function (node, material, deep = true) {
372
- const warn = ` You're using "setSourceNodeMaterial" which is not public API.
373
- Whilst it can help solving certain problems, it only works reliably in well defined situations and can cause unwanted side effects under some conditions.
374
- Use carefully at your own risk!`;
375
- console.warn(`!!! Warning !!!\n${warn}`);
376
- if (node instanceof instancedMesh_1.InstancedMesh) {
377
- node.sourceMesh.material = material;
378
- }
379
- if (deep) {
380
- node.getChildTransformNodes(true).forEach(child => setSourceNodeMaterial(child, material, deep));
381
- }
382
- };
383
- exports.setSourceNodeMaterial = setSourceNodeMaterial;
384
- /**
385
- * @param node
386
- * @param color
387
- * @param deep
388
- */
389
- const setMaterialColor = function (node, color, deep = true) {
390
- if (node instanceof abstractMesh_1.AbstractMesh && node.material) {
391
- const materialCls = node.material.getClassName();
392
- switch (materialCls) {
393
- case 'PBRMaterial':
394
- node.material.albedoColor = color.toLinearSpace();
395
- break;
396
- case 'StandardMaterial':
397
- node.material.diffuseColor = color;
398
- break;
399
- default:
400
- throw new Error(`Setting color for material of instance "${materialCls}" not implemented (yet).`);
401
- }
402
- }
403
- if (deep) {
404
- node.getChildTransformNodes(true).forEach(child => setMaterialColor(child, color, deep));
405
- }
406
- };
407
- exports.setMaterialColor = setMaterialColor;
408
- /**
409
- * @param node
410
- * @param texture
411
- * @param deep
412
- */
413
- const setMaterialTexture = function (node, texture, deep = true) {
414
- if (node instanceof abstractMesh_1.AbstractMesh && node.material) {
415
- const materialCls = node.material.getClassName();
416
- switch (materialCls) {
417
- case 'PBRMaterial':
418
- node.material.albedoTexture = texture;
419
- break;
420
- case 'StandardMaterial':
421
- node.material.diffuseTexture = texture;
422
- break;
423
- default:
424
- throw new Error(`Setting texture for material of instance "${materialCls}" not implemented (yet).`);
425
- }
426
- }
427
- if (deep) {
428
- node.getChildTransformNodes(true).forEach(child => setMaterialTexture(child, texture, deep));
429
- }
430
- };
431
- exports.setMaterialTexture = setMaterialTexture;
432
- /**
433
- * @param node
434
- * @param metallness
435
- * @param deep
436
- */
437
- const setMaterialMetallness = function (node, metallness, deep = true) {
438
- if (node instanceof abstractMesh_1.AbstractMesh && node.material) {
439
- const materialCls = node.material.getClassName();
440
- switch (materialCls) {
441
- case 'PBRMaterial':
442
- node.material.metallic = metallness;
443
- break;
444
- default:
445
- throw new Error(`Setting metallness for material of instance "${materialCls}" not implemented (yet).`);
446
- }
447
- }
448
- if (deep) {
449
- node.getChildTransformNodes(true).forEach(child => setMaterialMetallness(child, metallness, deep));
450
- }
451
- };
452
- exports.setMaterialMetallness = setMaterialMetallness;
453
- /**
454
- * @param node
455
- * @param roughness
456
- * @param deep
457
- */
458
- const setMaterialRoughness = function (node, roughness, deep = true) {
459
- if (node instanceof abstractMesh_1.AbstractMesh && node.material) {
460
- const materialCls = node.material.getClassName();
461
- switch (materialCls) {
462
- case 'PBRMaterial':
463
- node.material.roughness = roughness;
464
- break;
465
- case 'StandardMaterial':
466
- node.material.roughness = roughness;
467
- break;
468
- default:
469
- throw new Error(`Setting roughness for material of instance "${materialCls}" not implemented (yet).`);
470
- }
471
- }
472
- if (deep) {
473
- node.getChildTransformNodes(true).forEach(child => setMaterialRoughness(child, roughness, deep));
474
- }
475
- };
476
- exports.setMaterialRoughness = setMaterialRoughness;
477
- /**
478
- * @param node
479
- * @param layer
480
- * @param color
481
- * @param deep
482
- */
483
- const addToHighlightLayer = function (layer, color, node, deep = true) {
484
- if (node instanceof abstractMesh_1.AbstractMesh) {
485
- layer.addMesh(node, color);
486
- }
487
- if (deep) {
488
- node.getChildTransformNodes(true).forEach(child => addToHighlightLayer(layer, color, child, deep));
489
- }
490
- };
491
- exports.addToHighlightLayer = addToHighlightLayer;
492
- /**
493
- * @param node
494
- * @param layer
495
- * @param deep
496
- */
497
- const removeFromHighlightLayer = function (layer, node, deep = true) {
498
- if (node instanceof abstractMesh_1.AbstractMesh) {
499
- layer.removeMesh(node);
500
- }
501
- if (deep) {
502
- node.getChildTransformNodes(true).forEach(child => removeFromHighlightLayer(layer, child, deep));
503
- }
504
- };
505
- exports.removeFromHighlightLayer = removeFromHighlightLayer;
506
- /**
507
- * @param node
508
- * @param receiveShadows
509
- * @param deep
510
- */
511
- const setReceiveShadows = function (node, receiveShadows, deep = true) {
512
- if (node instanceof abstractMesh_1.AbstractMesh) {
513
- node.receiveShadows = receiveShadows;
514
- }
515
- if (deep) {
516
- node.getChildTransformNodes(true).forEach(child => setReceiveShadows(child, receiveShadows, deep));
517
- }
518
- };
519
- exports.setReceiveShadows = setReceiveShadows;
520
- /**
521
- * @param node
522
- * @param generator
523
- * @param deep
524
- */
525
- const addToShadowGenerator = function (generator, node, deep = true) {
526
- if (node instanceof abstractMesh_1.AbstractMesh) {
527
- // We have to remove the node because there's no duplicate check in babylon
528
- generator.removeShadowCaster(node, false);
529
- generator.addShadowCaster(node, false);
530
- }
531
- if (deep) {
532
- node.getChildTransformNodes(true).forEach(child => addToShadowGenerator(generator, child, deep));
533
- }
534
- };
535
- exports.addToShadowGenerator = addToShadowGenerator;
536
- /**
537
- * @param node
538
- * @param generator
539
- * @param deep
540
- */
541
- const removeFromShadowGenerator = function (generator, node, deep = true) {
542
- if (node instanceof abstractMesh_1.AbstractMesh) {
543
- generator.removeShadowCaster(node, false);
544
- }
545
- if (deep) {
546
- node.getChildTransformNodes(true).forEach(child => removeFromShadowGenerator(generator, child, deep));
547
- }
548
- };
549
- exports.removeFromShadowGenerator = removeFromShadowGenerator;
550
- /**
551
- * https://forum.babylonjs.com/t/get-mesh-bounding-box-position-and-size-in-2d-screen-coordinates/1058/3
552
- * @param mesh
553
- * @param scene
554
- * @param canvas
555
- */
556
- const getClientRectFromMesh = function (mesh, scene, canvas) {
557
- // get bounding box of the mesh
558
- const meshVectors = mesh.getBoundingInfo().boundingBox.vectors;
559
- // get the matrix and viewport needed to project the vectors onto the screen
560
- const worldMatrix = mesh.getWorldMatrix();
561
- const transformMatrix = scene.getTransformMatrix();
562
- const viewport = scene.activeCamera.viewport;
563
- // loop though all the vectors and project them against the current camera viewport to get a set of coordinates
564
- const coordinates = meshVectors.map(vector => {
565
- const projection = math_vector_1.Vector3.Project(vector, worldMatrix, transformMatrix, viewport);
566
- projection.x = projection.x * canvas.clientWidth;
567
- projection.y = projection.y * canvas.clientHeight;
568
- return projection;
569
- });
570
- // get the min and max for all the coordinates so we can calculate the largest possible screen size
571
- const maxX = Math.max(...coordinates.map(o => o.x));
572
- const minX = Math.min(...coordinates.map(o => o.x));
573
- const maxY = Math.max(...coordinates.map(o => o.y));
574
- const minY = Math.min(...coordinates.map(o => o.y));
575
- // return a ClientRect from this
576
- return {
577
- width: maxX - minX,
578
- height: maxY - minY,
579
- left: minX,
580
- top: minY,
581
- right: maxX,
582
- bottom: maxY,
583
- };
584
- };
585
- exports.getClientRectFromMesh = getClientRectFromMesh;
586
- /**
587
- * This type guard checks whether the given `BaseTextures` is any of its subtypes which comes with an
588
- * `onLoadObservable`.
589
- *
590
- * !!! Timing of when this function is called is important !!!
591
- * See the following for more details: https://forum.babylonjs.com/t/basetexture-whenallready-returns-too-early/34501/6
592
- */
593
- const isTextureWithOnLoadObservable = function (texture) {
594
- return !!texture.onLoadObservable;
595
- };
596
- exports.isTextureWithOnLoadObservable = isTextureWithOnLoadObservable;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ 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.assertTransformNode = exports.injectNodeMetadata = exports.cloneTransformNodeMaterial = exports.cloneNodeWithParents = exports.cloneTransformNode = exports.getDottedPathForNode = exports.mapToDottedNodes = exports.isTextureWithOnLoadObservable = exports.getRootNode = void 0;
4
+ const dottedPath_1 = require("../classes/dottedPath");
5
+ const sceneSetup_1 = require("../internal/sceneSetup");
6
+ const sceneLoaderHelper_1 = require("./sceneLoaderHelper");
7
+ const photoDome_1 = require("@babylonjs/core/Helpers/photoDome");
8
+ const light_1 = require("@babylonjs/core/Lights/light");
9
+ const cubeTexture_1 = require("@babylonjs/core/Materials/Textures/cubeTexture");
10
+ const math_axis_1 = require("@babylonjs/core/Maths/math.axis");
11
+ const math_color_1 = require("@babylonjs/core/Maths/math.color");
12
+ const math_vector_1 = require("@babylonjs/core/Maths/math.vector");
13
+ const abstractMesh_1 = require("@babylonjs/core/Meshes/abstractMesh");
14
+ const instancedMesh_1 = require("@babylonjs/core/Meshes/instancedMesh");
15
+ const transformNode_1 = require("@babylonjs/core/Meshes/transformNode");
16
+ const tools_1 = require("@babylonjs/core/Misc/tools");
17
+ const lodash_es_1 = require("lodash-es");
18
+ const backgroundDomeName = 'BackgroundDome_ViewerGenerated';
19
+ exports.backgroundDomeName = backgroundDomeName;
20
+ const envHelperMetadataName = 'viewerEnvHelper';
21
+ exports.envHelperMetadataName = envHelperMetadataName;
22
+ /**
23
+ * @param node
24
+ * @return Node
25
+ */
26
+ const getRootNode = function (node) {
27
+ let _node = node;
28
+ while (_node.parent) {
29
+ _node = _node.parent;
30
+ }
31
+ return _node;
32
+ };
33
+ exports.getRootNode = getRootNode;
34
+ /**
35
+ * @param nodes
36
+ * @param predicate
37
+ * @return Map<DottedPath, T>
38
+ */
39
+ const mapToDottedNodes = function (nodes, predicate) {
40
+ const map = new Map();
41
+ const addNodes = function (_node) {
42
+ if (predicate && predicate(_node)) {
43
+ map.set(_node.metadata.dottedPath, _node);
44
+ }
45
+ _node.getChildren().forEach(child => {
46
+ addNodes(child);
47
+ });
48
+ };
49
+ nodes.forEach(node => {
50
+ addNodes(node);
51
+ });
52
+ return map;
53
+ };
54
+ exports.mapToDottedNodes = mapToDottedNodes;
55
+ /**
56
+ * @param node
57
+ * @return DottedPath
58
+ */
59
+ const getDottedPathForNode = function (node) {
60
+ const dottedPath = dottedPath_1.DottedPath.create(node.name);
61
+ let _parent = node;
62
+ while (_parent.parent) {
63
+ _parent = _parent.parent;
64
+ dottedPath.unshiftPart(_parent.name);
65
+ }
66
+ return dottedPath;
67
+ };
68
+ exports.getDottedPathForNode = getDottedPathForNode;
69
+ /**
70
+ * @param node
71
+ * @param predicate
72
+ * @param deep
73
+ * @return TransformNode | null
74
+ */
75
+ const cloneTransformNode = function (node, predicate, deep = true) {
76
+ if (predicate && !predicate(node)) {
77
+ return null;
78
+ }
79
+ const clone = node.clone(node.name, node.parent, true);
80
+ if (clone) {
81
+ clone.metadata = (0, lodash_es_1.cloneDeep)(node.metadata);
82
+ // if the cloned node is of type InstancedMesh, due to a bug(?),
83
+ // the InstancedMesh.isEnabled state may have changed after cloning.
84
+ // in that case, set the clone's enabled state to the original's state
85
+ if (node.constructor.name === 'InstancedMesh') {
86
+ clone.setEnabled(node.isEnabled(false));
87
+ }
88
+ }
89
+ if (deep) {
90
+ const children = node.getChildTransformNodes(true);
91
+ children.forEach(child => {
92
+ const clonedChild = cloneTransformNode(child, predicate, deep);
93
+ if (clonedChild) {
94
+ clonedChild.parent = clone;
95
+ }
96
+ });
97
+ }
98
+ return clone;
99
+ };
100
+ exports.cloneTransformNode = cloneTransformNode;
101
+ /**
102
+ * @param node
103
+ */
104
+ const cloneNodeWithParents = function (node) {
105
+ let clone = null;
106
+ if (node instanceof transformNode_1.TransformNode) {
107
+ clone = node.clone(node.name, cloneNodeWithParents(node.parent), true);
108
+ }
109
+ else if (node instanceof light_1.Light) {
110
+ clone = node.clone(node.name, cloneNodeWithParents(node.parent));
111
+ }
112
+ else if (node) {
113
+ throw new Error(`Cloning of "${node === null || node === void 0 ? void 0 : node.constructor.name}" is not implemented (yet).`);
114
+ }
115
+ return clone;
116
+ };
117
+ exports.cloneNodeWithParents = cloneNodeWithParents;
118
+ /**
119
+ * @param node
120
+ * @param deep
121
+ * @param prefix
122
+ * @return TransformNode
123
+ */
124
+ const cloneTransformNodeMaterial = function (node, prefix = '', deep = true) {
125
+ if (node instanceof abstractMesh_1.AbstractMesh && node.material) {
126
+ const newMatName = dottedPath_1.DottedPath.create(prefix).addParts([node.material.name, 'clone', node.uniqueId.toString()]);
127
+ node.material = node.material.clone(newMatName.path);
128
+ }
129
+ if (deep) {
130
+ const children = node.getChildTransformNodes(true);
131
+ children.forEach(child => cloneTransformNodeMaterial(child, prefix, deep));
132
+ }
133
+ return node;
134
+ };
135
+ exports.cloneTransformNodeMaterial = cloneTransformNodeMaterial;
136
+ /**
137
+ * @param node
138
+ * @param deep
139
+ * @param metadata
140
+ */
141
+ const injectNodeMetadata = function (node, metadata, deep = true) {
142
+ node.metadata = (0, lodash_es_1.merge)({}, node.metadata, metadata);
143
+ if (deep && node instanceof transformNode_1.TransformNode) {
144
+ const children = node.getChildTransformNodes(true);
145
+ children.forEach(child => injectNodeMetadata(child, metadata, deep));
146
+ }
147
+ };
148
+ exports.injectNodeMetadata = injectNodeMetadata;
149
+ /**
150
+ * @param node
151
+ * @param assertCallable
152
+ * @param callableParameters
153
+ * @param deep
154
+ */
155
+ const assertTransformNode = function (node, assertCallable, callableParameters = [], deep = true) {
156
+ assertCallable(node, ...callableParameters);
157
+ if (deep) {
158
+ const children = node.getChildTransformNodes(true);
159
+ children.forEach(child => assertTransformNode(child, assertCallable, callableParameters, deep));
160
+ }
161
+ };
162
+ exports.assertTransformNode = assertTransformNode;
163
+ /**
164
+ * @param node
165
+ * @param deep
166
+ */
167
+ const activateTransformNode = function (node, deep = true) {
168
+ node.setEnabled(true);
169
+ /*
170
+ if( node instanceof AbstractMesh ) {
171
+ node.visibility = 1;
172
+ node.isPickable = true;
173
+ }
174
+ */
175
+ if (deep) {
176
+ node.getChildTransformNodes(true).forEach(child => activateTransformNode(child, deep));
177
+ }
178
+ };
179
+ exports.activateTransformNode = activateTransformNode;
180
+ /**
181
+ * @param node
182
+ * @param deep
183
+ */
184
+ const deactivateTransformNode = function (node, deep = true) {
185
+ node.setEnabled(false);
186
+ /*
187
+ if( node instanceof AbstractMesh ) {
188
+ node.visibility = 0;
189
+ node.isPickable = false;
190
+ }
191
+ */
192
+ if (deep) {
193
+ node.getChildTransformNodes(true).forEach(child => deactivateTransformNode(child, deep));
194
+ }
195
+ };
196
+ exports.deactivateTransformNode = deactivateTransformNode;
197
+ /**
198
+ * @param node
199
+ */
200
+ const enableNodeWithParents = function (node) {
201
+ node.setEnabled(true);
202
+ if (node.parent) {
203
+ enableNodeWithParents(node.parent);
204
+ }
205
+ };
206
+ exports.enableNodeWithParents = enableNodeWithParents;
207
+ /**
208
+ * @param node
209
+ */
210
+ const disableNodeWithParents = function (node) {
211
+ node.setEnabled(false);
212
+ if (node.parent) {
213
+ disableNodeWithParents(node.parent);
214
+ }
215
+ };
216
+ exports.disableNodeWithParents = disableNodeWithParents;
217
+ /**
218
+ * Applies a {@link TransformationDefinition} consecutively to ensure dependencies in positioning etc.
219
+ * @param node
220
+ * @param transformation
221
+ */
222
+ const transformTransformNode = function (node, transformation) {
223
+ // scaling
224
+ if (!(0, lodash_es_1.has)(node.metadata, 'scaling.initial')) {
225
+ injectNodeMetadata(node, { 'scaling.initial': node.scaling }, false);
226
+ }
227
+ const initialScaling = (0, lodash_es_1.get)(node.metadata, 'scaling.initial');
228
+ node.scaling = initialScaling.multiply(transformation.scaling);
229
+ // position
230
+ if (!(0, lodash_es_1.has)(node.metadata, 'position.initial')) {
231
+ injectNodeMetadata(node, { 'position.initial': node.absolutePosition.clone() }, false);
232
+ }
233
+ const initialPosition = (0, lodash_es_1.get)(node.metadata, 'position.initial');
234
+ node.setAbsolutePosition(initialPosition.add(transformation.position).multiply(transformation.scaling));
235
+ // rotation
236
+ if (!(0, lodash_es_1.has)(node.metadata, 'rotation.initial')) {
237
+ let rotationQuaternion = node.rotationQuaternion;
238
+ if (!rotationQuaternion) {
239
+ rotationQuaternion = math_vector_1.Quaternion.RotationYawPitchRoll(node.rotation.x, node.rotation.y, node.rotation.z);
240
+ }
241
+ injectNodeMetadata(node, { 'rotation.initial': rotationQuaternion.asArray() }, false);
242
+ }
243
+ const initialRotationQuaternion = math_vector_1.Quaternion.FromArray((0, lodash_es_1.get)(node.metadata, 'rotation.initial'));
244
+ node.rotationQuaternion = initialRotationQuaternion;
245
+ node.rotateAround(math_vector_1.Vector3.Zero(), math_axis_1.Axis.X, transformation.rotation.x);
246
+ node.rotateAround(math_vector_1.Vector3.Zero(), math_axis_1.Axis.Y, transformation.rotation.y);
247
+ node.rotateAround(math_vector_1.Vector3.Zero(), math_axis_1.Axis.Z, transformation.rotation.z);
248
+ node.computeWorldMatrix(true);
249
+ };
250
+ exports.transformTransformNode = transformTransformNode;
251
+ /**
252
+ * Apply changes of environment (background texture, etc.) consecutively in order to avoid dependency related issues.
253
+ * @param scene
254
+ * @param envDef
255
+ */
256
+ const changeEnvironment = function (scene, envDef) {
257
+ var _a;
258
+ // issue warning if both background texture and usedefault are set
259
+ if (envDef.environmentUseDefault && envDef.environmentBackground) {
260
+ console.warn(`!!! Warning !!! Spec parameter 'environment.usedefault' is being overruled by 'environment.background'.`);
261
+ }
262
+ const useDefaultEnv = envDef.environmentUseDefault && !envDef.environmentBackground;
263
+ // 1) set ENVIRONMENT_COLOR
264
+ const clearColorBefore = scene.clearColor.toString();
265
+ scene.clearColor = envDef.environmentColor
266
+ ? math_color_1.Color4.FromColor3(envDef.environmentColor)
267
+ : useDefaultEnv
268
+ ? sceneSetup_1.defaultEnvHelperColor
269
+ : sceneSetup_1.defaultSceneClearColor;
270
+ const clearColorChanged = clearColorBefore !== scene.clearColor.toString();
271
+ // 2) dispose existing & set new ENVIRONMENT (==texture)
272
+ const curEnvTexture = scene.environmentTexture;
273
+ const envTextureChanged = envDef.environment !== (curEnvTexture === null || curEnvTexture === void 0 ? void 0 : curEnvTexture.url);
274
+ if (curEnvTexture && (!envDef.environment || envTextureChanged)) {
275
+ curEnvTexture.dispose();
276
+ }
277
+ const rotation = envDef.environmentRotation !== undefined ? tools_1.Tools.ToRadians(envDef.environmentRotation) : 0;
278
+ if (envDef.environment && envTextureChanged) {
279
+ const envTexture = cubeTexture_1.CubeTexture.CreateFromPrefilteredData(envDef.environment, scene);
280
+ envTexture.rotationY = rotation;
281
+ scene.environmentTexture = envTexture;
282
+ }
283
+ else if (curEnvTexture && curEnvTexture.rotationY !== rotation) {
284
+ curEnvTexture.rotationY = rotation;
285
+ }
286
+ // 3) dispose existing & set new ENVIRONMENT_BACKGROUND
287
+ const curDome = scene.getNodeByName(backgroundDomeName);
288
+ const backgroundUrlChanged = envDef.environmentBackground !== (curDome === null || curDome === void 0 ? void 0 : curDome.texture.url);
289
+ if (curDome && (!envDef.environmentBackground || backgroundUrlChanged)) {
290
+ curDome.dispose();
291
+ }
292
+ const rotationPhotoDome = -1 * rotation;
293
+ if (envDef.environmentBackground && backgroundUrlChanged) {
294
+ const textureUrl = envDef.environmentBackground;
295
+ const dome = new photoDome_1.PhotoDome(backgroundDomeName, textureUrl, { resolution: 32, size: 450 }, scene);
296
+ // Rotate submesh to get them in line with environment texture
297
+ dome.getChildMeshes(true)[0].rotation.y = tools_1.Tools.ToRadians(90);
298
+ dome.rotation.y = rotationPhotoDome;
299
+ }
300
+ else if (curDome && curDome.rotation.y !== rotationPhotoDome) {
301
+ curDome.rotation.y = rotationPhotoDome;
302
+ }
303
+ // 4) dispose existing & set new ENVIRONMENT_USEDEFAULT (only if no background is set)
304
+ const curEnvHelper = (_a = scene.metadata) === null || _a === void 0 ? void 0 : _a[envHelperMetadataName];
305
+ if (curEnvHelper && !useDefaultEnv) {
306
+ curEnvHelper.dispose();
307
+ delete scene.metadata[envHelperMetadataName];
308
+ }
309
+ const envHelperMainColor = math_color_1.Color3.FromArray(scene.clearColor.asArray());
310
+ if (useDefaultEnv && !curEnvHelper) {
311
+ const envHelper = scene.createDefaultEnvironment({ sizeAuto: true });
312
+ envHelper === null || envHelper === void 0 ? void 0 : envHelper.setMainColor(envHelperMainColor);
313
+ if (envHelper) {
314
+ scene.metadata = (0, lodash_es_1.merge)({}, scene.metadata, { [envHelperMetadataName]: envHelper });
315
+ }
316
+ }
317
+ else if (curEnvHelper && useDefaultEnv && clearColorChanged) {
318
+ curEnvHelper.setMainColor(envHelperMainColor);
319
+ }
320
+ // 5) set ENVIRONMENT_INTENSITY
321
+ if (envDef.environmentIntensity !== undefined) {
322
+ scene.environmentIntensity = envDef.environmentIntensity;
323
+ }
324
+ };
325
+ exports.changeEnvironment = changeEnvironment;
326
+ /**
327
+ * @param node
328
+ * @param materialName
329
+ * @param deep
330
+ */
331
+ const setMaterial = function (variant, node, materialName, deep = true) {
332
+ if (node instanceof abstractMesh_1.AbstractMesh) {
333
+ const materialExists = variant.viewer.scene.getMaterialById(materialName);
334
+ const hasMissingMaterial = (0, lodash_es_1.has)(node.metadata, sceneLoaderHelper_1.missingMaterialMetadataName);
335
+ const deferMaterialCreation = !materialExists && !node.isEnabled();
336
+ if (deferMaterialCreation) {
337
+ injectNodeMetadata(node, { [sceneLoaderHelper_1.missingMaterialMetadataName]: materialName }, false);
338
+ // If it already had the missing material flag before, there already exists an observer...
339
+ if (!hasMissingMaterial) {
340
+ (0, sceneLoaderHelper_1.addMissingMaterialObserver)(node);
341
+ }
342
+ }
343
+ else {
344
+ node.material = variant.getOrCreateMaterial(materialName);
345
+ if (hasMissingMaterial) {
346
+ delete node.metadata[sceneLoaderHelper_1.missingMaterialMetadataName];
347
+ }
348
+ }
349
+ }
350
+ if (deep) {
351
+ node.getChildTransformNodes(true).forEach(child => setMaterial(variant, child, materialName, deep));
352
+ }
353
+ };
354
+ exports.setMaterial = setMaterial;
355
+ /**
356
+ * !!! Warning !!!
357
+ * This function is not public API. Whilst it can help solving certain problems, it only works reliably in well defined
358
+ * situations and can cause unwanted side effects under some conditions. Use carefully at your own risk!
359
+ *
360
+ * See https://combeenation.myjetbrains.com/youtrack/issue/CB-5906 for further details regarding this warning.
361
+ *
362
+ * Set material of an instanced meshes source mesh.
363
+ * Changes the material of all instanced meshes which have the same source mesh.
364
+ *
365
+ * @param node
366
+ * @param material
367
+ * @param deep
368
+ *
369
+ * @ignore
370
+ */
371
+ const setSourceNodeMaterial = function (node, material, deep = true) {
372
+ const warn = ` You're using "setSourceNodeMaterial" which is not public API.
373
+ Whilst it can help solving certain problems, it only works reliably in well defined situations and can cause unwanted side effects under some conditions.
374
+ Use carefully at your own risk!`;
375
+ console.warn(`!!! Warning !!!\n${warn}`);
376
+ if (node instanceof instancedMesh_1.InstancedMesh) {
377
+ node.sourceMesh.material = material;
378
+ }
379
+ if (deep) {
380
+ node.getChildTransformNodes(true).forEach(child => setSourceNodeMaterial(child, material, deep));
381
+ }
382
+ };
383
+ exports.setSourceNodeMaterial = setSourceNodeMaterial;
384
+ /**
385
+ * @param node
386
+ * @param color
387
+ * @param deep
388
+ */
389
+ const setMaterialColor = function (node, color, deep = true) {
390
+ if (node instanceof abstractMesh_1.AbstractMesh && node.material) {
391
+ const materialCls = node.material.getClassName();
392
+ switch (materialCls) {
393
+ case 'PBRMaterial':
394
+ node.material.albedoColor = color.toLinearSpace();
395
+ break;
396
+ case 'StandardMaterial':
397
+ node.material.diffuseColor = color;
398
+ break;
399
+ default:
400
+ throw new Error(`Setting color for material of instance "${materialCls}" not implemented (yet).`);
401
+ }
402
+ }
403
+ if (deep) {
404
+ node.getChildTransformNodes(true).forEach(child => setMaterialColor(child, color, deep));
405
+ }
406
+ };
407
+ exports.setMaterialColor = setMaterialColor;
408
+ /**
409
+ * @param node
410
+ * @param texture
411
+ * @param deep
412
+ */
413
+ const setMaterialTexture = function (node, texture, deep = true) {
414
+ if (node instanceof abstractMesh_1.AbstractMesh && node.material) {
415
+ const materialCls = node.material.getClassName();
416
+ switch (materialCls) {
417
+ case 'PBRMaterial':
418
+ node.material.albedoTexture = texture;
419
+ break;
420
+ case 'StandardMaterial':
421
+ node.material.diffuseTexture = texture;
422
+ break;
423
+ default:
424
+ throw new Error(`Setting texture for material of instance "${materialCls}" not implemented (yet).`);
425
+ }
426
+ }
427
+ if (deep) {
428
+ node.getChildTransformNodes(true).forEach(child => setMaterialTexture(child, texture, deep));
429
+ }
430
+ };
431
+ exports.setMaterialTexture = setMaterialTexture;
432
+ /**
433
+ * @param node
434
+ * @param metallness
435
+ * @param deep
436
+ */
437
+ const setMaterialMetallness = function (node, metallness, deep = true) {
438
+ if (node instanceof abstractMesh_1.AbstractMesh && node.material) {
439
+ const materialCls = node.material.getClassName();
440
+ switch (materialCls) {
441
+ case 'PBRMaterial':
442
+ node.material.metallic = metallness;
443
+ break;
444
+ default:
445
+ throw new Error(`Setting metallness for material of instance "${materialCls}" not implemented (yet).`);
446
+ }
447
+ }
448
+ if (deep) {
449
+ node.getChildTransformNodes(true).forEach(child => setMaterialMetallness(child, metallness, deep));
450
+ }
451
+ };
452
+ exports.setMaterialMetallness = setMaterialMetallness;
453
+ /**
454
+ * @param node
455
+ * @param roughness
456
+ * @param deep
457
+ */
458
+ const setMaterialRoughness = function (node, roughness, deep = true) {
459
+ if (node instanceof abstractMesh_1.AbstractMesh && node.material) {
460
+ const materialCls = node.material.getClassName();
461
+ switch (materialCls) {
462
+ case 'PBRMaterial':
463
+ node.material.roughness = roughness;
464
+ break;
465
+ case 'StandardMaterial':
466
+ node.material.roughness = roughness;
467
+ break;
468
+ default:
469
+ throw new Error(`Setting roughness for material of instance "${materialCls}" not implemented (yet).`);
470
+ }
471
+ }
472
+ if (deep) {
473
+ node.getChildTransformNodes(true).forEach(child => setMaterialRoughness(child, roughness, deep));
474
+ }
475
+ };
476
+ exports.setMaterialRoughness = setMaterialRoughness;
477
+ /**
478
+ * @param node
479
+ * @param layer
480
+ * @param color
481
+ * @param deep
482
+ */
483
+ const addToHighlightLayer = function (layer, color, node, deep = true) {
484
+ if (node instanceof abstractMesh_1.AbstractMesh) {
485
+ layer.addMesh(node, color);
486
+ }
487
+ if (deep) {
488
+ node.getChildTransformNodes(true).forEach(child => addToHighlightLayer(layer, color, child, deep));
489
+ }
490
+ };
491
+ exports.addToHighlightLayer = addToHighlightLayer;
492
+ /**
493
+ * @param node
494
+ * @param layer
495
+ * @param deep
496
+ */
497
+ const removeFromHighlightLayer = function (layer, node, deep = true) {
498
+ if (node instanceof abstractMesh_1.AbstractMesh) {
499
+ layer.removeMesh(node);
500
+ }
501
+ if (deep) {
502
+ node.getChildTransformNodes(true).forEach(child => removeFromHighlightLayer(layer, child, deep));
503
+ }
504
+ };
505
+ exports.removeFromHighlightLayer = removeFromHighlightLayer;
506
+ /**
507
+ * @param node
508
+ * @param receiveShadows
509
+ * @param deep
510
+ */
511
+ const setReceiveShadows = function (node, receiveShadows, deep = true) {
512
+ if (node instanceof abstractMesh_1.AbstractMesh) {
513
+ node.receiveShadows = receiveShadows;
514
+ }
515
+ if (deep) {
516
+ node.getChildTransformNodes(true).forEach(child => setReceiveShadows(child, receiveShadows, deep));
517
+ }
518
+ };
519
+ exports.setReceiveShadows = setReceiveShadows;
520
+ /**
521
+ * @param node
522
+ * @param generator
523
+ * @param deep
524
+ */
525
+ const addToShadowGenerator = function (generator, node, deep = true) {
526
+ if (node instanceof abstractMesh_1.AbstractMesh) {
527
+ // We have to remove the node because there's no duplicate check in babylon
528
+ generator.removeShadowCaster(node, false);
529
+ generator.addShadowCaster(node, false);
530
+ }
531
+ if (deep) {
532
+ node.getChildTransformNodes(true).forEach(child => addToShadowGenerator(generator, child, deep));
533
+ }
534
+ };
535
+ exports.addToShadowGenerator = addToShadowGenerator;
536
+ /**
537
+ * @param node
538
+ * @param generator
539
+ * @param deep
540
+ */
541
+ const removeFromShadowGenerator = function (generator, node, deep = true) {
542
+ if (node instanceof abstractMesh_1.AbstractMesh) {
543
+ generator.removeShadowCaster(node, false);
544
+ }
545
+ if (deep) {
546
+ node.getChildTransformNodes(true).forEach(child => removeFromShadowGenerator(generator, child, deep));
547
+ }
548
+ };
549
+ exports.removeFromShadowGenerator = removeFromShadowGenerator;
550
+ /**
551
+ * https://forum.babylonjs.com/t/get-mesh-bounding-box-position-and-size-in-2d-screen-coordinates/1058/3
552
+ * @param mesh
553
+ * @param scene
554
+ * @param canvas
555
+ */
556
+ const getClientRectFromMesh = function (mesh, scene, canvas) {
557
+ // get bounding box of the mesh
558
+ const meshVectors = mesh.getBoundingInfo().boundingBox.vectors;
559
+ // get the matrix and viewport needed to project the vectors onto the screen
560
+ const worldMatrix = mesh.getWorldMatrix();
561
+ const transformMatrix = scene.getTransformMatrix();
562
+ const viewport = scene.activeCamera.viewport;
563
+ // loop though all the vectors and project them against the current camera viewport to get a set of coordinates
564
+ const coordinates = meshVectors.map(vector => {
565
+ const projection = math_vector_1.Vector3.Project(vector, worldMatrix, transformMatrix, viewport);
566
+ projection.x = projection.x * canvas.clientWidth;
567
+ projection.y = projection.y * canvas.clientHeight;
568
+ return projection;
569
+ });
570
+ // get the min and max for all the coordinates so we can calculate the largest possible screen size
571
+ const maxX = Math.max(...coordinates.map(o => o.x));
572
+ const minX = Math.min(...coordinates.map(o => o.x));
573
+ const maxY = Math.max(...coordinates.map(o => o.y));
574
+ const minY = Math.min(...coordinates.map(o => o.y));
575
+ // return a ClientRect from this
576
+ return {
577
+ width: maxX - minX,
578
+ height: maxY - minY,
579
+ left: minX,
580
+ top: minY,
581
+ right: maxX,
582
+ bottom: maxY,
583
+ };
584
+ };
585
+ exports.getClientRectFromMesh = getClientRectFromMesh;
586
+ /**
587
+ * This type guard checks whether the given `BaseTextures` is any of its subtypes which comes with an
588
+ * `onLoadObservable`.
589
+ *
590
+ * !!! Timing of when this function is called is important !!!
591
+ * See the following for more details: https://forum.babylonjs.com/t/basetexture-whenallready-returns-too-early/34501/6
592
+ */
593
+ const isTextureWithOnLoadObservable = function (texture) {
594
+ return !!texture.onLoadObservable;
595
+ };
596
+ exports.isTextureWithOnLoadObservable = isTextureWithOnLoadObservable;
597
597
  //# sourceMappingURL=babylonHelper.js.map