Skip to content

Commit

Permalink
Refactor mesh, texture and SFX collections (LostArtefacts#618)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahm86 authored Apr 16, 2024
1 parent 4e109d6 commit 216a218
Show file tree
Hide file tree
Showing 84 changed files with 725 additions and 1,629 deletions.
Binary file modified Deps/TRGE.Coord.dll
Binary file not shown.
10 changes: 5 additions & 5 deletions SFXExport/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,19 @@ private static void ExtractFromSFX(string file, TRGameVersion version, bool rema
private static void ExtractFromPHD(string file, TRGameVersion version)
{
TR1Level level = new TR1LevelControl().Read(file);
for (int i = 0; i < level.NumSampleIndices; i++)
for (int i = 0; i < level.SampleIndices.Count; i++)
{
uint sampleStart = level.SampleIndices[i];
uint sampleEnd = i < level.NumSampleIndices - 1 ? level.SampleIndices[i + 1] : (uint)level.Samples.Length;
if (sampleEnd > level.Samples.Length)
uint sampleEnd = i < level.SampleIndices.Count - 1 ? level.SampleIndices[i + 1] : (uint)level.Samples.Count;
if (sampleEnd > level.Samples.Count)
{
sampleEnd = (uint)level.Samples.Length;
sampleEnd = (uint)level.Samples.Count;
}

using BinaryWriter writer = new(File.Create(Path.Combine(version.ToString(), i + ".wav")));
for (uint j = sampleStart; j < sampleEnd; j++)
{
writer.Write(level.Samples[j]);
writer.Write(level.Samples[(int)j]);
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions TREnvironmentEditor/Model/Types/Mirroring/EMMirrorFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,7 @@ private static void MirrorTextures(TR1Level level)
// Include all animated texture references too
foreach (TRAnimatedTexture anim in level.AnimatedTextures)
{
for (int i = 0; i < anim.Textures.Length; i++)
for (int i = 0; i < anim.Textures.Count; i++)
{
textureReferences.Add(anim.Textures[i]);
}
Expand Down Expand Up @@ -1161,7 +1161,7 @@ private static void MirrorTextures(TR2Level level)
// Include all animated texture references too
foreach (TRAnimatedTexture anim in level.AnimatedTextures)
{
for (int i = 0; i < anim.Textures.Length; i++)
for (int i = 0; i < anim.Textures.Count; i++)
{
textureReferences.Add(anim.Textures[i]);
}
Expand Down Expand Up @@ -1232,7 +1232,7 @@ private static void MirrorTextures(TR3Level level)

foreach (TRAnimatedTexture anim in level.AnimatedTextures)
{
for (int i = 0; i < anim.Textures.Length; i++)
for (int i = 0; i < anim.Textures.Count; i++)
{
textureReferences.Add(anim.Textures[i]);
}
Expand All @@ -1241,7 +1241,7 @@ private static void MirrorTextures(TR3Level level)
MirrorObjectTextures(textureReferences, level.ObjectTextures);
}

private static void MirrorObjectTextures(ISet<ushort> textureReferences, TRObjectTexture[] objectTextures)
private static void MirrorObjectTextures(ISet<ushort> textureReferences, List<TRObjectTexture> objectTextures)
{
// Flip the object texture vertices in the same way as done for faces
foreach (ushort textureRef in textureReferences)
Expand All @@ -1263,11 +1263,11 @@ private static void MirrorObjectTextures(ISet<ushort> textureReferences, TRObjec
}
}

private static void MirrorDependentFaces(IEnumerable<TRModel> models, ISet<ushort> textureReferences, Func<uint, TRMesh[]> meshAction)
private static void MirrorDependentFaces(IEnumerable<TRModel> models, ISet<ushort> textureReferences, Func<uint, List<TRMesh>> meshAction)
{
foreach (TRModel model in models)
{
TRMesh[] meshes = meshAction.Invoke(model.ID);
List<TRMesh> meshes = meshAction.Invoke(model.ID);
if (meshes == null)
{
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public override void ApplyToLevel(TR1Level level)
List<TRMesh> meshes = new();
foreach (uint modelID in ModelIDs)
{
TRMesh[] modelMeshes = TRMeshUtilities.GetModelMeshes(level, (TR1Type)modelID);
if (modelMeshes == null || modelMeshes.Length > 1)
List<TRMesh> modelMeshes = TRMeshUtilities.GetModelMeshes(level, (TR1Type)modelID);
if (modelMeshes == null || modelMeshes.Count > 1)
{
throw new NotSupportedException("Only models with single meshes can be mirrored.");
}
Expand All @@ -30,8 +30,8 @@ public override void ApplyToLevel(TR2Level level)
List<TRMesh> meshes = new();
foreach (uint modelID in ModelIDs)
{
TRMesh[] modelMeshes = TRMeshUtilities.GetModelMeshes(level, (TR2Type)modelID);
if (modelMeshes == null || modelMeshes.Length > 1)
List<TRMesh> modelMeshes = TRMeshUtilities.GetModelMeshes(level, (TR2Type)modelID);
if (modelMeshes == null || modelMeshes.Count > 1)
{
throw new NotSupportedException("Only models with single meshes can be mirrored.");
}
Expand All @@ -47,8 +47,8 @@ public override void ApplyToLevel(TR3Level level)
List<TRMesh> meshes = new();
foreach (uint modelID in ModelIDs)
{
TRMesh[] modelMeshes = TRMeshUtilities.GetModelMeshes(level, (TR3Type)modelID);
if (modelMeshes == null || modelMeshes.Length > 1)
List<TRMesh> modelMeshes = TRMeshUtilities.GetModelMeshes(level, (TR3Type)modelID);
if (modelMeshes == null || modelMeshes.Count> 1)
{
throw new NotSupportedException("Only models with single meshes can be mirrored.");
}
Expand Down Expand Up @@ -106,7 +106,7 @@ private static ISet<ushort> MirrorMeshes(List<TRMesh> meshes)
return textureReferences;
}

private static void MirrorObjectTextures(ISet<ushort> textureReferences, TRObjectTexture[] objectTextures)
private static void MirrorObjectTextures(ISet<ushort> textureReferences, List<TRObjectTexture> objectTextures)
{
foreach (ushort textureRef in textureReferences)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public override void ApplyToLevel(TR3Level level)
MirrorObjectTextures(level.ObjectTextures);
}

private void MirrorObjectTextures(TRObjectTexture[] levelTextures)
private void MirrorObjectTextures(List<TRObjectTexture> levelTextures)
{
foreach (ushort textureRef in Textures)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ public override void ApplyToLevel(TR3Level level)
UpdateSpriteEntities(level.Entities);
}

private void ConvertSpriteSequence(TRSpriteSequence[] sequences)
private void ConvertSpriteSequence(List<TRSpriteSequence> sequences)
{
if (Array.Find(sequences, s => s.SpriteID == NewSpriteID) == null)
if (sequences.Find(s => s.SpriteID == NewSpriteID) == null)
{
TRSpriteSequence oldSequence = Array.Find(sequences, s => s.SpriteID == OldSpriteID);
TRSpriteSequence oldSequence = sequences.Find(s => s.SpriteID == OldSpriteID);
if (oldSequence != null)
{
oldSequence.SpriteID = NewSpriteID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,17 @@ public class EMCopySpriteSequenceFunction : BaseEMFunction

public override void ApplyToLevel(TR1Level level)
{
List<TRSpriteSequence> sequences = level.SpriteSequences.ToList();
CopySpriteSequence(sequences);
level.SpriteSequences = sequences.ToArray();
level.NumSpriteSequences = (uint)sequences.Count;
CopySpriteSequence(level.SpriteSequences);
}

public override void ApplyToLevel(TR2Level level)
{
List<TRSpriteSequence> sequences = level.SpriteSequences.ToList();
CopySpriteSequence(sequences);
level.SpriteSequences = sequences.ToArray();
level.NumSpriteSequences = (uint)sequences.Count;
CopySpriteSequence(level.SpriteSequences);
}

public override void ApplyToLevel(TR3Level level)
{
List<TRSpriteSequence> sequences = level.SpriteSequences.ToList();
CopySpriteSequence(sequences);
level.SpriteSequences = sequences.ToArray();
level.NumSpriteSequences = (uint)sequences.Count;
CopySpriteSequence(level.SpriteSequences);
}

private void CopySpriteSequence(List<TRSpriteSequence> sequences)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public override void ApplyToLevel(TR1Level level)
};
importer.Import();

RemapFaces(data, level.NumObjectTextures - 1, modelID => TRMeshUtilities.GetModelMeshes(level, (TR1Type)modelID));
RemapFaces(data, level.ObjectTextures.Count - 1, modelID => TRMeshUtilities.GetModelMeshes(level, (TR1Type)modelID));
}

public override void ApplyToLevel(TR2Level level)
Expand All @@ -48,7 +48,7 @@ public override void ApplyToLevel(TR2Level level)
};
importer.Import();

RemapFaces(data, level.NumObjectTextures - 1, modelID => TRMeshUtilities.GetModelMeshes(level, (TR2Type)modelID));
RemapFaces(data, level.ObjectTextures.Count - 1, modelID => TRMeshUtilities.GetModelMeshes(level, (TR2Type)modelID));
}

public override void ApplyToLevel(TR3Level level)
Expand All @@ -69,7 +69,7 @@ public override void ApplyToLevel(TR3Level level)
};
importer.Import();

RemapFaces(data, level.NumObjectTextures - 1, modelID => TRMeshUtilities.GetModelMeshes(level, (TR3Type)modelID));
RemapFaces(data, level.ObjectTextures.Count - 1, modelID => TRMeshUtilities.GetModelMeshes(level, (TR3Type)modelID));
}

private List<EMMeshTextureData> PrepareImportData(List<TRModel> existingModels)
Expand All @@ -85,11 +85,11 @@ private List<EMMeshTextureData> PrepareImportData(List<TRModel> existingModels)
return importData;
}

private static void RemapFaces(List<EMMeshTextureData> data, uint maximumTexture, Func<short, TRMesh[]> meshAction)
private static void RemapFaces(List<EMMeshTextureData> data, int maximumTexture, Func<short, List<TRMesh>> meshAction)
{
foreach (EMMeshTextureData textureData in data)
{
TRMesh[] meshes = meshAction.Invoke(textureData.ModelID);
List<TRMesh> meshes = meshAction.Invoke(textureData.ModelID);
foreach (TRMesh mesh in meshes)
{
foreach (TRFace3 face in mesh.ColouredTriangles)
Expand All @@ -112,7 +112,7 @@ private static void RemapFaces(List<EMMeshTextureData> data, uint maximumTexture
}
}

private static ushort SelectReplacementTexture(EMMeshTextureData data, ushort currentTexture, int defaultTexture, uint maximumTexture)
private static ushort SelectReplacementTexture(EMMeshTextureData data, ushort currentTexture, int defaultTexture, int maximumTexture)
{
if (data.TextureMap != null && data.TextureMap.ContainsKey(currentTexture))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ public override void ApplyToLevel(TR1Level level)
TR1TexturePacker packer = new(level);
List<TRObjectTexture> textures = new(level.ObjectTextures);

List<EMTextureMap> mapping = BuildAndPackTextures(packer, textures);
level.ObjectTextures = textures.ToArray();
level.NumObjectTextures = (uint)textures.Count;

List<EMTextureMap> mapping = BuildAndPackTextures(packer, level.ObjectTextures);
mapping.ForEach(m => new EMRefaceFunction
{
TextureMap = m
Expand All @@ -31,10 +28,7 @@ public override void ApplyToLevel(TR2Level level)
TR2TexturePacker packer = new(level);
List<TRObjectTexture> textures = new(level.ObjectTextures);

List<EMTextureMap> mapping = BuildAndPackTextures(packer, textures);
level.ObjectTextures = textures.ToArray();
level.NumObjectTextures = (uint)textures.Count;

List<EMTextureMap> mapping = BuildAndPackTextures(packer, level.ObjectTextures);
mapping.ForEach(m => new EMRefaceFunction
{
TextureMap = m
Expand All @@ -46,10 +40,7 @@ public override void ApplyToLevel(TR3Level level)
TR3TexturePacker packer = new(level);
List<TRObjectTexture> textures = new(level.ObjectTextures);

List<EMTextureMap> mapping = BuildAndPackTextures(packer, textures);
level.ObjectTextures = textures.ToArray();
level.NumObjectTextures = (uint)textures.Count;

List<EMTextureMap> mapping = BuildAndPackTextures(packer, level.ObjectTextures);
mapping.ForEach(m => new EMRefaceFunction
{
TextureMap = m
Expand Down
Loading

0 comments on commit 216a218

Please sign in to comment.