11
11
using UnityEditor . AssetImporters ;
12
12
13
13
using STVertex = UnityEditor . SpeedTree . Importer . Vertex ;
14
- using SpeedTreeReader = UnityEditor . SpeedTree . Importer . SpeedTree9Reader ;
15
- using STMaterial = UnityEditor . SpeedTree . Importer . STMaterial ;
16
- using LightingSettings = UnityEditor . SpeedTree . Importer . LightingSettings ;
17
14
using Material = UnityEngine . Material ;
18
15
using Color = UnityEngine . Color ;
19
16
20
17
using static UnityEditor . SpeedTree . Importer . SpeedTreeImporterCommon ;
21
18
using static UnityEditor . SpeedTree . Importer . WindConfigSDK ;
22
19
using static UnityEditor . SpeedTree . Importer . SpeedTree9Importer ;
20
+ using static UnityEditor . SpeedTree . Importer . SpeedTree9Reader ;
23
21
24
22
namespace UnityEditor . SpeedTree . Importer
25
23
{
@@ -119,7 +117,7 @@ public STMeshGeometry(int vertexCount, int UVCount, int indexLod)
119
117
120
118
// Cache main objects, created during import process.
121
119
private AssetImportContext m_Context ;
122
- private SpeedTreeReader m_Tree ;
120
+ private SpeedTree9Reader m_Tree ;
123
121
private SpeedTreeImporterOutputData m_OutputImporterData ;
124
122
private Shader m_Shader ;
125
123
private SpeedTreeWindAsset m_WindAsset ;
@@ -144,14 +142,17 @@ public override bool SupportsRemappedAssetType(Type type)
144
142
public override void OnImportAsset ( AssetImportContext ctx )
145
143
{
146
144
m_Context = ctx ;
147
- m_Tree = new SpeedTreeReader ( ctx . assetPath ) ;
145
+ m_Tree = new SpeedTree9Reader ( ) ;
148
146
149
- if ( ! m_Tree . IsValid )
147
+ FileStatus status = m_Tree . Initialize ( ctx . assetPath ) ;
148
+ if ( status != FileStatus . Valid )
150
149
{
151
- ctx . LogImportError ( "Asset contains invalid data, import failed .") ;
150
+ ctx . LogImportError ( $ "Error while initializing the SpeedTree9 reader: { status } .") ;
152
151
return ;
153
152
}
154
153
154
+ m_Tree . ReadContent ( ) ;
155
+
155
156
CacheTreeImporterValues ( ctx . assetPath ) ;
156
157
157
158
m_RenderPipeline = GetCurrentRenderPipelineType ( ) ;
@@ -223,11 +224,11 @@ private void TriggerAllCabback()
223
224
private void CacheTreeImporterValues ( string assetPath )
224
225
{
225
226
// Variables used a lot are cached, since accessing any Reader array has a non-negligeable cost.
226
- m_HasFacingData = TreeHasFacingData ( m_Tree ) ;
227
+ m_HasFacingData = TreeHasFacingData ( ) ;
227
228
m_HasBranch2Data = m_Tree . Wind . DoBranch2 ;
228
229
m_LastLodIsBillboard = m_Tree . BillboardInfo . LastLodIsBillboard ;
229
- m_LODCount = m_Tree . Lods . Count ;
230
- m_CollisionObjectsCount = m_Tree . CollisionObjects . Count ;
230
+ m_LODCount = ( uint ) m_Tree . Lod . Length ;
231
+ m_CollisionObjectsCount = ( uint ) m_Tree . CollisionObjects . Length ;
231
232
232
233
WindConfigSDK windCfg = m_Tree . Wind ;
233
234
m_WindEnabled = ( windCfg . DoShared || windCfg . DoBranch1 || windCfg . DoBranch2 || windCfg . DoRipple )
@@ -266,7 +267,7 @@ internal void RegenerateMaterials()
266
267
private Mesh CreateMeshAndGeometry ( Lod lod , int lodIndex )
267
268
{
268
269
bool isBillboard = m_LastLodIsBillboard && ( lodIndex == ( m_LODCount - 1 ) ) ;
269
- int vertexCount = ( int ) lod . Vertices . Count ;
270
+ int vertexCount = ( int ) lod . Vertices . Length ;
270
271
int numUVs = CalculateNumUVs ( isBillboard ) ;
271
272
272
273
STMeshGeometry sTMeshGeometry = new STMeshGeometry ( vertexCount , numUVs , lodIndex ) ;
@@ -277,7 +278,7 @@ private Mesh CreateMeshAndGeometry(Lod lod, int lodIndex)
277
278
{
278
279
name = "LOD" + sTMeshGeometry . lodIndex + "_Mesh" ,
279
280
indexFormat = ( sTMeshGeometry . vertices . Length > 65535 ) ? IndexFormat . UInt32 : IndexFormat . UInt16 ,
280
- subMeshCount = ( int ) lod . DrawCalls . Count ,
281
+ subMeshCount = ( int ) lod . DrawCalls . Length ,
281
282
vertices = sTMeshGeometry . vertices ,
282
283
normals = sTMeshGeometry . normals ,
283
284
tangents = sTMeshGeometry . tangents ,
@@ -306,7 +307,7 @@ private void SetMeshIndices(Mesh mesh, Lod lod, DrawCall draw, int drawIndex)
306
307
{
307
308
int [ ] indices = new int [ draw . IndexCount ] ;
308
309
309
- SpeedTreeDataArray < uint > lodIndices = lod . Indices ;
310
+ uint [ ] lodIndices = lod . Indices ;
310
311
311
312
for ( int index = 0 ; index < draw . IndexCount ; ++ index )
312
313
{
@@ -338,7 +339,7 @@ private void CreateMeshAndLODObjects(Mesh mesh, int lodIndex, ref LOD[] lods)
338
339
339
340
private void CalculateMeshGeometry ( STMeshGeometry sTMeshGeometry , Lod lod , bool isBillboard )
340
341
{
341
- SpeedTreeDataArray < STVertex > vertices = lod . Vertices ;
342
+ STVertex [ ] vertices = lod . Vertices ;
342
343
343
344
for ( int i = 0 ; i < sTMeshGeometry . vertices . Length ; ++ i )
344
345
{
@@ -577,11 +578,11 @@ private void CreateMeshAndMaterials(bool regenerateMaterials = false)
577
578
// Loop each LOD (mesh) of the asset.
578
579
for ( int lodIndex = 0 ; lodIndex < m_LODCount ; ++ lodIndex )
579
580
{
580
- Lod lod = m_Tree . Lods [ lodIndex ] ;
581
+ Lod lod = m_Tree . Lod [ lodIndex ] ;
581
582
Mesh mesh = CreateMeshAndGeometry ( lod , lodIndex ) ;
582
583
583
584
// Loop each DrawCall (material) of the current mesh LOD.
584
- for ( int drawIndex = 0 ; drawIndex < lod . DrawCalls . Count ; ++ drawIndex )
585
+ for ( int drawIndex = 0 ; drawIndex < lod . DrawCalls . Length ; ++ drawIndex )
585
586
{
586
587
DrawCall draw = lod . DrawCalls [ drawIndex ] ;
587
588
@@ -676,10 +677,10 @@ private void RegenerateMaterialsFromTree()
676
677
{
677
678
for ( int lodIndex = 0 ; lodIndex < m_LODCount ; lodIndex ++ )
678
679
{
679
- Lod stLOD = m_Tree . Lods [ lodIndex ] ;
680
+ Lod stLOD = m_Tree . Lod [ lodIndex ] ;
680
681
681
682
// Loop necessary materials for current LOD.
682
- for ( int drawIndex = 0 ; drawIndex < stLOD . DrawCalls . Count ; ++ drawIndex )
683
+ for ( int drawIndex = 0 ; drawIndex < stLOD . DrawCalls . Length ; ++ drawIndex )
683
684
{
684
685
int matIndex = ( int ) stLOD . DrawCalls [ drawIndex ] . MaterialIndex ;
685
686
STMaterial stMaterial = m_Tree . Materials [ matIndex ] ;
@@ -693,13 +694,17 @@ private void RegenerateAndPopulateExternalMaterials(string assetPath)
693
694
{
694
695
// This object could potentially be cached, but this function is rarely triggered (only when bumping the material version)
695
696
// so the cost of caching it is not really interesting.
696
- m_Tree = new SpeedTreeReader ( assetPath ) ;
697
- if ( ! m_Tree . IsValid )
697
+ m_Tree = new SpeedTree9Reader ( ) ;
698
+
699
+ FileStatus status = m_Tree . Initialize ( assetPath ) ;
700
+ if ( status != FileStatus . Valid )
698
701
{
699
- Debug . LogError ( "Asset contains invalid data, impossible to regenerate materials .") ;
702
+ Debug . LogError ( $ "Error while initializing the SpeedTree9 reader: { status } .") ;
700
703
return ;
701
704
}
702
705
706
+ m_Tree . ReadContent ( ) ;
707
+
703
708
CacheTreeImporterValues ( assetPath ) ;
704
709
705
710
m_RenderPipeline = GetCurrentRenderPipelineType ( ) ;
@@ -801,7 +806,7 @@ private Material CreateMaterial(STMaterial stMaterial, int lod, string matName,
801
806
802
807
private bool SetMaterialTexture ( Material mat , STMaterial stMaterial , int indexMap , string path , int property )
803
808
{
804
- if ( stMaterial . Maps . Count > indexMap )
809
+ if ( stMaterial . Maps . Length > indexMap )
805
810
{
806
811
MaterialMap stMatMap = stMaterial . Maps [ indexMap ] ;
807
812
string mapPath = stMatMap . Path ;
@@ -892,8 +897,7 @@ private void SetMaterialTextureAndColorProperties(STMaterial stMaterial, Materia
892
897
{
893
898
// _Glossiness (== _Smoothness) is multipled in the shader with the texture values if ExtraTex is present.
894
899
// Set default value 1.0f to override the default value 0.5, otherwise, the original texture values will
895
- // be scaled down to half as much.
896
- // Same goes for _Metallic
900
+ // be scaled down to half as much. Same goes for _Metallic
897
901
mat . SetFloat ( MaterialProperties . GlossinessID , 1.0f ) ;
898
902
mat . SetFloat ( MaterialProperties . MetallicID , 1.0f ) ;
899
903
}
@@ -1091,28 +1095,31 @@ private unsafe SpeedTreeWindConfig9 CopySpeedTree9WindConfig(WindConfigSDK wind,
1091
1095
{
1092
1096
const bool CHECK_ZERO = true ;
1093
1097
const bool DONT_CHECK_ZERO = false ;
1094
- void CopyCurve ( in SpeedTreeDataArray < float > src , float * dst )
1098
+
1099
+ void CopyCurve ( in float [ ] src , float * dst )
1095
1100
{
1096
1101
const int NUM_CURVE_ELEMENTS = 20 ;
1097
- Debug . Assert ( src . Count == NUM_CURVE_ELEMENTS ) ;
1102
+ Debug . Assert ( src . Length == NUM_CURVE_ELEMENTS ) ;
1098
1103
for ( global ::System . Int32 i = 0 ; i < NUM_CURVE_ELEMENTS ; i ++ )
1099
1104
{
1100
1105
dst [ i ] = src [ i ] ;
1101
1106
}
1102
1107
}
1103
- void CopyCurveScale ( in SpeedTreeDataArray < float > src , float * dst , float scaleFactor )
1108
+
1109
+ void CopyCurveScale ( in float [ ] src , float * dst , float scaleFactor )
1104
1110
{
1105
1111
const int NUM_CURVE_ELEMENTS = 20 ;
1106
- Debug . Assert ( src . Count == NUM_CURVE_ELEMENTS ) ;
1112
+ Debug . Assert ( src . Length == NUM_CURVE_ELEMENTS ) ;
1107
1113
for ( global ::System . Int32 i = 0 ; i < NUM_CURVE_ELEMENTS ; i ++ )
1108
1114
{
1109
1115
dst [ i ] = src [ i ] * scaleFactor ;
1110
1116
}
1111
1117
}
1112
- bool ValidCurve ( SpeedTreeDataArray < float > curve , bool bCheckZero = CHECK_ZERO )
1118
+
1119
+ bool ValidCurve ( float [ ] curve , bool bCheckZero = CHECK_ZERO )
1113
1120
{
1114
1121
bool bNonZero = false ;
1115
- for ( int i = 0 ; i < curve . Count ; ++ i )
1122
+ for ( int i = 0 ; i < curve . Length ; ++ i )
1116
1123
{
1117
1124
bNonZero |= curve [ i ] != 0.0f ;
1118
1125
if ( float . IsNaN ( curve [ i ] ) )
@@ -1127,6 +1134,7 @@ bool ValidCurve(SpeedTreeDataArray<float> curve, bool bCheckZero = CHECK_ZERO)
1127
1134
}
1128
1135
return true ;
1129
1136
}
1137
+
1130
1138
bool BranchHasAllCurvesValid ( in WindBranch b )
1131
1139
{
1132
1140
return ValidCurve ( b . Bend )
@@ -1136,6 +1144,7 @@ bool BranchHasAllCurvesValid(in WindBranch b)
1136
1144
&& ValidCurve ( b . Flexibility , DONT_CHECK_ZERO
1137
1145
) ;
1138
1146
}
1147
+
1139
1148
bool RippleHasAllCurvesValid ( in WindRipple r )
1140
1149
{
1141
1150
return ValidCurve ( r . Planar )
@@ -1144,6 +1153,7 @@ bool RippleHasAllCurvesValid(in WindRipple r)
1144
1153
&& ValidCurve ( r . Flexibility , DONT_CHECK_ZERO
1145
1154
) ;
1146
1155
}
1156
+
1147
1157
SpeedTreeWindConfig9 cfg = new SpeedTreeWindConfig9 ( ) ;
1148
1158
1149
1159
// common
@@ -1164,9 +1174,10 @@ bool RippleHasAllCurvesValid(in WindRipple r)
1164
1174
cfg . treeExtentX = ( treeBounds . Max . X - treeBounds . Min . X ) * scaleFactor ;
1165
1175
cfg . treeExtentY = ( treeBounds . Max . Y - treeBounds . Min . Y ) * scaleFactor ;
1166
1176
cfg . treeExtentZ = ( treeBounds . Max . Z - treeBounds . Min . Z ) * scaleFactor ;
1177
+
1167
1178
if ( wind . DoShared )
1168
1179
{
1169
- WindBranch shared = wind . Shared ;
1180
+ WindConfigSDK . WindBranch shared = wind . Shared ;
1170
1181
CopyCurveScale ( shared . Bend , cfg . bendShared , scaleFactor ) ;
1171
1182
CopyCurveScale ( shared . Oscillation , cfg . oscillationShared , scaleFactor ) ;
1172
1183
CopyCurve ( shared . Speed , cfg . speedShared ) ;
@@ -1179,9 +1190,10 @@ bool RippleHasAllCurvesValid(in WindRipple r)
1179
1190
cfg . doShared = 1 ;
1180
1191
}
1181
1192
}
1193
+
1182
1194
if ( wind . DoBranch1 )
1183
1195
{
1184
- WindBranch branch1 = wind . Branch1 ;
1196
+ WindConfigSDK . WindBranch branch1 = wind . Branch1 ;
1185
1197
CopyCurveScale ( branch1 . Bend , cfg . bendBranch1 , scaleFactor ) ;
1186
1198
CopyCurveScale ( branch1 . Oscillation , cfg . oscillationBranch1 , scaleFactor ) ;
1187
1199
CopyCurve ( branch1 . Speed , cfg . speedBranch1 ) ;
@@ -1193,9 +1205,10 @@ bool RippleHasAllCurvesValid(in WindRipple r)
1193
1205
cfg . doBranch1 = 1 ;
1194
1206
}
1195
1207
}
1208
+
1196
1209
if ( wind . DoBranch2 )
1197
1210
{
1198
- WindBranch branch2 = wind . Branch2 ;
1211
+ WindConfigSDK . WindBranch branch2 = wind . Branch2 ;
1199
1212
CopyCurveScale ( branch2 . Bend , cfg . bendBranch2 , scaleFactor ) ;
1200
1213
CopyCurveScale ( branch2 . Oscillation , cfg . oscillationBranch2 , scaleFactor ) ;
1201
1214
CopyCurve ( branch2 . Speed , cfg . speedBranch2 ) ;
@@ -1207,9 +1220,10 @@ bool RippleHasAllCurvesValid(in WindRipple r)
1207
1220
cfg . doBranch2 = 1 ;
1208
1221
}
1209
1222
}
1223
+
1210
1224
if ( wind . DoRipple )
1211
1225
{
1212
- WindRipple ripple = wind . Ripple ;
1226
+ WindConfigSDK . WindRipple ripple = wind . Ripple ;
1213
1227
CopyCurveScale ( ripple . Planar , cfg . planarRipple , scaleFactor ) ;
1214
1228
CopyCurveScale ( ripple . Directional , cfg . directionalRipple , scaleFactor ) ;
1215
1229
CopyCurve ( ripple . Speed , cfg . speedRipple ) ;
@@ -1265,12 +1279,13 @@ private void CalculateScaleFactorFromUnit()
1265
1279
m_MeshSettings . scaleFactor = scaleFactor ;
1266
1280
}
1267
1281
1268
- private bool TreeHasFacingData ( SpeedTreeReader tree )
1282
+ private bool TreeHasFacingData ( )
1269
1283
{
1270
1284
for ( int lodIndex = 0 ; lodIndex < m_LODCount ; ++ lodIndex )
1271
1285
{
1272
- Lod lod = tree . Lods [ lodIndex ] ;
1273
- for ( int drawIndex = 0 ; drawIndex < lod . DrawCalls . Count ; ++ drawIndex )
1286
+ Lod lod = m_Tree . Lod [ lodIndex ] ;
1287
+
1288
+ for ( int drawIndex = 0 ; drawIndex < lod . DrawCalls . Length ; ++ drawIndex )
1274
1289
{
1275
1290
DrawCall draw = lod . DrawCalls [ drawIndex ] ;
1276
1291
if ( draw . ContainsFacingGeometry )
@@ -1386,7 +1401,6 @@ private static void OnPostprocessAllAssets(
1386
1401
{
1387
1402
if ( TryGetHashSpeedTreeAttributeMaterialSettings ( out List < string > strToHash ) )
1388
1403
{
1389
-
1390
1404
Hash128 hash = new Hash128 ( ) ;
1391
1405
1392
1406
foreach ( string str in strToHash )
@@ -1420,14 +1434,21 @@ private static bool TryGetHashSpeedTreeAttributeMaterialSettings(out List<string
1420
1434
return strToHash . Count > 0 ;
1421
1435
}
1422
1436
1423
- private static void ChangeTextureImporterSettingsForSt9Files ( string assetFilename )
1437
+ private static void ChangeTextureImporterSettingsForSt9Files ( string assetPath )
1424
1438
{
1425
- SpeedTreeReader tree = new SpeedTreeReader ( assetFilename ) ;
1426
- if ( ! tree . IsValid )
1439
+ SpeedTree9Reader tree = new SpeedTree9Reader ( ) ;
1440
+
1441
+ FileStatus status = tree . Initialize ( assetPath ) ;
1442
+ if ( status != FileStatus . Valid )
1443
+ {
1444
+ Debug . LogError ( $ "Error while initializing the SpeedTree9 reader: { status } .") ;
1427
1445
return ;
1446
+ }
1447
+
1448
+ tree . ReadContent ( ) ;
1428
1449
1429
- string path = Path . GetDirectoryName ( assetFilename ) + "/" ;
1430
- for ( int matIndex = 0 ; matIndex < tree . Materials . Count ; ++ matIndex )
1450
+ string path = Path . GetDirectoryName ( assetPath ) + "/" ;
1451
+ for ( int matIndex = 0 ; matIndex < tree . Materials . Length ; ++ matIndex )
1431
1452
{
1432
1453
STMaterial stMaterial = tree . Materials [ matIndex ] ;
1433
1454
@@ -1450,7 +1471,7 @@ private static bool TryGetTextureImporterFromIndex(
1450
1471
{
1451
1472
textureImporter = null ;
1452
1473
1453
- if ( stMaterial . Maps . Count <= index )
1474
+ if ( stMaterial . Maps . Length <= index )
1454
1475
return false ;
1455
1476
1456
1477
MaterialMap mat = stMaterial . Maps [ index ] ;
0 commit comments