From ad37026066760599dfb7c9ddfb4aa4a325d53e5b Mon Sep 17 00:00:00 2001 From: Darsu <95997959+dar-su@users.noreply.github.com> Date: Fri, 31 Jan 2025 08:04:48 +0700 Subject: [PATCH] attachment position cache test, please see if anything changed --- lua/arc9/client/cl_garbage.lua | 14 +++--- lua/weapons/arc9_base/cl_drawmodel.lua | 7 +-- lua/weapons/arc9_base/cl_model.lua | 68 ++++++++++++++++++++------ 3 files changed, 63 insertions(+), 26 deletions(-) diff --git a/lua/arc9/client/cl_garbage.lua b/lua/arc9/client/cl_garbage.lua index 35e3fba3..38d74925 100644 --- a/lua/arc9/client/cl_garbage.lua +++ b/lua/arc9/client/cl_garbage.lua @@ -21,13 +21,13 @@ function ARC9.CollectGarbage() ARC9.CSModelPile = newpile - if ARC9.Dev(2) then - if removed > 0 then - print("ARC9 GC: Removed " .. tostring(removed) .. " CSModels") - else - -- print("ARC9 GC: Nothing removed") - end - end + -- if ARC9.Dev(2) then + -- if removed > 0 then + -- print("ARC9 GC: Removed " .. tostring(removed) .. " CSModels") + -- else + -- -- print("ARC9 GC: Nothing removed") + -- end + -- end end hook.Add("PostCleanupMap", "ARC9.CleanGarbage", function() diff --git a/lua/weapons/arc9_base/cl_drawmodel.lua b/lua/weapons/arc9_base/cl_drawmodel.lua index 0f2f4157..fd69f296 100644 --- a/lua/weapons/arc9_base/cl_drawmodel.lua +++ b/lua/weapons/arc9_base/cl_drawmodel.lua @@ -25,7 +25,9 @@ function SWEP:DrawCustomModel(wm, custompos, customang) local owner = self:GetOwner() if !wm and !IsValid(owner) then return end - if !wm and owner:IsNPC() then return end + local lod = self:ShouldLOD() + local isnpc = owner:IsNPC() or lod > 0 + if !wm and isnpc then return end if wm and ARC9.RTScopeRender then return end if custompos then wm = true end @@ -37,7 +39,6 @@ function SWEP:DrawCustomModel(wm, custompos, customang) mdl = self.CModel else mdl = self.WModel - lod = self:ShouldLOD() if lod == 0 and mdl and mdl[1]:IsValid() then mdl[1]:SetMaterial(self:GetProcessedValue("Material", true)) @@ -74,7 +75,7 @@ function SWEP:DrawCustomModel(wm, custompos, customang) if lod < 2 then local onground = wm and !IsValid(owner) - local hidebones = self:GetHiddenBones(wm) + local hidebones = isnpc and {} or self:GetHiddenBones(wm) for _, model in ipairs(mdl or {}) do if model.IsAnimationProxy then continue end diff --git a/lua/weapons/arc9_base/cl_model.lua b/lua/weapons/arc9_base/cl_model.lua index 8a7788d8..abb87201 100644 --- a/lua/weapons/arc9_base/cl_model.lua +++ b/lua/weapons/arc9_base/cl_model.lua @@ -1,5 +1,10 @@ SWEP.ModelVersion = 0 local v0, a0 = Vector(0, 0, 0), Angle(0, 0, 0) +local wwWorldToLocal = WorldToLocal +local llLocalToWorld = LocalToWorld + +SWEP.AttPosCache = {} +-- SWEP.BonePosCache = {} function SWEP:GetAttachmentPos(slottbl, wm, idle, nomodeloffset, custompos, customang, dupli) dupli = dupli or 0 @@ -77,25 +82,56 @@ function SWEP:GetAttachmentPos(slottbl, wm, idle, nomodeloffset, custompos, cust bone = slottbl.DuplicateModels[dupli].Bone or bone end + local selfpos, selfang = self:GetPos(), self:GetAngles() + if parentmdl and bone then - local boneindex = parentmdl:LookupBone(bone) + -- local bonecached = false - if !boneindex then return v0, a0, v0 end + -- local possiblebonecache = self.BonePosCache[bone] -- bone cache + -- if possiblebonecache then + -- if (possiblebonecache[3] or 0) > CurTime() then + -- bpos, bang = llLocalToWorld(possiblebonecache[1], possiblebonecache[2], selfpos, selfang) + -- bonecached = true + -- end + -- end - if parentmdl == self:GetOwner() then - parentmdl:SetupBones() - parentmdl:InvalidateBoneCache() - end - local bonemat = parentmdl:GetBoneMatrix(boneindex) - if bonemat then - bpos = bonemat:GetTranslation() - bang = bonemat:GetAngles() - end + -- if !bonecached then + local boneindex = parentmdl:LookupBone(bone) + + if !boneindex then return v0, a0, v0 end + + if parentmdl == self:GetOwner() then + parentmdl:SetupBones() + parentmdl:InvalidateBoneCache() + end + local bonemat = parentmdl:GetBoneMatrix(boneindex) + if bonemat then + bpos = bonemat:GetTranslation() + bang = bonemat:GetAngles() + end + + if !bang or !bpos then + bang = selfang + bpos = selfpos + end + + -- local xpos, xang = wwWorldToLocal(bpos, bang, selfpos, selfang) -- bone cache + -- self.BonePosCache[bone] = {xpos, xang, CurTime() + 1} + -- end elseif custompos then bpos = custompos bang = customang or a0 end + + local possiblecache = self.AttPosCache[slottbl.Address] -- att pos cache + if possiblecache then + if (possiblecache[4] or 0) > CurTime() then + local qpos, qang = llLocalToWorld(possiblecache[1], possiblecache[2], bpos, bang) + return qpos, qang, possiblecache[3] + end + end + if slottbl.OriginalAddress then local eles = self:GetAttachmentElements() @@ -110,11 +146,6 @@ function SWEP:GetAttachmentPos(slottbl, wm, idle, nomodeloffset, custompos, cust end end - if !bang or !bpos then - bang = self:GetAngles() - bpos = self:GetPos() - end - if wm then offset_pos = offset_pos * (self.WorldModelOffset.Scale or 1) end @@ -171,6 +202,11 @@ function SWEP:GetAttachmentPos(slottbl, wm, idle, nomodeloffset, custompos, cust apos = data.pos or apos aang = data.ang or aang + if slottbl.Address then -- att pos cache + local ypos, yang = wwWorldToLocal(apos, aang, bpos, bang) + self.AttPosCache[slottbl.Address] = {ypos, yang, icon_offset, CurTime() + 5} + end + return apos, aang, icon_offset end