Skip to content

Effects ​

Effects change how a material looks: a cartoon look, a glowing edge, a color gradient, fog... Add them to a material as a list. They're applied in order, each one starting from the color the previous one produced.

js
import { toon, rim, edgeGlow, unlit, gradient, fog } from "./trid/render/effects.js";

scene.add({
  mesh: makeShape("sphere"),
  material: { fill: "#8ab4f8", smooth: true, effects: [toon({ steps: 3 }), rim()] },
});
toon · rim (animated) · gradient + edgeGlow · custom stripes + rim · drag to orbit
js
import {
  Scene, makeShape, WebGLRenderer, toon, rim, edgeGlow, gradient, OrbitCamera,
} from "./trid/index.js";

// A custom effect: stripes that scroll up over time
const stripes = {
  name: "stripes",
  uniforms: { uCount: 10, uSpeed: 0.3, uDark: "#1d2b3a" },
  glsl: `
vec4 effect(vec4 color, Surface s) {
  float band = step(0.5, fract(s.local.y * uCount - s.time * uSpeed));
  return mix(color, vec4(uDark.rgb, color.a), band);
}`,
};

export default function (canvas) {
  const scene = new Scene(canvas, {
    width: 800, height: 320, camera: { z: -2.4, fov: 0.7 },
    // Effects work per pixel with WebGL (the canvas renderer runs them per face)
    renderer: WebGLRenderer.isSupported() ? new WebGLRenderer() : undefined,
  });

  const ball = makeShape("sphere", { r: 0.27 });
  const glow = rim({ color: "#ffe08a", strength: 1 });

  const objects = [
    scene.add({ mesh: ball, pos: { x: -1.05, y: 0, z: 0 }, material: { fill: "#E9806E", smooth: true, effects: [toon({ steps: 3 })] } }),
    scene.add({ mesh: ball, pos: { x: -0.35, y: 0, z: 0 }, material: { fill: "#2a3550", smooth: true, effects: [glow] } }),
    scene.add({
      mesh: makeShape("box", { w: 0.4, h: 0.6, d: 0.4 }), pos: { x: 0.35, y: 0, z: 0 },
      material: { effects: [gradient({ from: "#E9806E", to: "#8ab4f8", min: -0.3, max: 0.3 }), edgeGlow({ color: "#ffffff", strength: 0.8 })] },
    }),
    scene.add({ mesh: ball, pos: { x: 1.05, y: 0, z: 0 }, material: { fill: "#8ab4f8", smooth: true, effects: [stripes, rim()] } }),
  ];

  scene.use(new OrbitCamera({ pitch: -0.25 }));

  scene.start((dt, time) => {
    for (const o of objects) o.rot.y += dt * 0.4;
    glow.uniforms.uStrength = 0.7 + 0.5 * Math.sin(time * 2); // settings can change every frame
  });

  return () => scene.destroy();
}

Use WebGL for effects

Effects are small shaders: with the WebGL renderer they run for every pixel. The canvas renderer runs them once per face, which looks blockier (and custom effects need a canvas version there).

Built-in effects ​

EffectOptions (defaults)What it does
toon()steps (3)Cartoon look: the light comes in a few hard steps
rim()color ("#ffffff"), power (2), strength (1)A glow along the edges, where the surface turns away from you. Higher power = thinner glow
edgeGlow()color ("#ffe08a"), width (0.08), strength (1)A glow along the border of each face. Made for boxy shapes, where rim barely shows. width is a fraction of the face. Uses texture coordinates; WebGL only
unlit()—Ignores the light: the fill color everywhere, like it glows
gradient()from, to, axis ("y"), min (-0.5), max (0.5), space ("local"), lit (true)Color changes along an axis. space: "world" measures in the world instead of the mesh
fog()near (1), far (5), color (the background)Fades into a color with distance from the camera

rim or edgeGlow?

rim lights up where the surface turns away from you, so it needs curved shapes: on a sphere it draws a glowing outline. A cube's faces are flat, so each face is either all glow or none. For boxes, crates and other flat-faced shapes, use edgeGlow.

Order matters: [toon(), rim()] draws a cartoon object with a glowing edge, while [rim(), unlit()] loses the rim (unlit replaces the color).

Changing settings ​

An effect's settings live in its uniforms and are read every frame, so you can animate them:

js
const glow = rim({ color: "#ffe08a" });
scene.add({ mesh, material: { fill: "#2a3550", effects: [glow] } });

scene.start((dt, time) => {
  glow.uniforms.uStrength = 0.7 + 0.5 * Math.sin(time * 2);
});
EffectUniforms
toonuSteps
rimuRimColor, uPower, uStrength
edgeGlowuGlowColor, uWidth, uStrength
gradientuFrom, uTo, uMin, uMax, uLit
foguNearFog, uFarFog, uFogColor

One effect object can be shared by many materials. They all change together.

Writing your own ​

An effect is a plain object. The built-in ones are written exactly like this, so their source is a good place to copy from.

js
const pulse = {
  name: "pulse", // optional, shown in error messages
  uniforms: { uSpeed: 3 },
  glsl: `
vec4 effect(vec4 color, Surface s) {
  float k = 0.75 + 0.25 * sin(s.time * uSpeed);
  return vec4(color.rgb * k, color.a);
}`,
};

scene.add({ mesh, material: { fill: "#E9806E", effects: [pulse] } });

The glsl code must define vec4 effect(vec4 color, Surface s): it gets the color so far and returns the new one. It's written in GLSL (OpenGL ES 1.0, the language of WebGL shaders). You can add helper functions next to it.

What s contains ​

FieldTypeMeaning
s.basevec4The material's fill color, times the texture's color
s.uvvec2Texture coordinates (0, 0 when the mesh has none)
s.normalvec3Unit normal in world space, facing the camera's side
s.positionvec3World position
s.localvec3Position in the mesh's own space (moves with the object)
s.viewvec3Unit direction from the point to the camera
s.lightDirvec3Unit direction the scene's first directional light comes from
s.diffusefloatHow much of the light comes from the lights rather than the ambient, 0 to 1. With the default single light: how much the point faces it
s.lightfloatThe brightness Trid used (the brightest channel of s.lightColor). With the default lights: ambient + (1 − ambient) × diffuse
s.lightColorvec3The light reaching the point, every light added up, as a color (can go above 1)
s.ambientfloatThe scene's ambient level (brightest channel)
s.depthfloatDistance to the camera
s.timefloatSeconds since the renderer started, for animation
s.backgroundvec4The scene's background color

The color that reaches the first effect is s.base × s.lightColor (the same as s.base × s.light with white lights).

In the canvas version, s.lightColor is an array [r, g, b].

Uniforms ​

Every entry in uniforms becomes a GLSL variable with the same name. Its type comes from the value:

ValueGLSL type
2.5float
true / falsefloat (1 or 0)
[1, 0], [1, 0, 0], [1, 0, 0, 1]vec2, vec3, vec4
{ x, y, z }vec3
"#ffe08a" (any CSS color)vec4 (r, g, b, a from 0 to 1)

Names are made unique behind the scenes, so the same effect can be used twice in one list, and two effects can use the same uniform or helper names.

Canvas version ​

The canvas renderer can't run GLSL. Give your effect a canvas function to support it; it runs once per face:

js
const pulse = {
  uniforms: { uSpeed: 3 },
  glsl: `...`,
  canvas: (color, s, u) => {
    const k = 0.75 + 0.25 * Math.sin(s.time * u.uSpeed);
    return [color[0] * k, color[1] * k, color[2] * k, color[3]];
  },
};

Colors are [r, g, b, a] arrays from 0 to 1, vectors are { x, y, z } objects, and s has the same fields as in GLSL. u is the effect's uniforms; s.rgba(value) turns a color uniform into an array. Without a canvas function, the canvas renderer skips the effect.

When something's wrong ​

If an effect's GLSL doesn't compile, the error is printed in the console once, and the object is drawn without its effects instead of breaking the scene.