@combeenation/3d-viewer 9.0.1 → 9.0.2-alpha1

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