Skip to main content

Custom effect

Custom effects are fragment shaders applied to the completed scene. This example inverts the scene colours and uses uAmount to control the blend.

#version 300 es
precision mediump float;

in vec2 vUV;
uniform sampler2D uSource;
uniform float uAmount;
out vec4 outputColor;

void main() {
vec4 source = texture(uSource, vUV);
vec3 inverted = vec3(1.0) - source.rgb;
outputColor = vec4(mix(source.rgb, inverted, uAmount), source.a);
}
const invert = await TPostProcessingEffect.fromSource(
this.jobs,
invertFragmentShader,
);

invert.setUniform('uAmount', 1);
this.postProcessing.add(invert);

TED supplies these values to every custom effect:

NameTypeValue
uSourcesampler2DThe output of the scene or previous effect
uResolutionvec2Output width and height in pixels
uTimefloatTime in seconds
vUVvec2Interpolated texture coordinates from 0 to 1

Use setUniform() for custom values. Effects can be adjusted or enabled and disabled at runtime, and shader files can be loaded with TPostProcessingEffect.fromUrl().