Skip to content

Commit

Permalink
Refactor room meshes (LostArtefacts#637)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahm86 authored Apr 30, 2024
1 parent 8e499e7 commit 7fc5ddf
Show file tree
Hide file tree
Showing 86 changed files with 1,271 additions and 2,053 deletions.
6 changes: 3 additions & 3 deletions TREnvironmentEditor/Helpers/EMVisibilityPortal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ public class EMVisibilityPortal
public short BaseRoom { get; set; }
public short AdjoiningRoom { get; set; }
public TRVertex Normal { get; set; }
public TRVertex[] Vertices { get; set; }
public List<TRVertex> Vertices { get; set; }

public TRRoomPortal ToPortal(EMLevelData levelData)
{
BaseRoom = (short)(BaseRoom < 0 ? levelData.NumRooms + BaseRoom : BaseRoom);
return new TRRoomPortal
return new()
{
AdjoiningRoom = (ushort)(AdjoiningRoom < 0 ? levelData.NumRooms + AdjoiningRoom : AdjoiningRoom),
Normal = Normal,
Expand All @@ -22,7 +22,7 @@ public TRRoomPortal ToPortal(EMLevelData levelData)

public static EMVisibilityPortal FromPortal(short baseRoom, TRRoomPortal portal)
{
return new EMVisibilityPortal
return new()
{
BaseRoom = baseRoom,
AdjoiningRoom = (short)portal.AdjoiningRoom,
Expand Down
60 changes: 17 additions & 43 deletions TREnvironmentEditor/Model/Types/Mirroring/EMMirrorFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,20 +347,7 @@ private void MirrorRooms(TR1Level level)
light.X = FlipWorldX(light.X);
}

// Move the static meshes
foreach (TR1RoomStaticMesh mesh in room.StaticMeshes)
{
mesh.X = (uint)FlipWorldX((int)mesh.X);

// Convert the angle to short units for consistency and then flip it if +/-X
int angle = mesh.Rotation + _south;
if (angle == _east || angle == _west)
{
angle *= -1;
angle -= _south;
mesh.Rotation = (ushort)angle;
}
}
MirrorRoomStaticMeshes(room.StaticMeshes);
}
}

Expand Down Expand Up @@ -421,20 +408,7 @@ private void MirrorRooms(TR2Level level)
light.X = FlipWorldX(light.X);
}

// Move the static meshes
foreach (TR2RoomStaticMesh mesh in room.StaticMeshes)
{
mesh.X = (uint)FlipWorldX((int)mesh.X);

// Convert the angle to short units for consistency and then flip it if +/-X
int angle = mesh.Rotation + _south;
if (angle == _east || angle == _west)
{
angle *= -1;
angle -= _south;
mesh.Rotation = (ushort)angle;
}
}
MirrorRoomStaticMeshes(room.StaticMeshes);
}
}

Expand Down Expand Up @@ -495,16 +469,19 @@ private void MirrorRooms(TR3Level level)
light.X = FlipWorldX(light.X);
}

// Move the static meshes
foreach (TR3RoomStaticMesh mesh in room.StaticMeshes)
{
mesh.X = (uint)FlipWorldX((int)mesh.X);
MirrorRoomStaticMeshes(room.StaticMeshes);
}
}

// Convert the angle to short units for consistency and then flip it
short angle = (short)(mesh.Rotation + _south);
angle *= -1;
angle -= _south;
mesh.Rotation = (ushort)angle;
private void MirrorRoomStaticMeshes<T>(IEnumerable<TRRoomStaticMesh<T>> meshes)
where T : Enum
{
foreach (TRRoomStaticMesh<T> mesh in meshes)
{
mesh.X = FlipWorldX(mesh.X);
if (mesh.Angle == _east || mesh.Angle == _west)
{
mesh.Angle *= -1;
}
}
}
Expand Down Expand Up @@ -1024,8 +1001,7 @@ private static void MirrorTextures(TR1Level level)

foreach (TR1RoomStaticMesh roomStaticMesh in room.StaticMeshes)
{
TR1Type id = roomStaticMesh.MeshID + TR1Type.SceneryBase;
TRStaticMesh staticMesh = level.StaticMeshes[id];
TRStaticMesh staticMesh = level.StaticMeshes[roomStaticMesh.ID];
if (!processedMeshes.Add(staticMesh))
{
continue;
Expand Down Expand Up @@ -1102,8 +1078,7 @@ private static void MirrorTextures(TR2Level level)

foreach (TR2RoomStaticMesh roomStaticMesh in room.StaticMeshes)
{
TR2Type id = roomStaticMesh.MeshID + TR2Type.SceneryBase;
TRStaticMesh staticMesh = level.StaticMeshes[id];
TRStaticMesh staticMesh = level.StaticMeshes[roomStaticMesh.ID];
if (!processedMeshes.Add(staticMesh))
{
continue;
Expand Down Expand Up @@ -1172,8 +1147,7 @@ private static void MirrorTextures(TR3Level level)

foreach (TR3RoomStaticMesh roomStaticMesh in room.StaticMeshes)
{
TR3Type id = roomStaticMesh.MeshID + TR3Type.SceneryBase;
TRStaticMesh staticMesh = level.StaticMeshes[id];
TRStaticMesh staticMesh = level.StaticMeshes[roomStaticMesh.ID];
if (!processedMeshes.Add(staticMesh))
{
continue;
Expand Down
30 changes: 15 additions & 15 deletions TREnvironmentEditor/Model/Types/Rooms/EMCopyRoomFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ public override void ApplyToLevel(TR1Level level)
newRoom.StaticMeshes.Add(new()
{
Intensity = baseRoom.StaticMeshes[i].Intensity,
MeshID = baseRoom.StaticMeshes[i].MeshID,
Rotation = baseRoom.StaticMeshes[i].Rotation,
X = (uint)(baseRoom.StaticMeshes[i].X + xdiff),
Y = (uint)(baseRoom.StaticMeshes[i].Y + ydiff),
Z = (uint)(baseRoom.StaticMeshes[i].Z + zdiff)
ID = baseRoom.StaticMeshes[i].ID,
Angle = baseRoom.StaticMeshes[i].Angle,
X = baseRoom.StaticMeshes[i].X + xdiff,
Y = baseRoom.StaticMeshes[i].Y + ydiff,
Z = baseRoom.StaticMeshes[i].Z + zdiff
});
}

Expand Down Expand Up @@ -266,11 +266,11 @@ public override void ApplyToLevel(TR2Level level)
{
Intensity1 = baseRoom.StaticMeshes[i].Intensity1,
Intensity2 = baseRoom.StaticMeshes[i].Intensity2,
MeshID = baseRoom.StaticMeshes[i].MeshID,
Rotation = baseRoom.StaticMeshes[i].Rotation,
X = (uint)(baseRoom.StaticMeshes[i].X + xdiff),
Y = (uint)(baseRoom.StaticMeshes[i].Y + ydiff),
Z = (uint)(baseRoom.StaticMeshes[i].Z + zdiff)
ID = baseRoom.StaticMeshes[i].ID,
Angle = baseRoom.StaticMeshes[i].Angle,
X = baseRoom.StaticMeshes[i].X + xdiff,
Y = baseRoom.StaticMeshes[i].Y + ydiff,
Z = baseRoom.StaticMeshes[i].Z + zdiff
});
}

Expand Down Expand Up @@ -409,12 +409,12 @@ public override void ApplyToLevel(TR3Level level)
newRoom.StaticMeshes.Add(new()
{
Colour = baseRoom.StaticMeshes[i].Colour,
MeshID = baseRoom.StaticMeshes[i].MeshID,
Rotation = baseRoom.StaticMeshes[i].Rotation,
ID = baseRoom.StaticMeshes[i].ID,
Unused = baseRoom.StaticMeshes[i].Unused,
X = (uint)(baseRoom.StaticMeshes[i].X + xdiff),
Y = (uint)(baseRoom.StaticMeshes[i].Y + ydiff),
Z = (uint)(baseRoom.StaticMeshes[i].Z + zdiff)
Angle = baseRoom.StaticMeshes[i].Angle,
X = baseRoom.StaticMeshes[i].X + xdiff,
Y = baseRoom.StaticMeshes[i].Y + ydiff,
Z = baseRoom.StaticMeshes[i].Z + zdiff
});
}

Expand Down
20 changes: 10 additions & 10 deletions TREnvironmentEditor/Model/Types/Rooms/EMImportRoomFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,11 @@ public override void ApplyToLevel(TR2Level level)
{
Intensity1 = roomDef.Room.StaticMeshes[i].Intensity1,
Intensity2 = roomDef.Room.StaticMeshes[i].Intensity2,
MeshID = roomDef.Room.StaticMeshes[i].MeshID,
Rotation = roomDef.Room.StaticMeshes[i].Rotation,
X = (uint)(roomDef.Room.StaticMeshes[i].X + xdiff),
Y = (uint)(roomDef.Room.StaticMeshes[i].Y + ydiff),
Z = (uint)(roomDef.Room.StaticMeshes[i].Z + zdiff)
ID = roomDef.Room.StaticMeshes[i].ID,
Angle = roomDef.Room.StaticMeshes[i].Angle,
X = roomDef.Room.StaticMeshes[i].X + xdiff,
Y = roomDef.Room.StaticMeshes[i].Y + ydiff,
Z = roomDef.Room.StaticMeshes[i].Z + zdiff
});
}

Expand Down Expand Up @@ -413,12 +413,12 @@ public override void ApplyToLevel(TR3Level level)
newRoom.StaticMeshes.Add(new()
{
Colour = roomDef.Room.StaticMeshes[i].Colour,
MeshID = roomDef.Room.StaticMeshes[i].MeshID,
Rotation = roomDef.Room.StaticMeshes[i].Rotation,
ID = roomDef.Room.StaticMeshes[i].ID,
Unused = roomDef.Room.StaticMeshes[i].Unused,
X = (uint)(roomDef.Room.StaticMeshes[i].X + xdiff),
Y = (uint)(roomDef.Room.StaticMeshes[i].Y + ydiff),
Z = (uint)(roomDef.Room.StaticMeshes[i].Z + zdiff)
Angle = roomDef.Room.StaticMeshes[i].Angle,
X = roomDef.Room.StaticMeshes[i].X + xdiff,
Y = roomDef.Room.StaticMeshes[i].Y + ydiff,
Z = roomDef.Room.StaticMeshes[i].Z + zdiff
});
}

Expand Down
41 changes: 21 additions & 20 deletions TREnvironmentEditor/Model/Types/Textures/EMAddStaticMeshFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ namespace TREnvironmentEditor.Model.Types;

public class EMAddStaticMeshFunction : BaseEMFunction
{
public uint MeshID { get; set; }
public ushort Intensity { get; set; }
public List<EMLocation> Locations { get; set; }
public TR2RoomStaticMesh Mesh { get; set; }
public bool IgnoreSectorEntities { get; set; }

public override void ApplyToLevel(TR1Level level)
Expand Down Expand Up @@ -49,12 +50,12 @@ public override void ApplyToLevel(TR1Level level)

room.StaticMeshes.Add(new()
{
X = (uint)location.X,
Y = (uint)(location.Y < 0 ? uint.MaxValue + location.Y : location.Y),
Z = (uint)location.Z,
Intensity = Mesh.Intensity1,
MeshID = Mesh.MeshID,
Rotation = (ushort)(location.Angle + short.MaxValue + 1)
X = location.X,
Y = location.Y,
Z = location.Z,
Intensity = Intensity,
ID = (TR1Type)MeshID,
Angle = location.Angle
});
}
}
Expand Down Expand Up @@ -97,13 +98,13 @@ public override void ApplyToLevel(TR2Level level)

room.StaticMeshes.Add(new()
{
X = (uint)location.X,
Y = (uint)(location.Y < 0 ? uint.MaxValue + location.Y : location.Y),
Z = (uint)location.Z,
Intensity1 = Mesh.Intensity1,
Intensity2 = Mesh.Intensity2,
MeshID = Mesh.MeshID,
Rotation = (ushort)(location.Angle + short.MaxValue + 1)
X = location.X,
Y = location.Y,
Z = location.Z,
Intensity1 = Intensity,
Intensity2 = Intensity,
ID = (TR2Type)MeshID,
Angle = location.Angle
});
}
}
Expand Down Expand Up @@ -146,12 +147,12 @@ public override void ApplyToLevel(TR3Level level)

room.StaticMeshes.Add(new()
{
X = (uint)location.X,
Y = (uint)(location.Y < 0 ? uint.MaxValue + location.Y : location.Y),
Z = (uint)location.Z,
Colour = Mesh.Intensity1,
MeshID = Mesh.MeshID,
Rotation = (ushort)(location.Angle + short.MaxValue + 1)
X = location.X,
Y = location.Y,
Z = location.Z,
Colour = Intensity,
ID = (TR3Type)MeshID,
Angle = location.Angle
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,11 @@ public override void ApplyToLevel(TR1Level level)
TR1Room room = level.Rooms[data.ConvertRoom(roomIndex)];
foreach (int meshIndex in Relocations[roomIndex].Keys)
{
TR1RoomStaticMesh mesh = room.StaticMeshes[meshIndex];
EMLocation location = new()
{
X = (int)mesh.X,
Y = (int)mesh.Y,
Z = (int)mesh.Z,
};
EMLocation relocation = Relocations[roomIndex][meshIndex];
AmendLocation(location, relocation);

mesh.X = (uint)location.X;
mesh.Y = (uint)location.Y;
mesh.Z = (uint)location.Z;
mesh.Rotation = (ushort)(relocation.Angle + short.MaxValue + 1);
MoveMesh(room.StaticMeshes[meshIndex], Relocations[roomIndex][meshIndex]);
}
}
}

private static void AmendLocation(EMLocation location, EMLocation amendment)
{
location.X += amendment.X;
location.Y += amendment.Y;
location.Z += amendment.Z;
}

public override void ApplyToLevel(TR2Level level)
{
EMLevelData data = GetData(level);
Expand All @@ -48,20 +28,7 @@ public override void ApplyToLevel(TR2Level level)
TR2Room room = level.Rooms[data.ConvertRoom(roomIndex)];
foreach (int meshIndex in Relocations[roomIndex].Keys)
{
TR2RoomStaticMesh mesh = room.StaticMeshes[meshIndex];
EMLocation location = new()
{
X = (int)mesh.X,
Y = (int)mesh.Y,
Z = (int)mesh.Z,
};
EMLocation relocation = Relocations[roomIndex][meshIndex];
AmendLocation(location, relocation);

mesh.X = (uint)location.X;
mesh.Y = (uint)location.Y;
mesh.Z = (uint)location.Z;
mesh.Rotation = (ushort)(relocation.Angle + short.MaxValue + 1);
MoveMesh(room.StaticMeshes[meshIndex], Relocations[roomIndex][meshIndex]);
}
}
}
Expand All @@ -74,21 +41,17 @@ public override void ApplyToLevel(TR3Level level)
TR3Room room = level.Rooms[data.ConvertRoom(roomIndex)];
foreach (int meshIndex in Relocations[roomIndex].Keys)
{
TR3RoomStaticMesh mesh = room.StaticMeshes[meshIndex];
EMLocation location = new()
{
X = (int)mesh.X,
Y = (int)mesh.Y,
Z = (int)mesh.Z,
};
EMLocation relocation = Relocations[roomIndex][meshIndex];
AmendLocation(location, relocation);

mesh.X = (uint)location.X;
mesh.Y = (uint)location.Y;
mesh.Z = (uint)location.Z;
mesh.Rotation = (ushort)(relocation.Angle + short.MaxValue + 1);
MoveMesh(room.StaticMeshes[meshIndex], Relocations[roomIndex][meshIndex]);
}
}
}

private static void MoveMesh<T>(TRRoomStaticMesh<T> mesh, EMLocation amendment)
where T : Enum
{
mesh.X += amendment.X;
mesh.Y += amendment.Y;
mesh.Z += amendment.Z;
mesh.Angle = amendment.Angle;
}
}
Loading

0 comments on commit 7fc5ddf

Please sign in to comment.