Shaders: the picture, from Lua
The game draws everything with GLSL ES shaders: the terrain, the water, the sky, the dinosaurs, the plants, the menus.
Since API 4 (release 2.3.0) the shader table opens them to script mods, in both games, on PC
and on Android. Four levels, from a value to your own GLSL:
| What | Example | |
|---|---|---|
| World effects | values the port's code inside every game shader uses: colour, style, fog, sonar, shape | shader.world.vision = "thermal" |
| Screen effects | a pass over the finished picture: blur, bloom, outlines, CRT, VHS ... | shader.screen.vhs = true |
| Post shaders | your own fragment shader over the picture | shader.post(glsl) |
| Live shaders | a game shader file's GLSL replaced while the game runs | shader.setSource("sh_world_water.fsh", glsl) |
While no mod sets anything the game looks exactly as it always did. Everything a mod changes is undone when it unloads
(F9, Reload scripts, the Mods screen, quitting); shader.reset() undoes it at once.
Shader Lab is a complete example: a window with presets, every value as a slider and a
GLSL editor.
World effects
shader.world is a table of values (the ShaderWorld type lists them with their
ranges). Setting one changes the picture at once:
-- an old film
shader.world.saturation = 0
shader.world.contrast = 1.25
shader.world.sepia = 0.8
-- night vision, half mixed in
shader.world:set{vision = "night", visionMix = 0.5}
-- a sonar ring sweeping out from the hunter every few seconds
shader.world:set{sonarStrength = 1.2, sonarSpeed = 6000, sonar = "#40ff90"}
-- the world curves away like a tiny planet, and sways
shader.world:set{curve = 1.6, wobble = 0.5}
shader.world:reset() -- the game's own again
The values work inside the game's own shaders, so they change the world where it is drawn: a fog by distance hides far
dinosaurs as well, the world curve bends the terrain and everything on it, and the HUD stays untouched.
shader.targets chooses which shaders take part: terrain, water, models (dinosaurs, animals, objects, the
hunter's weapon), plants, sky, particles, shadows and interface (menus, HUD, compass). The last two are off by
default: the game draws shadows by multiplying what is under them, so grading them darkens the ground twice.
shader.targets.sky = false -- a thermal world under the normal sky
shader.world.vision = "thermal"
The game's colours are dark (most of the world sits around 20-30 % brightness): brightness and contrast crush
them quickly. exposure (stops) and gamma keep the shapes.
Screen effects
shader.screen works the same way (ShaderScreen), over the finished picture: menus
and HUD included.
-- a CRT arcade cabinet
shader.screen:set{barrel = 0.6, scanlines = 0.7, crtMask = 0.6, chromatic = 2, vignette = 0.6, bloom = 0.3}
-- an old tape (the camcorder text is yours to draw: see Shader Lab)
shader.screen.vhs = true
-- comic book: toon bands in the world, black outlines on the screen
shader.world.toon = 5
shader.screen:set{outlines = 0.9, outlineThreshold = 0.35}
The screen pass only runs while a screen effect or a post shader is on; then the game draws into a framebuffer of the port's, which is drawn to the screen through the effects. Each effect samples the picture a few times (bloom 16 times, outlines 9): on a phone, use a few at a time.
Your own post shader
shader.post(source) compiles a fragment shader of yours and draws it over the picture, after the screen effects.
Before your code it gets the precision and:
uniform sampler2D screen; // the picture so far
uniform vec2 resolution; // in pixels
uniform float time; // seconds (back to 0 every 10 minutes)
varying vec2 uv; // 0-1, bottom left
local fx, err = shader.post([[
uniform float amount;
uniform vec3 tint;
void main() {
vec3 c = texture2D(screen, uv).rgb;
float d = distance(uv, vec2(0.5));
c = mix(c, c * tint, amount * smoothstep(0.2, 0.8, d)); // tinted edges
gl_FragColor = vec4(c, 1.0);
}]])
if not fx then print(err) return end -- the compiler's message, with your line numbers
fx:set("amount", 0.7)
fx:set("tint", {1.0, 0.3, 0.2}) -- vec3 from a table (or fx:set("tint", 1.0, 0.3, 0.2))
on("hit", function() fx:set("amount", 1) timer.after(0.3, function() fx:set("amount", 0.7) end) end)
keys.bind("G", function() fx.enabled = not fx.enabled end)
Several post shaders (of one mod or of several) run one after the other, in the order they were added. fx:remove()
takes one away; dropping the handle does not.
Live shaders
shader.files() lists the game's shader files and shader.source(file) gives one's GLSL. shader.setSource(file,
glsl) compiles new GLSL into every program that uses the file, while the game runs:
local file = "sh_world_water.fsh"
local src = shader.source(file)
src = src:gsub("gl_FragColor = ", "gl_FragColor = vec4(1.3, 0.8, 0.8, 1.0) * ") -- red water
local ok, err = shader.setSource(file, src)
if not ok then print(err) end -- refused: the game keeps the water shader it had
- The names the game sets (
input_mvp,input_texture,input_fog_params... and the attributes) must stay; read the original (shader.original(file)) to see them. Data file formats lists the files and which are used. - A source that does not compile, or a program that does not link with it, is refused:
falseand the compiler's message, and nothing changes. - The world effects keep working with your source: they wrap its
main(). shader.revert(file)puts the game's own back; unloading your mod does it for every file you changed.
Mods can still ship shader files by name (sh_world_water.fsh in the mod's folder replaces the game's at start,
The game's files). Since 2.3.0 such a file that does not compile no longer ends the game: the game's
own file is used, and the log says why.
Writing shaders that work everywhere
The shaders are GLSL ES 1.00 (OpenGL ES 2), and desktop GL compiles them too. Phones, and many desktop GPUs for
mediump, compute in 16-bit floats: the largest number is 65504 and precision is about 3 digits.
- Keep numbers small:
sin()of a large number gives NaN on some GPUs (black pixels),x * xoverflows quickly, and NaN times 0 is still NaN. timeis folded to 10 minutes for that reason; multiply it by small factors.- Hash functions without
sin()are safer:fract(vec3(p.xyx) * 0.1031)and its friends. - A post shader's
floatishighpwhere the GPU has it; the game's own fragment shaders default tomediump.
Limits
- Post shaders read the picture, not the depth: effects by distance belong in the world effects (
fogAmount,sonar,vision = "depth"), which run where the distance is known. - The screen effects and post shaders include the game's menus and HUD;
shader.targets.interfaceis about the world effects only. The port's own windows (F1, the console, mods'uiwindows) are drawn after them, untouched. - The game loads its shaders once, at start;
shader.setSourcechanges the loaded ones. A file a mod ships by name takes effect at the next start.