Attempt to optimize skeleton drawing. Results don't show much difference other than not drawing objects that are out of view giving some more fps, so I guess drawing functions themselves are quite expensive

This commit is contained in:
penolakushari 2024-12-24 22:07:54 +03:00
parent b7001ff354
commit 0b459b9727
2 changed files with 48 additions and 25 deletions

View File

@ -1486,33 +1486,42 @@ end
local SkeletonData = {}
local function DrawRecursiveBones(ent, bone, bonenodes)
local function DrawRecursiveBones(ent, bone, bonenodes, poscache)
local mainpos = ent:GetBonePosition(bone)
poscache[bone] = mainpos
mainpos = mainpos:ToScreen()
local nodecache = bonenodes[ent]
local nodeexist = nodecache and true or false
local mainvisible = mainpos.visible
for _, boneid in ipairs(ent:GetChildBones(bone)) do
SkeletonData[boneid] = bone
local pos = ent:GetBonePosition(boneid)
pos = pos:ToScreen()
local posvisible = pos.visible
surface.SetDrawColor(COLOR_WHITE:Unpack())
surface.DrawLine(mainpos.x, mainpos.y, pos.x, pos.y)
DrawRecursiveBones(ent, boneid, bonenodes)
if mainvisible or posvisible then
surface.SetDrawColor(COLOR_WHITE:Unpack())
surface.DrawLine(mainpos.x, mainpos.y, pos.x, pos.y)
end
DrawRecursiveBones(ent, boneid, bonenodes, poscache)
local color
if nodeexist and nodecache[boneid] then
color = BONETYPE_COLORS[nodecache[boneid].Type][1]
else
color = COLOR_RGMGREEN
end
surface.DrawCircle(pos.x, pos.y, 2.5, color)
if posvisible then
surface.DrawCircle(pos.x, pos.y, 2.5, color)
end
end
end
function DrawSkeleton(ent, bonenodes)
function DrawSkeleton(ent, bonenodes, poscache, calc)
if SkeletonData.ent ~= ent then
SkeletonData = {}
poscache = {}
local num = ent:GetBoneCount() - 1
for v = 0, num do
@ -1525,7 +1534,7 @@ function DrawSkeleton(ent, bonenodes)
pos = ent:GetPos()
end
DrawRecursiveBones(ent, v, bonenodes)
DrawRecursiveBones(ent, v, bonenodes, poscache)
pos = pos:ToScreen()
local color
@ -1540,33 +1549,42 @@ function DrawSkeleton(ent, bonenodes)
SkeletonData.ent = ent
else
if calc then poscache = {} end
for bone, parent in pairs(SkeletonData) do
if type(bone) ~= "number" or parent == -1 then continue end
local pos = ent:GetBonePosition(bone)
local pos = calc and ent:GetBonePosition(bone) or poscache[bone]
pos = pos:ToScreen()
local parentpos = ent:GetBonePosition(parent)
local parentpos = calc and ent:GetBonePosition(parent) or poscache[parent]
parentpos = parentpos:ToScreen()
surface.SetDrawColor(COLOR_WHITE:Unpack())
surface.DrawLine(parentpos.x, parentpos.y, pos.x, pos.y)
if pos.visible or parentpos.visible then
surface.SetDrawColor(COLOR_WHITE:Unpack())
surface.DrawLine(parentpos.x, parentpos.y, pos.x, pos.y)
end
end
for bone, parent in pairs(SkeletonData) do
if type(bone) ~= "number" then continue end
local pos = ent:GetBonePosition(bone)
local pos = calc and ent:GetBonePosition(bone) or poscache[bone]
if calc then poscache[bone] = pos end
pos = pos:ToScreen()
local color
if bonenodes and bonenodes[ent] and bonenodes[ent][bone] and bonenodes[ent][bone].Type then
color = BONETYPE_COLORS[bonenodes[ent][bone].Type][1]
else
color = COLOR_RGMGREEN
end
if pos.visible then
local color
if bonenodes and bonenodes[ent] and bonenodes[ent][bone] and bonenodes[ent][bone].Type then
color = BONETYPE_COLORS[bonenodes[ent][bone].Type][1]
else
color = COLOR_RGMGREEN
end
surface.DrawCircle(pos.x, pos.y, 2.5, color)
surface.DrawCircle(pos.x, pos.y, 2.5, color)
end
end
end
return poscache
end
hook.Add("PopulateToolMenu", "RagdollMoverUtilities", function(form)

View File

@ -4892,8 +4892,8 @@ hook.Add("KeyPress", "rgmSwitchSelectionMode", function(pl, key)
end
end)
local BoneColors = nil
local LastThink, LastEnt = 0, nil
local BoneColors, BonePoses = nil, nil
local LastSelectThink, LastSkeletonThink, LastEnt = 0, 0, nil
function TOOL:DrawHUD()
@ -4947,11 +4947,16 @@ function TOOL:DrawHUD()
})
local aimedbone = IsValid(tr.Entity) and (tr.Entity:GetClass() == "prop_ragdoll" and plTable.AimedBone or 0) or 0
if IsValid(ent) and EntityFilter(ent, self) and SkeletonDraw then
rgm.DrawSkeleton(ent, nodes)
local timecheck = (thinktime - LastSkeletonThink) > 0,0667 -- 1/15
BonePoses = rgm.DrawSkeleton(ent, nodes, BonePoses, timecheck)
if timecheck then
LastSkeletonThink = thinktime
end
end
if self:GetOperation() == 2 and IsValid(ent) then
local timecheck = (thinktime - LastThink) > 0.1
local timecheck = (thinktime - LastSelectThink) > 0.1
local calc = ( not LastEnt or LastEnt ~= ent ) or timecheck
if self:GetStage() == 0 then
@ -4962,7 +4967,7 @@ function TOOL:DrawHUD()
LastEnt = ent
if timecheck then
LastThink = thinktime
LastSelectThink = 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)