-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathstencil-mask.lua
50 lines (40 loc) · 1.45 KB
/
stencil-mask.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
-- https://github.com/Be1zebub/Small-GLua-Things/blob/master/libs/stencil-mask.lua
-- thx to stencil tutorial: https://github.com/Lexicality/stencil-tutorial
-- Simple stencil-mask wrapper
--[[ usage example:
draw.DrawMask(function()
surface.DrawPoly(GenerateStar(0, 0, 256, 256))
end, function()
surface.SetDrawColor(255, 255, 255)
surface.SetMaterial(fooBar)
surface.DrawTexturedRect(0, 0, 256, 256)
end)
]]--
local function Mask(doMask, doDraw, stencilCompareFunction)
render.ClearStencil()
render.SetStencilEnable(true)
render.SetStencilWriteMask(1)
render.SetStencilTestMask(1)
render.SetStencilFailOperation(STENCILOPERATION_REPLACE)
render.SetStencilPassOperation(STENCILOPERATION_ZERO)
render.SetStencilZFailOperation(STENCILOPERATION_ZERO)
render.SetStencilCompareFunction(STENCILCOMPARISONFUNCTION_NEVER)
render.SetStencilReferenceValue(1)
draw.NoTexture()
surface.SetDrawColor(255, 255, 255, 255)
doMask()
render.SetStencilFailOperation(STENCILOPERATION_ZERO)
render.SetStencilPassOperation(STENCILOPERATION_REPLACE)
render.SetStencilZFailOperation(STENCILOPERATION_ZERO)
render.SetStencilCompareFunction(stencilCompareFunction)
render.SetStencilReferenceValue(1)
doDraw()
render.SetStencilEnable(false)
render.ClearStencil()
end
function draw.DrawMask(doMask, doDraw)
Mask(doMask, doDraw, STENCILCOMPARISONFUNCTION_EQUAL)
end
function draw.DrawMaskInverted(doMask, doDraw)
Mask(doMask, doDraw, STENCILCOMPARISONFUNCTION_NOTEQUAL)
end