Shader
<Shader fragment> is the material of the mesh it sits in, inside a Landscape, drawn by a GLSL fragment shader you write. Every shader in the engine draws through it, the CRT, portal and energy orb materials included, so a shader you write takes the same inputs as one the engine ships, and you can start from the engine’s source.
Its props are in ShaderProps.
A ShaderValue is a number, a three Vector2, Vector3, Color or Texture. Change a uniform’s value from React state, and the next frame draws it; no frame callback is needed.
Godot’s shading language is the model: it declares TIME and UV for you, and you add a uniform per knob. The shader is text in your game’s source, so your agent writes and changes it as it does any other file.
What the fragment gets
Section titled “What the fragment gets”The engine declares these ahead of your code. Do not declare them yourself.
| Name | GLSL type | What it holds |
|---|---|---|
uTime |
float |
Seconds of world time, from the same clock the frame steps. A paused world holds it. |
uPointer |
vec2 |
The pointer over the canvas, -1 to 1 on each axis, 0 at the centre. |
vUv |
vec2 |
The mesh’s texture coordinates: 0 to 1 across a plane, from the bottom left. |
vWorldPosition |
vec3 |
The point’s place in the world, in metres. With three’s cameraPosition, it gives the view angle. |
each uniforms key |
float, vec2, vec3 or sampler2D |
The value you passed, typed from it: a number is a float, a Color is a vec3, a Texture is a sampler2D. |
Because uTime is the world’s clock and not the wall’s, a paused game freezes every shader, a devtools step advances them by one step, the devtools’ speed scales them, and a headless screenshot taken at a given step shows the same moment every run. A game’s shader keeps the world’s clock so it can show gameplay time, such as a charge filling or a door’s glow counting down. The engine’s water material and a map’s sea run on the effects’ clock instead, with the grass’s wind: the pause holds them and the speed leaves them at 1x. Two screenshots of the same step compare pixel for pixel, which is how an agent checks that a change did what it meant.
Write GLSL ES 1.00 as three’s own examples do: texture2D, varying and gl_FragColor. Three sets the precision and the GLSL version. Pass smoothstep its lower edge first: GLSL leaves smoothstep(0.5, 0.4, x) undefined, so a phone may draw it differently. For a falling edge, write 1.0 - smoothstep(0.4, 0.5, x).
Sample
Section titled “Sample”A ring that breathes on the world’s clock, in a colour the game chooses:
import { Entity, Shader, Views, Landscape } from "@spawnite/engine";import { Color } from "three";
const ring = `void main() { float distance = length(vUv - 0.5); float radius = 0.3 + 0.05 * sin(uTime * 3.0); float band = 1.0 - smoothstep(0.0, 0.03, abs(distance - radius)); gl_FragColor = vec4(uTint * band, band);}`;
export function Portal() { return ( <Landscape map="meadow"> <Entity position={[0, 1.2, -3]}> <Views> <mesh> <planeGeometry args={[2, 2]} /> <Shader fragment={ring} uniforms={{ uTint: new Color("cyan") }} transparent /> </mesh> </Views> </Entity> </Landscape> );}Include a helper
Section titled “Include a helper”The engine ships small GLSL helpers for noise, random values, distance shapes, colour and math, one family to a file. Include a file by its path, and call its function:
#include "spawnite/noise/simplex.glsl"
void main() { gl_FragColor = vec4(vec3(snoise(vUv * 4.0)), 1.0);}Include the function rather than retyping it from memory: a retyped noise or distance function can compile and still draw the wrong thing.
When the game builds, the engine’s Vite plugin, spawnite(), replaces each #include with the file, along with the files that file includes. The shader therefore needs nothing at runtime: it draws offline, headless and the same way every run. A game whose fragments include nothing gets no helper text in its bundle. Every game loads the plugin, and Game says what else it does.
The plugin reads an include only where the fragment is written in the source: at the start of a line inside a template literal, as in the recipes below. It skips an include inside a // or /* */ comment, so commenting one out takes its file out of the shader. A fragment put together at runtime, such as one typed into a text box, cannot include: the #include reaches the GLSL compiler, which fails on it as on any compile error below.
Write the path in double quotes. Three reads #include <…> as one of its own chunks, such as <common>, so the plugin stops the build on a helper written in angle brackets.
A path that names no file stops the build and lists the helpers. The dev server shows the same error over the page, with the module that holds the include:
[plugin spawnite] src/Portal.tsxShader: no helper at "spawnite/noise/perlin.glsl". The engine's helpers are spawnite/color/hsv.glsl, …Godot includes a .gdshaderinc when it imports the shader, and Unity and Unreal include .hlsl or .ush files when they compile it. The engine resolves its helpers at the same point: when the game builds, not when it runs.
The helpers
Section titled “The helpers”Each file’s header comment names its functions, what they return, its source and its licence. The engine ships the following:
| Include | Functions |
|---|---|
spawnite/noise/simplex.glsl |
snoise(vec2) and snoise(vec3): smooth simplex noise, about -1 to 1. |
spawnite/noise/classic.glsl |
cnoise(vec2) and cnoise(vec3): classic Perlin noise; pnoise(p, period) repeats, for a tiling texture. |
spawnite/noise/cellular.glsl |
cellular(vec2) and cellular(vec3): Worley noise, vec2(F1, F2), the distances to the two nearest cells. |
spawnite/noise/fbm.glsl |
fbm(vec2) and fbm(vec3): four octaves of simplex noise; #define FBM_OCTAVES 6 above it sets more. |
spawnite/random/hash.glsl |
hash11 to hash33: 0 to 1, the first digit the values out and the second the values in. |
spawnite/random/random.glsl |
random(float), random(vec2) and random(vec3): 0 to 1, the same for the same input. |
spawnite/sdf/circle.glsl |
sdCircle(vec2 p, float radius): signed distance to a circle, negative inside. |
spawnite/sdf/box.glsl |
sdBox(vec2 p, vec2 halfSize) and sdBox(vec3 p, vec3 halfSize): signed distance to a box. |
spawnite/sdf/sphere.glsl |
sdSphere(vec3 p, float radius): signed distance to a sphere. |
spawnite/color/oklab.glsl |
linearSrgbToOklab(vec3) and oklabToLinearSrgb(vec3): a colour space where a blend changes evenly. |
spawnite/color/hsv.glsl |
rgbToHsv(vec3) and hsvToRgb(vec3): hue, saturation and value, each 0 to 1. |
spawnite/color/palette.glsl |
palette(t, a, b, c, d): a colour that cycles with t, Inigo Quilez’s cosine palette. |
spawnite/math/remap.glsl |
remap(x, inMin, inMax, outMin, outMax) for a float, vec2 or vec3, unclamped. |
spawnite/math/saturate.glsl |
saturate(x): x clamped to 0 to 1. |
Three declares luminance(vec3) ahead of every fragment, with the weights of the renderer’s colour space, so call it without an include.
Licence and credit
Section titled “Licence and credit”Games on the platform earn money, so a shader you copy or port needs a licence that allows commercial use: MIT, BSD, CC0 or CC BY. Shadertoy’s default licence, CC BY-NC-SA, does not. Port a Shadertoy shader only when its own licence line allows commercial use. Keep the author’s name and link in a comment above the fragment, and the licence line when the licence asks for it.
Each helper is under MIT or MIT-0, as its header says. An MIT helper’s header holds its authors’ notice, and the include carries that header into the game’s bundle, so it needs nothing from you. A helper under MIT-0 needs no credit. Copy a helper into your game and change it as you like; keep an MIT header with the copy.
The engine does not ship LYGIA. A game that installs the lygia package itself can include its files by their path, such as #include "lygia/generative/snoise.glsl", under LYGIA’s own licence.
Recipes
Section titled “Recipes”Each recipe is a mesh you can copy into a scene and change. Put the mesh inside an Entity to place it.
Glow ring
Section titled “Glow ring”A ring that breathes, with a glow falling off on both sides. sdCircle gives the distance from the circle’s edge, negative inside, on a plane whose UVs run from -1 at one edge to 1 at the other.
import { Shader } from "@spawnite/engine";import { Color } from "three";
const glowRing = `#include "spawnite/sdf/circle.glsl"
void main() { float radius = 0.6 + 0.05 * sin(uTime * 3.0); float distance = abs(sdCircle(vUv * 2.0 - 1.0, radius)); float glow = 0.02 / distance; gl_FragColor = vec4(uTint * glow, clamp(glow, 0.0, 1.0));}`;
export function GlowRing() { return ( <mesh> <planeGeometry args={[2, 2]} /> <Shader fragment={glowRing} uniforms={{ uTint: new Color("cyan") }} transparent /> </mesh> );}Scrolling scanlines
Section titled “Scrolling scanlines”Horizontal lines that scroll upwards, with a brighter band passing through them. It needs no helper, so the recipe is plain GLSL.
import { Shader } from "@spawnite/engine";import { Color } from "three";
const scanlines = `void main() { float lines = 0.5 + 0.5 * sin((vUv.y - uTime * 0.05) * 300.0); float band = 1.0 - smoothstep(0.0, 0.1, abs(fract(vUv.y - uTime * 0.3) - 0.5)); vec3 colour = uTint * (0.4 + 0.4 * lines + band); gl_FragColor = vec4(colour, 1.0);}`;
export function Scanlines() { return ( <mesh> <planeGeometry args={[2, 2]} /> <Shader fragment={scanlines} uniforms={{ uTint: new Color("#40ff90") }} /> </mesh> );}Noise fog
Section titled “Noise fog”A layer of fog that drifts, fading out towards the plane’s edges. fbm layers four octaves of noise. The third coordinate is time, so the fog changes shape as it moves. Lay the plane flat above the floor with rotation={[-Math.PI / 2, 0, 0]}.
import { Shader } from "@spawnite/engine";import { Color } from "three";
const fog = `#include "spawnite/noise/fbm.glsl"
void main() { float noise = 0.5 + 0.5 * fbm(vec3(vUv * 3.0, uTime * 0.2)); float density = smoothstep(0.35, 0.75, noise); float edge = 1.0 - smoothstep(0.25, 0.5, length(vUv - 0.5)); gl_FragColor = vec4(uTint, density * edge);}`;
export function Fog() { return ( <mesh rotation={[-Math.PI / 2, 0, 0]}> <planeGeometry args={[6, 6]} /> <Shader fragment={fog} uniforms={{ uTint: new Color("#f4f8ff") }} transparent /> </mesh> );}Dissolve by threshold
Section titled “Dissolve by threshold”The surface burns away in patches, with a hot edge where it is about to go. Each point compares its noise value to a threshold and discards itself below it. The sample runs the threshold on uTime. To drive it from the game, pass a uProgress uniform from 0 to 1 in its place.
import { Shader } from "@spawnite/engine";import { Color } from "three";
const dissolve = `#include "spawnite/noise/simplex.glsl"
void main() { float noise = 0.5 + 0.5 * snoise(vUv * 6.0); float threshold = 0.5 + 0.5 * sin(uTime); if (noise < threshold) discard; float edge = 1.0 - smoothstep(threshold, threshold + 0.06, noise); vec3 colour = mix(uTint, vec3(1.0, 0.45, 0.1), edge); gl_FragColor = vec4(colour, 1.0);}`;
export function Dissolve() { return ( <mesh> <planeGeometry args={[2, 2]} /> <Shader fragment={dissolve} uniforms={{ uTint: new Color("#8060ff") }} /> </mesh> );}Port a Shadertoy shader
Section titled “Port a Shadertoy shader”A Shadertoy shader runs here once you rename its inputs. This recipe shows the renaming on Shadertoy’s default new shader, the editor’s starting code rather than a published shader. Before you port a published one, check that its own licence line allows commercial use, as Licence and credit says. The default new shader is:
void mainImage(out vec4 fragColor, in vec2 fragCoord) { vec2 uv = fragCoord / iResolution.xy; vec3 col = 0.5 + 0.5 * cos(iTime + uv.xyx + vec3(0, 2, 4)); fragColor = vec4(col, 1.0);}To port it, make the following changes:
- Replace
void mainImage(out vec4 fragColor, in vec2 fragCoord)withvoid main(), andfragColorwithgl_FragColor. - Replace
fragCoord / iResolution.xywithvUv. It runs from 0 to 1 across the mesh, as the division does across the screen. - Replace
iTimewithuTime, andiMousewithuPointer, which runs from -1 to 1. - Write each integer in a
vecas a float, such asvec3(0.0, 2.0, 4.0), because GLSL ES 1.00 does not convert them. - Keep the author’s credit and licence in a comment, as Licence and credit says.
import { Shader } from "@spawnite/engine";
// Ported from Shadertoy's default new shader.const rainbow = `void main() { vec2 uv = vUv; vec3 colour = 0.5 + 0.5 * cos(uTime + uv.xyx + vec3(0.0, 2.0, 4.0)); gl_FragColor = vec4(colour, 1.0);}`;
export function Rainbow() { return ( <mesh> <planeGeometry args={[2, 2]} /> <Shader fragment={rainbow} /> </mesh> );}A shader that reads iChannel0 needs a Texture uniform in its place, such as uniforms={{ uChannel: texture }}. A shader in several passes, with a Buffer A tab, does not port to one fragment.
Read a compile error
Section titled “Read a compile error”A fragment that does not compile draws nothing, and three prints the error to the console: the GL compiler’s message, then the source around the failing line, with that line marked >. The MCP’s next screenshot or describe_entity reply and the next spawnite play screenshot name each shader that failed since the shot before it, with the compiler’s message and the line marked >, so a blank mesh in a shot comes with its reason. The read_console tool returns the whole error. The line numbers count three’s own header of about 150 lines ahead of your code, so read the line marked >, not the number. This one names colour, which the fragment never declared:
THREE.WebGLProgram: Shader Error 0 - VALIDATE_STATUS false
Material Name:Material Type: ShaderMaterial
Program Info Log: Fragment shader is not compiled.
FRAGMENT
ERROR: 0:162: 'colour' : undeclared identifierERROR: 0:162: 'constructor' : not enough data provided for construction
158: uniform float uTime; 159: uniform vec2 uPointer; 160: varying vec2 vUv; 161: uniform vec3 uTint;> 162: void main() { gl_FragColor = vec4(colour, 1.0); }The browser then repeats WebGL: INVALID_OPERATION: useProgram: program not valid for every frame the broken material draws. Those lines follow from the first error; fix that one.
See it
Section titled “See it”pnpm exec spawnite play screenshot --until "steps >= 60" steps the game headless to the first second and writes a PNG, as Your first game shows. The same step shows the same moment, so take one before a change and one after, and compare them.
Start from a scaffold
Section titled “Start from a scaffold”The MCP’s add_shader tool writes src/shaders/<Name>.tsx into the game: a fragment that includes a helper, and a <Name>Material component to put inside a mesh. Templates shows the file.
Start from the engine’s shaders
Section titled “Start from the engine’s shaders”Each shader the engine ships is an exported GLSL string with a comment per block, so an agent can copy it, change one block and pass it as fragment. The CRT material and the water material link their source.
References
Section titled “References”Each of these collections can teach you a technique or give you a shader to copy:
| Source | Good for | How to use it here |
|---|---|---|
| Shadertoy | Thousands of complete effects: skies, water, fire, tunnels and glows. | The default licence, CC BY-NC-SA, does not allow commercial use: port only a shader whose own licence line does, as Port a Shadertoy shader shows. |
| godotshaders.com | Short shaders for game objects: outlines, dissolves, water, holograms and toon. | Take the fragment() body. Rename TIME to uTime and UV to vUv, and set gl_FragColor from ALBEDO and ALPHA. Keep the licence the page names. |
| three.js examples | Shaders written for the same renderer and GLSL version this engine uses. | Open an example’s source and copy its fragmentShader string from void main() on. Rename its time and UV inputs to uTime and vUv. |
| The Book of Shaders | Learning the ideas: shaping functions, colour, patterns, noise and cellular noise. | Read a chapter to learn why a recipe works. Its examples use u_time and gl_FragCoord.xy / u_resolution; rename them to uTime and vUv. |
| Inigo Quilez’s distance functions and palettes | The exact formula for a 2D shape’s distance, and a cosine colour palette. | Copy a function above main() and call it with vUv - 0.5. The helpers include his circle, box and sphere, and his palette as spawnite/color/palette.glsl. |
Where it runs
Section titled “Where it runs”Client. A shader changes nothing in the world; each player’s browser draws its own.