@combeenation/3d-viewer 9.1.0 → 9.2.0-beta1
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.
- package/dist/lib-cjs/api/manager/gltfExportManager.d.ts +1 -5
- package/dist/lib-cjs/api/manager/gltfExportManager.js +4 -29
- package/dist/lib-cjs/api/manager/gltfExportManager.js.map +1 -1
- package/dist/lib-cjs/api/util/geometryHelper.d.ts +7 -0
- package/dist/lib-cjs/api/util/geometryHelper.js +112 -0
- package/dist/lib-cjs/api/util/geometryHelper.js.map +1 -0
- package/dist/lib-cjs/api/util/structureHelper.js +6 -1
- package/dist/lib-cjs/api/util/structureHelper.js.map +1 -1
- package/dist/lib-cjs/buildinfo.json +1 -1
- package/dist/lib-cjs/commonjs.tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/api/manager/gltfExportManager.ts +11 -31
- package/src/api/util/geometryHelper.ts +136 -0
- package/src/api/util/structureHelper.ts +5 -1
package/package.json
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { bakeGeometryOfAllMeshes } from '..//util/geometryHelper';
|
|
1
2
|
import { Viewer } from '../classes/viewer';
|
|
2
3
|
import { injectMetadata } from '../util/babylonHelper';
|
|
3
4
|
import { isNodeIncludedInExclusionList } from '../util/structureHelper';
|
|
@@ -5,11 +6,10 @@ import { Engine } from '@babylonjs/core/Engines/engine';
|
|
|
5
6
|
import { SceneLoader } from '@babylonjs/core/Loading/sceneLoader';
|
|
6
7
|
import { PBRMaterial } from '@babylonjs/core/Materials/PBR/pbrMaterial';
|
|
7
8
|
import { Color3 } from '@babylonjs/core/Maths/math.color';
|
|
8
|
-
import { Vector3 } from '@babylonjs/core/Maths/math.vector';
|
|
9
9
|
import { AbstractMesh } from '@babylonjs/core/Meshes/abstractMesh';
|
|
10
|
-
import { Mesh } from '@babylonjs/core/Meshes/mesh';
|
|
11
10
|
import { TransformNode } from '@babylonjs/core/Meshes/transformNode';
|
|
12
11
|
import { SceneSerializer } from '@babylonjs/core/Misc/sceneSerializer';
|
|
12
|
+
import { Scene } from '@babylonjs/core/scene';
|
|
13
13
|
import { GLTF2Export, IExportOptions } from '@babylonjs/serializers/glTF/2.0';
|
|
14
14
|
import { has, merge } from 'lodash-es';
|
|
15
15
|
|
|
@@ -162,7 +162,8 @@ export class GltfExportManager {
|
|
|
162
162
|
|
|
163
163
|
if (optimizeForAR) {
|
|
164
164
|
this.setUniqueMeshNames(copiedScene);
|
|
165
|
-
|
|
165
|
+
// get rid of negative scalings and morph targets, since iOS can't handle it in the QuickLook app
|
|
166
|
+
bakeGeometryOfAllMeshes(copiedScene);
|
|
166
167
|
}
|
|
167
168
|
|
|
168
169
|
return { scene: copiedScene, sceneCopied: true };
|
|
@@ -199,10 +200,13 @@ export class GltfExportManager {
|
|
|
199
200
|
}
|
|
200
201
|
|
|
201
202
|
SceneLoader.ShowLoadingScreen = false;
|
|
202
|
-
const newScene = await SceneLoader.LoadAsync(
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
203
|
+
const newScene = await SceneLoader.LoadAsync(
|
|
204
|
+
'',
|
|
205
|
+
'data:' + JSON.stringify(serializedScene),
|
|
206
|
+
engine,
|
|
207
|
+
undefined,
|
|
208
|
+
'.babylon'
|
|
209
|
+
);
|
|
206
210
|
|
|
207
211
|
return newScene;
|
|
208
212
|
}
|
|
@@ -297,28 +301,4 @@ export class GltfExportManager {
|
|
|
297
301
|
currMesh.name = `${currMesh.name}_${currMesh.uniqueId}`;
|
|
298
302
|
});
|
|
299
303
|
}
|
|
300
|
-
|
|
301
|
-
/**
|
|
302
|
-
* Stores the effect of all mesh transformations in their geometry and reset the transformation afterwards.
|
|
303
|
-
* In this way negative scalings which lead to problems in AR on iOS devices can be eliminated.
|
|
304
|
-
*/
|
|
305
|
-
protected bakeTransformations(scene: Scene): void {
|
|
306
|
-
scene.meshes.forEach(mesh => {
|
|
307
|
-
if (mesh instanceof Mesh && mesh.geometry) {
|
|
308
|
-
// geometries can be shared across multiple meshes, first make them unique to avoid side-effects
|
|
309
|
-
mesh.makeGeometryUnique();
|
|
310
|
-
// this is the core functionality for storing the transformation data in the mesh geometry
|
|
311
|
-
mesh.bakeCurrentTransformIntoVertices();
|
|
312
|
-
}
|
|
313
|
-
});
|
|
314
|
-
|
|
315
|
-
// some nodes (mostly transform nodes), are not affected by the baking algorithm since they have no geometry
|
|
316
|
-
// => reset their transformation manually
|
|
317
|
-
const allNodes = scene.getNodes().filter(node => node instanceof TransformNode) as TransformNode[];
|
|
318
|
-
allNodes.forEach(node => {
|
|
319
|
-
node.position = new Vector3(0, 0, 0);
|
|
320
|
-
node.rotation = new Vector3(0, 0, 0);
|
|
321
|
-
node.scaling = new Vector3(1, 1, 1);
|
|
322
|
-
});
|
|
323
|
-
}
|
|
324
304
|
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { MorphTargetManager } from '@babylonjs/core';
|
|
2
|
+
import { VertexBuffer } from '@babylonjs/core/Buffers/buffer';
|
|
3
|
+
import { Vector3 } from '@babylonjs/core/Maths/math.vector';
|
|
4
|
+
import type { Geometry } from '@babylonjs/core/Meshes';
|
|
5
|
+
import { InstancedMesh } from '@babylonjs/core/Meshes/instancedMesh';
|
|
6
|
+
import { Mesh } from '@babylonjs/core/Meshes/mesh';
|
|
7
|
+
import { TransformNode } from '@babylonjs/core/Meshes/transformNode';
|
|
8
|
+
import type { MorphTarget } from '@babylonjs/core/Morph';
|
|
9
|
+
import type { FloatArray } from '@babylonjs/core/types';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Removes morph targets and sets transformation values back to their default values.
|
|
13
|
+
* The effects from morph targets and tranformation values will be "baked" in the geometry, so that the final appearance
|
|
14
|
+
* of the meshes stay the same.
|
|
15
|
+
*/
|
|
16
|
+
const bakeGeometryOfAllMeshes = function (scene: Scene) {
|
|
17
|
+
// instanced meshes have to be converted, since they share the geometry of the source mesh, which would lead to issues
|
|
18
|
+
// when baking the transformation values
|
|
19
|
+
scene.meshes.forEach(mesh => {
|
|
20
|
+
if (mesh instanceof InstancedMesh) {
|
|
21
|
+
convertInstancedMeshToMesh(mesh);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
// do the geometry baking for all meshes in the scene
|
|
26
|
+
scene.meshes.forEach(mesh => {
|
|
27
|
+
if (mesh instanceof Mesh) {
|
|
28
|
+
bakeGeometryOfMesh(mesh);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
// some nodes (mostly transform nodes), are not affected by the baking algorithm since they have no geometry
|
|
33
|
+
// => reset their transformation manually
|
|
34
|
+
const allNodes = scene.getNodes().filter(node => node instanceof TransformNode) as TransformNode[];
|
|
35
|
+
allNodes.forEach(node => {
|
|
36
|
+
resetTransformation(node);
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const convertInstancedMeshToMesh = function (instancedMesh: InstancedMesh) {
|
|
41
|
+
// first create a clone of the source mesh
|
|
42
|
+
const newMesh = instancedMesh.sourceMesh.clone(`${instancedMesh.name}`, instancedMesh.parent);
|
|
43
|
+
// apply the transformation data
|
|
44
|
+
newMesh.position = instancedMesh.position;
|
|
45
|
+
newMesh.rotation = instancedMesh.rotation;
|
|
46
|
+
newMesh.rotationQuaternion = instancedMesh.rotationQuaternion;
|
|
47
|
+
newMesh.scaling = instancedMesh.scaling;
|
|
48
|
+
// also sync the enabled state from the original instanced mesh
|
|
49
|
+
newMesh.setEnabled(instancedMesh.isEnabled(false));
|
|
50
|
+
|
|
51
|
+
// re-assign children of the original mesh, since that one won't be used anymore
|
|
52
|
+
instancedMesh.getChildren(undefined).forEach(childNode => (childNode.parent = newMesh));
|
|
53
|
+
// finally delete the original instanced mesh
|
|
54
|
+
instancedMesh.dispose();
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const bakeGeometryOfMesh = function (mesh: Mesh) {
|
|
58
|
+
if (!mesh.geometry) {
|
|
59
|
+
// no geometry available, nothing to do
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// geometries can be shared across multiple meshes, first make them unique to avoid side-effects
|
|
64
|
+
mesh.makeGeometryUnique();
|
|
65
|
+
|
|
66
|
+
const morphTargetManager = mesh.morphTargetManager;
|
|
67
|
+
const geometry = mesh.geometry;
|
|
68
|
+
|
|
69
|
+
if (morphTargetManager?.numTargets) {
|
|
70
|
+
// apply morph target vertices data to mesh geometry
|
|
71
|
+
// mostly only the "PositionKind" is implemented
|
|
72
|
+
bakeMorphTargetManagerIntoVertices(VertexBuffer.PositionKind, morphTargetManager, geometry);
|
|
73
|
+
bakeMorphTargetManagerIntoVertices(VertexBuffer.NormalKind, morphTargetManager, geometry);
|
|
74
|
+
bakeMorphTargetManagerIntoVertices(VertexBuffer.TangentKind, morphTargetManager, geometry);
|
|
75
|
+
bakeMorphTargetManagerIntoVertices(VertexBuffer.UVKind, morphTargetManager, geometry);
|
|
76
|
+
|
|
77
|
+
// remove morph target manager with all it's morph targets
|
|
78
|
+
mesh.morphTargetManager = null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// bake the transformation data in the mesh geometry, fortunately there is already a help function from Babylon.js
|
|
82
|
+
mesh.bakeCurrentTransformIntoVertices();
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const bakeMorphTargetManagerIntoVertices = function (
|
|
86
|
+
kind: string,
|
|
87
|
+
morphTargetManager: MorphTargetManager,
|
|
88
|
+
geometry: Geometry
|
|
89
|
+
) {
|
|
90
|
+
const origVerticesData = geometry.getVerticesData(kind);
|
|
91
|
+
if (!origVerticesData) {
|
|
92
|
+
// no vertices data for this kind availabe on the mesh geometry
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
let verticesData = [...origVerticesData];
|
|
97
|
+
for (let i = 0; i < morphTargetManager.numTargets; i++) {
|
|
98
|
+
const target = morphTargetManager.getTarget(i);
|
|
99
|
+
const targetVerticesData = getVerticesDataFromMorphTarget(kind, target);
|
|
100
|
+
if (targetVerticesData) {
|
|
101
|
+
// vertices data of this kind are implemented on the morph target
|
|
102
|
+
// add the influence of this morph target to the vertices data
|
|
103
|
+
// formula is taken from: https://doc.babylonjs.com/features/featuresDeepDive/mesh/morphTargets#basics
|
|
104
|
+
verticesData = verticesData.map(
|
|
105
|
+
(oldVal, idx) => oldVal + (targetVerticesData[idx] - origVerticesData[idx]) * target.influence
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// apply the updated vertices data
|
|
111
|
+
geometry.setVerticesData(kind, verticesData);
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const getVerticesDataFromMorphTarget = function (kind: string, morphTarget: MorphTarget): Nullable<FloatArray> {
|
|
115
|
+
switch (kind) {
|
|
116
|
+
case 'position':
|
|
117
|
+
return morphTarget.getPositions();
|
|
118
|
+
case 'normal':
|
|
119
|
+
return morphTarget.getNormals();
|
|
120
|
+
case 'tangent':
|
|
121
|
+
return morphTarget.getTangents();
|
|
122
|
+
case 'uv':
|
|
123
|
+
return morphTarget.getUVs();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return null;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const resetTransformation = function (node: TransformNode) {
|
|
130
|
+
node.position = new Vector3(0, 0, 0);
|
|
131
|
+
node.rotation = new Vector3(0, 0, 0);
|
|
132
|
+
node.rotationQuaternion = null;
|
|
133
|
+
node.scaling = new Vector3(1, 1, 1);
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
export { bakeGeometryOfAllMeshes };
|
|
@@ -13,7 +13,11 @@ import { Tags } from '@babylonjs/core/Misc/tags';
|
|
|
13
13
|
*/
|
|
14
14
|
const isNodeIncludedInExclusionList = function (node: TransformNode, list: ExcludedGeometryList): boolean {
|
|
15
15
|
const checkNode = (inputNode: TransformNode, nodeToCheck: TransformNode) => {
|
|
16
|
-
return inputNode.uniqueId === nodeToCheck.uniqueId;
|
|
16
|
+
// return inputNode.uniqueId === nodeToCheck.uniqueId;
|
|
17
|
+
// check name instead of unique id, since the unique id changes when copying the scene, which is the case in the
|
|
18
|
+
// GLB export
|
|
19
|
+
const nodeName = nodeToCheck.metadata?.[GltfExportManager.NAME_BEFORE_EXPORT_METADATA_PROPERTY] ?? nodeToCheck.name;
|
|
20
|
+
return inputNode.name === nodeName;
|
|
17
21
|
};
|
|
18
22
|
const checkElement = (inputEl: Element, nodeToCheck: TransformNode) => {
|
|
19
23
|
return inputEl.nodesFlat.some(m => checkNode(m, nodeToCheck));
|