Diorama

Made for a big screen.

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.

GitHub

DioramaDocs

Step 2 of 7

Write your own effect

Write an effect file that turns an image monochrome, with a number param and a color param you set from markup.

View as Markdown
On this page

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 {
function effect(
setup: EffectSetup<Empty>
): Effect<Empty> (+2 overloads)

Short form of defineEffect: defaults, optional options, setup. An unnamed effect takes its registry key (the file name when a build plugin finds it).

export default effect({ strength: 1 }, ({ surface, u }) => { ... })

effect
} from '@dioramajs/diorama'
import {
const luminance: (
color: Node,
luminanceCoefficients?: Node
) => Node<"float">
luminance
,
const mix: Mix
mix
,
const vec3: Vec3Function
vec3
,
const vec4: Vec4Function
vec4
} from 'three/tsl'
export default
effect<P>(
params: P & CheckSpec<P>,
setup: EffectSetup<ColorsIn<P>>
): Effect<WidenSpec<P>> (+2 overloads)
type P = {
readonly strength: 1;
}

Short form of defineEffect: defaults, optional options, setup. An unnamed effect takes its registry key (the file name when a build plugin finds it).

export default effect({ strength: 1 }, ({ surface, u }) => { ... })

effect
({
strength: 1
strength
: 1 }, ({
surface: Surface

The shared GL copy of the element, created on first access.

surface
,
u: Uniforms<ColorsIn<{
readonly strength: 1;
}>>

Input uniforms and one uniform per live param, for TSL graphs.

u
}) => {
surface: Surface

The shared GL copy of the element, created on first access.

surface
.
Surface.color(
transform: (color: Node<"vec4">, input: SurfaceInput) => Node
): Off

Changes the colour. sample(uv) reads the element's media with crop and masks applied.

color
((
color: Node<"vec4">
color
) => {
const
const gray: ConvertNode<"vec3">
gray
=
function vec3(node: ScalarNode): ConvertNode<"vec3"> (+6 overloads)
vec3
(
function luminance(
color: Node,
luminanceCoefficients?: Node
): Node<"float">
luminance
(
color: Node<"vec4">
color
.
Swizzle3From3<"float">.rgb: Node<"vec3">
rgb
))
return
function vec4(
xyz:
| Node<"vec3">
| Node<"color">
| Vector3
| Color
| Node<"vec4">,
w: Scalar
): JoinNode<"vec4"> (+10 overloads)
vec4
(
function mix(
a: Vec3OrFloat,
b: Vec3OrFloat,
t: Vec3OrFloat
): Node<"vec3"> (+3 overloads)
mix
(
color: Node<"vec4">
color
.
Swizzle3From3<"float">.rgb: Node<"vec3">
rgb
,
const gray: ConvertNode<"vec3">
gray
,
u: Uniforms<ColorsIn<{
readonly strength: 1;
}>>

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.

Put it on an image

Use the file name in data-dio, the same way as ripple in the previous step.

index.html
<img
src="/cover.jpg"
width="800"
height="450"
data-dio="mono"
/>
Paper ridges at dusk

Live preview needs WebGPU or WebGL 2. A photo of paper ridges turned gray by the mono effect.

This example failed to draw.

<img
src="/media/ridges.jpg"
width="800"
height="450"
data-dio="mono"
/>
import { effect } from '@dioramajs/diorama'
import { luminance, mix, vec3, vec4 } from 'three/tsl'
export default effect({ strength: 1 }, ({ surface, u }) => {
surface.color((color) => {
const gray = vec3(luminance(color.rgb))
return vec4(mix(color.rgb, gray, u.strength), color.a)
})
})

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 {
function effect(
setup: EffectSetup<Empty>
): Effect<Empty> (+2 overloads)

Short form of defineEffect: defaults, optional options, setup. An unnamed effect takes its registry key (the file name when a build plugin finds it).

export default effect({ strength: 1 }, ({ surface, u }) => { ... })

effect
} from '@dioramajs/diorama'
import {
const luminance: (
color: Node,
luminanceCoefficients?: Node
) => Node<"float">
luminance
,
const mix: Mix
mix
,
const vec3: Vec3Function
vec3
,
const vec4: Vec4Function
vec4
} from 'three/tsl'
export default
effect<P>(
params: P & CheckSpec<P>,
setup: EffectSetup<ColorsIn<P>>
): Effect<WidenSpec<P>> (+2 overloads)
type P = {
readonly strength: 1;
readonly ink: "#1d2b53";
}

Short form of defineEffect: defaults, optional options, setup. An unnamed effect takes its registry key (the file name when a build plugin finds it).

export default effect({ strength: 1 }, ({ surface, u }) => { ... })

effect
(
{
strength: 1
strength
: 1,
ink: "#1d2b53"
ink
: '#1d2b53' },
({
surface: Surface

The shared GL copy of the element, created on first access.

surface
,
u: Uniforms<
ColorsIn<{ readonly strength: 1; readonly ink: "#1d2b53"; }>
>

Input uniforms and one uniform per live param, for TSL graphs.

u
}) => {
surface: Surface

The shared GL copy of the element, created on first access.

surface
.
Surface.color(
transform: (color: Node<"vec4">, input: SurfaceInput) => Node
): Off

Changes the colour. sample(uv) reads the element's media with crop and masks applied.

color
((
color: Node<"vec4">
color
) => {
const
const gray: Node<"float">
gray
=
function luminance(
color: Node,
luminanceCoefficients?: Node
): Node<"float">
luminance
(
color: Node<"vec4">
color
.
Swizzle3From3<"float">.rgb: Node<"vec3">
rgb
)
// Dark parts take the ink, light parts go to white.
const
const toned: Node<"vec3">
toned
=
function mix(
a: Vec3OrFloat,
b: Vec3OrFloat,
t: Vec3OrFloat
): Node<"vec3"> (+3 overloads)
mix
(
u: Uniforms<
ColorsIn<{ readonly strength: 1; readonly ink: "#1d2b53"; }>
>

Input uniforms and one uniform per live param, for TSL graphs.

u
.
ink: UniformNode<"color">
ink
,
function vec3(x?: number, y?: number, z?: number): ConstNode<
"vec3",
Vector3
> (+6 overloads)
vec3
(1),
const gray: Node<"float">
gray
)
return
function vec4(
xyz:
| Node<"vec3">
| Node<"color">
| Vector3
| Color
| Node<"vec4">,
w: Scalar
): JoinNode<"vec4"> (+10 overloads)
vec4
(
function mix(
a: Vec3OrFloat,
b: Vec3OrFloat,
t: Vec3OrFloat
): Node<"vec3"> (+3 overloads)
mix
(
color: Node<"vec4">
color
.
Swizzle3From3<"float">.rgb: Node<"vec3">
rgb
,
const toned: Node<"vec3">
toned
,
u: Uniforms<
ColorsIn<{ readonly strength: 1; readonly ink: "#1d2b53"; }>
>

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"
/>
Paper ridges at dusk

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.

This example failed to draw.

Params of mono
<img
src="/media/ridges.jpg"
width="800"
height="450"
data-dio="mono"
data-dio-strength="0.8"
data-dio-ink="#5b1a2e"
/>
import { effect } from '@dioramajs/diorama'
import { luminance, mix, vec3, vec4 } from 'three/tsl'
export default effect(
{ strength: 1, ink: '#1d2b53' },
({ surface, u }) => {
surface.color((color) => {
const gray = luminance(color.rgb)
// Dark parts take the ink, light parts go to white.
const toned = mix(u.ink, vec3(1), gray)
return vec4(mix(color.rgb, toned, u.strength), color.a)
})
},
)

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.