Skip to content

Instantly share code, notes, and snippets.

@Be1zebub
Last active July 3, 2023 11:16
Show Gist options
  • Save Be1zebub/66a16af468e2f2d7e2a9fc0dc9ee9311 to your computer and use it in GitHub Desktop.
Save Be1zebub/66a16af468e2f2d7e2a9fc0dc9ee9311 to your computer and use it in GitHub Desktop.
some examples, how color arithmetic operators can be used
local function BlendColor(a, b, frac)
return a * (1 - frac) + b * frac
end
-- simple blending example
do
local red = Color(192, 57, 43)
local blue = Color(41, 128, 185)
hook.Add("HUDPaint", "color blending cycle", function()
local frac = (math.sin(RealTime() * 2) + 1) / 2
local blended = BlendColor(red, blue, frac)
surface.SetDrawColor(blended)
surface.DrawRect(64, 256 + 64, 128, 128)
end)
end
local function Clear(col)
render.Clear(col.r, col.g, col.b, col.a, true, true)
end
-- neon glow line
do
local rt = GetRenderTargetEx(
"NeonGlow",
1, 1, RT_SIZE_FULL_FRAME_BUFFER,
MATERIAL_RT_DEPTH_SEPARATE,
bit.bor(1, 256),
0,
IMAGE_FORMAT_RGBA8888
)
local rtMat = CreateMaterial("NeonGlow", "UnlitGeneric", {
["$basetexture"] = rt:GetName(),
["$translucent"] = "1"
})
local color = {
inner = Color(255, 255, 255),
outer = Color(255, 50, 70, 0)
}
local pos = Vector(256, 256 + 64)
local size = Vector(256, 2)
local distance = 10
local spread = 1
render.PushRenderTarget(rt)
render.Clear(0, 0, 0, 0, true, true)
for i = distance, 1, -1 do
local frac = i / distance
local step = (i - 1) * spread
render.SetViewPort(pos.x, pos.y - step, size.x, size.y + step * 2)
Clear(
BlendColor(color.inner, color.outer, frac)
)
end
render.SetViewPort(pos.x, pos.y, size.x, size.y)
Clear(color.inner)
render.PopRenderTarget()
hook.Add("HUDPaint", "NeonGlow", function()
render.SetMaterial(rtMat)
render.DrawScreenQuad()
end)
end
-- invert
do
local rt = GetRenderTarget("Invert", 256, 256)
local rtMat = CreateMaterial("Invert", "UnlitGeneric", {
["$basetexture"] = rt:GetName(),
["$translucent"] = "1"
})
render.PushRenderTarget(rt)
cam.Start2D()
surface.SetDrawColor(255, 255, 255)
surface.SetMaterial(Material("data/pre_bloom.png"))
surface.DrawTexturedRect(0, 0, 256, 256)
cam.End2D()
render.CapturePixels()
render.PopRenderTarget()
local y = 1
local fps = 5
hook.Add("HUDPaint", "Invert", function()
for _ = 1, fps do
if y < 256 then
render.PushRenderTarget(rt)
for x = 1, 256 do
local color = Color(render.ReadPixel(x, y))
color:Invert()
render.SetViewPort(x, y, 1, 1)
Clear(color)
end
render.PopRenderTarget()
y = y + 1
else
y = 1
render.PushRenderTarget(rt)
render.CapturePixels()
render.PopRenderTarget()
end
end
surface.SetDrawColor(255, 255, 255)
surface.SetMaterial(rtMat)
surface.DrawTexturedRect(512 + 64, 256 + 64, 256, 256)
end)
end
@Be1zebub
Copy link
Author

Facepunch/garrysmod#1971 commit required to run this gist

@Be1zebub
Copy link
Author

Be1zebub commented Jun 5, 2023

data/pre_bloom.png
pre_bloom
I used this pic when writing a bloom pseudo shader and reused it in this gist

@Be1zebub
Copy link
Author

Be1zebub commented Jun 5, 2023

if someone interested
compare
ingame

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment