Skip to content

Commit

Permalink
Some matrix optimizations (#1778)
Browse files Browse the repository at this point in the history
* Some matrix optimizations

* Fix error
  • Loading branch information
thegrb93 authored Jun 22, 2024
1 parent caefbeb commit 1bc3082
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
21 changes: 15 additions & 6 deletions lua/entities/starfall_screen/cl_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ function ENT:Initialize()
net.WriteUInt(self:EntIndex(), 16)
net.SendToServer()

self.Transform = {
lastUpdate = 0,
get = function(self)
if CurTime()>self.lastUpdate then
self.lastUpdate = CurTime()
self.matrixinv = self.matrix:GetInverseTR()
end
return self.matrix, self.matrixinv
end
}

local info = self.Monitor_Offsets[self:GetModel()]
if not info then
local mins = self:OBBMins()
Expand Down Expand Up @@ -52,7 +63,7 @@ function ENT:SetScreenMatrix(info)
self.Aspect = info.RatioX
self.Scale = info.RS
self.Origin = info.offset
self.Transform = self:GetWorldTransformMatrix() * self.ScreenMatrix
self.Transform.matrix = self:GetWorldTransformMatrix() * self.ScreenMatrix

local w, h = 512 / self.Aspect, 512
self.ScreenQuad = {Vector(0,0,0), Vector(w,0,0), Vector(w,h,0), Vector(0,h,0), Color(0, 0, 0, 255)}
Expand Down Expand Up @@ -106,12 +117,10 @@ function ENT:DrawTranslucent()
self:DrawModel()

if halo.RenderedEntity() == self then return end

local entityMatrix = self:GetWorldTransformMatrix()

-- Draw screen here
local transform = entityMatrix * self.ScreenMatrix
self.Transform = transform
local transform = self:GetWorldTransformMatrix() * self.ScreenMatrix
self.Transform.matrix = transform

cam.PushModelMatrix(transform)
render.ClearStencil()
render.SetStencilEnable(true)
Expand Down
34 changes: 15 additions & 19 deletions lua/starfall/libs_cl/render.lua
Original file line number Diff line number Diff line change
Expand Up @@ -714,28 +714,20 @@ end
-- ------------------------------------------------------------------ --

--- Pushes a matrix onto the model matrix stack.
-- @param VMatrix m The matrix
-- @param boolean? world Should the transformation be relative to the screen or world?
function render_library.pushMatrix(m, world)
if world == nil then
world = renderdata.usingRT
-- @param VMatrix transform The matrix
-- @param boolean? absolute (default false) Should the transformation be absolute with respect to world or multipled with existing stack?
function render_library.pushMatrix(transform, absolute)
if absolute == nil then
absolute = renderdata.usingRT
end

if not renderdata.isRendering then SF.Throw("Not in rendering hook.", 2) end
local id = #matrix_stack
if id + 1 > MATRIX_STACK_LIMIT then SF.Throw("Pushed too many matrices", 2) end
local newmatrix
if matrix_stack[id] then
newmatrix = matrix_stack[id] * munwrap(m)
else
newmatrix = munwrap(m)
if not world and renderdata.renderEnt and renderdata.renderEnt.Transform then
newmatrix = renderdata.renderEnt.Transform * newmatrix
end
end
transform = munwrap(transform)

matrix_stack[id + 1] = newmatrix
cam.PushModelMatrix(newmatrix)
matrix_stack[id + 1] = transform
cam.PushModelMatrix(transform, not absolute)
end

--- Enables a scissoring rect which limits the drawing area. Only works 2D contexts such as HUD or render targets.
Expand Down Expand Up @@ -2232,14 +2224,18 @@ function render_library.cursorPos(ply, screen)
end

if screen~=nil then screen = getent(screen) else screen = renderdata.renderEnt end
if not (screen and screen.Transform) then SF.Throw("Invalid screen", 2) end
if not screen then SF.Throw("Invalid screen", 2) end
local screenTransform = screen.Transform
if not screenTransform then SF.Throw("Invalid screen", 2) end

local transform, transforminv = screenTransform:get()

local Normal, Pos
-- Get monitor screen pos & size

Pos = screen:LocalToWorld(screen.Origin)

Normal = -screen.Transform:GetUp():GetNormalized()
Normal = -transform:GetUp():GetNormalized()

local Start = ply:GetShootPos()
local Dir = ply:GetAimVector()
Expand All @@ -2252,7 +2248,7 @@ function render_library.cursorPos(ply, screen)
local B = Normal:Dot(Pos-Start) / A
if (B >= 0) then
local w = 512 / screen.Aspect
local HitPos = screen.Transform:GetInverseTR() * (Start + Dir * B)
local HitPos = transforminv * (Start + Dir * B)
local x = HitPos.x / screen.Scale^2
local y = HitPos.y / screen.Scale^2
if x < 0 or x > w or y < 0 or y > 512 then return nil end -- Aiming off the screen
Expand Down

0 comments on commit 1bc3082

Please sign in to comment.