In this example we draw a lightning bolt over our player's head.
We shouldn't really draw the lightning in the PreDrawTranslucentRenderables hook as this causes issues rendering transparent objects behind the lightning, but it's a quick example of how the function works. Normally it should be drawn in a custom lua effect's EFFECT:Render.
This override might not work properly on Linux
Issue Tracker:
5693
local lightningMaterial
= Material(
"sprites/lgtning")
hook.
Add(
"PreDrawTranslucentRenderables",
"LightningExample",
function(isDrawingDepth, isDrawingSkybox)
if isDrawingDepth
or isDrawSkybox
then return end
local ply
= Entity(
1)
if !IsValid(ply)
then return end
local uv
= math.
Rand(
0,
1)
render.
OverrideBlend(
true, BLEND_SRC_COLOR, BLEND_SRC_ALPHA, BLENDFUNC_ADD, BLEND_ONE, BLEND_ZERO, BLENDFUNC_ADD )
render.
SetMaterial(lightningMaterial)
render.
StartBeam(
5)
render.
AddBeam(
ply:
GetPos()
+ Vector(
0,
0,
035),
20, uv,
Color(
255,
255,
255,
255))
render.
AddBeam(
ply:
GetPos()
+ Vector(
0,
0,
135)
+ Vector(
math.
Rand(
-20,
20)
,math.
Rand(
-20,
20),
0),
20, uv
*2,
Color(
255,
255,
255,
255))
render.
AddBeam(
ply:
GetPos()
+ Vector(
0,
0,
235)
+ Vector(
math.
Rand(
-20,
20)
,math.
Rand(
-20,
20),
0),
20, uv
*3,
Color(
255,
255,
255,
255))
render.
AddBeam(
ply:
GetPos()
+ Vector(
0,
0,
335)
+ Vector(
math.
Rand(
-20,
20)
,math.
Rand(
-20,
20),
0),
20, uv
*4,
Color(
255,
255,
255,
255))
render.
AddBeam(
ply:
GetPos()
+ Vector(
0,
0,
435)
+ Vector(
math.
Rand(
-20,
20)
,math.
Rand(
-20,
20),
0),
20, uv
*5,
Color(
255,
255,
255,
255))
render.
EndBeam()
render.
OverrideBlend(
false )
end )