This site is one 3D scene, built for a computer: a wide window and a mouse. Open dioramajs.dev on a desktop or a laptop to see it.This site is one 3D scene, built for a wide window. Make this one wider, or open dioramajs.dev on a larger screen.
In this step you write mono, an effect that turns an image monochrome and tints its shadows. It has two params: strength, how far the image goes from color to monochrome, and ink, the color of the shadows.
Create the file
An effect is a file in src/effects/ whose default export is effect(). The file name is the effect name, so src/effects/mono.ts is mono. Start with one param, strength, which defaults to 1.
src/effects/mono.ts
import{
functioneffect(
setup:EffectSetup<Empty>
):Effect<Empty>(+2overloads)
Short form of defineEffect: defaults, optional options, setup. An unnamed effect takes its
registry key (the file name when a build plugin finds it).
Input uniforms and one uniform per live param, for TSL graphs.
u.
strength: UniformNode<"float">
strength),
color: Node<"vec4">
color.
Swizzle1From4<"float">.a: Node<"float">
a)
})
})
The first argument of effect() holds the params and their defaults. The second is the setup function. It gets the effect context, and this effect uses two parts of it:
surface is the picture of the element on the canvas. surface.color() takes a function that gets the color of each pixel and returns a new one.
u holds the params and the built-in inputs as shader nodes. u.strength is not the number 1 but a value the GPU reads on every frame.
The functions luminance, mix, vec3 and vec4 come from three/tsl, the shading language of three.js. You write it in TypeScript, and three.js compiles it for WebGPU or WebGL 2.
The image shows in gray. Set data-dio-strength="0.6" and some of the color comes back: mix() blends the original color and the gray by strength.
Add a color param
A string default that reads as a CSS color makes a color param. Add ink and mix the gray between it and white, so the shadows take the ink and the highlights stay light.
src/effects/mono.ts
import{
functioneffect(
setup:EffectSetup<Empty>
):Effect<Empty>(+2overloads)
Short form of defineEffect: defaults, optional options, setup. An unnamed effect takes its
registry key (the file name when a build plugin finds it).
Input uniforms and one uniform per live param, for TSL graphs.
u.
strength: UniformNode<"float">
strength),
color: Node<"vec4">
color.
Swizzle1From4<"float">.a: Node<"float">
a)
})
},
)
Save the file. In development, the plugin replaces the effect on the open page without a reload. Set the ink from markup: a param in camel case becomes kebab case in the attribute, so a param inkColor would be data-dio-ink-color.
index.html
<img
src="/cover.jpg"
width="800"
height="450"
data-dio="mono"
data-dio-strength="0.8"
data-dio-ink="#5b1a2e"
/>
Live preview needs WebGPU or WebGL 2. The photo with mono at strength 0.8 and a wine red ink, with a slider for strength and a color picker for ink.
The shadows turn wine red, the highlights stay pale, and a fifth of the original color shows through.
Why it works
Setup runs once per element, when the effect starts on it. It does not draw anything: it describes how the picture changes, and Diorama compiles that description into one shader for the element. When a param changes, only the value of its uniform changes, so a new value costs one frame and no compile.
The param types come from the defaults: a number, a boolean, a tuple of 2 to 4 numbers, or a color. The plugin writes them to src/diorama.gen.d.ts, so code that uses the effect by name is type checked, as the fifth step shows.