evasivedocs
CS2 scriptingLua 5.4.9 · 25 guides · 292 API entries
Advanced

Custom shaders

scene lets a script make its own chams materials. You write a small HLSL pixel shader, and evasive draws players, your first person weapon or arms, or weapons on the ground with it.

Needs: Allow custom shaders. While it is off, scene is nil. Drawing on players also needs Allow game scripting. Shaders run on your GPU, and evasive switches off any material that gets too slow.

1. A flat material

Lua
if not scene then error("turn on Allow custom shaders in Scripts > Security", 0) end

local flat, err = scene.material{
    color = "#ff5050",
    hlsl = [[
float4 Surface(SurfaceInput s, MaterialParams p) {
  return s.color;
}]],
}
if not flat then print("material failed: " .. tostring(err)) end

events.on("tick", function()
    if not flat or not entities then return end
    for _, p in ipairs(entities.players("enemies")) do
        p:set_material(flat)
    end
end)

Every shader defines one function, Surface, that returns the colour of a pixel as float4(r, g, b, a) from 0 to 1. s.color is the material's color.

p:set_material(m) draws that player with your material instead of the chams settings for one second. Calling it every tick keeps it on while the player is listed. When the player leaves the list, or your script stops, the normal chams come back by themselves. Pass {lease = seconds} as a second argument for a longer hold, up to an hour, and p:set_material(nil) to clear it.

2. A rim light

s.facing is 1 where the surface faces the camera and falls to 0 at the edges. Raise its inverse to a power and you get a glow around the outline:

hlsl
float4 Surface(SurfaceInput s, MaterialParams p) {
  const float rim = pow(1.0f - s.facing, 2.5f);
  return float4(s.color.rgb * (0.2f + rim * 1.5f), s.color.a * saturate(0.1f + rim));
}

3. Params from the menu

params passes named values to the shader. A number becomes a float, a colour a float4. Change them any time with :set:

Lua
if not scene then error("turn on Allow custom shaders in Scripts > Security", 0) end

local power = ui.slider("Rim power", 0.5, 6, 2.5, {decimals = 1})
local edge = ui.color("Edge colour", color(190, 245, 255))

local glow = scene.material{
    color = "#4fd8ff",
    params = {power = 2.5, edgeColor = "#bef5ff"},
    hlsl = [[
float4 Surface(SurfaceInput s, MaterialParams p) {
  const float rim = pow(1.0f - s.facing, p.power);
  const float3 col = s.color.rgb * 0.25f + p.edgeColor.rgb * rim;
  return float4(col, s.color.a * saturate(0.1f + rim));
}]],
}

events.on("tick", function()
    if not glow or not entities then return end
    glow:set("power", power:get())
    glow:set("edgeColor", edge:get())
    for _, p in ipairs(entities.players("enemies")) do
        p:set_material(glow)
    end
end)

:set gives false for a name the material does not have. A value of the wrong type is an error. Params are uploaded to the GPU once a frame, however often you set them.

What the shader gets

SurfaceInput s has:

Field What it is
s.position The world position of the pixel.
s.normal The surface normal, pointing toward the eye.
s.facing 1 when the surface faces the camera, toward 0 at the edges.
s.view The view direction.
s.bind The position on the model in its rest pose, so patterns stick to the body.
s.uv bind.xy / 100, handy for textures.
s.color The material's colour, or occluded_color behind walls.
s.time Seconds, for animation.
s.screen The pixel position on screen.
s.depth The depth of the pixel.
s.occluded Set when the pixel is behind a wall.
s.hitgroup The hitgroup under the pixel, or -1.

MaterialParams p holds your params by name.

The material spec

Key What it does
hlsl The shader source. Required.
color The base colour, given to the shader as s.color.
occluded_color The colour behind walls. Defaults to color.
layer "both", "visible" (only the parts not behind a wall) or "occluded" (only the parts behind a wall).
params Named numbers and colours, up to 16.
textures Up to 4 images, from draw.load_image, a media cover, world.overview, or a .png or .jpg path in the scripts folder. Sample them with tex0(uv) to tex3(uv).

One colour in view and another behind walls, with the simplest shader there is:

Lua
local walls = scene and scene.material{
    color = "#7cff9a",
    occluded_color = "#ff6060",
    hlsl = [[float4 Surface(SurfaceInput s, MaterialParams p) { return s.color; }]],
}

Where materials go

Call What it draws
p:set_material(m, opts) A player. One second by default, or opts.lease seconds.
scene.viewmodel(m, part) Your first person "weapon" or "arms", until you call it again with nil.
scene.item(index, m, opts) A weapon on the ground, from entities.items(). One second by default. Needs a loaded map.

Compiling and errors

  • scene.material checks the text first. Problems it finds come back at once as nil and "line N: ...", counted in your shader text.
  • The real compile runs in the background, up to 4 per script at a time. Until it is done the material draws flat in its colour. m:ready() turns true when it draws as written.
  • Compiler errors show up in m:error() and in the console.
  • A compile that takes over 2 seconds fails with shader compile took too long. After two of those, the script cannot compile more until you reload it.
  • Loading the same source again, for example on a reload, is ready at once.

Rules for the shader text:

  • Shader model 5.0, up to 16 KiB of source and 2048 instructions.
  • No # directives, no textures or samplers of your own and no register().
  • Every loop needs [unroll(n)] with n at most 64, covering all its runs. [loop] is refused.
  • Define a function before you use it.
  • An array or struct holds at most 256 values, and an array needs a literal size.
  • Structs go at file scope, once per name, without inheritance. SurfaceInput, MaterialParams, VO and PO are taken. class, vector<>, matrix<> and typedef are refused.

The GPU budget

evasive measures every material on the GPU. One whose median time is over 0.5 ms a frame, or that takes 4 ms twice, or that pushes all materials together past 1 ms, is drawn flat from then on, and m:error() says why. m:gpu_ms() gives the smoothed time per frame so you can keep an eye on it.

Housekeeping

A script can have up to 8 materials. scene.materials() lists them with id, compiling, ready, gpu_ms, error and users. m:release() frees one and clears everything it was assigned to. All of them go away when the script unloads, reloads, stops on an error or is suspended.

The Hologram chams example puts all of this together.