@combeenation/3d-viewer 12.4.1 → 12.4.3

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 (114) hide show
  1. package/README.md +9 -9
  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 +702 -702
  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 +872 -872
  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 +215 -215
  29. package/dist/lib-cjs/api/classes/viewer.js +708 -708
  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 +344 -344
  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 +227 -227
  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 +80 -80
  41. package/dist/lib-cjs/api/manager/gltfExportManager.js +300 -299
  42. package/dist/lib-cjs/api/manager/gltfExportManager.js.map +1 -1
  43. package/dist/lib-cjs/api/manager/sceneManager.d.ts +33 -33
  44. package/dist/lib-cjs/api/manager/sceneManager.js +128 -128
  45. package/dist/lib-cjs/api/manager/tagManager.d.ts +118 -118
  46. package/dist/lib-cjs/api/manager/tagManager.js +530 -530
  47. package/dist/lib-cjs/api/manager/textureLoadManager.d.ts +22 -22
  48. package/dist/lib-cjs/api/manager/textureLoadManager.js +107 -107
  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 +238 -238
  54. package/dist/lib-cjs/api/util/babylonHelper.js +825 -825
  55. package/dist/lib-cjs/api/util/debugHelper.d.ts +9 -9
  56. package/dist/lib-cjs/api/util/debugHelper.js +93 -93
  57. package/dist/lib-cjs/api/util/deviceHelper.d.ts +9 -9
  58. package/dist/lib-cjs/api/util/deviceHelper.js +28 -28
  59. package/dist/lib-cjs/api/util/geometryHelper.d.ts +17 -17
  60. package/dist/lib-cjs/api/util/geometryHelper.js +112 -112
  61. package/dist/lib-cjs/api/util/globalTypes.d.ts +490 -490
  62. package/dist/lib-cjs/api/util/globalTypes.js +1 -1
  63. package/dist/lib-cjs/api/util/resourceHelper.d.ts +58 -58
  64. package/dist/lib-cjs/api/util/resourceHelper.js +214 -214
  65. package/dist/lib-cjs/api/util/sceneLoaderHelper.d.ts +58 -58
  66. package/dist/lib-cjs/api/util/sceneLoaderHelper.js +228 -228
  67. package/dist/lib-cjs/api/util/stringHelper.d.ts +13 -13
  68. package/dist/lib-cjs/api/util/stringHelper.js +32 -32
  69. package/dist/lib-cjs/api/util/structureHelper.d.ts +9 -9
  70. package/dist/lib-cjs/api/util/structureHelper.js +57 -57
  71. package/dist/lib-cjs/buildinfo.json +3 -3
  72. package/dist/lib-cjs/commonjs.tsconfig.tsbuildinfo +1 -1
  73. package/dist/lib-cjs/index.d.ts +63 -63
  74. package/dist/lib-cjs/index.js +128 -128
  75. package/package.json +93 -92
  76. package/src/api/classes/animationInterface.ts +10 -10
  77. package/src/api/classes/dottedPath.ts +181 -181
  78. package/src/api/classes/element.ts +766 -766
  79. package/src/api/classes/event.ts +457 -457
  80. package/src/api/classes/eventBroadcaster.ts +52 -52
  81. package/src/api/classes/fuzzyMap.ts +21 -21
  82. package/src/api/classes/parameter.ts +686 -686
  83. package/src/api/classes/parameterObservable.ts +73 -73
  84. package/src/api/classes/parameterizable.ts +87 -87
  85. package/src/api/classes/placementAnimation.ts +162 -162
  86. package/src/api/classes/variant.ts +965 -965
  87. package/src/api/classes/variantInstance.ts +123 -123
  88. package/src/api/classes/variantParameterizable.ts +83 -83
  89. package/src/api/classes/viewer.ts +751 -751
  90. package/src/api/classes/viewerError.ts +63 -63
  91. package/src/api/classes/viewerLight.ts +335 -335
  92. package/src/api/internal/debugViewer.ts +90 -90
  93. package/src/api/internal/lensRendering.ts +9 -9
  94. package/src/api/internal/sceneSetup.ts +208 -208
  95. package/src/api/manager/animationManager.ts +143 -143
  96. package/src/api/manager/gltfExportManager.ts +337 -334
  97. package/src/api/manager/sceneManager.ts +134 -134
  98. package/src/api/manager/tagManager.ts +572 -572
  99. package/src/api/manager/textureLoadManager.ts +107 -107
  100. package/src/api/manager/variantInstanceManager.ts +306 -306
  101. package/src/api/store/specStorage.ts +68 -68
  102. package/src/api/util/babylonHelper.ts +915 -915
  103. package/src/api/util/debugHelper.ts +121 -121
  104. package/src/api/util/deviceHelper.ts +31 -31
  105. package/src/api/util/geometryHelper.ts +142 -142
  106. package/src/api/util/globalTypes.ts +566 -566
  107. package/src/api/util/resourceHelper.ts +201 -201
  108. package/src/api/util/sceneLoaderHelper.ts +247 -247
  109. package/src/api/util/stringHelper.ts +30 -30
  110. package/src/api/util/structureHelper.ts +62 -62
  111. package/src/buildinfo.json +3 -3
  112. package/src/dev.ts +70 -70
  113. package/src/index.ts +116 -116
  114. package/src/types.d.ts +49 -49
@@ -1,121 +1,121 @@
1
- import { AxesViewer } from '@babylonjs/core/Debug/axesViewer';
2
- import { DynamicTexture } from '@babylonjs/core/Materials/Textures/dynamicTexture';
3
- import { StandardMaterial } from '@babylonjs/core/Materials/standardMaterial';
4
- import { Color3 } from '@babylonjs/core/Maths/math.color';
5
- import { Vector3 } from '@babylonjs/core/Maths/math.vector';
6
- import { MeshBuilder } from '@babylonjs/core/Meshes/meshBuilder';
7
- import { TransformNode } from '@babylonjs/core/Meshes/transformNode';
8
- import { UtilityLayerRenderer } from '@babylonjs/core/Rendering/utilityLayerRenderer';
9
-
10
- type DebugAxisKeys = 'X' | 'Y' | 'Z';
11
- type DebugAxisConfig = { color: Color3; position: Vector3 };
12
-
13
- const _DEBUG_AXIS_MAP: Record<DebugAxisKeys, DebugAxisConfig> = {
14
- X: { color: Color3.Red().scale(0.5), position: new Vector3(1, 0, 0) },
15
- Y: { color: Color3.Green().scale(0.5), position: new Vector3(0, 1, 0) },
16
- Z: { color: Color3.Blue().scale(0.5), position: new Vector3(0, 0, 1) },
17
- };
18
-
19
- const _WORLD_COORD_ROOT_KEY = '__world_coordinates__';
20
- let _AXES_VIEWER: AxesViewer;
21
-
22
- /**
23
- * Create debug coordinate system located in world origin.
24
- * This function is based on Babylon.js `AxesViewer`, axis texts are added manually.
25
- */
26
- export function showWorldCoordinates(dimension: number) {
27
- // make sure to remove already existing debug coordinate systems
28
- hideWorldCoordinates();
29
-
30
- // draw in utility layer, so that there is no interaction with the actually scene content (eg: glb export, autofocus)
31
- const utilityLayerScene = UtilityLayerRenderer.DefaultUtilityLayer.utilityLayerScene;
32
-
33
- const worldCoordRoot = new TransformNode('__world_coordinates__', utilityLayerScene);
34
- // axes viewer coordinate system is a bit too large
35
- // multiply with unify factor to create arrows which exactly match the length of the input dimension
36
- const factor = _getWorldCoordinatesAxesUnifyFactor();
37
- _AXES_VIEWER = new AxesViewer(utilityLayerScene, dimension * factor);
38
-
39
- _prepareWorldCoordinateAxis('X', _AXES_VIEWER.xAxis, worldCoordRoot, dimension, utilityLayerScene);
40
- _prepareWorldCoordinateAxis('Y', _AXES_VIEWER.yAxis, worldCoordRoot, dimension, utilityLayerScene);
41
- _prepareWorldCoordinateAxis('Z', _AXES_VIEWER.zAxis, worldCoordRoot, dimension, utilityLayerScene);
42
- }
43
-
44
- /**
45
- * Remove meshes and materials that are associated with the debug world coordinates system
46
- */
47
- export function hideWorldCoordinates() {
48
- const utilityLayerScene = UtilityLayerRenderer.DefaultUtilityLayer.utilityLayerScene;
49
-
50
- _AXES_VIEWER?.dispose();
51
-
52
- const worldCoordRoot = utilityLayerScene.getTransformNodeByName(_WORLD_COORD_ROOT_KEY);
53
- if (worldCoordRoot) {
54
- worldCoordRoot.dispose(false, true);
55
- }
56
- }
57
-
58
- /**
59
- * Adjust and enhance coordinate axes to fulfill our needs.
60
- * - moves nodes into common root
61
- * - adds text node
62
- */
63
- function _prepareWorldCoordinateAxis(
64
- text: 'X' | 'Y' | 'Z',
65
- axis: TransformNode,
66
- root: TransformNode,
67
- dimension: number,
68
- utilityLayerScene: Scene
69
- ) {
70
- // create unique names
71
- axis.name = `${_WORLD_COORD_ROOT_KEY}.${text}`;
72
- axis.parent = root;
73
-
74
- // create text mesh via dynamic texture
75
- const dynamicTexture = new DynamicTexture(`${_WORLD_COORD_ROOT_KEY}.${text}`, 50, utilityLayerScene, true);
76
- dynamicTexture.hasAlpha = true;
77
- // 42.5 is a magic offset, so that the text is vertically centered for font size 50px
78
- // horizontal centering works well with the standard behaviour of Babylon.js (setting "null" as "width")
79
- dynamicTexture.drawText(text, null, 42.5, 'bold 50px Arial', 'white', 'transparent', true);
80
-
81
- const material = new StandardMaterial(`${_WORLD_COORD_ROOT_KEY}.${text}`, utilityLayerScene);
82
- material.disableLighting = true;
83
- material.emissiveColor = _DEBUG_AXIS_MAP[text].color;
84
- material.diffuseTexture = dynamicTexture;
85
-
86
- const plane = MeshBuilder.CreatePlane(
87
- `${_WORLD_COORD_ROOT_KEY}.${text}.TextPlane`,
88
- { size: dimension / 10 },
89
- utilityLayerScene
90
- );
91
- // make sure that text is located outside of arrow
92
- plane.position = _DEBUG_AXIS_MAP[text].position.multiplyByFloats(
93
- dimension * 1.05,
94
- dimension * 1.05,
95
- dimension * 1.05
96
- );
97
- plane.parent = root;
98
- plane.material = material;
99
- // will be rendered on top of "default" meshes => taken from Babylon.js repo "AxesViewer" implementation
100
- plane.renderingGroupId = 2;
101
- // setting billboard mode ensures, that the text is always readable
102
- plane.billboardMode = TransformNode.BILLBOARDMODE_ALL;
103
- }
104
-
105
- /**
106
- * Calculate factor for creating world coordinate axes with exactly one unit in length
107
- */
108
- function _getWorldCoordinatesAxesUnifyFactor() {
109
- // These constants are taken from the Babylon.js repository
110
- const coneZPosOffset = 0.3;
111
- const coneHeight = 0.075;
112
- const baseScaling = 4;
113
-
114
- // cone is moved 0.3 units away from the center
115
- // the origin of the cone will be located on 1.2, if combined with the base scaling of 4
116
- // invert that offset to get the origin to 1
117
- // also consider the cone height to move the tip of the cone to 1 instead the center
118
- const factor = 1 / ((coneZPosOffset + coneHeight / 2) * baseScaling);
119
-
120
- return factor;
121
- }
1
+ import { AxesViewer } from '@babylonjs/core/Debug/axesViewer';
2
+ import { DynamicTexture } from '@babylonjs/core/Materials/Textures/dynamicTexture';
3
+ import { StandardMaterial } from '@babylonjs/core/Materials/standardMaterial';
4
+ import { Color3 } from '@babylonjs/core/Maths/math.color';
5
+ import { Vector3 } from '@babylonjs/core/Maths/math.vector';
6
+ import { MeshBuilder } from '@babylonjs/core/Meshes/meshBuilder';
7
+ import { TransformNode } from '@babylonjs/core/Meshes/transformNode';
8
+ import { UtilityLayerRenderer } from '@babylonjs/core/Rendering/utilityLayerRenderer';
9
+
10
+ type DebugAxisKeys = 'X' | 'Y' | 'Z';
11
+ type DebugAxisConfig = { color: Color3; position: Vector3 };
12
+
13
+ const _DEBUG_AXIS_MAP: Record<DebugAxisKeys, DebugAxisConfig> = {
14
+ X: { color: Color3.Red().scale(0.5), position: new Vector3(1, 0, 0) },
15
+ Y: { color: Color3.Green().scale(0.5), position: new Vector3(0, 1, 0) },
16
+ Z: { color: Color3.Blue().scale(0.5), position: new Vector3(0, 0, 1) },
17
+ };
18
+
19
+ const _WORLD_COORD_ROOT_KEY = '__world_coordinates__';
20
+ let _AXES_VIEWER: AxesViewer;
21
+
22
+ /**
23
+ * Create debug coordinate system located in world origin.
24
+ * This function is based on Babylon.js `AxesViewer`, axis texts are added manually.
25
+ */
26
+ export function showWorldCoordinates(dimension: number) {
27
+ // make sure to remove already existing debug coordinate systems
28
+ hideWorldCoordinates();
29
+
30
+ // draw in utility layer, so that there is no interaction with the actually scene content (eg: glb export, autofocus)
31
+ const utilityLayerScene = UtilityLayerRenderer.DefaultUtilityLayer.utilityLayerScene;
32
+
33
+ const worldCoordRoot = new TransformNode('__world_coordinates__', utilityLayerScene);
34
+ // axes viewer coordinate system is a bit too large
35
+ // multiply with unify factor to create arrows which exactly match the length of the input dimension
36
+ const factor = _getWorldCoordinatesAxesUnifyFactor();
37
+ _AXES_VIEWER = new AxesViewer(utilityLayerScene, dimension * factor);
38
+
39
+ _prepareWorldCoordinateAxis('X', _AXES_VIEWER.xAxis, worldCoordRoot, dimension, utilityLayerScene);
40
+ _prepareWorldCoordinateAxis('Y', _AXES_VIEWER.yAxis, worldCoordRoot, dimension, utilityLayerScene);
41
+ _prepareWorldCoordinateAxis('Z', _AXES_VIEWER.zAxis, worldCoordRoot, dimension, utilityLayerScene);
42
+ }
43
+
44
+ /**
45
+ * Remove meshes and materials that are associated with the debug world coordinates system
46
+ */
47
+ export function hideWorldCoordinates() {
48
+ const utilityLayerScene = UtilityLayerRenderer.DefaultUtilityLayer.utilityLayerScene;
49
+
50
+ _AXES_VIEWER?.dispose();
51
+
52
+ const worldCoordRoot = utilityLayerScene.getTransformNodeByName(_WORLD_COORD_ROOT_KEY);
53
+ if (worldCoordRoot) {
54
+ worldCoordRoot.dispose(false, true);
55
+ }
56
+ }
57
+
58
+ /**
59
+ * Adjust and enhance coordinate axes to fulfill our needs.
60
+ * - moves nodes into common root
61
+ * - adds text node
62
+ */
63
+ function _prepareWorldCoordinateAxis(
64
+ text: 'X' | 'Y' | 'Z',
65
+ axis: TransformNode,
66
+ root: TransformNode,
67
+ dimension: number,
68
+ utilityLayerScene: Scene
69
+ ) {
70
+ // create unique names
71
+ axis.name = `${_WORLD_COORD_ROOT_KEY}.${text}`;
72
+ axis.parent = root;
73
+
74
+ // create text mesh via dynamic texture
75
+ const dynamicTexture = new DynamicTexture(`${_WORLD_COORD_ROOT_KEY}.${text}`, 50, utilityLayerScene, true);
76
+ dynamicTexture.hasAlpha = true;
77
+ // 42.5 is a magic offset, so that the text is vertically centered for font size 50px
78
+ // horizontal centering works well with the standard behaviour of Babylon.js (setting "null" as "width")
79
+ dynamicTexture.drawText(text, null, 42.5, 'bold 50px Arial', 'white', 'transparent', true);
80
+
81
+ const material = new StandardMaterial(`${_WORLD_COORD_ROOT_KEY}.${text}`, utilityLayerScene);
82
+ material.disableLighting = true;
83
+ material.emissiveColor = _DEBUG_AXIS_MAP[text].color;
84
+ material.diffuseTexture = dynamicTexture;
85
+
86
+ const plane = MeshBuilder.CreatePlane(
87
+ `${_WORLD_COORD_ROOT_KEY}.${text}.TextPlane`,
88
+ { size: dimension / 10 },
89
+ utilityLayerScene
90
+ );
91
+ // make sure that text is located outside of arrow
92
+ plane.position = _DEBUG_AXIS_MAP[text].position.multiplyByFloats(
93
+ dimension * 1.05,
94
+ dimension * 1.05,
95
+ dimension * 1.05
96
+ );
97
+ plane.parent = root;
98
+ plane.material = material;
99
+ // will be rendered on top of "default" meshes => taken from Babylon.js repo "AxesViewer" implementation
100
+ plane.renderingGroupId = 2;
101
+ // setting billboard mode ensures, that the text is always readable
102
+ plane.billboardMode = TransformNode.BILLBOARDMODE_ALL;
103
+ }
104
+
105
+ /**
106
+ * Calculate factor for creating world coordinate axes with exactly one unit in length
107
+ */
108
+ function _getWorldCoordinatesAxesUnifyFactor() {
109
+ // These constants are taken from the Babylon.js repository
110
+ const coneZPosOffset = 0.3;
111
+ const coneHeight = 0.075;
112
+ const baseScaling = 4;
113
+
114
+ // cone is moved 0.3 units away from the center
115
+ // the origin of the cone will be located on 1.2, if combined with the base scaling of 4
116
+ // invert that offset to get the origin to 1
117
+ // also consider the cone height to move the tip of the cone to 1 instead the center
118
+ const factor = 1 / ((coneZPosOffset + coneHeight / 2) * baseScaling);
119
+
120
+ return factor;
121
+ }
@@ -1,31 +1,31 @@
1
- import { SpecStorage } from '../store/specStorage';
2
-
3
- /**
4
- * Checks if the current device is an iPhone
5
- */
6
- const getIsIPhone = () => {
7
- const isIPhone = /iPhone/.test(navigator.userAgent);
8
-
9
- return isIPhone;
10
- };
11
-
12
- /**
13
- * Checks if the current device is "scaled down", as defined in the Spec
14
- */
15
- const getIsScaledDownDevice = () => {
16
- const sceneJson = SpecStorage.get<SceneJson>('scene');
17
-
18
- if (!sceneJson.engine?.limitTextureSize) {
19
- // no down scaling defined
20
- return false;
21
- }
22
-
23
- // check device
24
- const deviceFits =
25
- sceneJson.engine.limitTextureSize.devices === 'all' ||
26
- (sceneJson.engine.limitTextureSize.devices === 'iPhone' && getIsIPhone());
27
-
28
- return deviceFits;
29
- };
30
-
31
- export { getIsIPhone, getIsScaledDownDevice };
1
+ import { SpecStorage } from '../store/specStorage';
2
+
3
+ /**
4
+ * Checks if the current device is an iPhone
5
+ */
6
+ const getIsIPhone = () => {
7
+ const isIPhone = /iPhone/.test(navigator.userAgent);
8
+
9
+ return isIPhone;
10
+ };
11
+
12
+ /**
13
+ * Checks if the current device is "scaled down", as defined in the Spec
14
+ */
15
+ const getIsScaledDownDevice = () => {
16
+ const sceneJson = SpecStorage.get<SceneJson>('scene');
17
+
18
+ if (!sceneJson.engine?.limitTextureSize) {
19
+ // no down scaling defined
20
+ return false;
21
+ }
22
+
23
+ // check device
24
+ const deviceFits =
25
+ sceneJson.engine.limitTextureSize.devices === 'all' ||
26
+ (sceneJson.engine.limitTextureSize.devices === 'iPhone' && getIsIPhone());
27
+
28
+ return deviceFits;
29
+ };
30
+
31
+ export { getIsIPhone, getIsScaledDownDevice };
@@ -1,142 +1,142 @@
1
- import { VertexBuffer } from '@babylonjs/core/Buffers/buffer';
2
- import { Vector3 } from '@babylonjs/core/Maths/math.vector';
3
- import type { Geometry } from '@babylonjs/core/Meshes/geometry';
4
- import { InstancedMesh } from '@babylonjs/core/Meshes/instancedMesh';
5
- import { Mesh } from '@babylonjs/core/Meshes/mesh';
6
- import { TransformNode } from '@babylonjs/core/Meshes/transformNode';
7
- import type { MorphTarget } from '@babylonjs/core/Morph/morphTarget';
8
- import { MorphTargetManager } from '@babylonjs/core/Morph/morphTargetManager';
9
- import type { FloatArray } from '@babylonjs/core/types';
10
-
11
- /**
12
- * Creates a "standard" mesh from an instanced mesh, by cloning the source mesh and applying transformation data
13
- */
14
- const createMeshFromInstancedMesh = function (
15
- instancedMesh: InstancedMesh,
16
- newName: string,
17
- clonedParent: Nullable<TransformNode>
18
- ) {
19
- // first create a clone of the source mesh
20
- const newMesh = instancedMesh.sourceMesh.clone(newName, clonedParent, true);
21
- // apply the transformation data, it's important to create clones of the transformations to not touch the original
22
- // transformation when applying changes (eg: geometry baking)
23
- newMesh.position = instancedMesh.position.clone();
24
- newMesh.rotation = instancedMesh.rotation.clone();
25
- newMesh.scaling = instancedMesh.scaling.clone();
26
-
27
- // rotation quaternion is optional
28
- if (instancedMesh.rotationQuaternion) {
29
- newMesh.rotationQuaternion = instancedMesh.rotationQuaternion.clone();
30
- }
31
-
32
- // also sync the enabled state from the original instanced mesh
33
- newMesh.setEnabled(instancedMesh.isEnabled(false));
34
-
35
- return newMesh;
36
- };
37
-
38
- /**
39
- * Removes transformation data from the mesh and stores it in the geometry, which is called "baking".
40
- * Also considers the geometry change from morph targets.
41
- */
42
- const bakeGeometryOfMesh = function (mesh: Mesh) {
43
- if (!mesh.geometry) {
44
- // no geometry available, nothing to do
45
- return;
46
- }
47
-
48
- // geometries can be shared across multiple meshes, first make them unique to avoid side-effects
49
- mesh.makeGeometryUnique();
50
-
51
- // Babylon.js already provides a function for baking the current skeleton changes into the geometry
52
- if (mesh.skeleton) {
53
- mesh.applySkeleton(mesh.skeleton);
54
- mesh.skeleton = null;
55
- }
56
-
57
- // NOTE: in difference to skeletons and transformations there is no baking function for morph targets (yet)
58
- // however another approach could be to re-apply the position and normals data, as there are nice functions for it
59
- // - `getPositionData(applySkeleton: boolean = false, applyMorph: boolean = false)`
60
- // - `getNormalsData(applySkeleton: boolean = false, applyMorph: boolean = false)`
61
- // you can decide if skeletons and morph targets can be added, which is exactly what we want
62
- // I'm still hesitant to use it because "tangent" and "UV" kinds are not supported, whereas I'm not sure if it's
63
- // required
64
- // => try it out when there is enough time for detailed regression tests!
65
- const morphTargetManager = mesh.morphTargetManager;
66
- const geometry = mesh.geometry;
67
-
68
- if (morphTargetManager?.numTargets) {
69
- // apply morph target vertices data to mesh geometry
70
- // mostly only the "PositionKind" is implemented
71
- _bakeMorphTargetManagerIntoVertices(VertexBuffer.PositionKind, morphTargetManager, geometry);
72
- _bakeMorphTargetManagerIntoVertices(VertexBuffer.NormalKind, morphTargetManager, geometry);
73
- _bakeMorphTargetManagerIntoVertices(VertexBuffer.TangentKind, morphTargetManager, geometry);
74
- _bakeMorphTargetManagerIntoVertices(VertexBuffer.UVKind, morphTargetManager, geometry);
75
-
76
- // remove morph target manager with all it's morph targets
77
- mesh.morphTargetManager = null;
78
- }
79
-
80
- // bake the transformation data in the mesh geometry, fortunately there is already a help function from Babylon.js
81
- mesh.bakeCurrentTransformIntoVertices();
82
- };
83
-
84
- /**
85
- * Resets transformation to initial state
86
- */
87
- const resetTransformation = function (node: TransformNode) {
88
- node.position = new Vector3(0, 0, 0);
89
- node.rotation = new Vector3(0, 0, 0);
90
- node.rotationQuaternion = null;
91
- node.scaling = new Vector3(1, 1, 1);
92
- };
93
-
94
- /**
95
- * @param kind morph targets can affect various vertices kinds, whereas "position" is the most common one
96
- * still other kinds (like normals or tangents) can be affected as well and can be provided on this input
97
- */
98
- const _bakeMorphTargetManagerIntoVertices = function (
99
- kind: string,
100
- morphTargetManager: MorphTargetManager,
101
- geometry: Geometry
102
- ) {
103
- const origVerticesData = geometry.getVerticesData(kind);
104
- if (!origVerticesData) {
105
- // no vertices data for this kind availabe on the mesh geometry
106
- return;
107
- }
108
-
109
- let verticesData = [...origVerticesData];
110
- for (let i = 0; i < morphTargetManager.numTargets; i++) {
111
- const target = morphTargetManager.getTarget(i);
112
- const targetVerticesData = _getVerticesDataFromMorphTarget(kind, target);
113
- if (targetVerticesData) {
114
- // vertices data of this kind are implemented on the morph target
115
- // add the influence of this morph target to the vertices data
116
- // formula is taken from: https://doc.babylonjs.com/features/featuresDeepDive/mesh/morphTargets#basics
117
- verticesData = verticesData.map(
118
- (oldVal, idx) => oldVal + (targetVerticesData[idx] - origVerticesData[idx]) * target.influence
119
- );
120
- }
121
- }
122
-
123
- // apply the updated vertices data
124
- geometry.setVerticesData(kind, verticesData);
125
- };
126
-
127
- const _getVerticesDataFromMorphTarget = function (kind: string, morphTarget: MorphTarget): Nullable<FloatArray> {
128
- switch (kind) {
129
- case VertexBuffer.PositionKind:
130
- return morphTarget.getPositions();
131
- case VertexBuffer.NormalKind:
132
- return morphTarget.getNormals();
133
- case VertexBuffer.TangentKind:
134
- return morphTarget.getTangents();
135
- case VertexBuffer.UVKind:
136
- return morphTarget.getUVs();
137
- }
138
-
139
- return null;
140
- };
141
-
142
- export { createMeshFromInstancedMesh, bakeGeometryOfMesh, resetTransformation };
1
+ import { VertexBuffer } from '@babylonjs/core/Buffers/buffer';
2
+ import { Vector3 } from '@babylonjs/core/Maths/math.vector';
3
+ import type { Geometry } from '@babylonjs/core/Meshes/geometry';
4
+ import { InstancedMesh } from '@babylonjs/core/Meshes/instancedMesh';
5
+ import { Mesh } from '@babylonjs/core/Meshes/mesh';
6
+ import { TransformNode } from '@babylonjs/core/Meshes/transformNode';
7
+ import type { MorphTarget } from '@babylonjs/core/Morph/morphTarget';
8
+ import { MorphTargetManager } from '@babylonjs/core/Morph/morphTargetManager';
9
+ import type { FloatArray } from '@babylonjs/core/types';
10
+
11
+ /**
12
+ * Creates a "standard" mesh from an instanced mesh, by cloning the source mesh and applying transformation data
13
+ */
14
+ const createMeshFromInstancedMesh = function (
15
+ instancedMesh: InstancedMesh,
16
+ newName: string,
17
+ clonedParent: Nullable<TransformNode>
18
+ ) {
19
+ // first create a clone of the source mesh
20
+ const newMesh = instancedMesh.sourceMesh.clone(newName, clonedParent, true);
21
+ // apply the transformation data, it's important to create clones of the transformations to not touch the original
22
+ // transformation when applying changes (eg: geometry baking)
23
+ newMesh.position = instancedMesh.position.clone();
24
+ newMesh.rotation = instancedMesh.rotation.clone();
25
+ newMesh.scaling = instancedMesh.scaling.clone();
26
+
27
+ // rotation quaternion is optional
28
+ if (instancedMesh.rotationQuaternion) {
29
+ newMesh.rotationQuaternion = instancedMesh.rotationQuaternion.clone();
30
+ }
31
+
32
+ // also sync the enabled state from the original instanced mesh
33
+ newMesh.setEnabled(instancedMesh.isEnabled(false));
34
+
35
+ return newMesh;
36
+ };
37
+
38
+ /**
39
+ * Removes transformation data from the mesh and stores it in the geometry, which is called "baking".
40
+ * Also considers the geometry change from morph targets.
41
+ */
42
+ const bakeGeometryOfMesh = function (mesh: Mesh) {
43
+ if (!mesh.geometry) {
44
+ // no geometry available, nothing to do
45
+ return;
46
+ }
47
+
48
+ // geometries can be shared across multiple meshes, first make them unique to avoid side-effects
49
+ mesh.makeGeometryUnique();
50
+
51
+ // Babylon.js already provides a function for baking the current skeleton changes into the geometry
52
+ if (mesh.skeleton) {
53
+ mesh.applySkeleton(mesh.skeleton);
54
+ mesh.skeleton = null;
55
+ }
56
+
57
+ // NOTE: in difference to skeletons and transformations there is no baking function for morph targets (yet)
58
+ // however another approach could be to re-apply the position and normals data, as there are nice functions for it
59
+ // - `getPositionData(applySkeleton: boolean = false, applyMorph: boolean = false)`
60
+ // - `getNormalsData(applySkeleton: boolean = false, applyMorph: boolean = false)`
61
+ // you can decide if skeletons and morph targets can be added, which is exactly what we want
62
+ // I'm still hesitant to use it because "tangent" and "UV" kinds are not supported, whereas I'm not sure if it's
63
+ // required
64
+ // => try it out when there is enough time for detailed regression tests!
65
+ const morphTargetManager = mesh.morphTargetManager;
66
+ const geometry = mesh.geometry;
67
+
68
+ if (morphTargetManager?.numTargets) {
69
+ // apply morph target vertices data to mesh geometry
70
+ // mostly only the "PositionKind" is implemented
71
+ _bakeMorphTargetManagerIntoVertices(VertexBuffer.PositionKind, morphTargetManager, geometry);
72
+ _bakeMorphTargetManagerIntoVertices(VertexBuffer.NormalKind, morphTargetManager, geometry);
73
+ _bakeMorphTargetManagerIntoVertices(VertexBuffer.TangentKind, morphTargetManager, geometry);
74
+ _bakeMorphTargetManagerIntoVertices(VertexBuffer.UVKind, morphTargetManager, geometry);
75
+
76
+ // remove morph target manager with all it's morph targets
77
+ mesh.morphTargetManager = null;
78
+ }
79
+
80
+ // bake the transformation data in the mesh geometry, fortunately there is already a help function from Babylon.js
81
+ mesh.bakeCurrentTransformIntoVertices();
82
+ };
83
+
84
+ /**
85
+ * Resets transformation to initial state
86
+ */
87
+ const resetTransformation = function (node: TransformNode) {
88
+ node.position = new Vector3(0, 0, 0);
89
+ node.rotation = new Vector3(0, 0, 0);
90
+ node.rotationQuaternion = null;
91
+ node.scaling = new Vector3(1, 1, 1);
92
+ };
93
+
94
+ /**
95
+ * @param kind morph targets can affect various vertices kinds, whereas "position" is the most common one
96
+ * still other kinds (like normals or tangents) can be affected as well and can be provided on this input
97
+ */
98
+ const _bakeMorphTargetManagerIntoVertices = function (
99
+ kind: string,
100
+ morphTargetManager: MorphTargetManager,
101
+ geometry: Geometry
102
+ ) {
103
+ const origVerticesData = geometry.getVerticesData(kind);
104
+ if (!origVerticesData) {
105
+ // no vertices data for this kind availabe on the mesh geometry
106
+ return;
107
+ }
108
+
109
+ let verticesData = [...origVerticesData];
110
+ for (let i = 0; i < morphTargetManager.numTargets; i++) {
111
+ const target = morphTargetManager.getTarget(i);
112
+ const targetVerticesData = _getVerticesDataFromMorphTarget(kind, target);
113
+ if (targetVerticesData) {
114
+ // vertices data of this kind are implemented on the morph target
115
+ // add the influence of this morph target to the vertices data
116
+ // formula is taken from: https://doc.babylonjs.com/features/featuresDeepDive/mesh/morphTargets#basics
117
+ verticesData = verticesData.map(
118
+ (oldVal, idx) => oldVal + (targetVerticesData[idx] - origVerticesData[idx]) * target.influence
119
+ );
120
+ }
121
+ }
122
+
123
+ // apply the updated vertices data
124
+ geometry.setVerticesData(kind, verticesData);
125
+ };
126
+
127
+ const _getVerticesDataFromMorphTarget = function (kind: string, morphTarget: MorphTarget): Nullable<FloatArray> {
128
+ switch (kind) {
129
+ case VertexBuffer.PositionKind:
130
+ return morphTarget.getPositions();
131
+ case VertexBuffer.NormalKind:
132
+ return morphTarget.getNormals();
133
+ case VertexBuffer.TangentKind:
134
+ return morphTarget.getTangents();
135
+ case VertexBuffer.UVKind:
136
+ return morphTarget.getUVs();
137
+ }
138
+
139
+ return null;
140
+ };
141
+
142
+ export { createMeshFromInstancedMesh, bakeGeometryOfMesh, resetTransformation };