shader

The game's shaders: world and screen effects, your own post shaders, live GLSL.

The game draws everything with GLSL shaders, and shader opens them to mods (since API 4), on PC and Android, in both games. The Shaders guide walks through it.

  • World effects (shader.world): values the port's code inside every game shader uses: colour (exposure, contrast, saturation, hue ...), style (visions like thermal or night vision, toon bands, posterize, sepia), a fog and a sonar ring by distance, and shape (waves, world curve, wobble, PS1 vertex snapping). shader.targets says which groups of shaders they change (the menus and the HUD are off by default).
  • Screen effects (shader.screen): a pass over the finished picture: blur, pixelate, bloom, outlines, CRT, scanlines, VHS, kaleidoscope ...
  • Post shaders (shader.post): your own fragment shader over the picture, after the screen effects, with uniforms you set from Lua.
  • Live shaders (shader.setSource): replace a game shader file's GLSL while the game runs. A source that does not compile or link is refused with the error, and the game keeps the shader it had.

While no mod sets a value the game looks exactly as it always did; the screen pass only runs while a screen effect or a post shader is on. Everything goes back to the game's own when your mod unloads (F9, Reload scripts, the Mods screen, quitting), and shader.reset() does it at once. A shader file a mod ships (sh_world_water.fsh in its folder, see The game's files) that does not compile no longer ends the game: the game's own file is used, and the log says why.

Fields

FieldTypeAccess
shader.worldShaderWorldread / writeThe world effects (fields on the Types page).
shader.screenShaderScreenread / writeThe screen effects.
shader.targetsShaderTargetsread / writeWhich shader groups the world effects change.

Functions

# shader.post(source, name?) → PostShader?, string?

since API 4

Adds a fragment shader of your own over the picture, after the screen effects (several run in the order they were added). Before your code it gets uniform sampler2D screen; (the picture so far), varying vec2 uv; (0-1, bottom left), uniform vec2 resolution; (pixels) and uniform float time; (seconds, back to 0 every 10 minutes), and the precision; your line 1 is the first line of source in error messages. Declare more uniforms and set them with fx:set. It stays until removed or your mod unloads (dropping the handle does not remove it).

ParameterType
sourcestringa GLSL ES 1.00 fragment shader (it gets screen, uv, resolution, time)
name optionalstringfor the log (default: your mod's id)
Returns
PostShader?the post shader, or nil
string?the compile or link error
local fx, err = shader.post([[
  uniform float strength;
  void main() {
    vec3 c = texture2D(screen, uv).rgb;
    float grey = dot(c, vec3(0.3, 0.59, 0.11));
    gl_FragColor = vec4(mix(c, vec3(grey), strength), 1.0);
  }]])
if not fx then print(err) return end
fx:set("strength", 0.8)
keys.bind("G", function() fx.enabled = not fx.enabled end)

# shader.files() → table

since API 4

The game's shader files that are loaded (the game loads them at start).

Returns
tablethe loaded shader files: {"sh_gui_sprite.fsh", ...}

# shader.programs() → table

since API 4

The game's shader programs: their files and the target group (shader.targets) they belong to.

Returns
table{{name, vertex, fragment, target}, ...}

# shader.source(file) → string

since API 4

A shader file's GLSL as the game uses it now (a mod's change included; without the API's own code).

ParameterType
filestringa loaded shader file, e.g. "sh_world_water.fsh"
Returns
stringits GLSL

# shader.original(file) → string

since API 4

The shader file's GLSL as the game loaded it.

ParameterType
filestring
Returns
string

# shader.setSource(file, source) → boolean, string?

since API 4

Replaces a game shader file while the game runs. The uniform and attribute names the game sets (input_mvp, input_texture ...) must stay. A source that does not compile or link is refused and the game keeps the shader it had. The world effects still work with your source (they wrap its main). Undone when your mod unloads.

ParameterType
filestringa loaded shader file
sourcestringits new GLSL
Returns
booleantrue: every program with the file uses it now
string?the error when it was refused
local file = "sh_world_water.fsh"
local src = shader.source(file)
-- redder water: change the last colour the shader writes
src = src:gsub("gl_FragColor = ", "gl_FragColor = vec4(1.3, 0.8, 0.8, 1.0) * ")
local ok, err = shader.setSource(file, src)
if not ok then print(err) end

# shader.revert(file) → boolean, string?

since API 4

The game's own GLSL for the file again.

ParameterType
filestring
Returns
boolean
string?error

# shader.reset()

since API 4

Everything back at once: world and screen effects, targets, every post shader and changed shader file (every mod's).