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

Custom shaders

Chams materials with your own HLSL.

scene

Chams materials with the script's own HLSL pixel shader, drawn on players, the first-person weapon or arms and dropped weapons.

4 entriesAllow custom shaders

Everything here needs Allow custom shaders in Scripts > Security (off by default).

scene.material

function
scene.material(spec) -> Material | nil, string

Your own chams material: an HLSL pixel shader you write, for Player:set_material / scene.viewmodel / scene.item. spec = {hlsl: string (required), color?, occluded_color? (behind walls, default color), layer?: "both" | "visible" | "occluded", params?: {name = number | colour string / table}, textures?: {image | file, ...} (at most 4: draw.load_image / media / world.overview images or a .png / .jpg in the scripts folder)}. hlsl defines float4 Surface(SurfaceInput s, MaterialParams p) with s = {position, normal (towards the eye), facing (1 = facing the camera), view, bind (bind-pose position), uv (bind.xy / 100), color, time (seconds), screen (pixels), depth, occluded, hitgroup (-1)} and p = the params in name order (a number is a float, a colour a float4); tex0(uv)..tex3(uv) sample the textures in order (wrapping, straight alpha; a missing one is transparent). ps_5_0, at most 16 KiB of source, 16 params, 2048 instructions; no #directives, own textures / samplers, register() or [loop]: every loop needs [unroll(n)], n <= 64, covering all its iterations, and a function is defined before its first use. An array, and a struct with its nested members, holds at most 256 values (vector / matrix components and struct members count); an array needs a literal size. A struct is defined at file scope (not in a function, struct or block) and once per name (SurfaceInput, MaterialParams, VO and PO are taken), without inheritance; class, vector<>, matrix<> and typedef are refused. What the text check refuses comes back at once as "line N: ..." of your text; the compile itself runs in the background (at most 4 a script at a time): the material draws flat in its color until Material:ready(), and compiler errors, or "shader compile took too long" when it is not done within 2 s, arrive as Material:error() and on the console. A source that took too long is refused without compiling again while it is among the 16 most recent such sources (of any script). After 2 compiles that took too long the script cannot start or queue more until it is reloaded. A reload of the same source is ready at once. A material over the GPU budget (a median over 0.5 ms, 4 ms twice, 1 ms for all of them) is drawn flat from then on and Material:error() says why. At most 8 a script; released with the script (unload, reload, error, suspend). Needs the "Allow custom shaders" permission (shaders).

Arguments
NameTypeDescription
spectable
Returns
  • Material | nil, string
Example
Lua
local holo = scene.material{ color = "#4fd8ff", hlsl = [[float4 Surface(SurfaceInput s, MaterialParams p) { return s.color * (0.4 + 0.6 * pow(1 - s.facing, 2)); }]] }

scene.viewmodel

function
scene.viewmodel(m [, part]) -> boolean

The first-person "weapon" or "arms" drawn with m (also when weapon / hands chams are off) until cleared (nil = back to the chams settings).

Arguments
NameTypeDefaultDescription
mMaterial | nil
partoptionalstring"weapon"
Returns
  • boolean
Example
Lua
scene.viewmodel(holo, "weapon")

scene.item

function
scene.item(index, m [, opts]) -> boolean

A dropped weapon (entities.items index) drawn with m for 1 s, or opts.lease seconds; renew it to keep it. Needs the map depth (a loaded map).

Arguments
NameTypeDescription
indexinteger
mMaterial | nil
optsoptionaltable
Returns
  • boolean
Example
Lua
for _, it in ipairs(entities.items()) do scene.item(it.index, holo) end

scene.materials

function
scene.materials() -> table[]

This script's materials: {id, compiling, ready, gpu_ms?, error?, users}.

Returns
  • table[]
Example
Lua
for _, m in ipairs(scene.materials()) do print(m.id, m.gpu_ms) end

Material

A chams material (scene.material). Free it with :release(); it goes with the script.

7 entriesAllow custom shaders

Everything here needs Allow custom shaders in Scripts > Security (off by default).

Material:set

method
Material:set(name, value) -> boolean

Set a param (uploaded once a frame). false for an unknown name; a value of the wrong type is an error.

Arguments
NameTypeDescription
namestring
valuenumber | color
Returns
  • boolean
Example
Lua
holo:set("speed", 1.2)

Material:get

method
Material:get(name) -> number | color | nil

A param's value.

Arguments
NameTypeDescription
namestring
Returns
  • number | color | nil
Example
Lua
local s = holo:get("speed")

Material:error

method
Material:error() -> string | nil

Why the material is not drawn as written: compiler errors ("line N: ..."), "shader compile took too long", "2 shader compiles of this script took too long: reload it to compile more", "disabled: over GPU budget". nil while it compiles.

Returns
  • string | nil
Example
Lua
print(holo:error())

Material:ready

method
Material:ready() -> boolean

True once it draws as written: its compile finished without an error (and it was not disabled).

Returns
  • boolean
Example
Lua
if holo:ready() then print("hologram compiled") end

Material:gpu_ms

method
Material:gpu_ms() -> number | nil

Its smoothed GPU time a frame (sampled frames); nil until measured.

Returns
  • number | nil
Example
Lua
print(holo:gpu_ms())

Material:release

method
Material:release()

Free it and clear what it is assigned to.

Example
Lua
holo:release()

Material.id

field

Its id (scene.materials).

Returns
  • integer