From 26977b69c92eeb524c9ab91d19e7337431bbd4fc Mon Sep 17 00:00:00 2001 From: penolakushari <45951611+penolakushari@users.noreply.github.com> Date: Mon, 23 Dec 2024 20:12:54 +0300 Subject: [PATCH] Attempt to optimize the distance drawing by having it run distance check every 0.1 of a second rather than each tick --- lua/autorun/ragdollmover.lua | 26 +++++++++++++------ lua/weapons/gmod_tool/stools/ragdollmover.lua | 14 +++++++++- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/lua/autorun/ragdollmover.lua b/lua/autorun/ragdollmover.lua index ee4bde6..b407944 100644 --- a/lua/autorun/ragdollmover.lua +++ b/lua/autorun/ragdollmover.lua @@ -1105,20 +1105,24 @@ concommand.Add("ragdollmover_changelog", function() showChangelog() end) -function AdvBoneSelectRender(ent, bonenodes) +function AdvBoneSelectRender(ent, bonenodes, prevbones, calc) local mx, my = input.GetCursorPos() -- possible bug on mac https://wiki.facepunch.com/gmod/input.GetCursorPos local nodesExist = bonenodes and bonenodes[ent] and true local bonedistances = {} local plpos = LocalPlayer():EyePos() local mindist, maxdist = nil, nil - for i = 0, ent:GetBoneCount() - 1 do - local dist = plpos:DistToSqr( ent:GetBonePosition(i) ) - if not mindist or mindist > dist then mindist = dist end - if not maxdist or maxdist < dist then maxdist = dist end - bonedistances[i] = dist + if calc then + prevbones = {} + + for i = 0, ent:GetBoneCount() - 1 do + local dist = plpos:DistToSqr( ent:GetBonePosition(i) ) + if not mindist or mindist > dist then mindist = dist end + if not maxdist or maxdist < dist then maxdist = dist end + bonedistances[i] = dist + end + maxdist = maxdist - mindist end - maxdist = maxdist - mindist local selectedBones = {} for i = 0, ent:GetBoneCount() - 1 do @@ -1137,12 +1141,16 @@ function AdvBoneSelectRender(ent, bonenodes) v.y = v.y + y end + if calc then + prevbones[i] = ( ( bonedistances[i] - mindist ) < maxdist * 0.5 ) and 1 or 2 + end + if dist < 576 then -- 24 pixels surface.SetDrawColor(COLOR_BRIGHT_YELLOW:Unpack()) table.insert(selectedBones, {name, i}) else if nodesExist and bonenodes[ent][i] and bonenodes[ent][i].Type then - surface.SetDrawColor(BONETYPE_COLORS[bonenodes[ent][i].Type][( ( bonedistances[i] - mindist ) < maxdist * 0.5 ) and 1 or 2]:Unpack()) + surface.SetDrawColor(BONETYPE_COLORS[bonenodes[ent][i].Type][prevbones[i]]:Unpack()) else surface.SetDrawColor(COLOR_RGMGREEN:Unpack()) end @@ -1197,6 +1205,8 @@ function AdvBoneSelectRender(ent, bonenodes) draw.SimpleTextOutlined(selectedBones[i + 1][1], "RagdollMoverFont", xPos, yPos, color, TEXT_ALIGN_LEFT, TEXT_ALIGN_BOTTOM, OUTLINE_WIDTH, COLOR_RGMBLACK) end + + return prevbones end function AdvBoneSelectPick(ent, bonenodes) diff --git a/lua/weapons/gmod_tool/stools/ragdollmover.lua b/lua/weapons/gmod_tool/stools/ragdollmover.lua index e4bd7ef..46d318b 100644 --- a/lua/weapons/gmod_tool/stools/ragdollmover.lua +++ b/lua/weapons/gmod_tool/stools/ragdollmover.lua @@ -4892,6 +4892,9 @@ hook.Add("KeyPress", "rgmSwitchSelectionMode", function(pl, key) end end) +local BoneColors = nil +local LastThink, LastEnt = 0, nil + function TOOL:DrawHUD() if not RAGDOLLMOVER[pl] then RAGDOLLMOVER[pl] = {} end @@ -4902,6 +4905,7 @@ function TOOL:DrawHUD() local bone = plTable.Bone local axis = plTable.Axis local moving = plTable.Moving or false + local thinktime = CurTime() --We don't draw the axis if we don't have the axis entity or the target entity, --or if we're not allowed to draw it. @@ -4947,11 +4951,19 @@ function TOOL:DrawHUD() end if self:GetOperation() == 2 and IsValid(ent) then + local timecheck = (thinktime - LastThink) > 0.1 + local calc = ( not LastEnt or LastEnt ~= ent ) or timecheck + if self:GetStage() == 0 then - rgm.AdvBoneSelectRender(ent, nodes) + BoneColors = rgm.AdvBoneSelectRender(ent, nodes, BoneColors, calc) else rgm.AdvBoneSelectRadialRender(ent, plTable.SelectedBones, nodes, ResetMode) end + + LastEnt = ent + if timecheck then + LastThink = thinktime + end elseif IsValid(tr.Entity) and EntityFilter(tr.Entity, self) and (not bone or aimedbone ~= bone or tr.Entity ~= ent) and not moving then rgm.DrawBoneConnections(tr.Entity, aimedbone) rgm.DrawBoneName(tr.Entity, aimedbone)