Skip to content

Commit 7cc3021

Browse files
committed
fixes 836 - instead of overriding the node mesh id for shared meshes, we override now the UnityMeshData and LoadedMesh to fix the issue
1 parent 4ebdfca commit 7cc3021

File tree

1 file changed

+52
-24
lines changed

1 file changed

+52
-24
lines changed

Runtime/Scripts/SceneImporter/ImporterMeshes.cs

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ protected virtual async Task ConstructMesh(GLTFMesh mesh, int meshIndex, Cancell
6060
}
6161
else if (_assetCache.MeshCache[meshIndex].LoadedMesh)
6262
{
63+
// Make sure we created all materials in case this mesh is shared because of deduplication
64+
await CreateMeshMaterials(mesh);
6365
return;
6466
}
6567

@@ -101,25 +103,39 @@ protected virtual async Task ConstructMesh(GLTFMesh mesh, int meshIndex, Cancell
101103
{
102104
var primitive = mesh.Primitives[i];
103105
var primCache = meshCache.Primitives[i];
104-
unityData.Topology[i] = GetTopology(primitive.Mode);
105-
unityData.DrawModes[i] = primitive.Mode;
106-
107-
ConvertAttributeAccessorsToUnityTypes(primCache, unityData, unityData.subMeshVertexOffset[i], i);
106+
if (!unityData.subMeshDataCreated[i])
107+
{
108+
unityData.Topology[i] = GetTopology(primitive.Mode);
109+
unityData.DrawModes[i] = primitive.Mode;
108110

109-
await CreateMaterials(primitive);
111+
ConvertAttributeAccessorsToUnityTypes(primCache, unityData, unityData.subMeshVertexOffset[i], i);
112+
113+
cancellationToken.ThrowIfCancellationRequested();
110114

111-
cancellationToken.ThrowIfCancellationRequested();
112-
113-
if (unityData.Topology[i] == MeshTopology.Triangles && primitive.Indices != null && primitive.Indices.Value != null)
114-
{
115-
Statistics.TriangleCount += primitive.Indices.Value.Count / 3;
115+
if (unityData.Topology[i] == MeshTopology.Triangles && primitive.Indices != null &&
116+
primitive.Indices.Value != null)
117+
{
118+
Statistics.TriangleCount += primitive.Indices.Value.Count / 3;
119+
}
116120
}
117-
}
118121

119-
Statistics.VertexCount += unityData.Vertices.Length;
122+
await CreateMaterials(primitive);
123+
}
124+
125+
if (unityData.Vertices != null)
126+
Statistics.VertexCount += unityData.Vertices.Length;
120127
await ConstructUnityMesh(unityData, meshIndex, mesh.Name);
121128
}
122129

130+
private async Task CreateMeshMaterials(GLTFMesh mesh)
131+
{
132+
for (int i = 0; i < mesh.Primitives.Count; ++i)
133+
{
134+
var primitive = mesh.Primitives[i];
135+
await CreateMaterials(primitive);
136+
}
137+
}
138+
123139
private static uint[] CalculateSubMeshVertexOffset(GLTFMesh mesh, out uint totalVertCount)
124140
{
125141
Dictionary<int, AccessorId> accessorIds = new Dictionary<int, AccessorId>();
@@ -656,6 +672,9 @@ private UnityMeshData CreateUnityMeshData(GLTFMesh gltfMesh, int meshIndex, bool
656672
/// <returns></returns>
657673
protected async Task ConstructUnityMesh(UnityMeshData unityMeshData, int meshIndex, string meshName)
658674
{
675+
if (_assetCache.MeshCache[meshIndex].LoadedMesh != null)
676+
return;
677+
659678
await YieldOnTimeoutAndThrowOnLowMemory();
660679
Mesh mesh = new Mesh
661680
{
@@ -703,7 +722,10 @@ protected async Task ConstructUnityMesh(UnityMeshData unityMeshData, int meshInd
703722
mesh.UploadMeshData(true);
704723
}
705724

706-
_assetCache.MeshCache[meshIndex].LoadedMesh = mesh;
725+
// Assign the loaded mesh to all MeshCache entries that reference the same UnityMeshData
726+
for (int i = 0; i < _assetCache.UnityMeshDataCache.Length; i++)
727+
if (_assetCache.UnityMeshDataCache[i] == unityMeshData)
728+
_assetCache.MeshCache[i].LoadedMesh = mesh;
707729

708730
// Free up some memory
709731
unityMeshData.Clear();
@@ -1403,6 +1425,9 @@ private void CheckForMeshDuplicates()
14031425
|| _assetCache.UnityMeshDataCache[meshIndex] == null)
14041426
continue;
14051427

1428+
if (_assetCache.UnityMeshDataCache[i] == _assetCache.UnityMeshDataCache[meshIndex])
1429+
continue;
1430+
14061431
var meshIsEqual = _assetCache.UnityMeshDataCache[i]
14071432
.IsEqual(_assetCache.UnityMeshDataCache[meshIndex]);
14081433

@@ -1413,17 +1438,20 @@ private void CheckForMeshDuplicates()
14131438

14141439
foreach (var dm in meshDuplicates)
14151440
{
1416-
if (_gltfRoot.Nodes == null) continue;
1417-
for (int i = 0; i < _gltfRoot.Nodes.Count; i++)
1418-
{
1419-
if (_gltfRoot.Nodes[i].Mesh != null && _gltfRoot.Nodes[i].Mesh.Id == dm.Key)
1420-
{
1421-
if (_gltfRoot.Nodes[i].Weights == null && _gltfRoot.Meshes[dm.Value].Weights != null)
1422-
_gltfRoot.Nodes[i].Weights = _gltfRoot.Meshes[_gltfRoot.Nodes[i].Mesh.Id].Weights;
1423-
1424-
_gltfRoot.Nodes[i].Mesh.Id = dm.Value;
1425-
}
1426-
}
1441+
_assetCache.UnityMeshDataCache[dm.Key] = _assetCache.UnityMeshDataCache[dm.Value];
1442+
1443+
// if (_gltfRoot.Nodes == null) continue;
1444+
// for (int i = 0; i < _gltfRoot.Nodes.Count; i++)
1445+
// {
1446+
// if (_gltfRoot.Nodes[i].Mesh != null && _gltfRoot.Nodes[i].Mesh.Id == dm.Key)
1447+
// {
1448+
// if (_gltfRoot.Nodes[i].Weights == null && _gltfRoot.Meshes[dm.Value].Weights != null)
1449+
// _gltfRoot.Nodes[i].Weights = _gltfRoot.Meshes[_gltfRoot.Nodes[i].Mesh.Id].Weights;
1450+
//
1451+
//
1452+
// _gltfRoot.Nodes[i].Mesh.Id = dm.Value;
1453+
// }
1454+
// }
14271455
}
14281456
}
14291457
}

0 commit comments

Comments
 (0)