Attempt to optimize the distance drawing by having it run distance check every 0.1 of a second rather than each tick

This commit is contained in:
penolakushari 2024-12-23 20:12:54 +03:00
parent e81fd36d8f
commit 26977b69c9
2 changed files with 31 additions and 9 deletions

View File

@ -1105,13 +1105,16 @@ 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
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
@ -1119,6 +1122,7 @@ function AdvBoneSelectRender(ent, bonenodes)
bonedistances[i] = dist
end
maxdist = maxdist - mindist
end
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)

View File

@ -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)