TLightingLooktypenacatamalon
TLightingLook: TToonLook | TUnlitLookHow a material's surface is lit: one of these, in place of the engine's own light.
Writing a shader as typed values, and the node graph a .shader file holds.
107 símbolos
TLightingLook: TToonLook | TUnlitLookHow a material's surface is lit: one of these, in place of the engine's own light.
TStepLook: TMangaDotsLook | TRimLook | TGroundBounceLookSomething done to a surface once it is lit: as many as wanted, in order.
compileShader(asked: TShaderGraph<S>) => TCompiledShader<S>Turns a composed graph into the fields a material takes, in both languages.
The result spreads straight into createMaterial, and whichever backend draws it takes the half
it speaks. Nothing at the call site names a backend, and that is the point: the values are
typed, so the second language is a second walk over the graph rather than a translation, and the
two halves cannot drift apart because nobody wrote either.
On the way it works out anything used twice only once, reads each leaf the way its stage offers
it (and refuses one read where it does not exist), gathers every parameter into uniforms, and
writes each noise function once.
The family comes back in the type, so the result spreads into a material for sprites or for models and the material knows which it is.
A sprite shader needs color and has no vertex stage. A model shader takes color, position
or both, and a stage left out keeps the engine's own. color may be a vec3 (alpha 1) or a
vec4; position is a vec3.
askedTShaderGraph<S>const Level = () => {
const glow = compileShader({
shader: 'mesh3d',
color: composerMix(composerSurface(), composerVec4(0.3, 0.7, 1, 1), composerFresnel(3)),
position: composerAdd(composerVertexPosition(), composerMul(composerVertexNormal(), composerUniform('swell', 0.1))),
});
const material = createMaterial({ tint: '#e08a3a', ...glow });
createMesh({ geometry: useIcoSphereGeometry(), material });
return createScene();
};compileShaderGraph(doc: TShaderGraphDoc, src: string) => TParsedShaderCompiles a .shader file into what a material needs: its hooks in both languages, its
parameters and their types. The same shape reading a .wgsl gives, so nothing that uses a
shader file can tell the two apart.
It writes no shader code of its own. It turns the file into the same values the composer
functions build and hands them to compileShader, so a graph drawn on a canvas and the same
graph written in code cannot drift, and every check the composer functions make applies to a
wired graph too.
Only what reaches an output is built: a node nothing is wired to adds nothing, so a canvas with a few loose nodes on it still compiles. A loop is caught and named. An output left unwired keeps the engine's own for that stage, so a new file is valid and its material draws with the engine's shader until something is connected.
docTShaderGraphDocThe graph, from parseShaderGraph.
srcstringWhere it came from, for the error messages.
const src = '/shaders/glow.shader';
const parsed = compileShaderGraph(parseShaderGraph(await (await fetch(src)).text(), src), src);composerAbs: (a: TComposerInput) => TComposerNodeThe value without its sign, lane by lane.
composerAdd: TComposerBinaryOpAdds lane by lane, a number spreading over a vector (composerAdd(color, 0.1) lightens every
channel). Two vectors of different sizes are an error.
composerAtan2(y: TComposerInput, x: TComposerInput) => TComposerNodeThe angle of the point (x, y) around the origin, in radians from -π to π, lane by lane: 0
pointing along +x, growing towards +y. y comes first, as in every language that has it.
It is what turns a place into "how far round": a swirl, rays from a sun, a ring that fills like a
clock, a pattern wrapped around a centre. Taken around the middle of a sprite,
composerAtan2(uv.y - 0.5, uv.x - 0.5); around a model's axis, from the corner's own position.
Divide by 2π and add 0.5 for a turn from 0 to 1. At the origin itself, where there is no
angle, 0, on every card.
yTComposerInputThe second coordinate. A number spreads over a vector given as x.
xTComposerInputThe first coordinate.
// eight rays turning around the middle of a sprite
const centred = composerSub(composerUv(), 0.5);
const angle = composerAtan2(composerY(centred), composerX(centred));
const rays = composerStep(0.5, composerFract(composerAdd(composerMul(angle, 8 / (Math.PI * 2)), composerTime())));
const shader = compileShader({ shader: 'sprite2d', color: composerVec4(rays, rays, rays, 1) });composerCameraPos() => TComposerNodeWhere the camera stands, in the world (vec3): the place the fog measures from. How far away a
point is, composerDistance(composerCameraPos(), composerWorldPos()), is what bands, a fade or
detail by distance are worked out from. With an orthographic camera it is only where the camera
was put, since its rays leave from no one point. Models only, colour stage.
// Darker bands the further away: three steps across twenty units.
const far = composerDistance(composerCameraPos(), composerWorldPos());
const band = composerFloor(composerMul(composerClamp(composerDiv(far, 20), 0, 1), 3));
compileShader({ shader: 'mesh3d', color: composerMul(composerSurface(), composerSub(1, composerMul(band, 0.25))) });composerCarry() => TComposerNodeWhat the vertex stage carried to this pixel (vec4): the graph's carry, worked out at each
corner and smeared across the triangle. How high a wave lifted the water here, for foam on its
crests; a light of the graph's own worked out at the corners, the look of the era; where the point
was in the model's own space, for bands or a fill line that stay on the model as it moves. A carry
smaller than four numbers is filled out with zeros, and none at all reads as zeros. Models only,
colour stage.
const lift = composerMul(composerSin(composerAdd(composerX(composerVertexPosition()), composerTime())), 0.2);
const sea = compileShader({
shader: 'mesh3d',
position: composerAdd(composerVertexPosition(), composerMul(composerVertexNormal(), lift)),
carry: lift,
color: composerMix(composerSurface(), composerVec4(1, 1, 1, 1), composerSmoothstep(0.12, 0.2, composerX(composerCarry()))),
});composerCellularNoise(pos: TComposerNode) => TComposerNodeCell noise (f32): the distance to the nearest of a scatter of random points, which gives
cells, cracks or scales. Takes a vec2 or a vec3.
posTComposerNodeWhere to read it: a vec2 or a vec3.
composerCelShade(value: TComposerNode, steps: TComposerInput) => TComposerNodeCuts a gradient into steps flat bands, the cartoon look. Put the light through it for hard
lighting, or any other gradient. Same type in as out; more steps is smoother.
valueTComposerNodeThe gradient to cut.
stepsTComposerInputHow many bands. Default 3.
composerClamp(value: TComposerInput, lo: TComposerInput, hi: TComposerInput) => TComposerNodeKeeps value between lo and hi, lane by lane. Numbers as bounds spread over value.
valueTComposerInputWhat to keep inside.
loTComposerInputThe lowest it may be.
hiTComposerInputThe highest it may be.
composerCos: (a: TComposerInput) => TComposerNodeCosine of each lane, in radians.
composerCross(a: TComposerNode, b: TComposerNode) => TComposerNodeA vector at right angles to two vec3s.
aTComposerNodeOne vec3.
bTComposerNodeThe other.
composerCubeUv() => TComposerNodeWhere the surface reflects the world on a picture of its surroundings drawn as the six faces of a
cube, three across and two down, as useEnvironment draws them: in the world's own axes, so the
same from wherever it is seen. Feed it to a map sample of that picture.
import { composerCubeUv, composerMapSample } from 'nacatamalon/shader';
const around = composerMapSample('room', composerCubeUv(), { scale: 0 });composerDistance(a: TComposerNode, b: TComposerNode) => TComposerNodeHow far apart two points of the same size are (f32).
aTComposerNodeOne point.
bTComposerNodeThe other.
composerDiv: TComposerBinaryOpDivides lane by lane, a number spreading over a vector.
composerDot(a: TComposerNode, b: TComposerNode) => TComposerNodeThe dot product of two vectors of the same size (f32): how much they point the same way, and
the heart of any lighting sum.
aTComposerNodeOne vector.
bTComposerNodeThe other, the same size.
composerEmissive() => TComposerNodeThe light the material gives off by itself (vec3), so a graph can rebuild the engine's own
look exactly. Models only, colour stage.
composerEnvUv() => TComposerNodeWhere the surface's reflection of the world lands on a round picture of its surroundings (vec2):
read a map there and the model looks like chrome, the reflection the consoles of the era made with
a second texture. Use it as the coordinate of composerMapSample. Models only, colour stage.
const chrome = composerMapSample('env', composerEnvUv());composerFbm(pos: TComposerNode, octaves: number) => TComposerNodeLayers of noise, each twice as fine and half as strong as the one before (f32): the detailed,
natural kind, for terrain, smoke or lava. octaves is written into the shader, so it is a small
fixed number (4 unless you say). Takes a vec2 or a vec3.
posTComposerNodeWhere to read it: a vec2 or a vec3.
octavesnumberHow many layers. Default 4.
composerFloat(value: number) => TComposerNodeA constant number. The only kind of constant: vectors are made with composerVec2,
composerVec3 and composerVec4, which wrap their plain numbers in this on their own.
valuenumberThe number.
composerFloor: (a: TComposerInput) => TComposerNodeThe largest whole number not above each lane.
composerFract: (a: TComposerInput) => TComposerNodeWhat is left after the whole part (x - floor(x)), lane by lane: the basis of anything that
repeats or tiles.
composerFresnel(power: TComposerInput) => TComposerNodeThe rim term, from 0 where the surface faces the camera to 1 where it turns away (f32): the
start of a rim light, a hologram or a force field. A higher power pulls the rim in to the
silhouette.
It reads which way the surface faces and where the camera is, so it only exists in a model's colour stage, and the compiler says so anywhere else.
powerTComposerInputHow tightly the rim hugs the silhouette. Default 3.
composerFrontFacing() => TComposerNodeWhich side of the triangle is being drawn (f32): 1 the side facing the camera, 0 the other.
Only ever 0 on a material that keeps the other side (cull: 'front' or 'none'): mixed by it, a
liquid seen from below takes another colour, or a leaf is lighter underneath. Models only, colour
stage.
composerInstanceData() => TComposerNodeThe four numbers of the instance a corner belongs to (vec4), for a model drawn as many
(createMeshInstances): what set was given as data, a seed, a height, how far along it is.
Zeros on a single model. Models only, vertex stage: hand what the colour stage needs of them on
with carry.
composerInstanceOrigin() => TComposerNodeWhere the instance a corner belongs to stands (vec3), in the space the vertex position is in, for
a model drawn as many (createMeshInstances): the foot of a blade of grass, whatever corner of it
this is. Zeros on a single model. Models only, vertex stage.
composerLayerUv() => TComposerNodeWhere this pixel is across the whole of a map's layer, from 0 to 1 (vec2): a shine that
crosses the map rather than each tile on its own. On a sprite, the same as composerLocalUv(), so
one material does for both. Sprites and map layers only.
composerLength(a: TComposerNode) => TComposerNodeHow long a vector is (f32).
aTComposerNodeThe vector.
composerLight() => TComposerNodeHow much light reaches this point, and of what colour (vec3).
It is handed over on its own so a graph can light things its own way, in hard bands or with
a rim. The engine's own look is the surface times this, plus composerEmissive. The light is
worked out at the corners and blended in between, as the consoles of the era did, or at every
pixel on a material with perPixel. Models only, colour stage.
composerLocalUv() => TComposerNodeWhere this pixel is across what is drawn, from 0 to 1 (vec2): the whole sprite however its
frames change, one tile of a map, one letter of a text or one piece of a nine-slice. Where a mask
is read so a sprite crumbles in its own shape and not the sheet's. It turns over with the picture
when the sprite or the tile is flipped. Sprites and map layers only.
const grain = composerMapSample('grain', composerLocalUv());composerMapSample(name: string, coord: TComposerNode, options?: TComposerSampleOptions) => TComposerNodeReads one of the material's extra maps by name, at any coordinate (vec4): a reflection, a mask, a
detail, a ripple. The material carries the picture under the same name in its maps; one it does
not carry reads white. On a model it reads in the colour graph and in the vertex graph too, to move
the corners by a picture (a height map, footprints in snow); read it there at a place worked out
from composerVertexPosition() rather than composerUv(), so the surface is lit by the shape the
map gives it (see TMaterialOptions.vertex). On a sprite or a map's layer, colour graph only:
read it at composerLocalUv() for a mask that stays put while the frames change.
A map with mipmaps (useLoadTexture({ mipmaps: true })) is read in a model's colour graph from
the copy its size on the screen asks for, judged by how the model's own picture coordinate runs:
read it at one that runs faster, say so with scale.
namestringThe map's name, as the material's maps gives it: lowercase first, letters and digits.
coordTComposerNodeWhere to read it, in 0-1.
optionsopcionalTComposerSampleOptionsscale, for a place that runs faster than the model's picture coordinate.
const detail = composerMapSample('detail', composerMul(composerUv(), 8), { scale: 8 });composerMax: TComposerBinaryOpThe larger of two values, lane by lane. A number spreads over a vector.
composerMin: TComposerBinaryOpThe smaller of two values, lane by lane. A number spreads over a vector.
composerMix(a: TComposerInput, b: TComposerInput, t: TComposerInput) => TComposerNodeBlends from a to b by t: 0 gives a, 1 gives b. a and b are the same size, and t
is either a number or that size too.
aTComposerInputWhat 0 gives.
bTComposerInputWhat 1 gives.
tTComposerInputHow far from a to b.
composerMod: TComposerBinaryOpThe remainder the way shader authors expect it, x - y * floor(x / y), which always takes the
sign of y. Built from the pieces above, because WGSL's own % keeps the sign of x instead
and the two backends would disagree on negative numbers. A number as y spreads over x.
composerMul: TComposerBinaryOpMultiplies lane by lane, a number spreading over a vector (composerMul(color, 2) doubles the
brightness; two vectors multiply lane by lane).
composerNormalize(a: TComposerNode) => TComposerNodeThe same direction, one unit long. Vectors only.
aTComposerNodeThe vector.
composerOcclusion() => TComposerNodeHow open this place is to the light from all around (f32): 1 wide open, 0 shut in a crease,
from the material's occlusion, and 1 with none. It is already in composerLight(); this is it on
its own, to dim a shine inside the creases or gather dirt in them. Models only, colour stage.
// The shine kept out of the creases too, as the light from all around already is.
const lit = composerAdd(
composerMul(composerSwizzle(composerSurface(), 'rgb'), composerLight()),
composerMul(composerShine(), composerOcclusion()),
);composerOneMinus(a: TComposerInput) => TComposerNode1 - x, lane by lane: the opposite of a mask or of a falloff, in one step of a pipe.
aTComposerInputThe value.
composerPipe(seed: TComposerInput, steps: TComposerStep[]) => TComposerNodePasses a value through a list of steps, left to right, so a long expression reads in the order it happens instead of from the inside out.
composerPipe(x, composerMul(2), composerAdd(1), composerSin) reads "take x, double it, add one,
then the sine", and is exactly composerSin(composerAdd(composerMul(x, 2), 1)). Any one-operand
function is a step as it is, and the two-operand ones that put the value on the left
(composerAdd, composerMul, composerPow...) become one when given a single operand. For
anything else, a small function (n) => ... is a step too.
seedTComposerInputThe value to start with.
stepsTComposerStep[]What to do to it, in order.
// 0.5 + 0.5 * sin(uv.y * lineCount + time * speed)
const bands = composerPipe(
composerY(composerUv()),
composerMul(composerUniform('lineCount', 120)),
composerAdd(composerMul(composerTime(), composerUniform('speed', 6))),
composerSin,
composerMul(0.5),
composerAdd(0.5),
);composerPolar(coord: TComposerNode, center: TComposerInput) => TComposerNodeA place read round a centre (vec2): x how far from it, 0 there and 1 at the middle of the
picture's edge (the circle that fits in it), and y how far round, a turn from 0 to 1. Read a
picture at it and a strip becomes a ring: a dial, a radar, a target, rays from a sun, a ring that
fills like a clock (composerStep(turn, fill)), and with composerTime() added to either, one
that spins or flows outwards.
Round as composerAtan2 goes: y starts at 0 on the left (-x), is 0.5 on the right, and
grows towards +y of the place given, down a sprite's picture. To lay a strip of pictures round
it, swap the two (composerSwizzle(polar, 'yx')); to repeat it, multiply: composerMul(polar, composerVec2(1, 12)) is twelve times round. Where the turn starts again, on the left, a picture
meets itself: draw it so its two ends join.
coordTComposerNodeThe place, composerUv() when left out.
centerTComposerInputThe centre, in the same units. Default the middle of the picture, (0.5, 0.5).
// A ring of numbers round a dial: the picture is the strip, laid once round.
const polar = composerPolar();
const strip = composerTextureSample(composerSwizzle(polar, 'yx'));
const material = createMaterial({ ...compileShader({ shader: 'sprite2d', color: strip }) });composerPow: TComposerBinaryOpbase to the power of exp, lane by lane. A number as exp spreads over base.
composerReflect(i: TComposerNode, n: TComposerNode) => TComposerNodeBounces the direction i off a surface facing n. Both vectors of the same size.
iTComposerNodeThe direction that arrives.
nTComposerNodeThe way the surface faces.
composerResolution() => TComposerNodeThe game's size in pixels (vec2), which the engine writes for every material. For keeping a
pattern square, or for effects measured in pixels.
composerSaturate(a: TComposerInput) => TComposerNodeKeeps each lane between 0 and 1, in one step of a pipe. Short for composerClamp(x, 0, 1).
aTComposerInputThe value.
composerScreenPos() => TComposerNodeWhere on the screen the surface is, in the game's pixels (vec2): (0, 0) at the top left, the
game's width and height at the bottom right, whatever the size of the window. A dot screen, a
hatch or a dither laid over a model lines up with the screen through this rather than sliding
with the model. Models only, colour stage.
composerShine() => TComposerNodeThe highlight the lamps put on the surface (vec3), already shadowed: the shine a material's
specular and shininess ask for. The built-in look adds it on top of the lit colour. Models
only, colour stage.
composerSign: (a: TComposerInput) => TComposerNodeThe sign of each lane: -1, 0 or +1.
composerSimplexNoise(pos: TComposerNode) => TComposerNodeSmooth noise at a position (f32, 0 to 1): the usual source of anything organic, like clouds,
flowing water or a dissolve mask. Takes a vec2 or a vec3. Scale the position to change how
busy it is, and add composerTime() to move it.
posTComposerNodeWhere to read it: a vec2 or a vec3.
composerSin: (a: TComposerInput) => TComposerNodeSine of each lane, in radians.
composerSmoothstep(edge0: TComposerInput, edge1: TComposerInput, value: TComposerInput) => TComposerNodeA soft cut: eases from 0 to 1 as value goes from edge0 to edge1. Numbers as edges spread
over value.
edge0TComposerInputWhere the ramp starts.
edge1TComposerInputWhere it ends.
valueTComposerInputWhat is ramped.
composerSqrt: (a: TComposerInput) => TComposerNodeSquare root of each lane.
composerStep(edge: TComposerInput, value: TComposerInput) => TComposerNodeA hard cut: 0 where value is below edge, 1 from there on, lane by lane. A number as edge
spreads over value.
edgeTComposerInputWhere the cut is.
valueTComposerInputWhat is cut.
composerSub: TComposerBinaryOpSubtracts lane by lane, a number spreading over a vector.
composerSurface() => TComposerNodeThe colour the engine would have drawn here (vec4): the picture, already read and already
tinted (and on a model, painted by its corners' colour), before any light. Where most colour effects start. Colour stage only.
composerSwizzle(value: TComposerNode, mask: string) => TComposerNodePicks components of a vector by their letters ('bgr', 'xy'). The number of letters, one to
four, is the size of the result, and a single letter gives a plain number.
The letters come from one set, xyzw or rgba, never both in one mask, and each must exist in
the value it reads: both are checked here rather than left to the card.
valueTComposerNodeThe vector to read.
maskstringOne to four letters from xyzw or rgba.
composerTextureSample(coord: TComposerNode, options?: TComposerSampleOptions) => TComposerNodeReads the material's own picture, tinted, at any coordinate (vec4). What waves, pixelation, a
colour split or a blur are made of: composerSurface() is this read at the fragment's own
coordinate. Colour stage only.
A picture with mipmaps on a model is read from the copy its size on the screen asks for, judged
by how the model's own picture coordinate runs. Read at one that runs faster (uv * 4), say so
with scale.
coordTComposerNodeWhere to read the picture, in 0-1.
optionsopcionalTComposerSampleOptionsscale, for a place that runs faster than the model's picture coordinate.
const tiled = composerTextureSample(composerMul(composerUv(), 4), { scale: 4 });composerTime() => TComposerNodeSeconds since the game started (f32), which the engine writes for every material. Anything that
moves on its own is driven by this, with no code per frame.
composerUniform(name: string, value: number | number[]) => TComposerNodeA parameter with a name, which the game can change while it runs.
What value looks like sets its type (a number, or a list of two, three or four) and is what it
starts at. compileShader gathers every parameter of the graph into the material's uniforms,
so writing material.uniforms.<name> moves it on the next frame. time and resolution are
taken: the engine writes those, and they are read with composerTime and composerResolution.
namestringWhat the game calls it: material.uniforms.<name>.
valuenumber | number[]What it starts at. A number, or a list of two, three or four.
composerUv() => TComposerNodeThe texture coordinate (vec2). There in every stage; in a model's vertex graph it is the
corner's own.
composerUv2() => TComposerNodeA second picture coordinate (vec2): the model file's own or createGeometry's uv2, or the
first again when it has none. For a
second picture laid over the first by coordinates of its own: baked light, a decal, a detail.
Models only, in either stage.
composerVec2(args: TComposerInput[]) => TComposerNodeA vector of two. Two numbers, one vec2, or one number copied into both.
argsTComposerInput[]Two numbers or nodes, one vec2, or one number for both.
composerVec3(args: TComposerInput[]) => TComposerNodeA vector of three, the everyday one for colours and directions. Three numbers, a vec2 and a
number, one vec3, or one number copied into all three.
argsTComposerInput[]Three numbers or nodes, a smaller vector and the rest, or one number for all.
composerVec4(args: TComposerInput[]) => TComposerNodeA vector of four, usually a colour with its alpha. Four numbers, a vec3 and a number (colour
and alpha), one vec4, or one number copied into all four.
argsTComposerInput[]Four numbers or nodes, smaller vectors and the rest, or one number for all.
composerVertexBitangent() => TComposerNodeWhich way down the surface the picture runs, in the model's own space (vec3). Models only,
vertex stage.
composerVertexColor() => TComposerNodeThe colour painted on the model's corners (vec4), smeared across each triangle. Models only, in
either stage.
It is already part of composerSurface. On its own it is for using the paint as something other
than colour: how much a corner sways in the wind, or how much of a second picture shows through,
which is how the era blended grass into a dirt path without a second texture pass.
composerVertexNormal() => TComposerNodeWhich way the corner faces, in the model's own space (vec3). Moving a corner along it swells
the model. Models only, vertex stage.
composerVertexPosition() => TComposerNodeWhere the corner is, in the model's own space (vec3): the value a position graph moves, to
make it wobble, swell or jitter. Models only, vertex stage.
composerVertexTangent() => TComposerNodeWhich way across the surface the picture runs, in the model's own space (vec3). Models only,
vertex stage.
composerViewDir() => TComposerNodeThe direction from this point towards the camera, one unit long (vec3). What a rim or a shine
is worked out from. Models only, colour stage.
composerW(value: TComposerNode) => TComposerNodeThe fourth component of a vector, as a number. Short for composerSwizzle(v, 'w').
valueTComposerNodeThe vector.
composerWorldBitangent() => TComposerNodeWhich way down the surface the picture runs, in the world (vec3): the normal crossed with the
tangent, turned over where the picture is mirrored. Models only, colour stage.
composerWorldNormal() => TComposerNodeWhich way the surface faces, in the world (vec3). Models only, colour stage; in a vertex graph
use composerVertexNormal.
composerWorldPos() => TComposerNodeWhere this point of the surface is, in the world (vec3). Models only, colour stage.
composerWorldTangent() => TComposerNodeWhich way across the surface the picture runs, in the world (vec3, one long, square to the
normal). With the bitangent and the normal it is the frame a relief or a ripple moves the picture
in. Models only, colour stage.
composerX(value: TComposerNode) => TComposerNodeThe first component of a vector, as a number. Short for composerSwizzle(v, 'x').
valueTComposerNodeThe vector.
composerY(value: TComposerNode) => TComposerNodeThe second component of a vector, as a number. Short for composerSwizzle(v, 'y').
valueTComposerNodeThe vector.
composerZ(value: TComposerNode) => TComposerNodeThe third component of a vector, as a number. Short for composerSwizzle(v, 'z').
valueTComposerNodeThe vector.
groundBounce(options: { color, reach, amount, groundY }) => TGroundBounceLookThe colour of the ground coming up onto what stands on it, as light bounces off a bright floor:
the closer a part is to the ground and the more it looks down, the more it goes towards the
ground's colour; nothing of it reach above the ground, nor on a face looking up. A lamp's light
that bounces, without a lamp, so it goes as well on a matcap as on the engine's light. For a
material's steps.
It works from where each pixel is in the world, so a model that jumps loses it on the way up and gets it back as it lands.
options{ color, reach, amount, groundY }color, the ground's, what getColor takes, a TColor or [r, g, b] (default an
orange); reach how high it reaches, in the world's units (default 1); amount how far a face on
the ground and looking down goes towards the colour, 0 to 1 (default 0.5); groundY where the
ground is (default 0).
declare const red: TTexture;
createMaterial({ ...matcap(red), steps: [groundBounce({ color: '#ff7a33', reach: 1.4 })] });mangaDots(options: { size, fill, ink }) => TMangaDotsLookDots of ink in the shadow, the way printed comics shaded: the darker, the bigger. They are laid out
on the screen and not on the model, so they stay put as it turns. For a material's steps.
After toonLighting the dots go exactly where its bands went dark; with the engine's light they grow
smoothly into the dark.
options{ size, fill, ink }size how far apart the dots are, in game pixels (default 5); fill how big a dot
grows in full shadow, as a part of its cell (default 0.3; past 0.5 they touch); ink its colour,
what getColor takes, a TColor or [r, g, b] (default a deep violet).
declare const tint: TColor;
createMaterial({ shader: 'mesh3d', tint, lighting: toonLighting(), steps: [mangaDots({ size: 4 })] });parseShaderGraph(source: unknown, src: string) => TShaderGraphDocReads a .shader file and checks its shape.
It takes the text of the file or JSON already parsed, because both callers exist: a game loading
it has text in hand, and an editor holds a graph it has just changed and wants checked before it
writes it. A broken shape (a node with no id, a place that is not two numbers, an unknown family)
is refused here, naming the node. What the graph means (a kind of node nobody knows, a wire
into an input that does not exist, a loop) is compileShaderGraph's, which knows the catalogue.
sourceunknownThe file's text, or its JSON.
srcstringWhere it came from, for the error messages.
const graph = parseShaderGraph(await (await fetch('/shaders/glow.shader')).text(), '/shaders/glow.shader');rim(options: { color, power, amount }) => TRimLookA light along the edge that turns away from the camera, as if lit from behind. The edge goes
towards color and never past it, so it brightens without burning. For a material's steps.
options{ color, power, amount }color, what getColor takes, a TColor or [r, g, b] (default white); power
how tightly it keeps to the silhouette (default 4); amount how far the edge goes towards the
colour, 0 to 1 (default 0.7).
toonLighting(options: { bands, shadow, spot }) => TToonLookThe light cut into flat bands, the cartoon look: the surface is lit or it is not, with nothing
smeared between, and the shine is one hard spot. For a material's lighting.
It cuts the light every light of the scene gives (the sun, the lamps, the shadows they cast), and only its brightness: each band keeps the colour of the light that made it, so a warm lamp gives warm bands and a white light greys.
It also says which part it put in shadow, so a step after it (mangaDots) inks exactly there.
options{ bands, shadow, spot }bands how many steps of light (default 2); shadow how bright the darkest one
is, 0 to 1 (default 0.45); spot where the shine is cut, 0 to 1 (default 0.75).
declare const tint: TColor;
createMaterial({ shader: 'mesh3d', tint, lighting: toonLighting({ bands: 3 }) });unlitLighting() => TUnlitLookNo light at all: the surface exactly as it is painted (its picture, its tint and what it gives off
itself), the same from every side and in every shadow. For a material's lighting: a sky, a
screen, a sign that glows, anything that is not lit but is a light.
declare const tint: TColor;
createMaterial({ shader: 'mesh3d', tint, lighting: unlitLighting() });What compileShader gives back: exactly the fields createMaterial takes, so it is spread
straight in, createMaterial({ ...compiled }).
A stage left at the engine's own is missing, the same as not writing it in createMaterial.
uniforms holds every parameter found in the graph, ready to be changed while the game runs.
shaderSfragmentopcionalstringThe colour hook in WGSL, missing when the graph has no color.
vertexopcionalstringThe vertex hook in WGSL, missing when the graph has no position.
fragmentGlslopcionalstringThe same two hooks in GLSL, always, never on request.
That is what makes a composed shader work on both backends by construction rather than when somebody remembers to ask. The values are typed, so the second language is a second walk over the same graph and not a translation of the first one's text.
vertexGlslopcionalstringuniformsTUniformNumbersTComposerBinaryOp: (a: TComposerInput, b: TComposerInput) => TComposerNodeThe two ways to call a two-operand node: both operands give a value, one gives a step for
composerPipe, with the value coming down the pipe as the first operand.
TComposerInput: TComposerNode | numberWhat an operand of a composer function may be: another value, or a plain number, which becomes a
constant. So composerAdd(composerX(composerUv()), 0.5) needs no composerFloat(0.5).
One value in a composed shader: what kind of value it is, and how to build it from the values it depends on.
It is plain data, never a closure. kind picks how it is written out, params carries its
literal operands and deps the values it is made from. That is what lets a graph be written to
JSON and read back, the same rule every other piece of state in the engine follows.
Never built by hand: the composer* functions fill these fields consistently and check their
operands as they go.
kindstringHow it is written out: literal, input, uniform, texture, construct, swizzle, binop or call.
typeTUniformTypeWhat it evaluates to. Every operation checks its operands against this.
depsTComposerNode[]The values it is made from, in argument order.
paramsopcionalnumber | string[]Literal operands: a number, an operator, a swizzle mask or the name of the function called.
inputopcionalTInputKindFor a leaf, which value of the running shader it reads.
uniformopcional{ name, value }For a parameter, the name the shader reads it by and the value it starts at.
helpersopcionalTHelperDef[]Functions this node needs written at the top of the shader.
How composerTextureSample and composerMapSample read a picture that has mipmaps.
scaleopcionalTComposerInput | typeOperatorHow many times as fast the place read runs as the model's own picture coordinate: 8 for
uv * 8, [4, 2] for one that runs faster across than down, or a value worked out in the
graph (a parameter, say). It picks the mipmap that place asks for; without it a picture
repeated eight times is read eight times too sharp far away, and shimmers. 0 reads the picture
itself, for a place that does not follow the surface (composerEnvUv()). Only a model's colour
graph reads mipmaps; anywhere else it changes nothing. Left out, 1.
TComposerStep: (value: TComposerNode) => TComposerNodeOne stage of composerPipe: it takes the value flowing through and returns the next.
Every one-operand function already is one, and the two-operand ones become one when given a
single operand (composerMul(2)), so they chain without wrapping.
A note on the canvas: a titled rectangle behind the nodes, for grouping part of a graph and saying what it is for.
It has an array of its own rather than being a kind of node, because it is not one: no wires, no value, no place in the graph. As a node, the compiler, the type check and the previews would each need an exception for the one entry that is not part of the shader.
idstringtextstringThe note. May be empty while it is being written.
pos[number, number]Top-left corner, in the same space as a node's pos.
size[number, number]Width and height: a note is resized, a node is not.
coloropcional[number, number, number]An accent, [r, g, b] from 0 to 1. Left out, the editor picks.
A wire: output out of node from feeds input in of node to.
By name rather than by position, so adding an input to a kind of node cannot quietly rewire every file that uses it.
fromstringoutstringtostringinstringOne node in a saved graph: which kind of node it is, where it sits on the canvas, and its literals.
It does not describe itself. Its inputs, outputs and code all come from the catalogue, looked
up by type, so a file stays small and a node learns something new when the catalogue changes
rather than when every file is rewritten.
pos belongs to the editor and is in the file on purpose, like a box's place in a scene: how a
graph is laid out is part of what it says to whoever reads it.
idstringtypestringWhich entry of the catalogue this is.
pos[number, number]Where it sits on the canvas, [x, y].
paramsopcionalRecord<string, TParamValue>Its literals. One left out takes the catalogue's value.
TGraphTarget: Exclude<TMaterialShader, 'post'>The families a graph file can be for: the two that draw things, deliberately not every family a material has.
'post' is missing because no node writes a screen-wide effect, so a graph claiming it would
compile to nothing. Spelling it as its own type makes that gap a compile error in the one place
that could open it, and the reader refuses the same value, so the two cannot disagree.
The colour of the ground coming up onto what stands on it, as groundBounce() writes it down.
look'groundBounce'colornumber[]The ground's colour, [r, g, b] from 0 to 1.
reachnumberHow high above the ground it reaches, in the world's units: none of it from there up.
amountnumberHow far a face right on the ground and looking down goes towards the colour, 0 none to 1
all the way.
groundYnumberWhere the ground is: its height in the world.
A function a node needs written once at the top of the shader (the noise functions, for instance), kept only once however many nodes ask for it.
Both languages ride on the node rather than living in a table per language looked up by name. A helper exists because a node needs it, and keeping the two apart would let a graph ask for a helper one language never defined: a failure that shows up as a shader that does not compile in somebody's browser, rather than as a missing entry while writing it.
namestringwgslstringglslstringTInputKind: 'uv' | 'time' | 'resolution' | 'surface' | 'worldNormal' | 'worldPos' | 'viewDir' | 'cameraPos' | 'light' | 'emissive' | 'shine' | 'envUv' | 'cubeUv' | 'vertexPosition' | 'vertexNormal' | 'vertexColor' | 'screenPos' | 'uv2' | 'worldTangent' | 'worldBitangent' | 'vertexTangent' | 'vertexBitangent' | 'instanceData' | 'instanceOrigin' | 'frontFacing' | 'carry' | 'occlusion' | 'localUv' | 'layerUv'A value a leaf reads from the running shader: a function argument (uv, pos), one of the
values the engine writes for every material (time), or what the model's colour hook is told
about the place it is colouring (worldNormal, light).
Each one is only there in some stages, and the compiler refuses a leaf read where it does not
exist (light in a vertex graph, say) instead of emitting code the card then rejects.
TLook: TLightingLook | TStepLookAny look, as it is written down: plain data, so a scene document can keep it and a material can be made from it again.
Dots of ink in the shadow, the way printed comics shaded, as mangaDots() writes it down.
look'mangaDots'sizenumberHow far apart the dots are, in game pixels.
fillnumberHow big a dot grows in full shadow, as a part of its cell. Past 0.5 they touch.
inknumber[]The colour of the ink, [r, g, b] from 0 to 1.
TParamValue: number | number[] | stringA literal a node keeps in its own body instead of receiving through a wire: a swizzle mask, a number of octaves, a colour. Only what JSON keeps intact and an inspector can show a control for.
A light along the edge that turns away from the camera, as rim() writes it down.
look'rim'colornumber[]The colour the edge goes towards, [r, g, b] from 0 to 1.
powernumberHow tightly it keeps to the silhouette: higher is thinner.
amountnumberHow far the edge goes towards the colour, 0 none to 1 all the way.
What compileShader is handed: the family the shader is for, and up to three results.
color is what the colour hook returns and position is where the vertex hook moves a corner
to (models only), and carry is what the vertex hook hands on to the colour hook. Leave one out and
that stage keeps the engine's own.
The family is kept in the type, so what comes out is known to be a sprite's or a model's.
shaderScoloropcionalTComposerNodepositionopcionalTComposerNodecarryopcionalTComposerNodeUp to four numbers worked out at each corner and handed to the colour stage, which reads them
with composerCarry(), smeared across the triangle: how high a wave lifted the water, for
foam on its crests; a light of the graph's own worked out at the corners; where the point was
in the model's own space (composerVec4(composerVertexPosition(), 0)), for bands or a fill line
that stay on the model as it moves.
A number, a vec2, a vec3 or a vec4, filled out with zeros. Written with position, so
what the two have in common is worked out once. Models only.
normalopcionalTComposerNodeWhich way the surface faces at each corner, for the light, in the space position is in (a
vec3, brought back to length one): a blade of grass lit as if it faced up, a flat card lit as
if it were round. Left out, the surface is measured where position moved it. Written with
position, as carry is. Models only.
lightingopcionalTLookHow a model's surface is lit, in place of the engine's light: toonLighting() or unlitLighting(). With
steps, it makes the colour, so it is never given with color. Models only.
stepsopcionaltypeOperatorWhat is done to a model's surface once it is lit, in order: mangaDots(), rim(), groundBounce(). Models only.
A .shader file: a shader drawn as nodes on a canvas.
It sits next to a hand-written .wgsl and next to a graph composed in code, and all three end
up as the same thing, so a material cannot tell which one it was given. The file holds no shader
source in any language, only nodes and wires as plain JSON.
target means what @shader means at the top of a .wgsl: it picks the hooks, so it is not a
preference but part of what gets compiled.
formatnumberkind'shadergraph'targetTGraphTargetnodesTGraphNodeDoc[]edgesTGraphEdgeDoc[]commentsopcionalTGraphCommentDoc[]The notes on the canvas. A graph written by hand or by a tool has none, and then the field is not there at all: it is a graph nobody annotated, not a missing field.
Light cut into flat bands, the cartoon look, as toonLighting() writes it down.
look'toon'bandsnumberHow many bands of light, from the darkest to the full.
shadownumberHow bright the darkest band is, 0 black to 1 as lit as the rest.
spotnumberWhere the shine is cut into one hard spot, 0 everywhere it shines to 1 nowhere.
No light at all: the surface as it is painted, as unlitLighting() writes it down.
look'unlit'