Skip to content

TSL Nodes

What This Example Shows

This example demonstrates the Material Effect system — the recommended way to create per-sprite shader effects. It showcases 8 visual modes on an animated sprite:

EffectDescription
NormalNo effect — base sprite rendering
Damage FlashAdditive white flash that fades out
DissolvePixelated noise-based dissolve
PowerupContinuous hue rotation (rainbow)
PetrifyDesaturation to grayscale
SelectGreen outline around sprite
ShadowDark blue tint with reduced alpha
PixelateResolution reduction and restoration

Defining Effects

Effects are defined with createMaterialEffect, which takes a schema for per-sprite data and a TSL node builder:

import { createMaterialEffect } from 'three-flatland'
import { tintAdditive, hueShift } from '@three-flatland/nodes'
import { vec4 } from 'three/tsl'
const DamageFlash = createMaterialEffect({
name: 'damageFlash',
schema: { intensity: 1 } as const,
node: ({ inputColor, attrs }) => {
const flashed = tintAdditive(inputColor, [1, 1, 1], attrs.intensity)
return vec4(flashed.rgb.mul(inputColor.a), inputColor.a)
},
})
const Powerup = createMaterialEffect({
name: 'powerup',
schema: { angle: 0 } as const,
node: ({ inputColor, attrs }) => hueShift(inputColor, attrs.angle),
})

Applying Effects at Runtime

Instantiate an effect, add it to a sprite, and animate its properties:

const flash = new DamageFlash()
sprite.addEffect(flash)
// Animate in render loop
flash.intensity = Math.max(0, 1 - elapsed / 0.3)
// Switch effects
sprite.removeEffect(flash)

Next Steps