@@ -60,6 +60,8 @@ protected virtual async Task ConstructMesh(GLTFMesh mesh, int meshIndex, Cancell
60
60
}
61
61
else if ( _assetCache . MeshCache [ meshIndex ] . LoadedMesh )
62
62
{
63
+ // Make sure we created all materials in case this mesh is shared because of deduplication
64
+ await CreateMeshMaterials ( mesh ) ;
63
65
return ;
64
66
}
65
67
@@ -101,25 +103,39 @@ protected virtual async Task ConstructMesh(GLTFMesh mesh, int meshIndex, Cancell
101
103
{
102
104
var primitive = mesh . Primitives [ i ] ;
103
105
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 ;
108
110
109
- await CreateMaterials ( primitive ) ;
111
+ ConvertAttributeAccessorsToUnityTypes ( primCache , unityData , unityData . subMeshVertexOffset [ i ] , i ) ;
112
+
113
+ cancellationToken . ThrowIfCancellationRequested ( ) ;
110
114
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
+ }
116
120
}
117
- }
118
121
119
- Statistics . VertexCount += unityData . Vertices . Length ;
122
+ await CreateMaterials ( primitive ) ;
123
+ }
124
+
125
+ if ( unityData . Vertices != null )
126
+ Statistics . VertexCount += unityData . Vertices . Length ;
120
127
await ConstructUnityMesh ( unityData , meshIndex , mesh . Name ) ;
121
128
}
122
129
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
+
123
139
private static uint [ ] CalculateSubMeshVertexOffset ( GLTFMesh mesh , out uint totalVertCount )
124
140
{
125
141
Dictionary < int , AccessorId > accessorIds = new Dictionary < int , AccessorId > ( ) ;
@@ -656,6 +672,9 @@ private UnityMeshData CreateUnityMeshData(GLTFMesh gltfMesh, int meshIndex, bool
656
672
/// <returns></returns>
657
673
protected async Task ConstructUnityMesh ( UnityMeshData unityMeshData , int meshIndex , string meshName )
658
674
{
675
+ if ( _assetCache . MeshCache [ meshIndex ] . LoadedMesh != null )
676
+ return ;
677
+
659
678
await YieldOnTimeoutAndThrowOnLowMemory ( ) ;
660
679
Mesh mesh = new Mesh
661
680
{
@@ -703,7 +722,10 @@ protected async Task ConstructUnityMesh(UnityMeshData unityMeshData, int meshInd
703
722
mesh . UploadMeshData ( true ) ;
704
723
}
705
724
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 ;
707
729
708
730
// Free up some memory
709
731
unityMeshData . Clear ( ) ;
@@ -1403,6 +1425,9 @@ private void CheckForMeshDuplicates()
1403
1425
|| _assetCache . UnityMeshDataCache [ meshIndex ] == null )
1404
1426
continue ;
1405
1427
1428
+ if ( _assetCache . UnityMeshDataCache [ i ] == _assetCache . UnityMeshDataCache [ meshIndex ] )
1429
+ continue ;
1430
+
1406
1431
var meshIsEqual = _assetCache . UnityMeshDataCache [ i ]
1407
1432
. IsEqual ( _assetCache . UnityMeshDataCache [ meshIndex ] ) ;
1408
1433
@@ -1413,17 +1438,20 @@ private void CheckForMeshDuplicates()
1413
1438
1414
1439
foreach ( var dm in meshDuplicates )
1415
1440
{
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
+ // }
1427
1455
}
1428
1456
}
1429
1457
}
0 commit comments