Facepunch / garrysmod-issues

Garry's Mod issue tracker
137 stars 56 forks source link

Creating a 3D rendering context with non-default properties breaks Vector:ToScreen() #1404

Open willox opened 9 years ago

willox commented 9 years ago
hook.Add("PreDrawHUD", "", function()

    cam.Start3D(nil, nil, 20) -- Create and destroy a 3D rendering context with a FOV of 20
    cam.End3D()

    cam.Start2D()
        -- This call to ToScreen thinks that the screen's FOV is 20
        local pos = Vector(0, 0, 0):ToScreen()
        local x, y = pos.x, pos.y

        surface.SetDrawColor(Color(255, 0, 0))

        surface.DrawRect(pos.x - 16, pos.y - 16, 32, 32)
    cam.End2D()

    cam.Start3D() -- Create and destroy a default 3D rendering context
    cam.End3D()

    cam.Start2D()
        -- This call to ToScreen thinks that the screen's FOV is its real value
        local pos = Vector(0, 0, 0):ToScreen()
        local x, y = pos.x, pos.y

        surface.SetDrawColor(Color(0, 255, 0))

        surface.DrawRect(pos.x - 16, pos.y - 16, 32, 32)
    cam.End2D()

end)

This example shows the issue by modifying the FOV, but changing any parameter to Start3D will cause this.

The confusing thing about this is that in some cases it is optimal behaviour, but usually it isn't.

DBotThePony commented 8 years ago

Any updates? This bug forces DModelPanel to break ToScreen() waht called after model is drawn https://github.com/garrynewman/garrysmod/blob/master/garrysmod/lua/vgui/dmodelpanel.lua#L151

Update: Calling

cam.Start3D() 
cam.End3D()

with no arguments after panel is drawn will fix ToScreen()

2016-05-01_09-16_h-srv-gm_test-garrysmod-addon 2016-05-01_09-16_garry s mod 2016-05-01_09-16_h-srv-gm_test-garrysmod-addon 2

Removing cam context 2016-05-01_09-17_h-srv-gm_test-garrysmod-addon 2016-05-01_09-18_garry s mod