Skip to content

Commit

Permalink
Refactor mesh storage (LostArtefacts#629)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahm86 authored Apr 27, 2024
1 parent 6081cca commit 87b8e15
Show file tree
Hide file tree
Showing 56 changed files with 418 additions and 915 deletions.
64 changes: 20 additions & 44 deletions TREnvironmentEditor/Model/Types/Mirroring/EMMirrorFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -561,35 +561,24 @@ private void MirrorBoxes(List<TR2Box> boxes)

private static void MirrorStaticMeshes(TR1Level level)
{
MirrorStaticMeshes(level.StaticMeshes, delegate (TRStaticMesh staticMesh)
{
return TRMeshUtilities.GetMesh(level, staticMesh.Mesh);
});
MirrorStaticMeshes(level.StaticMeshes);
}

private static void MirrorStaticMeshes(TR2Level level)
{
MirrorStaticMeshes(level.StaticMeshes, delegate (TRStaticMesh staticMesh)
{
return TRMeshUtilities.GetMesh(level, staticMesh.Mesh);
});
MirrorStaticMeshes(level.StaticMeshes);
}

private static void MirrorStaticMeshes(TR3Level level)
{
MirrorStaticMeshes(level.StaticMeshes, delegate (TRStaticMesh staticMesh)
{
return TRMeshUtilities.GetMesh(level, staticMesh.Mesh);
});
MirrorStaticMeshes(level.StaticMeshes);
}

private static void MirrorStaticMeshes(List<TRStaticMesh> staticMeshes, Func<TRStaticMesh, TRMesh> meshFunc)
private static void MirrorStaticMeshes(List<TRStaticMesh> staticMeshes)
{
foreach (TRStaticMesh staticMesh in staticMeshes)
{
TRMesh mesh = meshFunc(staticMesh);

foreach (TRVertex vert in mesh.Vertices)
foreach (TRVertex vert in staticMesh.Mesh.Vertices)
{
vert.X *= -1;
}
Expand Down Expand Up @@ -1048,29 +1037,27 @@ private static void MirrorTextures(TR1Level level)
continue;
}

TRMesh mesh = TRMeshUtilities.GetMesh(level, staticMesh.Mesh);

// Flip the faces and store texture references
foreach (TRMeshFace face in mesh.TexturedRectangles)
foreach (TRMeshFace face in staticMesh.Mesh.TexturedRectangles)
{
face.SwapVertices(0, 3);
face.SwapVertices(1, 2);
textureReferences.Add(face.Texture);
}

foreach (TRMeshFace face in mesh.ColouredRectangles)
foreach (TRMeshFace face in staticMesh.Mesh.ColouredRectangles)
{
face.SwapVertices(0, 3);
face.SwapVertices(1, 2);
}

foreach (TRMeshFace face in mesh.TexturedTriangles)
foreach (TRMeshFace face in staticMesh.Mesh.TexturedTriangles)
{
face.SwapVertices(0, 2);
textureReferences.Add(face.Texture);
}

foreach (TRMeshFace face in mesh.ColouredTriangles)
foreach (TRMeshFace face in staticMesh.Mesh.ColouredTriangles)
{
face.SwapVertices(0, 2);
}
Expand All @@ -1091,8 +1078,7 @@ private static void MirrorTextures(TR1Level level)
// Models such as doors may use textures also used on walls, but
// these models aren't mirrored so the texture will end up being
// upside down. Rotate the relevant mesh faces.
MirrorDependentFaces(level.Models, textureReferences,
modelID => TRMeshUtilities.GetModelMeshes(level, (TR1Type)modelID));
MirrorDependentFaces(level.Models, textureReferences);
}

private static void MirrorTextures(TR2Level level)
Expand Down Expand Up @@ -1129,29 +1115,27 @@ private static void MirrorTextures(TR2Level level)
continue;
}

TRMesh mesh = TRMeshUtilities.GetMesh(level, staticMesh.Mesh);

// Flip the faces and store texture references
foreach (TRMeshFace face in mesh.TexturedRectangles)
foreach (TRMeshFace face in staticMesh.Mesh.TexturedRectangles)
{
face.SwapVertices(0, 3);
face.SwapVertices(1, 2);
textureReferences.Add(face.Texture);
}

foreach (TRMeshFace face in mesh.ColouredRectangles)
foreach (TRMeshFace face in staticMesh.Mesh.ColouredRectangles)
{
face.SwapVertices(0, 3);
face.SwapVertices(1, 2);
}

foreach (TRMeshFace face in mesh.TexturedTriangles)
foreach (TRMeshFace face in staticMesh.Mesh.TexturedTriangles)
{
face.SwapVertices(0, 2);
textureReferences.Add(face.Texture);
}

foreach (TRMeshFace face in mesh.ColouredTriangles)
foreach (TRMeshFace face in staticMesh.Mesh.ColouredTriangles)
{
face.SwapVertices(0, 2);
}
Expand Down Expand Up @@ -1201,29 +1185,27 @@ private static void MirrorTextures(TR3Level level)
continue;
}

TRMesh mesh = TRMeshUtilities.GetMesh(level, staticMesh.Mesh);

// Flip the faces and store texture references
foreach (TRMeshFace face in mesh.TexturedRectangles)
foreach (TRMeshFace face in staticMesh.Mesh.TexturedRectangles)
{
face.SwapVertices(0, 3);
face.SwapVertices(1, 2);
textureReferences.Add((ushort)(face.Texture & 0x0fff));
}

foreach (TRMeshFace face in mesh.ColouredRectangles)
foreach (TRMeshFace face in staticMesh.Mesh.ColouredRectangles)
{
face.SwapVertices(0, 3);
face.SwapVertices(1, 2);
}

foreach (TRMeshFace face in mesh.TexturedTriangles)
foreach (TRMeshFace face in staticMesh.Mesh.TexturedTriangles)
{
face.SwapVertices(0, 2);
textureReferences.Add((ushort)(face.Texture & 0x0fff));
}

foreach (TRMeshFace face in mesh.ColouredTriangles)
foreach (TRMeshFace face in staticMesh.Mesh.ColouredTriangles)
{
face.SwapVertices(0, 2);
}
Expand Down Expand Up @@ -1263,17 +1245,11 @@ private static void MirrorObjectTextures(ISet<ushort> textureReferences, List<TR
}
}

private static void MirrorDependentFaces(IEnumerable<TRModel> models, ISet<ushort> textureReferences, Func<uint, List<TRMesh>> meshAction)
private static void MirrorDependentFaces(IEnumerable<TRModel> models, ISet<ushort> textureReferences)
{
foreach (TRModel model in models)
{
List<TRMesh> meshes = meshAction.Invoke(model.ID);
if (meshes == null)
{
continue;
}

foreach (TRMesh mesh in meshes)
foreach (TRMesh mesh in model.Meshes)
{
foreach (TRMeshFace face in mesh.TexturedRectangles)
{
Expand Down
55 changes: 12 additions & 43 deletions TREnvironmentEditor/Model/Types/Mirroring/EMMirrorModelFunction.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using TRLevelControl.Helpers;
using TRLevelControl.Model;
using TRLevelControl.Model;
using TRModelTransporter.Model.Textures;

namespace TREnvironmentEditor.Model.Types;
Expand All @@ -10,60 +9,30 @@ public class EMMirrorModelFunction : BaseEMFunction

public override void ApplyToLevel(TR1Level level)
{
List<TRMesh> meshes = new();
foreach (uint modelID in ModelIDs)
{
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.");
}

meshes.Add(modelMeshes[0]);
}

MirrorObjectTextures(MirrorMeshes(meshes), level.ObjectTextures);
IEnumerable<TRModel> models = level.Models
.Where(m => ModelIDs.Contains(m.ID) && m.Meshes.Count == 1);
MirrorObjectTextures(MirrorMeshes(models), level.ObjectTextures);
}

public override void ApplyToLevel(TR2Level level)
{
List<TRMesh> meshes = new();
foreach (uint modelID in ModelIDs)
{
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.");
}

meshes.Add(modelMeshes[0]);
}

MirrorObjectTextures(MirrorMeshes(meshes), level.ObjectTextures);
IEnumerable<TRModel> models = level.Models
.Where(m => ModelIDs.Contains(m.ID) && m.Meshes.Count == 1);
MirrorObjectTextures(MirrorMeshes(models), level.ObjectTextures);
}

public override void ApplyToLevel(TR3Level level)
{
List<TRMesh> meshes = new();
foreach (uint modelID in ModelIDs)
{
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.");
}

meshes.Add(modelMeshes[0]);
}

MirrorObjectTextures(MirrorMeshes(meshes), level.ObjectTextures);
IEnumerable<TRModel> models = level.Models
.Where(m => ModelIDs.Contains(m.ID) && m.Meshes.Count == 1);
MirrorObjectTextures(MirrorMeshes(models), level.ObjectTextures);
}

private static ISet<ushort> MirrorMeshes(List<TRMesh> meshes)
private static ISet<ushort> MirrorMeshes(IEnumerable<TRModel> models)
{
ISet<ushort> textureReferences = new HashSet<ushort>();

foreach (TRMesh mesh in meshes)
foreach (TRMesh mesh in models.SelectMany(m => m.Meshes))
{
foreach (TRVertex vert in mesh.Vertices)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using TRLevelControl.Helpers;
using TRLevelControl.Model;
using TRLevelControl.Model;

namespace TREnvironmentEditor.Model.Types;

Expand All @@ -11,7 +10,7 @@ public override void ApplyToLevel(TR1Level level)
{
IEnumerable<TRMesh> meshes = level.StaticMeshes.ToList()
.FindAll(s => MeshIDs.Contains(s.ID))
.Select(s => TRMeshUtilities.GetMesh(level, s.Mesh));
.Select(s => s.Mesh);

MirrorMeshes(meshes);
}
Expand All @@ -20,7 +19,7 @@ public override void ApplyToLevel(TR2Level level)
{
IEnumerable<TRMesh> meshes = level.StaticMeshes.ToList()
.FindAll(s => MeshIDs.Contains(s.ID))
.Select(s => TRMeshUtilities.GetMesh(level, s.Mesh));
.Select(s => s.Mesh);

MirrorMeshes(meshes);
}
Expand All @@ -29,7 +28,7 @@ public override void ApplyToLevel(TR3Level level)
{
IEnumerable<TRMesh> meshes = level.StaticMeshes.ToList()
.FindAll(s => MeshIDs.Contains(s.ID))
.Select(s => TRMeshUtilities.GetMesh(level, s.Mesh));
.Select(s => s.Mesh);

MirrorMeshes(meshes);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using TREnvironmentEditor.Helpers;
using TRLevelControl.Helpers;
using TRLevelControl.Model;
using TRModelTransporter.Transport;

Expand Down Expand Up @@ -27,7 +26,7 @@ public override void ApplyToLevel(TR1Level level)
};
importer.Import();

RemapFaces(data, level.ObjectTextures.Count - 1, modelID => TRMeshUtilities.GetModelMeshes(level, (TR1Type)modelID));
RemapFaces(data, level.ObjectTextures.Count - 1, modelID => level.Models.Find(m => m.ID == modelID));
}

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

RemapFaces(data, level.ObjectTextures.Count - 1, modelID => TRMeshUtilities.GetModelMeshes(level, (TR2Type)modelID));
RemapFaces(data, level.ObjectTextures.Count - 1, modelID => level.Models.Find(m => m.ID == modelID));
}

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

RemapFaces(data, level.ObjectTextures.Count - 1, modelID => TRMeshUtilities.GetModelMeshes(level, (TR3Type)modelID));
RemapFaces(data, level.ObjectTextures.Count - 1, modelID => level.Models.Find(m => m.ID == modelID));
}

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

private static void RemapFaces(List<EMMeshTextureData> data, int maximumTexture, Func<short, List<TRMesh>> meshAction)
private static void RemapFaces(List<EMMeshTextureData> data, int maximumTexture, Func<short, TRModel> modelAction)
{
foreach (EMMeshTextureData textureData in data)
{
List<TRMesh> meshes = meshAction.Invoke(textureData.ModelID);
foreach (TRMesh mesh in meshes)
TRModel model= modelAction.Invoke(textureData.ModelID);
foreach (TRMesh mesh in model.Meshes)
{
foreach (TRMeshFace face in mesh.ColouredTriangles)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using TRLevelControl.Helpers;
using TRLevelControl.Model;
using TRLevelControl.Model;

namespace TREnvironmentEditor.Model;

Expand All @@ -11,21 +10,21 @@ public class EMCreateStaticMeshFunction : BaseEMFunction
public override void ApplyToLevel(TR1Level level)
{
TRStaticMesh newMesh = Info.Clone();
newMesh.Mesh = (ushort)TRMeshUtilities.InsertMesh(level, Mesh);
newMesh.Mesh = Mesh;
level.StaticMeshes.Add(newMesh);
}

public override void ApplyToLevel(TR2Level level)
{
TRStaticMesh newMesh = Info.Clone();
newMesh.Mesh = (ushort)TRMeshUtilities.InsertMesh(level, Mesh);
newMesh.Mesh = Mesh;
level.StaticMeshes.Add(newMesh);
}

public override void ApplyToLevel(TR3Level level)
{
TRStaticMesh newMesh = Info.Clone();
newMesh.Mesh = (ushort)TRMeshUtilities.InsertMesh(level, Mesh);
newMesh.Mesh = Mesh;
level.StaticMeshes.Add(newMesh);
}
}
8 changes: 8 additions & 0 deletions TRLevelControl/Build/IMeshProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using TRLevelControl.Model;

namespace TRLevelControl.Build;

public interface IMeshProvider
{
TRMesh GetObjectMesh(long pointer);
}
Loading

0 comments on commit 87b8e15

Please sign in to comment.