Properly Sizing and Centering a Model in a DModelPanel

Hey, I’m making a hud that requires I render the player’s world model onto the hud. I’m having some trouble ensuring that I get a consistent look with all or most weapons.

I would really appreciate any help anyone can give me here.



local modelPanel = LocalPlayer().modelPanel

if not modelPanel then
	modelPanel = vgui.Create( "DModelPanel" )
	LocalPlayer().modelPanel = modelPanel
end

local x = ScrW() - 300
local y = ScrH() - 280

modelPanel:SetSize( 500, 500 )
modelPanel:SetFOV( 110 )
modelPanel:SetPos( x, y )
modelPanel:SetModel( LocalPlayer():GetActiveWeapon():GetWeaponWorldModel() )
modelPanel:SetColor( Color( 0, 255, 0 ) )
function modelPanel:LayoutEntity( Entity ) return end

local PrevMins, PrevMaxs = modelPanel.Entity:GetRenderBounds()
modelPanel:SetCamPos(PrevMins:Distance(PrevMaxs) * Vector(0.75, 0.75, 0.5))
modelPanel:SetLookAt((PrevMaxs + PrevMins)/2)



I’m pretty sure there was code somewhere to center anything in a DModelPanel like this but I just can’t find it on the wiki anymore.

Here’s what it currently looks like

and here’s what the overall goal is (ignoring aspect ratio differences):

You’ll probably have to play around with the CamPos, LookAt and FOV for each weapon and then store those values on the SWEP. You could use a DAdjustableModelPanel just to get the values quickly and then tweak them from there, though bear in mind DAdjustableModelPanel’s rotation is gimbal-locked somehow.

Trying to generate it automatically won’t get very satisfying results since every model is different – half of them don’t even point in the same direction.

Yeah, remember that those icons are created manually for some reason

Well, in this case they were made manually for 2002 reasons - but I know what you mean

I’m very sure that there was code to center models arbitrarily in a view somewhere on the wiki but I can’t find it now

Also, if it’s not possible how are spawn icons generated for custom models?

Spawn icons just try to fit the entire model in-frame. They don’t care about how good it looks, what angle you’re seeing it from or which way it’s pointing.

You can always try use the position of it, instead of rotating using RenderBounds (as I tried renderbounds and it wouldnt let me resize it)

But with this code, your model can be resized.



	modelPanel:SetSize( 500, 500 )
	modelPanel:SetFOV( 110 ) // To zoom in
	modelPanel:SetPos( 50, 50 )
	modelPanel:SetModel( LocalPlayer():GetActiveWeapon():GetWeaponWorldModel() )
	modelPanel:SetColor( Color( 0, 255, 0 ) )
	function modelPanel:LayoutEntity( Entity ) return end
	
        local eyepos = modelPanel.Entity:GetPos()
	eyepos:Add( Vector( 0, 25, 25 ) ) // Play with me to rotate
	local PrevMins, PrevMaxs = modelPanel.Entity:GetRenderBounds()
	modelPanel:SetCamPos(eyepos-Vector(0,0,25)) // Play with me to rotate
	modelPanel:SetLookAt((PrevMaxs + PrevMins)/2)