@combeenation/3d-viewer 7.0.0-beta1 → 7.0.0-beta2

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