From 2f031910b39003cc3807b9a147b5e051581803e5 Mon Sep 17 00:00:00 2001 From: hondacrx Date: Wed, 28 Apr 2021 10:32:24 -0400 Subject: [PATCH] Core/Vmaps: Reduce memory used by vmaps (and their size, slightly) Port From (https://github.com/TrinityCore/TrinityCore/commit/bb8f22ed2013f8cb6b9d61c738f2ebd96b83722f) --- .../Game/Collision/Management/VMapManager.cs | 12 ++-- .../Game/Collision/Models/GameObjectModel.cs | 2 - Source/Game/Collision/Models/ModelInstance.cs | 51 +++++++++------ Source/Game/Collision/Models/WorldModel.cs | 63 +++++++++---------- 4 files changed, 64 insertions(+), 64 deletions(-) diff --git a/Source/Game/Collision/Management/VMapManager.cs b/Source/Game/Collision/Management/VMapManager.cs index cf1f91ec4..826c22727 100644 --- a/Source/Game/Collision/Management/VMapManager.cs +++ b/Source/Game/Collision/Management/VMapManager.cs @@ -292,19 +292,15 @@ namespace Game.Collision var model = iLoadedModelFiles.LookupByKey(filename); if (model == null) { - WorldModel worldmodel = new(); - if (!worldmodel.ReadFile(VMapPath + filename)) + model = new ManagedModel(); + if (!model.GetModel().ReadFile(VMapPath + filename)) { Log.outError(LogFilter.Server, "VMapManager: could not load '{0}'", filename); return null; } Log.outDebug(LogFilter.Maps, "VMapManager: loading file '{0}'", filename); - - worldmodel.Flags = flags; - - model = new ManagedModel(); - model.SetModel(worldmodel); + model.GetModel().Flags = flags; iLoadedModelFiles.Add(filename, model); } @@ -382,7 +378,7 @@ namespace Game.Collision { public ManagedModel() { - iModel = null; + iModel = new(); iRefCount = 0; } diff --git a/Source/Game/Collision/Models/GameObjectModel.cs b/Source/Game/Collision/Models/GameObjectModel.cs index df25edec0..b964ed50b 100644 --- a/Source/Game/Collision/Models/GameObjectModel.cs +++ b/Source/Game/Collision/Models/GameObjectModel.cs @@ -60,7 +60,6 @@ namespace Game.Collision if (iModel == null) return false; - name = modelData.name; iPos = modelOwner.GetPosition(); iScale = modelOwner.GetScale(); iInvScale = 1.0f / iScale; @@ -224,7 +223,6 @@ namespace Game.Collision Log.outInfo(LogFilter.ServerLoading, "Loaded {0} GameObject models in {1} ms", StaticModelList.models.Count, Time.GetMSTimeDiffToNow(oldMSTime)); } - string name; bool _collisionEnabled; AxisAlignedBox iBound; Matrix3 iInvRot; diff --git a/Source/Game/Collision/Models/ModelInstance.cs b/Source/Game/Collision/Models/ModelInstance.cs index 234b65ba0..cf99b6f6f 100644 --- a/Source/Game/Collision/Models/ModelInstance.cs +++ b/Source/Game/Collision/Models/ModelInstance.cs @@ -29,9 +29,23 @@ namespace Game.Collision ParentSpawn = 1 << 2 } - public class ModelSpawn + public class ModelMinimalData { + public byte flags; + public byte adtId; + public uint Id; + public Vector3 iPos; + public float iScale; + public AxisAlignedBox iBound; + public string name; + } + + public class ModelSpawn : ModelMinimalData + { + public Vector3 iRot; + public ModelSpawn() { } + public ModelSpawn(ModelSpawn spawn) { flags = spawn.flags; @@ -48,8 +62,8 @@ namespace Game.Collision { spawn = new ModelSpawn(); - spawn.flags = reader.ReadUInt32(); - spawn.adtId = reader.ReadUInt16(); + spawn.flags = reader.ReadByte(); + spawn.adtId = reader.ReadByte(); spawn.Id = reader.ReadUInt32(); spawn.iPos = reader.Read(); spawn.iRot = reader.Read(); @@ -67,30 +81,33 @@ namespace Game.Collision spawn.name = reader.ReadString((int)nameLen); return true; } - - public uint flags; - public ushort adtId; - public uint Id; - public Vector3 iPos; - public Vector3 iRot; - public float iScale; - public AxisAlignedBox iBound; - public string name; } - public class ModelInstance : ModelSpawn + public class ModelInstance : ModelMinimalData { + Matrix3 iInvRot; + float iInvScale; + WorldModel iModel; + public ModelInstance() { iInvScale = 0.0f; iModel = null; } + public ModelInstance(ModelSpawn spawn, WorldModel model) - : base(spawn) { + flags = spawn.flags; + adtId = spawn.adtId; + Id = spawn.Id; + iPos = spawn.iPos; + iScale = spawn.iScale; + iBound = spawn.iBound; + name = spawn.name; + iModel = model; - iInvRot = Matrix3.fromEulerAnglesZYX(MathFunctions.PI * iRot.Y / 180.0f, MathFunctions.PI * iRot.X / 180.0f, MathFunctions.PI * iRot.Z / 180.0f).inverse(); + iInvRot = Matrix3.fromEulerAnglesZYX(MathFunctions.PI * spawn.iRot.Y / 180.0f, MathFunctions.PI * spawn.iRot.X / 180.0f, MathFunctions.PI * spawn.iRot.Z / 180.0f).inverse(); iInvScale = 1.0f / iScale; } @@ -193,9 +210,5 @@ namespace Game.Collision } public void SetUnloaded() { iModel = null; } - - Matrix3 iInvRot; - float iInvScale; - WorldModel iModel; } } diff --git a/Source/Game/Collision/Models/WorldModel.cs b/Source/Game/Collision/Models/WorldModel.cs index 8f0ad0efc..540e3405e 100644 --- a/Source/Game/Collision/Models/WorldModel.cs +++ b/Source/Game/Collision/Models/WorldModel.cs @@ -25,21 +25,29 @@ namespace Game.Collision { public struct MeshTriangle { + public uint idx0; + public uint idx1; + public uint idx2; + public MeshTriangle(uint na, uint nb, uint nc) { idx0 = na; idx1 = nb; idx2 = nc; } - - public uint idx0; - public uint idx1; - public uint idx2; } public class WmoLiquid { + uint iTilesX; + uint iTilesY; + Vector3 iCorner; + uint iType; + float[] iHeight; + byte[] iFlags; + public WmoLiquid() { } + public WmoLiquid(uint width, uint height, Vector3 corner, uint type) { iTilesX = width; @@ -58,6 +66,7 @@ namespace Game.Collision iFlags = null; } } + public WmoLiquid(WmoLiquid other) { if (this == other) @@ -155,23 +164,23 @@ namespace Game.Collision } public uint GetLiquidType() { return iType; } - float[] GetHeightStorage() { return iHeight; } - byte[] GetFlagsStorage() { return iFlags; } - - uint iTilesX; - uint iTilesY; - Vector3 iCorner; - uint iType; - float[] iHeight; - byte[] iFlags; } public class GroupModel : IModel { + AxisAlignedBox iBound; + uint iMogpFlags; + uint iGroupWMOID; + List vertices = new(); + List triangles = new(); + BIH meshTree = new(); + WmoLiquid iLiquid; + public GroupModel() { iLiquid = null; } + public GroupModel(GroupModel other) { iBound = other.iBound; @@ -185,6 +194,7 @@ namespace Game.Collision if (other.iLiquid != null) iLiquid = new WmoLiquid(other.iLiquid); } + public GroupModel(uint mogpFlags, uint groupWMOID, AxisAlignedBox bound) { iBound = bound; @@ -193,11 +203,6 @@ namespace Game.Collision iLiquid = null; } - void SetLiquidData(WmoLiquid liquid) - { - iLiquid = liquid; - } - public bool ReadFromFile(BinaryReader reader) { triangles.Clear(); @@ -294,22 +299,15 @@ namespace Game.Collision public uint GetMogpFlags() { return iMogpFlags; } public uint GetWmoID() { return iGroupWMOID; } - - AxisAlignedBox iBound; - uint iMogpFlags; - uint iGroupWMOID; - List vertices = new(); - List triangles = new(); - BIH meshTree = new(); - WmoLiquid iLiquid; } public class WorldModel : IModel { - public WorldModel() - { - RootWMOID = 0; - } + public uint Flags; + + uint RootWMOID; + List groupModels = new(); + BIH groupTree = new(); public override bool IntersectRay(Ray ray, ref float distance, bool stopAtFirstHit, ModelIgnoreFlags ignoreFlags) { @@ -408,10 +406,5 @@ namespace Game.Collision return groupTree.ReadFromFile(reader); } } - - List groupModels = new(); - BIH groupTree = new(); - uint RootWMOID; - public uint Flags; } }