Skip to content

Commit 1b4b79b

Browse files
author
Unity Technologies
committed
Unity 2023.3.0b7 C# reference source code
1 parent 4e215c0 commit 1b4b79b

File tree

96 files changed

+5585
-5597
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+5585
-5597
lines changed

Editor/Mono/AssetPipeline/SpeedTree/SpeedTree9Importer.cs

Lines changed: 65 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@
1111
using UnityEditor.AssetImporters;
1212

1313
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;
1714
using Material = UnityEngine.Material;
1815
using Color = UnityEngine.Color;
1916

2017
using static UnityEditor.SpeedTree.Importer.SpeedTreeImporterCommon;
2118
using static UnityEditor.SpeedTree.Importer.WindConfigSDK;
2219
using static UnityEditor.SpeedTree.Importer.SpeedTree9Importer;
20+
using static UnityEditor.SpeedTree.Importer.SpeedTree9Reader;
2321

2422
namespace UnityEditor.SpeedTree.Importer
2523
{
@@ -119,7 +117,7 @@ public STMeshGeometry(int vertexCount, int UVCount, int indexLod)
119117

120118
// Cache main objects, created during import process.
121119
private AssetImportContext m_Context;
122-
private SpeedTreeReader m_Tree;
120+
private SpeedTree9Reader m_Tree;
123121
private SpeedTreeImporterOutputData m_OutputImporterData;
124122
private Shader m_Shader;
125123
private SpeedTreeWindAsset m_WindAsset;
@@ -144,14 +142,17 @@ public override bool SupportsRemappedAssetType(Type type)
144142
public override void OnImportAsset(AssetImportContext ctx)
145143
{
146144
m_Context = ctx;
147-
m_Tree = new SpeedTreeReader(ctx.assetPath);
145+
m_Tree = new SpeedTree9Reader();
148146

149-
if (!m_Tree.IsValid)
147+
FileStatus status = m_Tree.Initialize(ctx.assetPath);
148+
if (status != FileStatus.Valid)
150149
{
151-
ctx.LogImportError("Asset contains invalid data, import failed.");
150+
ctx.LogImportError($"Error while initializing the SpeedTree9 reader: {status}.");
152151
return;
153152
}
154153

154+
m_Tree.ReadContent();
155+
155156
CacheTreeImporterValues(ctx.assetPath);
156157

157158
m_RenderPipeline = GetCurrentRenderPipelineType();
@@ -223,11 +224,11 @@ private void TriggerAllCabback()
223224
private void CacheTreeImporterValues(string assetPath)
224225
{
225226
// 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();
227228
m_HasBranch2Data = m_Tree.Wind.DoBranch2;
228229
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;
231232

232233
WindConfigSDK windCfg = m_Tree.Wind;
233234
m_WindEnabled = (windCfg.DoShared || windCfg.DoBranch1 || windCfg.DoBranch2 || windCfg.DoRipple)
@@ -266,7 +267,7 @@ internal void RegenerateMaterials()
266267
private Mesh CreateMeshAndGeometry(Lod lod, int lodIndex)
267268
{
268269
bool isBillboard = m_LastLodIsBillboard && (lodIndex == (m_LODCount - 1));
269-
int vertexCount = (int)lod.Vertices.Count;
270+
int vertexCount = (int)lod.Vertices.Length;
270271
int numUVs = CalculateNumUVs(isBillboard);
271272

272273
STMeshGeometry sTMeshGeometry = new STMeshGeometry(vertexCount, numUVs, lodIndex);
@@ -277,7 +278,7 @@ private Mesh CreateMeshAndGeometry(Lod lod, int lodIndex)
277278
{
278279
name = "LOD" + sTMeshGeometry.lodIndex + "_Mesh",
279280
indexFormat = (sTMeshGeometry.vertices.Length > 65535) ? IndexFormat.UInt32 : IndexFormat.UInt16,
280-
subMeshCount = (int)lod.DrawCalls.Count,
281+
subMeshCount = (int)lod.DrawCalls.Length,
281282
vertices = sTMeshGeometry.vertices,
282283
normals = sTMeshGeometry.normals,
283284
tangents = sTMeshGeometry.tangents,
@@ -306,7 +307,7 @@ private void SetMeshIndices(Mesh mesh, Lod lod, DrawCall draw, int drawIndex)
306307
{
307308
int[] indices = new int[draw.IndexCount];
308309

309-
SpeedTreeDataArray<uint> lodIndices = lod.Indices;
310+
uint[] lodIndices = lod.Indices;
310311

311312
for (int index = 0; index < draw.IndexCount; ++index)
312313
{
@@ -338,7 +339,7 @@ private void CreateMeshAndLODObjects(Mesh mesh, int lodIndex, ref LOD[] lods)
338339

339340
private void CalculateMeshGeometry(STMeshGeometry sTMeshGeometry, Lod lod, bool isBillboard)
340341
{
341-
SpeedTreeDataArray<STVertex> vertices = lod.Vertices;
342+
STVertex[] vertices = lod.Vertices;
342343

343344
for (int i = 0; i < sTMeshGeometry.vertices.Length; ++i)
344345
{
@@ -577,11 +578,11 @@ private void CreateMeshAndMaterials(bool regenerateMaterials = false)
577578
// Loop each LOD (mesh) of the asset.
578579
for (int lodIndex = 0; lodIndex < m_LODCount; ++lodIndex)
579580
{
580-
Lod lod = m_Tree.Lods[lodIndex];
581+
Lod lod = m_Tree.Lod[lodIndex];
581582
Mesh mesh = CreateMeshAndGeometry(lod, lodIndex);
582583

583584
// 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)
585586
{
586587
DrawCall draw = lod.DrawCalls[drawIndex];
587588

@@ -676,10 +677,10 @@ private void RegenerateMaterialsFromTree()
676677
{
677678
for (int lodIndex = 0; lodIndex < m_LODCount; lodIndex++)
678679
{
679-
Lod stLOD = m_Tree.Lods[lodIndex];
680+
Lod stLOD = m_Tree.Lod[lodIndex];
680681

681682
// 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)
683684
{
684685
int matIndex = (int)stLOD.DrawCalls[drawIndex].MaterialIndex;
685686
STMaterial stMaterial = m_Tree.Materials[matIndex];
@@ -693,13 +694,17 @@ private void RegenerateAndPopulateExternalMaterials(string assetPath)
693694
{
694695
// This object could potentially be cached, but this function is rarely triggered (only when bumping the material version)
695696
// 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)
698701
{
699-
Debug.LogError("Asset contains invalid data, impossible to regenerate materials.");
702+
Debug.LogError($"Error while initializing the SpeedTree9 reader: {status}.");
700703
return;
701704
}
702705

706+
m_Tree.ReadContent();
707+
703708
CacheTreeImporterValues(assetPath);
704709

705710
m_RenderPipeline = GetCurrentRenderPipelineType();
@@ -801,7 +806,7 @@ private Material CreateMaterial(STMaterial stMaterial, int lod, string matName,
801806

802807
private bool SetMaterialTexture(Material mat, STMaterial stMaterial, int indexMap, string path, int property)
803808
{
804-
if (stMaterial.Maps.Count > indexMap)
809+
if (stMaterial.Maps.Length > indexMap)
805810
{
806811
MaterialMap stMatMap = stMaterial.Maps[indexMap];
807812
string mapPath = stMatMap.Path;
@@ -892,8 +897,7 @@ private void SetMaterialTextureAndColorProperties(STMaterial stMaterial, Materia
892897
{
893898
// _Glossiness (== _Smoothness) is multipled in the shader with the texture values if ExtraTex is present.
894899
// 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
897901
mat.SetFloat(MaterialProperties.GlossinessID, 1.0f);
898902
mat.SetFloat(MaterialProperties.MetallicID, 1.0f);
899903
}
@@ -1091,28 +1095,31 @@ private unsafe SpeedTreeWindConfig9 CopySpeedTree9WindConfig(WindConfigSDK wind,
10911095
{
10921096
const bool CHECK_ZERO = true;
10931097
const bool DONT_CHECK_ZERO = false;
1094-
void CopyCurve(in SpeedTreeDataArray<float> src, float* dst)
1098+
1099+
void CopyCurve(in float[] src, float* dst)
10951100
{
10961101
const int NUM_CURVE_ELEMENTS = 20;
1097-
Debug.Assert(src.Count == NUM_CURVE_ELEMENTS);
1102+
Debug.Assert(src.Length == NUM_CURVE_ELEMENTS);
10981103
for (global::System.Int32 i = 0; i < NUM_CURVE_ELEMENTS; i++)
10991104
{
11001105
dst[i] = src[i];
11011106
}
11021107
}
1103-
void CopyCurveScale(in SpeedTreeDataArray<float> src, float* dst, float scaleFactor)
1108+
1109+
void CopyCurveScale(in float[] src, float* dst, float scaleFactor)
11041110
{
11051111
const int NUM_CURVE_ELEMENTS = 20;
1106-
Debug.Assert(src.Count == NUM_CURVE_ELEMENTS);
1112+
Debug.Assert(src.Length == NUM_CURVE_ELEMENTS);
11071113
for (global::System.Int32 i = 0; i < NUM_CURVE_ELEMENTS; i++)
11081114
{
11091115
dst[i] = src[i] * scaleFactor;
11101116
}
11111117
}
1112-
bool ValidCurve(SpeedTreeDataArray<float> curve, bool bCheckZero = CHECK_ZERO)
1118+
1119+
bool ValidCurve(float[] curve, bool bCheckZero = CHECK_ZERO)
11131120
{
11141121
bool bNonZero = false;
1115-
for (int i = 0; i < curve.Count; ++i)
1122+
for (int i = 0; i < curve.Length; ++i)
11161123
{
11171124
bNonZero |= curve[i] != 0.0f;
11181125
if (float.IsNaN(curve[i]))
@@ -1127,6 +1134,7 @@ bool ValidCurve(SpeedTreeDataArray<float> curve, bool bCheckZero = CHECK_ZERO)
11271134
}
11281135
return true;
11291136
}
1137+
11301138
bool BranchHasAllCurvesValid(in WindBranch b)
11311139
{
11321140
return ValidCurve(b.Bend)
@@ -1136,6 +1144,7 @@ bool BranchHasAllCurvesValid(in WindBranch b)
11361144
&& ValidCurve(b.Flexibility, DONT_CHECK_ZERO
11371145
);
11381146
}
1147+
11391148
bool RippleHasAllCurvesValid(in WindRipple r)
11401149
{
11411150
return ValidCurve(r.Planar)
@@ -1144,6 +1153,7 @@ bool RippleHasAllCurvesValid(in WindRipple r)
11441153
&& ValidCurve(r.Flexibility, DONT_CHECK_ZERO
11451154
);
11461155
}
1156+
11471157
SpeedTreeWindConfig9 cfg = new SpeedTreeWindConfig9();
11481158

11491159
// common
@@ -1164,9 +1174,10 @@ bool RippleHasAllCurvesValid(in WindRipple r)
11641174
cfg.treeExtentX = (treeBounds.Max.X - treeBounds.Min.X) * scaleFactor;
11651175
cfg.treeExtentY = (treeBounds.Max.Y - treeBounds.Min.Y) * scaleFactor;
11661176
cfg.treeExtentZ = (treeBounds.Max.Z - treeBounds.Min.Z) * scaleFactor;
1177+
11671178
if (wind.DoShared)
11681179
{
1169-
WindBranch shared = wind.Shared;
1180+
WindConfigSDK.WindBranch shared = wind.Shared;
11701181
CopyCurveScale(shared.Bend, cfg.bendShared, scaleFactor);
11711182
CopyCurveScale(shared.Oscillation, cfg.oscillationShared, scaleFactor);
11721183
CopyCurve(shared.Speed, cfg.speedShared);
@@ -1179,9 +1190,10 @@ bool RippleHasAllCurvesValid(in WindRipple r)
11791190
cfg.doShared = 1;
11801191
}
11811192
}
1193+
11821194
if (wind.DoBranch1)
11831195
{
1184-
WindBranch branch1 = wind.Branch1;
1196+
WindConfigSDK.WindBranch branch1 = wind.Branch1;
11851197
CopyCurveScale(branch1.Bend, cfg.bendBranch1, scaleFactor);
11861198
CopyCurveScale(branch1.Oscillation, cfg.oscillationBranch1, scaleFactor);
11871199
CopyCurve(branch1.Speed, cfg.speedBranch1);
@@ -1193,9 +1205,10 @@ bool RippleHasAllCurvesValid(in WindRipple r)
11931205
cfg.doBranch1 = 1;
11941206
}
11951207
}
1208+
11961209
if (wind.DoBranch2)
11971210
{
1198-
WindBranch branch2 = wind.Branch2;
1211+
WindConfigSDK.WindBranch branch2 = wind.Branch2;
11991212
CopyCurveScale(branch2.Bend, cfg.bendBranch2, scaleFactor);
12001213
CopyCurveScale(branch2.Oscillation, cfg.oscillationBranch2, scaleFactor);
12011214
CopyCurve(branch2.Speed, cfg.speedBranch2);
@@ -1207,9 +1220,10 @@ bool RippleHasAllCurvesValid(in WindRipple r)
12071220
cfg.doBranch2 = 1;
12081221
}
12091222
}
1223+
12101224
if (wind.DoRipple)
12111225
{
1212-
WindRipple ripple = wind.Ripple;
1226+
WindConfigSDK.WindRipple ripple = wind.Ripple;
12131227
CopyCurveScale(ripple.Planar, cfg.planarRipple, scaleFactor);
12141228
CopyCurveScale(ripple.Directional, cfg.directionalRipple, scaleFactor);
12151229
CopyCurve(ripple.Speed, cfg.speedRipple);
@@ -1265,12 +1279,13 @@ private void CalculateScaleFactorFromUnit()
12651279
m_MeshSettings.scaleFactor = scaleFactor;
12661280
}
12671281

1268-
private bool TreeHasFacingData(SpeedTreeReader tree)
1282+
private bool TreeHasFacingData()
12691283
{
12701284
for (int lodIndex = 0; lodIndex < m_LODCount; ++lodIndex)
12711285
{
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)
12741289
{
12751290
DrawCall draw = lod.DrawCalls[drawIndex];
12761291
if(draw.ContainsFacingGeometry)
@@ -1386,7 +1401,6 @@ private static void OnPostprocessAllAssets(
13861401
{
13871402
if (TryGetHashSpeedTreeAttributeMaterialSettings(out List<string> strToHash))
13881403
{
1389-
13901404
Hash128 hash = new Hash128();
13911405

13921406
foreach (string str in strToHash)
@@ -1420,14 +1434,21 @@ private static bool TryGetHashSpeedTreeAttributeMaterialSettings(out List<string
14201434
return strToHash.Count > 0;
14211435
}
14221436

1423-
private static void ChangeTextureImporterSettingsForSt9Files(string assetFilename)
1437+
private static void ChangeTextureImporterSettingsForSt9Files(string assetPath)
14241438
{
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}.");
14271445
return;
1446+
}
1447+
1448+
tree.ReadContent();
14281449

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)
14311452
{
14321453
STMaterial stMaterial = tree.Materials[matIndex];
14331454

@@ -1450,7 +1471,7 @@ private static bool TryGetTextureImporterFromIndex(
14501471
{
14511472
textureImporter = null;
14521473

1453-
if (stMaterial.Maps.Count <= index)
1474+
if (stMaterial.Maps.Length <= index)
14541475
return false;
14551476

14561477
MaterialMap mat = stMaterial.Maps[index];

0 commit comments

Comments
 (0)