Documentation / @debuffjp/core / Shader
Interface: Shader
Defined in: spryte/dist/shader.d.ts:59
A custom fragment effect, applied to everything drawn inside draw.withShader(shader, fn).
The source defines vec4 effect(vec4 color, highp vec2 uv), which returns the pixel's colour. Keep uv and any texture coordinate you compute highp: the default precision is mediump, fp16 on many mobile GPUs, too coarse for images past about 1024 px (before 0.16, source(uv) itself dropped uv to mediump). color is the draw's tint and alpha, premultiplied. source(uv) samples the image being drawn. uv is in texture (atlas) space, so it isn't 0 to 1 across a sprite packed into an atlas, and on a render target uv.y runs bottom to top: frameUv() gives the position inside the drawn frame instead, from (0, 0) at its top left to (1, 1) at its bottom right (since spryte 0.10). Declare your own uniforms and set them with set(). Return premultiplied colour (rgb already multiplied by alpha).
rect(), line() and circle() run the effect with frameUv() across them, so a full-screen vignette can be a rect() over the view with no render target. The vector shapes (polygon, polyline, roundRect, ring) and mesh draw without the effect. Up to 0.12.3 this comment said shapes get (0, 0), which was wrong for both.
const wave = draw.createShader(`
uniform float time;
vec4 effect(vec4 color, highp vec2 uv) {
uv.x += sin(uv.y * 40.0 + time * 6.0) * 0.01;
return source(uv) * color;
}
`);
wave.set("time", t);
draw.withShader(wave, () => draw.sprite(hero, x, y));Since spryte 0.13, set() also takes uniform arrays and textures:
const fx = draw.createShader(`
uniform vec4 pool[64];
uniform sampler2D noise;
vec4 effect(vec4 color, highp vec2 uv) {
float n = texture(noise, frameUv()).r;
return source(uv) * color * pool[0].a * n;
}
`);
fx.set("pool", poolValues); // Float32Array of 64 * 4 numbers
fx.set("noise", noiseCanvas, { smooth: true });WebGL2 only. On the Canvas 2D fallback, drawing inside withShader happens without the effect (warned once).
Properties
source
readonlysource:string
Defined in: spryte/dist/shader.d.ts:60
Methods
set()
Call Signature
set(
name,value):this
Defined in: spryte/dist/shader.d.ts:107
Set a uniform declared in the source. Applies to draws made after this call. A name the GLSL compiler removed because the effect never reads it is ignored, whatever the value.
- A number sets a
float. A 2, 3 or 4 number tuple sets avec2,vec3orvec4. - A
Float32Arrayor number array sets afloat,vec2,vec3orvec4array (since 0.13), packed flat. Its length must be a whole number of elements and at most the declared size: 10 numbers for avec4array throws an error naming the uniform. A shorter array sets the first elements and leaves the rest. The array is read when the batch draws, so callset()again after changing its contents. - A texture (an image, canvas,
ImageBitmaporRenderTarget) sets asampler2D(since 0.13). Read it withtexture(name, uv), whereuvruns 0 to 1 across the whole texture: from the top row of an image, but from the bottom row of a render target. Colours are premultiplied. A shader can't sample the render target it is drawing into:set(),withShaderorinto()throws when that would happen. Eachsampler2Dgets its own texture unit from 8 up, fixed per program, because units 0 to 7 hold the sprites of a batch. WebGL2 guarantees 16 units, so a shader can count on 8 textures; one that reads more than the GPU has units for throws when first used. Filtering is nearest unlessoptions.smoothis true.
A changed value ends the current batch, so the sprites queued before it draw with the old one. Since 0.16, the value a uniform already has keeps the batch: numbers and arrays compare by value, and a texture or the same array object passed again always counts as a change. To vary an effect per sprite, keep one withShader around the whole walk and pass the per-sprite value with the draw instead of calling set().
A wrong kind of value (a texture on a float, an array on a sampler2D) throws too. All of these throw at set() while the shader is in use, or when withShader or into() would start using values set earlier, and always before anything changes. On the Canvas 2D fallback shaders don't run, so nothing is checked and the draws happen without the effect.
fx.set("time", 1.5); // float
fx.set("pool", new Float32Array(64 * 4)); // uniform vec4 pool[64], packed flat
fx.set("noise", noiseCanvas, { smooth: true }); // uniform sampler2D noiseParameters
name
string
value
Returns
this
Call Signature
set(
name,texture,options?):this
Defined in: spryte/dist/shader.d.ts:108
Set a uniform declared in the source. Applies to draws made after this call. A name the GLSL compiler removed because the effect never reads it is ignored, whatever the value.
- A number sets a
float. A 2, 3 or 4 number tuple sets avec2,vec3orvec4. - A
Float32Arrayor number array sets afloat,vec2,vec3orvec4array (since 0.13), packed flat. Its length must be a whole number of elements and at most the declared size: 10 numbers for avec4array throws an error naming the uniform. A shorter array sets the first elements and leaves the rest. The array is read when the batch draws, so callset()again after changing its contents. - A texture (an image, canvas,
ImageBitmaporRenderTarget) sets asampler2D(since 0.13). Read it withtexture(name, uv), whereuvruns 0 to 1 across the whole texture: from the top row of an image, but from the bottom row of a render target. Colours are premultiplied. A shader can't sample the render target it is drawing into:set(),withShaderorinto()throws when that would happen. Eachsampler2Dgets its own texture unit from 8 up, fixed per program, because units 0 to 7 hold the sprites of a batch. WebGL2 guarantees 16 units, so a shader can count on 8 textures; one that reads more than the GPU has units for throws when first used. Filtering is nearest unlessoptions.smoothis true.
A changed value ends the current batch, so the sprites queued before it draw with the old one. Since 0.16, the value a uniform already has keeps the batch: numbers and arrays compare by value, and a texture or the same array object passed again always counts as a change. To vary an effect per sprite, keep one withShader around the whole walk and pass the per-sprite value with the draw instead of calling set().
A wrong kind of value (a texture on a float, an array on a sampler2D) throws too. All of these throw at set() while the shader is in use, or when withShader or into() would start using values set earlier, and always before anything changes. On the Canvas 2D fallback shaders don't run, so nothing is checked and the draws happen without the effect.
fx.set("time", 1.5); // float
fx.set("pool", new Float32Array(64 * 4)); // uniform vec4 pool[64], packed flat
fx.set("noise", noiseCanvas, { smooth: true }); // uniform sampler2D noiseParameters
name
string
texture
options?
Returns
this