i dont know really how to use the stencil buffers either, but Jinto gave some really good example code. this is what i use:
include(“shared.lua”)
local MaterialBlurX = Material( “pp/blurx” );
local MaterialBlurY = Material( “pp/blury” );
local MaterialWhite = CreateMaterial( “WhiteMaterial”, “VertexLitGeneric”, {
["$basetexture"] = “color/white”,
["$vertexalpha"] = “1”,
["$model"] = “1”, } );
local MaterialComposite = CreateMaterial( “CompositeMaterial”, “UnlitGeneric”, {
["$basetexture"] = “_rt_FullFrameFB”,
["$additive"] = “1”, } );
// we need two render targets, if you don’t want to create them yourself, you can // use the bloom textures, they are quite low resolution though.
// render.GetBloomTex0() and render.GetBloomTex1();
local RT1 = render.GetBloomTex0()
local RT2 = render.GetBloomTex1();
/------------------------------------ RenderToStencil() ------------------------------------/
local function RenderToStencil( entity )
// tell the stencil buffer we’re going to write a value of one wherever the model
// is rendered
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 );
// this uses a small hack to render ignoring depth while not drawing color
// i couldn’t find a function in the engine to disable writing to the color channels
// i did find one for shaders though, but I don’t feel like writing a shader for this.
cam.IgnoreZ( true );
render.SetBlend( 0 );
SetMaterialOverride( MaterialWhite );
entity:DrawModel();
SetMaterialOverride();
render.SetBlend( 1 );
cam.IgnoreZ( false );
// don’t need this for the next pass
render.SetStencilEnable( false );
end
/------------------------------------ RenderToGlowTexture() ------------------------------------/
local function RenderToGlowTexture( entity )
local w, h = ScrW(), ScrH();
// draw into the white texture
local oldRT = render.GetRenderTarget();
render.SetRenderTarget( RT1 );
render.SetViewPort( 0, 0, 512, 512 );
cam.IgnoreZ( true );
render.SuppressEngineLighting( true );
render.SetColorModulation( 1, 0, 0 );
// Set color of glow here
SetMaterialOverride( MaterialWhite );
entity:DrawModel();
SetMaterialOverride();
render.SetColorModulation( 1, 1, 1 );
render.SuppressEngineLighting( false );
cam.IgnoreZ( false );
render.SetViewPort( 0, 0, w, h );
render.SetRenderTarget( oldRT );
end
/------------------------------------ RenderScene() ------------------------------------/
local function RenderScene( Origin, Angles )
render.ClearRenderTarget( RT1, Color( 0, 0, 0, 255 ) );
end hook.Add( “RenderScene”, “ResetGlow”, RenderScene );
/------------------------------------ RenderScreenspaceEffects() ------------------------------------/
local function RenderScreenspaceEffects( )
MaterialBlurX:SetMaterialTexture( “$basetexture”, RT1 );
MaterialBlurY:SetMaterialTexture( “$basetexture”, RT2 );
MaterialBlurX:SetMaterialFloat( “$size”, 2 );
MaterialBlurY:SetMaterialFloat( “$size”, 2 );
local oldRT = render.GetRenderTarget();
// blur horizontally
render.SetRenderTarget( RT2 );
render.SetMaterial( MaterialBlurX );
render.DrawScreenQuad();
// blur vertically
render.SetRenderTarget( RT1 );
render.SetMaterial( MaterialBlurY );
render.DrawScreenQuad();
render.SetRenderTarget( oldRT );
// tell the stencil buffer we’re only going to draw
// where the player models are not.
render.SetStencilEnable( true );
render.SetStencilReferenceValue( 0 );
render.SetStencilTestMask( 1 );
render.SetStencilCompareFunction( STENCILCOMPARISONFUNCTION_EQUAL );
render.SetStencilPassOperation( STENCILOPERATION_ZERO );
// composite the scene
MaterialComposite:SetMaterialTexture( “$basetexture”, RT1 );
render.SetMaterial( MaterialComposite );
render.DrawScreenQuad();
// don’t need this anymore
render.SetStencilEnable( false );
end
hook.Add( “RenderScreenspaceEffects”, “CompositeGlow”, RenderScreenspaceEffects );
/------------------------------------ PrePlayerDraw() ------------------------------------/
function ENT:Draw()
self.Entity:DrawModel()
RenderToStencil( self );
RenderToGlowTexture( self );
end
[editline]03:01PM[/editline]
however, that requires some render targets’ give me a sec to write it so it dosent