Skip to content

Commit

Permalink
Use TRFace in room meshes (LostArtefacts#639)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahm86 authored Apr 30, 2024
1 parent d97ddf9 commit 2320522
Show file tree
Hide file tree
Showing 34 changed files with 401 additions and 741 deletions.
10 changes: 5 additions & 5 deletions TREnvironmentEditor/Helpers/EMTextureGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public ushort GetWall(int height)
return result == ushort.MaxValue ? Floor : result;
}

public void RandomizeRotation(TRFace4 face, int height)
public void RandomizeRotation(TRFace face, int height)
{
if (RandomRotationSeed <= 0)
{
Expand All @@ -71,7 +71,7 @@ public void RandomizeRotation(TRFace4 face, int height)
switch (_generator.Next(0, 4))
{
case 1:
remap = new Dictionary<int, int>
remap = new()
{
[0] = 1,
[1] = 2,
Expand All @@ -80,7 +80,7 @@ public void RandomizeRotation(TRFace4 face, int height)
};
break;
case 2:
remap = new Dictionary<int, int>
remap = new()
{
[0] = 2,
[1] = 3,
Expand All @@ -89,7 +89,7 @@ public void RandomizeRotation(TRFace4 face, int height)
};
break;
case 3:
remap = new Dictionary<int, int>
remap = new()
{
[0] = 3,
[1] = 0,
Expand All @@ -104,7 +104,7 @@ public void RandomizeRotation(TRFace4 face, int height)

if (remap != null && height == TRConsts.Step4)
{
face.Vertices = EMModifyFaceFunction.RotateVertices(face.Vertices, new EMFaceRotation
EMModifyFaceFunction.RotateVertices(face.Vertices, new()
{
VertexRemap = remap
});
Expand Down
198 changes: 60 additions & 138 deletions TREnvironmentEditor/Model/Types/Mirroring/EMMirrorFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -930,60 +930,18 @@ private void MirrorNullMeshes(TR3Level level)

private static void MirrorTextures(TR1Level level)
{
// Collect unique texture references from each of the rooms
ISet<ushort> textureReferences = new HashSet<ushort>();

// Keep track of static meshes so they are only processed once,
// and so we only target those actually in use in rooms.
ISet<TRStaticMesh> processedMeshes = new HashSet<TRStaticMesh>();
HashSet<ushort> textureReferences = new();
HashSet<TRStaticMesh> processedMeshes = new();

foreach (TR1Room room in level.Rooms)
{
// Invert the faces, otherwise they are inside out
foreach (TRFace4 f in room.Mesh.Rectangles)
{
Swap(f.Vertices, 0, 3);
Swap(f.Vertices, 1, 2);
textureReferences.Add(f.Texture);
}

foreach (TRFace3 f in room.Mesh.Triangles)
{
Swap(f.Vertices, 0, 2);
textureReferences.Add(f.Texture);
}

MirrorRoomMeshTextures(room.Mesh, textureReferences);
foreach (TR1RoomStaticMesh roomStaticMesh in room.StaticMeshes)
{
TRStaticMesh staticMesh = level.StaticMeshes[roomStaticMesh.ID];
if (!processedMeshes.Add(staticMesh))
{
continue;
}

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

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

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

foreach (TRMeshFace face in staticMesh.Mesh.ColouredTriangles)
{
face.SwapVertices(0, 2);
MirrorMeshTextures(staticMesh.Mesh, textureReferences);
}
}
}
Expand All @@ -1007,60 +965,18 @@ private static void MirrorTextures(TR1Level level)

private static void MirrorTextures(TR2Level level)
{
// Collect unique texture references from each of the rooms
ISet<ushort> textureReferences = new HashSet<ushort>();

// Keep track of static meshes so they are only processed once,
// and so we only target those actually in use in rooms.
ISet<TRStaticMesh> processedMeshes = new HashSet<TRStaticMesh>();
HashSet<ushort> textureReferences = new();
HashSet<TRStaticMesh> processedMeshes = new();

foreach (TR2Room room in level.Rooms)
{
// Invert the faces, otherwise they are inside out
foreach (TRFace4 f in room.Mesh.Rectangles)
{
Swap(f.Vertices, 0, 3);
Swap(f.Vertices, 1, 2);
textureReferences.Add(f.Texture);
}

foreach (TRFace3 f in room.Mesh.Triangles)
{
Swap(f.Vertices, 0, 2);
textureReferences.Add(f.Texture);
}

MirrorRoomMeshTextures(room.Mesh, textureReferences);
foreach (TR2RoomStaticMesh roomStaticMesh in room.StaticMeshes)
{
TRStaticMesh staticMesh = level.StaticMeshes[roomStaticMesh.ID];
if (!processedMeshes.Add(staticMesh))
{
continue;
}

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

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

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

foreach (TRMeshFace face in staticMesh.Mesh.ColouredTriangles)
{
face.SwapVertices(0, 2);
MirrorMeshTextures(staticMesh.Mesh, textureReferences);
}
}
}
Expand All @@ -1079,57 +995,18 @@ private static void MirrorTextures(TR2Level level)

private static void MirrorTextures(TR3Level level)
{
ISet<ushort> textureReferences = new HashSet<ushort>();

ISet<TRStaticMesh> processedMeshes = new HashSet<TRStaticMesh>();
HashSet<ushort> textureReferences = new();
HashSet<TRStaticMesh> processedMeshes = new();

foreach (TR3Room room in level.Rooms)
{
// Invert the faces, otherwise they are inside out
foreach (TRFace4 f in room.Mesh.Rectangles)
{
Swap(f.Vertices, 0, 3);
Swap(f.Vertices, 1, 2);
textureReferences.Add((ushort)(f.Texture & 0x0fff));
}

foreach (TRFace3 f in room.Mesh.Triangles)
{
Swap(f.Vertices, 0, 2);
textureReferences.Add((ushort)(f.Texture & 0x0fff));
}

MirrorRoomMeshTextures(room.Mesh, textureReferences);
foreach (TR3RoomStaticMesh roomStaticMesh in room.StaticMeshes)
{
TRStaticMesh staticMesh = level.StaticMeshes[roomStaticMesh.ID];
if (!processedMeshes.Add(staticMesh))
{
continue;
}

// Flip the faces and store texture references
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 staticMesh.Mesh.ColouredRectangles)
{
face.SwapVertices(0, 3);
face.SwapVertices(1, 2);
}

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

foreach (TRMeshFace face in staticMesh.Mesh.ColouredTriangles)
if (processedMeshes.Add(staticMesh))
{
face.SwapVertices(0, 2);
MirrorMeshTextures(staticMesh.Mesh, textureReferences);
}
}
}
Expand All @@ -1145,6 +1022,51 @@ private static void MirrorTextures(TR3Level level)
MirrorObjectTextures(textureReferences, level.ObjectTextures);
}

private static void MirrorRoomMeshTextures<T, V>(TRRoomMesh<T, V> mesh, ISet<ushort> textureReferences)
where T : Enum
where V : TRRoomVertex
{
foreach (TRFace face in mesh.Rectangles)
{
face.SwapVertices(0, 3);
face.SwapVertices(1, 2);
textureReferences.Add(face.Texture);
}

foreach (TRFace face in mesh.Triangles)
{
face.SwapVertices(0, 2);
textureReferences.Add(face.Texture);
}
}

private static void MirrorMeshTextures(TRMesh mesh, ISet<ushort> textureReferences)
{
foreach (TRMeshFace face in mesh.TexturedRectangles)
{
face.SwapVertices(0, 3);
face.SwapVertices(1, 2);
textureReferences.Add(face.Texture);
}

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

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

foreach (TRMeshFace face in mesh.ColouredTriangles)
{
face.SwapVertices(0, 2);
}
}

private static void MirrorObjectTextures(ISet<ushort> textureReferences, List<TRObjectTexture> objectTextures)
{
// Flip the object texture vertices in the same way as done for faces
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private static ISet<ushort> MirrorMeshes(IEnumerable<TRModel> models)
{
face.SwapVertices(0, 3);
face.SwapVertices(1, 2);
textureReferences.Add((ushort)(face.Texture & 0x0fff));
textureReferences.Add(face.Texture);
}

foreach (TRMeshFace face in mesh.ColouredRectangles)
Expand All @@ -66,7 +66,7 @@ private static ISet<ushort> MirrorMeshes(IEnumerable<TRModel> models)
foreach (TRMeshFace face in mesh.TexturedTriangles)
{
face.SwapVertices(0, 2);
textureReferences.Add((ushort)(face.Texture & 0x0fff));
textureReferences.Add(face.Texture);
}

foreach (TRMeshFace face in mesh.ColouredTriangles)
Expand Down
Loading

0 comments on commit 2320522

Please sign in to comment.