Skip to content

Commit

Permalink
Merge pull request #758 from wiox/halo_improvements
Browse files Browse the repository at this point in the history
Halo module improvements
  • Loading branch information
robotboy655 committed Aug 7, 2014
2 parents bee8fd5 + 0768920 commit 2dcedc8
Showing 1 changed file with 109 additions and 118 deletions.
227 changes: 109 additions & 118 deletions garrysmod/lua/includes/modules/halo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function Add( ents, color, blurx, blury, passes, add, ignorez )
if ( add == nil ) then add = true end
if ( ignorez == nil ) then ignorez = false end

local t =
local t =
{
Ents = ents,
Color = color,
Expand All @@ -26,148 +26,139 @@ function Add( ents, color, blurx, blury, passes, add, ignorez )
Additive = add,
IgnoreZ = ignorez
}

table.insert( List, t )

end

function Render( entry )

local OldRT = render.GetRenderTarget()

-- Copy what's currently on the screen to another texture
render.CopyRenderTargetToTexture( rt_Store )

-- Clear the colour and the stencils, not the depth
if ( entry.Additive ) then
if ( entry.Additive ) then
render.Clear( 0, 0, 0, 255, false, true )
else
render.Clear( 255, 255, 255, 255, false, true )
end


-- FILL STENCIL
-- Write to the stencil..
cam.Start3D( EyePos(), EyeAngles() )

cam.IgnoreZ( entry.IgnoreZ )
render.OverrideDepthEnable( true, false ) -- Don't write depth

render.SetStencilEnable( true )
render.SetStencilFailOperation( STENCILOPERATION_KEEP )
render.SetStencilZFailOperation( STENCILOPERATION_KEEP )
render.SetStencilPassOperation( STENCILOPERATION_REPLACE )
render.SetStencilCompareFunction( STENCILCOMPARISONFUNCTION_ALWAYS )
render.SetStencilWriteMask( 1 )
render.SetStencilReferenceValue( 1 )

render.SetBlend( 0 ) -- don't render any colour

for k, v in pairs( entry.Ents ) do

if ( !IsValid( v ) ) then continue end

v:DrawModel()

end

cam.End3D()

-- FILL COLOUR
-- Write to the colour buffer
cam.Start3D( EyePos(), EyeAngles() )

render.MaterialOverride( matColor )

-- Write to the stencil
render.SetStencilEnable( true )

cam.IgnoreZ( entry.IgnoreZ )

render.SetStencilEnable( true )
render.SetStencilWriteMask( 0 )
render.SetStencilReferenceValue( 0 )
render.SetStencilTestMask( 1 )
render.SetStencilFailOperation( STENCILOPERATION_KEEP )
render.SetStencilPassOperation( STENCILOPERATION_KEEP )
render.SetStencilZFailOperation( STENCILOPERATION_KEEP )
render.SetStencilCompareFunction( STENCILCOMPARISONFUNCTION_NOTEQUAL )

for k, v in pairs( entry.Ents ) do

if ( !IsValid( v ) ) then continue end

render.SetColorModulation( entry.Color.r/255, entry.Color.g/255, entry.Color.b/255 )
render.SetBlend( entry.Color.a/255 )

v:DrawModel()

end

render.MaterialOverride( nil )
render.SetStencilEnable( false )

cam.End3D()

-- BLUR IT
render.CopyRenderTargetToTexture( rt_Stencil )
render.OverrideDepthEnable( false, false )
render.SetStencilEnable( false )
render.BlurRenderTarget( rt_Stencil, entry.BlurX, entry.BlurY, 1 )

-- Put our scene back
render.SetRenderTarget( OldRT )
render.SetColorModulation( 1, 1, 1 )
render.SetStencilEnable( false )
render.OverrideDepthEnable( true, false )
render.SetBlend( 1 )
mat_Copy:SetTexture( "$basetexture", rt_Store )
render.SetMaterial( mat_Copy )
render.DrawScreenQuad()


-- DRAW IT TO THE SCEEN

render.SetStencilEnable( true )
render.SetStencilWriteMask( 0 )
render.SetStencilReferenceValue( 0 )
render.SetStencilTestMask( 1 )
render.SetStencilFailOperation( STENCILOPERATION_KEEP )
render.SetStencilPassOperation( STENCILOPERATION_KEEP )
render.SetStencilZFailOperation( STENCILOPERATION_KEEP )
render.SetStencilCompareFunction( STENCILCOMPARISONFUNCTION_EQUAL )

if ( entry.Additive ) then
mat_Add:SetTexture( "$basetexture", rt_Stencil )
render.SetMaterial( mat_Add )
else
mat_Sub:SetTexture( "$basetexture", rt_Stencil )
render.SetMaterial( mat_Sub )
end

for i=0, entry.DrawPasses do
render.DrawScreenQuad()
end

-- PUT EVERYTHING BACK HOW WE FOUND IT

render.SetStencilWriteMask( 0 )
render.SetStencilReferenceValue( 0 )
render.SetStencilTestMask( 0 )
render.SetStencilEnable( false )
render.OverrideDepthEnable( false )
render.SetBlend( 1 )

render.SetStencilWriteMask( 1 )
render.SetStencilTestMask( 1 )
render.SetStencilReferenceValue( 1 )

render.SetStencilCompareFunction( STENCIL_ALWAYS )
render.SetStencilPassOperation( STENCIL_REPLACE )
render.SetStencilFailOperation( STENCIL_KEEP )
render.SetStencilZFailOperation( STENCIL_KEEP )

for k, v in pairs( entry.Ents ) do

if ( !IsValid( v ) ) then continue end

v:DrawModel()

end

render.SetStencilCompareFunction( STENCIL_EQUAL )
render.SetStencilPassOperation( STENCIL_KEEP )
render.SetStencilFailOperation( STENCIL_KEEP )
render.SetStencilZFailOperation( STENCIL_KEEP )

render.MaterialOverride( matColor )

local r, g, b, a = entry.Color.r / 255,
entry.Color.g / 255,
entry.Color.b / 255,
entry.Color.a / 255

for k, v in pairs( entry.Ents ) do

if ( !IsValid( v ) ) then continue end

render.SetColorModulation( r, g, b )
render.SetBlend( a )

v:DrawModel()

end

render.SetColorModulation( 1, 1, 1 )

render.MaterialOverride( nil )

cam.IgnoreZ( false )

render.SetStencilEnable( false )


-- Copy stencil to rt and blur it
render.CopyRenderTargetToTexture( rt_Stencil )
render.BlurRenderTarget( rt_Stencil, entry.BlurX, entry.BlurY, 1 )

-- Re-render stored scene
render.SetRenderTarget( OldRT )
mat_Copy:SetTexture( "$basetexture", rt_Store )
render.SetMaterial( mat_Copy )
render.DrawScreenQuad()


-- Draw halos to the screen!
render.SetStencilEnable( true )

render.SetStencilCompareFunction( STENCIL_NOTEQUAL )
render.SetStencilPassOperation( STENCIL_KEEP )
render.SetStencilFailOperation( STENCIL_KEEP )
render.SetStencilZFailOperation( STENCIL_KEEP )

if ( entry.Additive ) then

mat_Add:SetTexture( "$basetexture", rt_Stencil )
render.SetMaterial( mat_Add )

else

mat_Sub:SetTexture( "$basetexture", rt_Stencil )
render.SetMaterial( mat_Sub )

end

for i = 0, entry.DrawPasses do

render.DrawScreenQuad()

end

render.SetStencilEnable( false )


render.SetStencilWriteMask( 0 )
render.SetStencilReferenceValue( 0 )
render.SetStencilTestMask( 0 )
end

hook.Add( "PostDrawEffects", "RenderHalos", function()

hook.Run( "PreDrawHalos" )

if ( #List == 0 ) then return end

for k, v in pairs( List ) do
Render( v )
end


cam.Start3D()

for k, v in pairs( List ) do
Render( v )
end

cam.End3D()

List = {}

end )

0 comments on commit 2dcedc8

Please sign in to comment.