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

Write an effect

Build an effect from an empty file to a torch that follows the pointer, then add your own three.js objects, composition and error handling.

View as Markdown
On this page

This guide builds torch, a soft light that follows the pointer over an image and magnifies what is under it. Each step adds one part of the effect API, and the whole file is at the end. The later steps leave the torch and show what an effect can do beyond the picture of its element. For a finished light that follows the pointer, use the built-in spotlight.

Create the file

An effect is a file in the effects folder that exports effect(): src/effects/ with Vite, Astro and Next.js, app/effects/ with Nuxt. The file name is the effect name, so torch.ts is the token torch in data-dio. A file named after a built-in effect replaces the built-in, and development builds warn with EFFECT_OVERRIDE.

src/effects/torch.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'
export default
effect<P>(
params: P & CheckSpec<P>,
setup: EffectSetup<ColorsIn<P>>
): Effect<WidenSpec<P>> (+2 overloads)
type P = {
readonly radius: 0.35;
readonly strength: 0.6;
readonly tint: "#ffd27a";
}

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
(
{
radius: 0.35
radius
: 0.35,
strength: 0.6
strength
: 0.6,
tint: "#ffd27a"
tint
: '#ffd27a' },
({
surface: Surface

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

surface
,
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly tint: "#ffd27a";
}
>
>

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

u
}) => {
// Runs once per element, when the element nears the viewport.
},
)

The first argument holds the params with their defaults, and their types come from the defaults.

DefaultParam typeIn the shader
0.35numberfloat uniform
truebooleanfloat uniform, 0 or 1
[0.5, 0.5], 2 to 4 numberstuplevec2 to vec4 uniform
'#ffd27a', rgb(...), hsl(...)colorcolor uniform
oneOf('soft', 'hard')one of the listed stringsnone, a change runs setup again
'img', text('#cafe')stringnone, a change runs setup again

Param names are lowerCamelCase. Input names (time, hover, pointer and the rest) and option names (when, animate, media and the rest) are taken: the type check and EFFECT_DEFINITION both name the clash.

The setup function gets the effect context. Its methods are bound, so you can destructure what you need. The next steps fill it in.

Light the picture

surface is the element’s picture as a plane on the canvas. surface.color() takes a function that gets the current color and returns a new one, written in TSL, the node language of three.js. u holds the params as uniforms (u.radius, u.tint) and the built-in inputs (u.pointer, u.hover, u.size).

src/effects/torch.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 smoothstep: Smoothstep
smoothstep
,
const vec2: Vec2Function
vec2
,
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 radius: 0.35;
readonly strength: 0.6;
readonly tint: "#ffd27a";
}

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
(
{
radius: 0.35
radius
: 0.35,
strength: 0.6
strength
: 0.6,
tint: "#ffd27a"
tint
: '#ffd27a' },
({
surface: Surface

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

surface
,
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly tint: "#ffd27a";
}
>
>

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

u
}) => {
const
const aspect: JoinNode<"vec2">
aspect
=
function vec2(
x: Scalar,
y: Scalar
): JoinNode<"vec2"> (+4 overloads)
vec2
(
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly tint: "#ffd27a";
}
>
>

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

u
.
InputUniforms.size: UniformNode<"vec2">

Box size in CSS px.

size
.
Swizzle1From1<"float">.x: Node<"float">
x
.
NumExtensions<"float">.div: AddSubMulDivNumberVecNumExtensions
(
b: NumberParameterForNumType<"float">,
...params: NumberParameterForNumType<"float">[]
) => Node<"float"> (+3 overloads)
div
(
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly tint: "#ffd27a";
}
>
>

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

u
.
InputUniforms.size: UniformNode<"vec2">

Box size in CSS px.

size
.
Swizzle1From2<"float">.y: Node<"float">
y
), 1)
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
, {
uv: Node<"vec2">

Box UV after all uv transforms.

uv
}) => {
const
const distance: Node<"float">
distance
=
uv: Node<"vec2">

Box UV after all uv transforms.

uv
.
NumVec2Extensions<"float">.sub: AddSubMulDivNumberVecVec2Extensions
(
b: Vec2OrLessOrNumberParameterForNumType<"float">,
...params: Vec2OrLessOrNumberParameterForNumType<"float">[]
) => Node<"vec2"> (+2 overloads)
sub
(
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly tint: "#ffd27a";
}
>
>

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

u
.
InputUniforms.pointer: UniformNode<"vec2">

Pointer in element UV, y up like uv(). 0..1 inside the box, not clamped outside.

pointer
).
mul: (
b: Vec2OrLessOrNumberParameterForNumType<"float">,
...params: Vec2OrLessOrNumberParameterForNumType<"float">[]
) => Node<"vec2"> (+6 overloads)
mul
(
const aspect: JoinNode<"vec2">
aspect
).
FloatOrVecExtensions<"vec2">.length: () => Node<"float">
length
()
const
const edge: Node<"float">
edge
=
function smoothstep(
low: FloatOrNumber,
high: FloatOrNumber,
x: FloatOrNumber
): Node<"float"> (+3 overloads)
smoothstep
(0,
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly tint: "#ffd27a";
}
>
>

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

u
.
radius: UniformNode<"float">
radius
,
const distance: Node<"float">
distance
)
const
const light: Node<"float">
light
=
const edge: Node<"float">
edge
.
FloatOrVecExtensions<"float">.oneMinus: () => Node<"float">
oneMinus
().
mul: (
b: NumberParameterForNumType<"float">,
...params: NumberParameterForNumType<"float">[]
) => Node<"float"> (+7 overloads)
mul
(
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly tint: "#ffd27a";
}
>
>

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

u
.
InputUniforms.hover: UniformNode<"float">

Damped 0..1 while the pointer is over the box. 0 under reduced motion.

hover
,
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly tint: "#ffd27a";
}
>
>

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

u
.
strength: UniformNode<"float">
strength
)
return
function vec4(
xyz:
| Node<"vec3">
| Node<"color">
| Vector3
| Color
| Node<"vec4">,
w: Scalar
): JoinNode<"vec4"> (+10 overloads)
vec4
(
color: Node<"vec4">
color
.
Swizzle3From3<"float">.rgb: Node<"vec3">
rgb
.
add: (
b: Vec3OrLessOrNumberParameterForNumType<"float">,
...params: Vec3OrLessOrNumberParameterForNumType<"float">[]
) => Node<"vec3"> (+2 overloads)
add
(
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly tint: "#ffd27a";
}
>
>

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

u
.
tint: UniformNode<"color">
tint
.
ColorExtensions.mul: (
b: Number<"float">
) => Node<"vec3">
mul
(
const light: Node<"float">
light
)),
color: Node<"vec4">
color
.
Swizzle1From4<"float">.a: Node<"float">
a
)
})
},
)
  • uv is the box UV, 0 to 1 with y up. u.pointer uses the same space, so the distance between them is a distance in the box.
  • u.size is the box size in CSS pixels. Multiplying by the aspect ratio keeps the light round on a wide image.
  • u.hover rises from 0 to 1 while the pointer is over the element and falls back when it leaves, so the light fades in and out.

On an <img> or <video> the surface prepares the picture itself, crops it like CSS object-fit and object-position, and applies border-radius, the clip of an ancestor with overflow: hidden and the CSS opacity of the element. Once the plane is drawn, the element gets data-dio-state="drawn" and its DOM picture is hidden with filter: opacity(0). Layout, clicks, focus and alt stay with the element.

Paper ridges at dusk

Live preview needs WebGPU or WebGL 2. A photo with the torch effect: a warm light follows the pointer and fades out when it leaves.

This example failed to draw.

Params of torch
<img
src="/media/ridges.jpg"
width="800"
height="450"
data-dio="torch"
/>
import { effect } from '@dioramajs/diorama'
import { smoothstep, vec2, vec4 } from 'three/tsl'
export default effect(
{ radius: 0.35, strength: 0.6, tint: '#ffd27a' },
({ surface, u }) => {
const aspect = vec2(u.size.x.div(u.size.y), 1)
surface.color((color, { uv }) => {
const distance = uv.sub(u.pointer).mul(aspect).length()
const edge = smoothstep(0, u.radius, distance)
const light = edge.oneMinus().mul(u.hover, u.strength)
return vec4(color.rgb.add(u.tint.mul(light)), color.a)
})
},
)

Set params from markup

A param is an attribute named data-dio- plus the param name in kebab case.

index.html
<img
src="/cover.jpg"
data-dio="torch"
data-dio-radius="0.5"
data-dio-tint="#9ad7ff"
/>

Numbers, booleans, colors and tuples are uniforms, so a new attribute value changes the uniform and draws one frame. A color also takes var(--brand) and currentColor, read from the element’s computed style when it is set and again in every frame the element draws, so it follows a theme switch from the next frame. A switch made while nothing draws shows after app.invalidate(). Writes from code work the same way and beat attributes: app.attach('.hero', 'torch', { radius: 0.5 }) returns a handle, and handle.params.radius = 0.4 draws in the next frame. The build plugin writes the types of your effects to src/diorama.gen.d.ts, so the name and the params are checked.

Choose when it draws

Diorama draws a frame only when something changed. An effect that reads no clock draws when the page scrolls, when the pointer moves over it and when a param changes. Inputs that fade, such as hover and velocity, keep frames coming until they settle. torch reads u.pointer and u.hover only, so a page at rest draws nothing.

Reading a clock changes this. Read u.time and the effect draws every frame while it is on screen. Read u.hoverTime, seconds that run only while the pointer is over the element and while hover fades, and it draws only then.

src/effects/torch.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 smoothstep: Smoothstep
smoothstep
,
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 radius: 0.35;
readonly strength: 0.6;
readonly tint: "#ffd27a";
}

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
(
{
radius: 0.35
radius
: 0.35,
strength: 0.6
strength
: 0.6,
tint: "#ffd27a"
tint
: '#ffd27a' },
({
surface: Surface

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

surface
,
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly tint: "#ffd27a";
}
>
>

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

u
}) => {
// A pulse that runs under the pointer only.
const
const pulse: Node<"float">
pulse
=
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly tint: "#ffd27a";
}
>
>

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

u
.
InputUniforms.hoverTime: UniformNode<"float">

Seconds that run while hovered or while hover decays, frozen at rest and under reduced motion. Reading it implies animate: 'hover'.

hoverTime
.
mul: (
b: NumberParameterForNumType<"float">,
...params: NumberParameterForNumType<"float">[]
) => Node<"float"> (+7 overloads)
mul
(3).
FloatOrVecExtensions<"float">.sin: () => Node<"float">
sin
().
mul: (
b: NumberParameterForNumType<"float">,
...params: NumberParameterForNumType<"float">[]
) => Node<"float"> (+7 overloads)
mul
(0.1).
NumExtensions<"float">.add: AddSubMulDivNumberVecNumExtensions
(
b: NumberParameterForNumType<"float">,
...params: NumberParameterForNumType<"float">[]
) => Node<"float"> (+3 overloads)
add
(1)
const
const radius: Node<"float">
radius
=
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly tint: "#ffd27a";
}
>
>

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

u
.
radius: UniformNode<"float">
radius
.
mul: (
b: NumberParameterForNumType<"float">,
...params: NumberParameterForNumType<"float">[]
) => Node<"float"> (+7 overloads)
mul
(
const pulse: Node<"float">
pulse
)
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
, {
uv: Node<"vec2">

Box UV after all uv transforms.

uv
}) => {
const
const edge: Node<"float">
edge
=
function smoothstep(
low: FloatOrNumber,
high: FloatOrNumber,
x: FloatOrNumber
): Node<"float"> (+3 overloads)
smoothstep
(0,
const radius: Node<"float">
radius
,
uv: Node<"vec2">

Box UV after all uv transforms.

uv
.
NumVec2Extensions<"float">.sub: AddSubMulDivNumberVecVec2Extensions
(
b: Vec2OrLessOrNumberParameterForNumType<"float">,
...params: Vec2OrLessOrNumberParameterForNumType<"float">[]
) => Node<"vec2"> (+2 overloads)
sub
(
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly tint: "#ffd27a";
}
>
>

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

u
.
InputUniforms.pointer: UniformNode<"vec2">

Pointer in element UV, y up like uv(). 0..1 inside the box, not clamped outside.

pointer
).
FloatOrVecExtensions<"vec2">.length: () => Node<"float">
length
())
const
const light: Node<"float">
light
=
const edge: Node<"float">
edge
.
FloatOrVecExtensions<"float">.oneMinus: () => Node<"float">
oneMinus
().
mul: (
b: NumberParameterForNumType<"float">,
...params: NumberParameterForNumType<"float">[]
) => Node<"float"> (+7 overloads)
mul
(
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly tint: "#ffd27a";
}
>
>

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

u
.
InputUniforms.hover: UniformNode<"float">

Damped 0..1 while the pointer is over the box. 0 under reduced motion.

hover
,
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly tint: "#ffd27a";
}
>
>

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

u
.
strength: UniformNode<"float">
strength
)
return
function vec4(
xyz:
| Node<"vec3">
| Node<"color">
| Vector3
| Color
| Node<"vec4">,
w: Scalar
): JoinNode<"vec4"> (+10 overloads)
vec4
(
color: Node<"vec4">
color
.
Swizzle3From3<"float">.rgb: Node<"vec3">
rgb
.
add: (
b: Vec3OrLessOrNumberParameterForNumType<"float">,
...params: Vec3OrLessOrNumberParameterForNumType<"float">[]
) => Node<"vec3"> (+2 overloads)
add
(
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly tint: "#ffd27a";
}
>
>

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

u
.
tint: UniformNode<"color">
tint
.
ColorExtensions.mul: (
b: Number<"float">
) => Node<"vec3">
mul
(
const light: Node<"float">
light
)),
color: Node<"vec4">
color
.
Swizzle1From4<"float">.a: Node<"float">
a
)
})
},
)

To choose yourself, pass options between the params and the setup function. animate is the frame policy.

animateAsks for frames
'input'Never. Frames come from scroll, pointer and param changes
'hover'While the pointer is over the element and while hover fades
'visible'Every frame while the element is on screen
(ctx) => booleanWhile the function returns true, checked after each frame
{ fps: 12 }Time frames at most 12 times a second; input frames run at full rate

The update function can also answer for itself: returning true asks for the next frame and false for none, which suits state kept in JavaScript, such as a damped position. Without animate the answer decides alone; with it, true adds frames to the policy.

An effect never asks for frames while its element is off screen or the tab is hidden. Under reduced motion 'hover' and 'visible' ask for none, as Reduced motion explains. An update that returns true still gets its frames there, so let it settle at once when input.reducedMotion is set.

Skip touch devices

The torch needs a pointer that hovers. when takes a media query, and 'hover' is short for '(hover: hover)'. Where the query does not match, the instance is off, the element stays plain HTML and the engine does not start for it.

src/effects/torch.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 smoothstep: Smoothstep
smoothstep
,
const vec2: Vec2Function
vec2
,
const vec4: Vec4Function
vec4
} from 'three/tsl'
export default
effect<P>(
params: P & CheckSpec<P>,
options: EffectOptions<ColorsIn<P>>,
setup: EffectSetup<ColorsIn<P>>
): Effect<WidenSpec<P>> (+2 overloads)
type P = {
readonly radius: 0.35;
readonly strength: 0.6;
readonly tint: "#ffd27a";
}

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
(
{
radius: 0.35
radius
: 0.35,
strength: 0.6
strength
: 0.6,
tint: "#ffd27a"
tint
: '#ffd27a' },
{
when?: When | undefined

Media query the page must match or the instance is off. 'hover' means '(hover: hover)'.

when
: 'hover' },
({
surface: Surface

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

surface
,
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly tint: "#ffd27a";
}
>
>

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

u
}) => {
const
const aspect: JoinNode<"vec2">
aspect
=
function vec2(
x: Scalar,
y: Scalar
): JoinNode<"vec2"> (+4 overloads)
vec2
(
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly tint: "#ffd27a";
}
>
>

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

u
.
InputUniforms.size: UniformNode<"vec2">

Box size in CSS px.

size
.
Swizzle1From1<"float">.x: Node<"float">
x
.
NumExtensions<"float">.div: AddSubMulDivNumberVecNumExtensions
(
b: NumberParameterForNumType<"float">,
...params: NumberParameterForNumType<"float">[]
) => Node<"float"> (+3 overloads)
div
(
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly tint: "#ffd27a";
}
>
>

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

u
.
InputUniforms.size: UniformNode<"vec2">

Box size in CSS px.

size
.
Swizzle1From2<"float">.y: Node<"float">
y
), 1)
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
, {
uv: Node<"vec2">

Box UV after all uv transforms.

uv
}) => {
const
const distance: Node<"float">
distance
=
uv: Node<"vec2">

Box UV after all uv transforms.

uv
.
NumVec2Extensions<"float">.sub: AddSubMulDivNumberVecVec2Extensions
(
b: Vec2OrLessOrNumberParameterForNumType<"float">,
...params: Vec2OrLessOrNumberParameterForNumType<"float">[]
) => Node<"vec2"> (+2 overloads)
sub
(
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly tint: "#ffd27a";
}
>
>

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

u
.
InputUniforms.pointer: UniformNode<"vec2">

Pointer in element UV, y up like uv(). 0..1 inside the box, not clamped outside.

pointer
).
mul: (
b: Vec2OrLessOrNumberParameterForNumType<"float">,
...params: Vec2OrLessOrNumberParameterForNumType<"float">[]
) => Node<"vec2"> (+6 overloads)
mul
(
const aspect: JoinNode<"vec2">
aspect
).
FloatOrVecExtensions<"vec2">.length: () => Node<"float">
length
()
const
const edge: Node<"float">
edge
=
function smoothstep(
low: FloatOrNumber,
high: FloatOrNumber,
x: FloatOrNumber
): Node<"float"> (+3 overloads)
smoothstep
(0,
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly tint: "#ffd27a";
}
>
>

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

u
.
radius: UniformNode<"float">
radius
,
const distance: Node<"float">
distance
)
const
const light: Node<"float">
light
=
const edge: Node<"float">
edge
.
FloatOrVecExtensions<"float">.oneMinus: () => Node<"float">
oneMinus
().
mul: (
b: NumberParameterForNumType<"float">,
...params: NumberParameterForNumType<"float">[]
) => Node<"float"> (+7 overloads)
mul
(
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly tint: "#ffd27a";
}
>
>

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

u
.
InputUniforms.hover: UniformNode<"float">

Damped 0..1 while the pointer is over the box. 0 under reduced motion.

hover
,
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly tint: "#ffd27a";
}
>
>

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

u
.
strength: UniformNode<"float">
strength
)
return
function vec4(
xyz:
| Node<"vec3">
| Node<"color">
| Vector3
| Color
| Node<"vec4">,
w: Scalar
): JoinNode<"vec4"> (+10 overloads)
vec4
(
color: Node<"vec4">
color
.
Swizzle3From3<"float">.rgb: Node<"vec3">
rgb
.
add: (
b: Vec3OrLessOrNumberParameterForNumType<"float">,
...params: Vec3OrLessOrNumberParameterForNumType<"float">[]
) => Node<"vec3"> (+2 overloads)
add
(
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly tint: "#ffd27a";
}
>
>

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

u
.
tint: UniformNode<"color">
tint
.
ColorExtensions.mul: (
b: Number<"float">
) => Node<"vec3">
mul
(
const light: Node<"float">
light
)),
color: Node<"vec4">
color
.
Swizzle1From4<"float">.a: Node<"float">
a
)
})
},
)

Diorama never guesses when: an effect that reacts to a tap can be what you want, so the choice is yours.

Magnify under the pointer

surface.uv() moves the point the picture is read from. It runs before every color function, so color in surface.color() already shows the bent picture. Add a zoom param and pull the UV toward the pointer inside the light.

src/effects/torch.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 smoothstep: Smoothstep
smoothstep
,
const vec2: Vec2Function
vec2
,
const vec4: Vec4Function
vec4
} from 'three/tsl'
import type {
type Node<TNodeType = unknown> = NodeClass<NodeClassEventMap>
& NodeElements
& (unknown extends TNodeType ? {} : NodeExtensions<TNodeType>)
& NodeExtras[TNodeType & string]
& { __TypeScript_NODE_TYPE__: TNodeType; }
const Node: {
new <TNodeType>(nodeType?: TNodeType | null): Node<TNodeType>;
new (nodeType?: string | null): Node;
captureStackTrace: boolean;
}
Node
} from 'three/webgpu'
export default
effect<P>(
params: P & CheckSpec<P>,
options: EffectOptions<ColorsIn<P>>,
setup: EffectSetup<ColorsIn<P>>
): Effect<...> (+2 overloads)
type P = {
readonly radius: 0.35;
readonly strength: 0.6;
readonly zoom: 0.2;
readonly tint: "#ffd27a";
}

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
(
{
radius: 0.35
radius
: 0.35,
strength: 0.6
strength
: 0.6,
zoom: 0.2
zoom
: 0.2,
tint: "#ffd27a"
tint
: '#ffd27a' },
{
when?: When | undefined

Media query the page must match or the instance is off. 'hover' means '(hover: hover)'.

when
: 'hover' },
({
surface: Surface

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

surface
,
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly zoom: 0.2;
readonly tint: "#ffd27a";
}
>
>

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

u
}) => {
const
const aspect: JoinNode<"vec2">
aspect
=
function vec2(
x: Scalar,
y: Scalar
): JoinNode<"vec2"> (+4 overloads)
vec2
(
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly zoom: 0.2;
readonly tint: "#ffd27a";
}
>
>

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

u
.
InputUniforms.size: UniformNode<"vec2">

Box size in CSS px.

size
.
Swizzle1From1<"float">.x: Node<"float">
x
.
NumExtensions<"float">.div: AddSubMulDivNumberVecNumExtensions
(
b: NumberParameterForNumType<"float">,
...params: NumberParameterForNumType<"float">[]
) => Node<"float"> (+3 overloads)
div
(
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly zoom: 0.2;
readonly tint: "#ffd27a";
}
>
>

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

u
.
InputUniforms.size: UniformNode<"vec2">

Box size in CSS px.

size
.
Swizzle1From2<"float">.y: Node<"float">
y
), 1)
const
const inside: (uv: Node<"vec2">) => Node<"float">
inside
= (
uv: Node<"vec2">
uv
:
type Node<TNodeType = unknown> = NodeClass<NodeClassEventMap>
& NodeElements
& (unknown extends TNodeType ? {} : NodeExtensions<TNodeType>)
& NodeExtras[TNodeType & string]
& { __TypeScript_NODE_TYPE__: TNodeType; }
Node
<'vec2'>) => {
const
const distance: Node<"float">
distance
=
uv: Node<"vec2">
uv
.
NumVec2Extensions<"float">.sub: AddSubMulDivNumberVecVec2Extensions
(
b: Vec2OrLessOrNumberParameterForNumType<"float">,
...params: Vec2OrLessOrNumberParameterForNumType<"float">[]
) => Node<"vec2"> (+2 overloads)
sub
(
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly zoom: 0.2;
readonly tint: "#ffd27a";
}
>
>

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

u
.
InputUniforms.pointer: UniformNode<"vec2">

Pointer in element UV, y up like uv(). 0..1 inside the box, not clamped outside.

pointer
).
mul: (
b: Vec2OrLessOrNumberParameterForNumType<"float">,
...params: Vec2OrLessOrNumberParameterForNumType<"float">[]
) => Node<"vec2"> (+6 overloads)
mul
(
const aspect: JoinNode<"vec2">
aspect
).
FloatOrVecExtensions<"vec2">.length: () => Node<"float">
length
()
const
const edge: Node<"float">
edge
=
function smoothstep(
low: FloatOrNumber,
high: FloatOrNumber,
x: FloatOrNumber
): Node<"float"> (+3 overloads)
smoothstep
(0,
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly zoom: 0.2;
readonly tint: "#ffd27a";
}
>
>

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

u
.
radius: UniformNode<"float">
radius
,
const distance: Node<"float">
distance
)
return
const edge: Node<"float">
edge
.
FloatOrVecExtensions<"float">.oneMinus: () => Node<"float">
oneMinus
().
mul: (
b: NumberParameterForNumType<"float">,
...params: NumberParameterForNumType<"float">[]
) => Node<"float"> (+7 overloads)
mul
(
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly zoom: 0.2;
readonly tint: "#ffd27a";
}
>
>

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

u
.
InputUniforms.hover: UniformNode<"float">

Damped 0..1 while the pointer is over the box. 0 under reduced motion.

hover
)
}
surface: Surface

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

surface
.
Surface.uv(transform: (uv: Node<"vec2">) => Node): Off

Bends box UV (0..1, y up) before sampling.

uv
((
uv: Node<"vec2">
uv
) => {
const
const pull: Node<"float">
pull
=
const inside: (uv: Node<"vec2">) => Node<"float">
inside
(
uv: Node<"vec2">
uv
).
mul: (
b: NumberParameterForNumType<"float">,
...params: NumberParameterForNumType<"float">[]
) => Node<"float"> (+7 overloads)
mul
(
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly zoom: 0.2;
readonly tint: "#ffd27a";
}
>
>

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

u
.
zoom: UniformNode<"float">
zoom
).
FloatOrVecExtensions<"float">.oneMinus: () => Node<"float">
oneMinus
()
return
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly zoom: 0.2;
readonly tint: "#ffd27a";
}
>
>

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

u
.
InputUniforms.pointer: UniformNode<"vec2">

Pointer in element UV, y up like uv(). 0..1 inside the box, not clamped outside.

pointer
.
add: (
b: Vec2OrLessOrNumberParameterForNumType<"float">,
...params: Vec2OrLessOrNumberParameterForNumType<"float">[]
) => Node<"vec2"> (+3 overloads)
add
(
uv: Node<"vec2">
uv
.
NumVec2Extensions<"float">.sub: AddSubMulDivNumberVecVec2Extensions
(
b: Vec2OrLessOrNumberParameterForNumType<"float">,
...params: Vec2OrLessOrNumberParameterForNumType<"float">[]
) => Node<"vec2"> (+2 overloads)
sub
(
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly zoom: 0.2;
readonly tint: "#ffd27a";
}
>
>

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

u
.
InputUniforms.pointer: UniformNode<"vec2">

Pointer in element UV, y up like uv(). 0..1 inside the box, not clamped outside.

pointer
).
mul: (
b: Vec2OrLessOrNumberParameterForNumType<"float">,
...params: Vec2OrLessOrNumberParameterForNumType<"float">[]
) => Node<"vec2"> (+6 overloads)
mul
(
const pull: Node<"float">
pull
))
})
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
, {
box: Node<"vec2">

Box UV before transforms.

box
}) => {
const
const light: Node<"float">
light
=
const inside: (uv: Node<"vec2">) => Node<"float">
inside
(
box: Node<"vec2">

Box UV before transforms.

box
).
mul: (
b: NumberParameterForNumType<"float">,
...params: NumberParameterForNumType<"float">[]
) => Node<"float"> (+7 overloads)
mul
(
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly zoom: 0.2;
readonly tint: "#ffd27a";
}
>
>

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

u
.
strength: UniformNode<"float">
strength
)
return
function vec4(
xyz:
| Node<"vec3">
| Node<"color">
| Vector3
| Color
| Node<"vec4">,
w: Scalar
): JoinNode<"vec4"> (+10 overloads)
vec4
(
color: Node<"vec4">
color
.
Swizzle3From3<"float">.rgb: Node<"vec3">
rgb
.
add: (
b: Vec3OrLessOrNumberParameterForNumType<"float">,
...params: Vec3OrLessOrNumberParameterForNumType<"float">[]
) => Node<"vec3"> (+2 overloads)
add
(
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly zoom: 0.2;
readonly tint: "#ffd27a";
}
>
>

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

u
.
tint: UniformNode<"color">
tint
.
ColorExtensions.mul: (
b: Number<"float">
) => Node<"vec3">
mul
(
const light: Node<"float">
light
)),
color: Node<"vec4">
color
.
Swizzle1From4<"float">.a: Node<"float">
a
)
})
},
)

The second argument of the color function also has box, the UV before any uv function, and sample(uv), which reads the picture at any UV with the crop and masks applied. A point outside the box reads the nearest edge pixel, like ClampToEdgeWrapping.

surface.position() moves the vertices of the plane. x and y are fractions of the box from -0.5 to 0.5, z is in CSS pixels toward the viewer. Ask for more vertices with surface.segments(); the largest request of all effects on the element wins.

src/effects/lift.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 vec3: Vec3Function
vec3
} from 'three/tsl'
export default
effect<P>(
params: P & CheckSpec<P>,
setup: EffectSetup<ColorsIn<P>>
): Effect<WidenSpec<P>> (+2 overloads)
type P = {
readonly height: 40;
}

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
({
height: 40
height
: 40 }, ({
surface: Surface

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

surface
,
u: Uniforms<ColorsIn<{
readonly height: 40;
}>>

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.segments(count: number): void

Requests plane subdivisions. The largest request wins, default 1.

segments
(32)
surface: Surface

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

surface
.
Surface.position(
transform: (position: Node<"vec3">, uv: Node<"vec2">) => Node
): Off

Moves vertices in the plane's local 1x1 space.

position
((
position: Node<"vec3">
position
,
uv: Node<"vec2">
uv
) => {
const
const near: Node<"float">
near
=
uv: Node<"vec2">
uv
.
NumVec2Extensions<"float">.sub: AddSubMulDivNumberVecVec2Extensions
(
b: Vec2OrLessOrNumberParameterForNumType<"float">,
...params: Vec2OrLessOrNumberParameterForNumType<"float">[]
) => Node<"vec2"> (+2 overloads)
sub
(
u: Uniforms<ColorsIn<{
readonly height: 40;
}>>

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

u
.
InputUniforms.pointer: UniformNode<"vec2">

Pointer in element UV, y up like uv(). 0..1 inside the box, not clamped outside.

pointer
).
FloatOrVecExtensions<"vec2">.length: () => Node<"float">
length
().
FloatOrVecExtensions<"float">.oneMinus: () => Node<"float">
oneMinus
().
FloatExtensions.max: MinMaxFloatExtension
(
y: FloatOrNumber,
...params: FloatOrNumber[]
) => Node<"float"> (+3 overloads)
max
(0)
return
position: Node<"vec3">
position
.
add: (
b: Vec3OrLessOrNumberParameterForNumType<"float">,
...params: Vec3OrLessOrNumberParameterForNumType<"float">[]
) => Node<"vec3"> (+2 overloads)
add
(
function vec3(
x: Scalar,
y: Scalar,
z: Scalar
): JoinNode<"vec3"> (+6 overloads)
vec3
(0, 0,
const near: Node<"float">
near
.
mul: (
b: NumberParameterForNumType<"float">,
...params: NumberParameterForNumType<"float">[]
) => Node<"float"> (+7 overloads)
mul
(
u: Uniforms<ColorsIn<{
readonly height: 40;
}>>

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

u
.
InputUniforms.hover: UniformNode<"float">

Damped 0..1 while the pointer is over the box. 0 under reduced motion.

hover
,
u: Uniforms<ColorsIn<{
readonly height: 40;
}>>

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

u
.
height: UniformNode<"float">
height
)))
})
})

Put more than one effect on an element

List several names in data-dio. Every element has one picture, and its effects share it in the order of the tokens: each uv function gets the UV of the one before, each color function gets the color of the one before.

index.html
<img
src="/cover.jpg"
data-dio="ripple torch"
data-dio-radius="0.5"
data-dio-torch-strength="0.8"
/>
  • data-dio-radius is short: only torch has a radius.
  • ripple and torch both have a strength, so the short data-dio-strength applies to neither and a development build warns. Name the effect: data-dio-torch-strength.
  • An effect runs once per element. Markup, app.attach() and a parent’s ctx.attach() with the same name on the same element share one instance.
  • Changing the order of the tokens rebuilds the material without running setup again.

Draw your own objects

The surface is one option. Any three.js object works through the context, and Diorama keeps it on the element and disposes it with the instance.

  • place(object, options) glues an object to the element’s box and returns it. With fit: 'box', the default, a 1 by 1 object is stretched to the box; 'contain' scales it by the smaller side, and 'none' keeps pixel units.
  • viewport is a group pinned to the viewport: its origin is the center of the screen, its units are pixels, and it does not scroll with the page. Use it for the background of a sticky section.
  • update, the function setup returns, runs in every drawn frame while the element is on screen. It gets input, the inputs as numbers, and the frame time.
src/effects/halo.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 {
class Mesh<
TGeometry extends BufferGeometry = BufferGeometry<
NormalBufferAttributes,
BufferGeometryEventMap
>,
TMaterial extends Material
| Material[] = Material<...>
| Material<...>[],
TEventMap extends Object3DEventMap = Object3DEventMap
>

Class representing triangular

https://en.wikipedia.org/wiki/Polygon_mesh polygon mesh

based objects.

@remarks ― Also serves as a base for other classes such as THREE.SkinnedMesh SkinnedMesh, THREE.InstancedMesh InstancedMesh.

@example

const geometry = new THREE.BoxGeometry(1, 1, 1);
const material = new THREE.MeshBasicMaterial({
color: 0xffff00
});
const Mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);

@see ― https://threejs.org/docs/index.html#api/en/objects/Mesh Official Documentation

@see ― https://github.com/mrdoob/three.js/blob/master/src/objects/Mesh.js Source

Mesh
,
class MeshBasicNodeMaterial

Node material version of

MeshBasicMaterial

.

MeshBasicNodeMaterial
,
class RingGeometry

A class for generating a two-dimensional ring geometry.

@example

const geometry = new THREE.RingGeometry(1, 5, 32);
const material = new THREE.MeshBasicMaterial({
color: 0xffff00,
side: THREE.DoubleSide
});
const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);

@see ― https://threejs.org/docs/index.html#api/en/geometries/RingGeometry Official Documentation

@see ― https://github.com/mrdoob/three.js/blob/master/src/geometries/RingGeometry.js Source

RingGeometry
} from 'three/webgpu'
export default
effect<P>(
params: P & CheckSpec<P>,
setup: EffectSetup<ColorsIn<P>>
): Effect<WidenSpec<P>> (+2 overloads)
type P = {
readonly speed: 1;
readonly color: "#ffd27a";
}

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
(
{
speed: 1
speed
: 1,
color: "#ffd27a"
color
: '#ffd27a' },
({
place: <T extends Object3D>(
object: T,
options?: PlaceOptions
) => T

Glues an object to the box so it follows layout and pose. Disposed with the instance.

place
,
params: Readonly<
ParamsOf<
ColorsIn<{ readonly speed: 1; readonly color: "#ffd27a"; }>
>
>

Live resolved values. Read them in update, a read in setup is a snapshot.

params
,
u: Uniforms<
ColorsIn<{ readonly speed: 1; readonly color: "#ffd27a"; }>
>

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

u
}) => {
const
const material: MeshBasicNodeMaterial
material
= new
new MeshBasicNodeMaterial(
parameters?: MeshBasicNodeMaterialParameters
): MeshBasicNodeMaterial

Constructs a new mesh basic node material.

@param ― parameters - The configuration parameter.

MeshBasicNodeMaterial
({
transparent?: boolean | undefined

Defines whether this material is transparent. This has an effect on rendering as transparent objects need special treatment and are rendered after non-transparent objects.

When set to true, the extent to which the material is transparent is controlled by

Material#opacity

.

@default ― false

transparent
: true })
const material: MeshBasicNodeMaterial
material
.
NodeMaterialNodeProperties.colorNode:
| Node<"float">
| Node<"vec2">
| Node<"vec3">
| Node<"vec4">
| Node<"color">
| null

The diffuse color of node materials is by default inferred from the color and map properties. This node property allows to overwrite the default and define the diffuse color with a node instead.

material.colorNode = color( 0xff0000 ); // define red color

If you don't want to overwrite the diffuse color but modify the existing values instead, use

materialColor

.

material.colorNode = materialColor.mul( color( 0xff0000 ) ); // give diffuse colors a red tint

@default ― null

colorNode
=
u: Uniforms<
ColorsIn<{ readonly speed: 1; readonly color: "#ffd27a"; }>
>

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

u
.
color: UniformNode<"color">
color
// Three quarters of a ring, so the turn shows.
const
const geometry: RingGeometry
geometry
= new
new RingGeometry(
innerRadius?: number,
outerRadius?: number,
thetaSegments?: number,
phiSegments?: number,
thetaStart?: number,
thetaLength?: number
): RingGeometry

Create a new instance of

RingGeometry

@param ― innerRadius Expects a Float. Default 0.5.

@param ― outerRadius Expects a Float. Default 1.

@param ― thetaSegments Number of segments. A higher number means the ring will be more round. Minimum is 3. Expects a Integer. Default 32.

@param ― phiSegments Number of segments per ring segment. Minimum is 1. Expects a Integer. Default 1.

@param ― thetaStart Starting angle. Expects a Float. Default 0.

@param ― thetaLength Central angle. Expects a Float. Default Math.PI * 2.

RingGeometry
(
0.46,
0.5,
64,
1,
0,
var Math: Math

An intrinsic object that provides basic mathematics functionality and constants.

Math
.
Math.PI: number

Pi. This is the ratio of the circumference of a circle to its diameter.

PI
* 1.5,
)
const
const ring: Mesh<
RingGeometry,
MeshBasicNodeMaterial,
Object3DEventMap
>
ring
=
place: <
Mesh<RingGeometry, MeshBasicNodeMaterial, Object3DEventMap>
>(
object: Mesh<
RingGeometry,
MeshBasicNodeMaterial,
Object3DEventMap
>,
options?: PlaceOptions
) => Mesh<RingGeometry, MeshBasicNodeMaterial, Object3DEventMap>

Glues an object to the box so it follows layout and pose. Disposed with the instance.

place
(new
new Mesh<
RingGeometry,
MeshBasicNodeMaterial,
Object3DEventMap
>(
geometry?: RingGeometry | undefined,
material?: MeshBasicNodeMaterial | undefined
): Mesh<RingGeometry, MeshBasicNodeMaterial, Object3DEventMap>

Create a new instance of

Mesh

@param ― geometry An instance of THREE.BufferGeometry BufferGeometry. Default THREE.BufferGeometry new THREE.BufferGeometry().

@param ― material A single or an array of THREE.Material Material. Default THREE.MeshBasicMaterial new THREE.MeshBasicMaterial().

Mesh
(
const geometry: RingGeometry
geometry
,
const material: MeshBasicNodeMaterial
material
))
return (
input: EffectInput
input
) => {
const ring: Mesh<
RingGeometry,
MeshBasicNodeMaterial,
Object3DEventMap
>
ring
.
Object3D<
TEventMap extends Object3DEventMap = Object3DEventMap
>.rotation: Euler

Object's local rotation (

https://en.wikipedia.org/wiki/Euler_angles Euler angles

), in radians.

@defaultValue ― new THREE.Euler() - that is (0, 0, 0, Euler.DEFAULT_ORDER).

rotation
.
Euler.z: number

@default ― 0

z
=
input: EffectInput
input
.
EffectInput.time: number

Seconds while visible, like u.time.

time
*
params: Readonly<
ParamsOf<
ColorsIn<{ readonly speed: 1; readonly color: "#ffd27a"; }>
>
>

Live resolved values. Read them in update, a read in setup is a snapshot.

params
.
speed: number
speed
}
},
)

Reading input.time or input.delta in update counts as a clock, so halo draws every frame while it is visible. An effect that only eases toward the pointer reads delta too: its update returns true while it still moves and false once it settles, and the answer decides in place of the clock, as the 10-minute tutorial shows. Read params in update or through u: a value read from params in setup is a snapshot of that moment.

Diorama disposes the geometry, materials and textures of placed objects with the instance. Pass { dispose: false } for shared ones, and hand textures and render targets that live inside TSL nodes to onDispose. signal aborts when the instance goes away, for fetch and other async work in setup. Setup may be async: the element shows its DOM until the promise settles.

Folded from one sheet

A paper lantern, cut and creased by hand.

Live preview needs WebGPU or WebGL 2. A round card with the halo effect: a thin three-quarter ring turns around it while it is on screen.

This example failed to draw.

Params of halo
<div class="card" data-dio="halo">
<h3>Folded from one sheet</h3>
<p>A paper lantern, cut and creased by hand.</p>
</div>
import { effect } from '@dioramajs/diorama'
import { Mesh, MeshBasicNodeMaterial, RingGeometry } from 'three/webgpu'
export default effect(
{ speed: 1, color: '#ffd27a' },
({ place, params, u }) => {
const material = new MeshBasicNodeMaterial({ transparent: true })
material.colorNode = u.color
// Three quarters of a ring, so the turn shows.
const geometry = new RingGeometry(
0.46,
0.5,
64,
1,
0,
Math.PI * 1.5,
)
const ring = place(new Mesh(geometry, material))
return (input) => {
ring.rotation.z = input.time * params.speed
}
},
)

Read the element’s picture yourself

With media: true the picture is ready before setup, and map is its texture. coverUV(uv) maps box UV to texture UV with the element’s object-fit, and mask(uv) is the alpha of radius, clip and CSS opacity. place(mesh, { replace: true }) makes your mesh the element’s picture instead of the surface.

src/effects/drift.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 texture: {
(
value?: Texture | TextureNode,
uvNode?: Node | null,
levelNode?: Node | number | null,
biasNode?: Node | null
): TextureNode;
<TNodeType>(
value?: Texture | TextureNode,
uvNode?: Node | null,
levelNode?: Node | number | null,
biasNode?: Node | null
): TextureNode<TNodeType>;
}
texture
,
const uv: (index?: number) => AttributeNode<"vec2">
uv
} from 'three/tsl'
import {
class Mesh<
TGeometry extends BufferGeometry = BufferGeometry<
NormalBufferAttributes,
BufferGeometryEventMap
>,
TMaterial extends Material
| Material[] = Material<...>
| Material<...>[],
TEventMap extends Object3DEventMap = Object3DEventMap
>

Class representing triangular

https://en.wikipedia.org/wiki/Polygon_mesh polygon mesh

based objects.

@remarks ― Also serves as a base for other classes such as THREE.SkinnedMesh SkinnedMesh, THREE.InstancedMesh InstancedMesh.

@example

const geometry = new THREE.BoxGeometry(1, 1, 1);
const material = new THREE.MeshBasicMaterial({
color: 0xffff00
});
const Mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);

@see ― https://threejs.org/docs/index.html#api/en/objects/Mesh Official Documentation

@see ― https://github.com/mrdoob/three.js/blob/master/src/objects/Mesh.js Source

Mesh
,
class MeshBasicNodeMaterial

Node material version of

MeshBasicMaterial

.

MeshBasicNodeMaterial
,
class PlaneGeometry

A class for generating plane geometries.

@example

const geometry = new THREE.PlaneGeometry(1, 1);
const material = new THREE.MeshBasicMaterial({
color: 0xffff00,
side: THREE.DoubleSide
});
const plane = new THREE.Mesh(geometry, material);
scene.add(plane);

@see ― https://threejs.org/docs/index.html#api/en/geometries/PlaneGeometry Official Documentation

@see ― https://github.com/mrdoob/three.js/blob/master/src/geometries/PlaneGeometry.js Source

PlaneGeometry
,
} from 'three/webgpu'
export default
effect<P>(
params: P & CheckSpec<P>,
options: EffectOptions<ColorsIn<P>>,
setup: EffectSetup<ColorsIn<P>>
): Effect<WidenSpec<P>> (+2 overloads)
type P = {
readonly depth: 0.04;
}

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
(
{
depth: 0.04
depth
: 0.04 },
{
media?: boolean | "optional" | undefined

Needs the or as a texture. Implies on: 'img, video', ready before setup. 'optional' reads it when the element is one and runs without it on other elements.

media
: true },
({
map: Texture<unknown, TextureEventMap>

The or as a texture, shared by all effects on the element. Needs media: true.

map
,
coverUV: (uv?: Node<"vec2">) => Node<"vec2">

Maps box UV to texture UV with the element's object-fit and object-position. Defaults to uv().

coverUV
,
mask: (uv?: Node<"vec2">) => Node<"float">

0..1 alpha mask at box UV for own materials: contain bars, radius, overflow clip, opacity.

mask
,
place: <T extends Object3D>(
object: T,
options?: PlaceOptions
) => T

Glues an object to the box so it follows layout and pose. Disposed with the instance.

place
,
u: Uniforms<ColorsIn<{
readonly depth: 0.04;
}>>

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

u
}) => {
const
const shift: Node<"vec2">
shift
=
u: Uniforms<ColorsIn<{
readonly depth: 0.04;
}>>

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

u
.
InputUniforms.pointer: UniformNode<"vec2">

Pointer in element UV, y up like uv(). 0..1 inside the box, not clamped outside.

pointer
.
NumVec2Extensions<"float">.sub: AddSubMulDivNumberVecVec2Extensions
(
b: Vec2OrLessOrNumberParameterForNumType<"float">,
...params: Vec2OrLessOrNumberParameterForNumType<"float">[]
) => Node<"vec2"> (+2 overloads)
sub
(0.5).
mul: (
b: Vec2OrLessOrNumberParameterForNumType<"float">,
...params: Vec2OrLessOrNumberParameterForNumType<"float">[]
) => Node<"vec2"> (+6 overloads)
mul
(
u: Uniforms<ColorsIn<{
readonly depth: 0.04;
}>>

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

u
.
depth: UniformNode<"float">
depth
)
const
const material: MeshBasicNodeMaterial
material
= new
new MeshBasicNodeMaterial(
parameters?: MeshBasicNodeMaterialParameters
): MeshBasicNodeMaterial

Constructs a new mesh basic node material.

@param ― parameters - The configuration parameter.

MeshBasicNodeMaterial
({
transparent?: boolean | undefined

Defines whether this material is transparent. This has an effect on rendering as transparent objects need special treatment and are rendered after non-transparent objects.

When set to true, the extent to which the material is transparent is controlled by

Material#opacity

.

@default ― false

transparent
: true })
const material: MeshBasicNodeMaterial
material
.
NodeMaterialNodeProperties.colorNode:
| Node<"vec2">
| Node<"float">
| Node<"color">
| Node<"vec3">
| Node<"vec4">
| null

The diffuse color of node materials is by default inferred from the color and map properties. This node property allows to overwrite the default and define the diffuse color with a node instead.

material.colorNode = color( 0xff0000 ); // define red color

If you don't want to overwrite the diffuse color but modify the existing values instead, use

materialColor

.

material.colorNode = materialColor.mul( color( 0xff0000 ) ); // give diffuse colors a red tint

@default ― null

colorNode
=
function texture(
value?: Texture | TextureNode,
uvNode?: Node | null,
levelNode?: Node | number | null,
biasNode?: Node | null
): TextureNode (+1 overload)
texture
(
map: Texture<unknown, TextureEventMap>

The or as a texture, shared by all effects on the element. Needs media: true.

map
,
coverUV: (uv?: Node<"vec2">) => Node<"vec2">

Maps box UV to texture UV with the element's object-fit and object-position. Defaults to uv().

coverUV
(
function uv(index?: number): AttributeNode<"vec2">
uv
().
add: (
b: Vec2OrLessOrNumberParameterForNumType<"float">,
...params: Vec2OrLessOrNumberParameterForNumType<"float">[]
) => Node<"vec2"> (+3 overloads)
add
(
const shift: Node<"vec2">
shift
)))
const material: MeshBasicNodeMaterial
material
.
NodeMaterialNodeProperties.opacityNode: Node | null

The opacity of node materials is by default inferred from the opacity and alphaMap properties. This node property allows to overwrite the default and define the opacity with a node instead.

If you don't want to overwrite the opacity but modify the existing value instead, use

materialOpacity

.

@default ― null

opacityNode
=
mask: (uv?: Node<"vec2">) => Node<"float">

0..1 alpha mask at box UV for own materials: contain bars, radius, overflow clip, opacity.

mask
()
const
const plane: Mesh<
PlaneGeometry,
MeshBasicNodeMaterial,
Object3DEventMap
>
plane
= new
new Mesh<
PlaneGeometry,
MeshBasicNodeMaterial,
Object3DEventMap
>(
geometry?: PlaneGeometry | undefined,
material?: MeshBasicNodeMaterial | undefined
): Mesh<PlaneGeometry, MeshBasicNodeMaterial, Object3DEventMap>

Create a new instance of

Mesh

@param ― geometry An instance of THREE.BufferGeometry BufferGeometry. Default THREE.BufferGeometry new THREE.BufferGeometry().

@param ― material A single or an array of THREE.Material Material. Default THREE.MeshBasicMaterial new THREE.MeshBasicMaterial().

Mesh
(new
new PlaneGeometry(
width?: number,
height?: number,
widthSegments?: number,
heightSegments?: number
): PlaneGeometry

Create a new instance of

PlaneGeometry

@param ― width Width along the X axis. Expects a Float. Default 1

@param ― height Height along the Y axis. Expects a Float. Default 1

@param ― widthSegments Number of segmented faces along the width of the sides. Expects a Integer. Default 1

@param ― heightSegments Number of segmented faces along the height of the sides. Expects a Integer. Default 1

PlaneGeometry
(1, 1),
const material: MeshBasicNodeMaterial
material
)
place: <
Mesh<PlaneGeometry, MeshBasicNodeMaterial, Object3DEventMap>
>(
object: Mesh<
PlaneGeometry,
MeshBasicNodeMaterial,
Object3DEventMap
>,
options?: PlaceOptions
) => Mesh<
PlaneGeometry,
MeshBasicNodeMaterial,
Object3DEventMap
>

Glues an object to the box so it follows layout and pose. Disposed with the instance.

place
(
const plane: Mesh<
PlaneGeometry,
MeshBasicNodeMaterial,
Object3DEventMap
>
plane
, {
PlaceOptions.replace?: boolean | undefined

Use this object as the element's GL copy instead of the surface mesh.

replace
: true })
},
)

media: true limits the effect to <img> and <video>; on another element it is skipped with EFFECT_TARGET. Without it, map throws. Apart from the context the file is plain three.js, so it moves to another project with few changes.

Compose child elements

layers(selector) returns the layers of matching descendants in document order and keeps the array up to date. pose(layer, target, weight) blends one of them from its layout toward a target for the current frame. Call it in update; a layer you stop posing returns to its layout by itself.

src/effects/wheel.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'
export default
effect<P>(
params: P & CheckSpec<P>,
setup: EffectSetup<ColorsIn<P>>
): Effect<WidenSpec<P>> (+2 overloads)
type P = {
readonly items: "img";
readonly radius: 600;
}

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
(
{
items: "img"
items
: 'img',
radius: 600
radius
: 600 },
({
layers: (selector: string) => readonly Layer[]

Live layers of matching descendants in document order. The array updates in place.

layers
,
params: Readonly<
ParamsOf<
ColorsIn<{ readonly items: "img"; readonly radius: 600; }>
>
>

Live resolved values. Read them in update, a read in setup is a snapshot.

params
,
pose: (
layer: Layer,
target: PoseTarget,
weight: number
) => void

Blends a layer from its layout towards target by weight for this frame. Call from update. A taken copy wins: with a box only the pose's above applies, and the copy's own radius, clip and non-zero above beat the pose's.

pose
}) => {
const
const items: readonly Layer[]
items
=
layers: (selector: string) => readonly Layer[]

Live layers of matching descendants in document order. The array updates in place.

layers
(
params: Readonly<
ParamsOf<
ColorsIn<{ readonly items: "img"; readonly radius: 600; }>
>
>

Live resolved values. Read them in update, a read in setup is a snapshot.

params
.
items: string
items
)
return ({
pin: number

Pin progress, like u.pin.

pin
}) => {
const
const r: number
r
=
params: Readonly<
ParamsOf<
ColorsIn<{ readonly items: "img"; readonly radius: 600; }>
>
>

Live resolved values. Read them in update, a read in setup is a snapshot.

params
.
radius: number
radius
const items: readonly Layer[]
items
.
ReadonlyArray<Layer>.forEach(
callbackfn: (
value: Layer,
index: number,
array: readonly Layer[]
) => void,
thisArg?: any
): void

Performs the specified action for each element in an array.

@param ― callbackfn A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.

@param ― thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.

forEach
((
item: Layer
item
,
i: number
i
) => {
const
const angle: number
angle
= (
i: number
i
/
const items: readonly Layer[]
items
.
ReadonlyArray<Layer>.length: number

Gets the length of the array. This is a number one higher than the highest element defined in an array.

length
) *
var Math: Math

An intrinsic object that provides basic mathematics functionality and constants.

Math
.
Math.PI: number

Pi. This is the ratio of the circumference of a circle to its diameter.

PI
* 2 +
pin: number

Pin progress, like u.pin.

pin
*
var Math: Math

An intrinsic object that provides basic mathematics functionality and constants.

Math
.
Math.PI: number

Pi. This is the ratio of the circumference of a circle to its diameter.

PI
const
const x: number
x
=
var Math: Math

An intrinsic object that provides basic mathematics functionality and constants.

Math
.
Math.sin(x: number): number

Returns the sine of a number.

@param ― x A numeric expression that contains an angle measured in radians.

sin
(
const angle: number
angle
) *
const r: number
r
const
const z: number
z
=
var Math: Math

An intrinsic object that provides basic mathematics functionality and constants.

Math
.
Math.cos(x: number): number

Returns the cosine of a number.

@param ― x A numeric expression that contains an angle measured in radians.

cos
(
const angle: number
angle
) *
const r: number
r
-
const r: number
r
pose: (
layer: Layer,
target: PoseTarget,
weight: number
) => void

Blends a layer from its layout towards target by weight for this frame. Call from update. A taken copy wins: with a box only the pose's above applies, and the copy's own radius, clip and non-zero above beat the pose's.

pose
(
item: Layer
item
, {
PoseTarget.x?: number | undefined

Centre x in world px.

x
,
PoseTarget.z?: number | undefined

Depth in world px, towards the viewer.

z
,
PoseTarget.rotateY?: number | undefined

Rotation around y in radians.

rotateY
:
const angle: number
angle
},
pin: number

Pin progress, like u.pin.

pin
)
})
}
},
)
index.html
<section
class="sticky"
data-dio="wheel"
data-dio-radius="480"
>
<img src="/work/aurora.jpg" />
<img src="/work/ridges.jpg" />
<img src="/work/lantern.jpg" />
</section>

pin goes from 0 when the top of the section reaches the top of the viewport to 1 when its bottom reaches the bottom, so the images circle while the section is pinned. Targets are in world pixels with the viewport center at 0,0 and y up; angles are in radians. Every matching image gets a plain picture for the pose, even without effects of its own.

Rebuild on a change

Uniform params change live. A string or oneOf param has no uniform, so a change runs setup again: in wheel, a new items selector picks other children. Name other params in rebuild when setup uses their value to build something, such as the number of instances.

src/effects/sparks.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 {
class InstancedMesh<
TGeometry extends BufferGeometry = BufferGeometry<
NormalBufferAttributes,
BufferGeometryEventMap
>,
TMaterial extends Material
| Material[] = Material<...>
| Material<...>[],
TEventMap extends Object3DEventMap = Object3DEventMap
>

A special version of

THREE.Mesh Mesh

with instanced rendering support

@remarks ― Use InstancedMesh if you have to render a large number of objects with the same geometry and material(s) but with different world transformations The usage of InstancedMesh will help you to reduce the number of draw calls and thus improve the overall rendering performance in your application.

@see ― Example : https://threejs.org/examples/#webgl_instancing_dynamic WebGL / instancing / dynamic

@see ― Example : https://threejs.org/examples/#webgl_instancing_performance WebGL / instancing / performance

@see ― Example : https://threejs.org/examples/#webgl_instancing_scatter WebGL / instancing / scatter

@see ― Example : https://threejs.org/examples/#webgl_instancing_raycast WebGL / instancing / raycast

@see ― https://threejs.org/docs/index.html#api/en/objects/InstancedMesh Official Documentation

@see ― https://github.com/mrdoob/three.js/blob/master/src/objects/InstancedMesh.js Source

InstancedMesh
,
class Matrix4

Represents a 4x4 matrix.

The most common use of a 4x4 matrix in 3D computer graphics is as a transformation matrix. For an introduction to transformation matrices as used in WebGL, check out this tutorial

This allows a 3D vector representing a point in 3D space to undergo transformations such as translation, rotation, shear, scale, reflection, orthogonal or perspective projection and so on, by being multiplied by the matrix. This is known as applying the matrix to the vector.

A Note on Row-Major and Column-Major Ordering:

The constructor and

Matrix3#set

method take arguments in row-major order, while internally they are stored in the

Matrix3#elements

array in column-major order. This means that calling:

const m = new THREE.Matrix4();
m.set( 11, 12, 13, 14,
21, 22, 23, 24,
31, 32, 33, 34,
41, 42, 43, 44 );

will result in the elements array containing:

m.elements = [ 11, 21, 31, 41,
12, 22, 32, 42,
13, 23, 33, 43,
14, 24, 34, 44 ];

and internally all calculations are performed using column-major ordering. However, as the actual ordering makes no difference mathematically and most people are used to thinking about matrices in row-major order, the three.js documentation shows matrices in row-major order. Just bear in mind that if you are reading the source code, you'll have to take the transpose of any matrices outlined here to make sense of the calculations.

Matrix4
,
class MeshBasicNodeMaterial

Node material version of

MeshBasicMaterial

.

MeshBasicNodeMaterial
,
class PlaneGeometry

A class for generating plane geometries.

@example

const geometry = new THREE.PlaneGeometry(1, 1);
const material = new THREE.MeshBasicMaterial({
color: 0xffff00,
side: THREE.DoubleSide
});
const plane = new THREE.Mesh(geometry, material);
scene.add(plane);

@see ― https://threejs.org/docs/index.html#api/en/geometries/PlaneGeometry Official Documentation

@see ― https://github.com/mrdoob/three.js/blob/master/src/geometries/PlaneGeometry.js Source

PlaneGeometry
,
} from 'three/webgpu'
const
const spread: () => number
spread
= () => (
var Math: Math

An intrinsic object that provides basic mathematics functionality and constants.

Math
.
Math.random(): number

Returns a pseudorandom number between 0 and 1.

random
() - 0.5) * 400
export default
effect<P>(
params: P & CheckSpec<P>,
options: EffectOptions<ColorsIn<P>>,
setup: EffectSetup<ColorsIn<P>>
): Effect<WidenSpec<P>> (+2 overloads)
type P = {
readonly count: 40;
readonly color: "#ffd27a";
}

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
(
{
count: 40
count
: 40,
color: "#ffd27a"
color
: '#ffd27a' },
{
rebuild?: readonly ("count" | "color")[] | undefined

Params whose change re-runs setup. String and choice params always do.

rebuild
: ['count'] },
({
place: <T extends Object3D>(
object: T,
options?: PlaceOptions
) => T

Glues an object to the box so it follows layout and pose. Disposed with the instance.

place
,
params: Readonly<
ParamsOf<
ColorsIn<{ readonly count: 40; readonly color: "#ffd27a"; }>
>
>

Live resolved values. Read them in update, a read in setup is a snapshot.

params
,
u: Uniforms<
ColorsIn<{ readonly count: 40; readonly color: "#ffd27a"; }>
>

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

u
}) => {
const
const material: MeshBasicNodeMaterial
material
= new
new MeshBasicNodeMaterial(
parameters?: MeshBasicNodeMaterialParameters
): MeshBasicNodeMaterial

Constructs a new mesh basic node material.

@param ― parameters - The configuration parameter.

MeshBasicNodeMaterial
()
const material: MeshBasicNodeMaterial
material
.
NodeMaterialNodeProperties.colorNode:
| Node<"float">
| Node<"vec2">
| Node<"vec3">
| Node<"vec4">
| Node<"color">
| null

The diffuse color of node materials is by default inferred from the color and map properties. This node property allows to overwrite the default and define the diffuse color with a node instead.

material.colorNode = color( 0xff0000 ); // define red color

If you don't want to overwrite the diffuse color but modify the existing values instead, use

materialColor

.

material.colorNode = materialColor.mul( color( 0xff0000 ) ); // give diffuse colors a red tint

@default ― null

colorNode
=
u: Uniforms<
ColorsIn<{ readonly count: 40; readonly color: "#ffd27a"; }>
>

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

u
.
color: UniformNode<"color">
color
const
const plane: PlaneGeometry
plane
= new
new PlaneGeometry(
width?: number,
height?: number,
widthSegments?: number,
heightSegments?: number
): PlaneGeometry

Create a new instance of

PlaneGeometry

@param ― width Width along the X axis. Expects a Float. Default 1

@param ― height Height along the Y axis. Expects a Float. Default 1

@param ― widthSegments Number of segmented faces along the width of the sides. Expects a Integer. Default 1

@param ― heightSegments Number of segmented faces along the height of the sides. Expects a Integer. Default 1

PlaneGeometry
(6, 6)
const
const sparks: InstancedMesh<
PlaneGeometry,
MeshBasicNodeMaterial,
Object3DEventMap
>
sparks
= new
new InstancedMesh<
PlaneGeometry,
MeshBasicNodeMaterial,
Object3DEventMap
>(
geometry: PlaneGeometry | undefined,
material: MeshBasicNodeMaterial | undefined,
count: number
): InstancedMesh<
PlaneGeometry,
MeshBasicNodeMaterial,
Object3DEventMap
>

Create a new instance of

InstancedMesh

@param ― geometry An instance of BufferGeometry.

@param ― material A single or an array of Material. Default is a new MeshBasicMaterial .

@param ― count The maximum number of instances of this Mesh. Expects a Integer

InstancedMesh
(
const plane: PlaneGeometry
plane
,
const material: MeshBasicNodeMaterial
material
,
params: Readonly<
ParamsOf<
ColorsIn<{ readonly count: 40; readonly color: "#ffd27a"; }>
>
>

Live resolved values. Read them in update, a read in setup is a snapshot.

params
.
count: number
count
)
const
const matrix: Matrix4
matrix
= new
new Matrix4(): Matrix4 (+1 overload)

Constructs a new 4x4 matrix. This constructor initializes the matrix as an identity matrix.

Matrix4
()
for (let
let i: number
i
= 0;
let i: number
i
<
params: Readonly<
ParamsOf<
ColorsIn<{ readonly count: 40; readonly color: "#ffd27a"; }>
>
>

Live resolved values. Read them in update, a read in setup is a snapshot.

params
.
count: number
count
;
let i: number
i
++) {
const matrix: Matrix4
matrix
.
Matrix4.makeTranslation(
x: number,
y: number,
z: number
): Matrix4 (+1 overload)

Sets this matrix as a translation transform from the given vector.

@param ― x - The amount to translate in the X axis.

@param ― y - The amount to translate in the Y axis.

@param ― z - The amount to translate in the z axis.

@return ― A reference to this matrix.

makeTranslation
(
const spread: () => number
spread
(),
const spread: () => number
spread
(), 0)
const sparks: InstancedMesh<
PlaneGeometry,
MeshBasicNodeMaterial,
Object3DEventMap
>
sparks
.
InstancedMesh<
PlaneGeometry,
MeshBasicNodeMaterial,
Object3DEventMap
>.setMatrixAt(index: number, matrix: Matrix4): InstancedMesh<
PlaneGeometry,
MeshBasicNodeMaterial,
Object3DEventMap
>

Sets the given local transformation matrix to the defined instance. Make sure you set

InstancedBufferAttribute.needsUpdate .instanceMatrix.needsUpdate()

flag to true after updating all the matrices. Negatively scaled matrices are not supported.

@param ― index The index of an instance. Values have to be in the range [0, count]. Expects a Integer

@param ― matrix A 4x4 matrix representing the local transformation of a single instance.

setMatrixAt
(
let i: number
i
,
const matrix: Matrix4
matrix
)
}
place: <
InstancedMesh<
PlaneGeometry,
MeshBasicNodeMaterial,
Object3DEventMap
>
>(
object: InstancedMesh<
PlaneGeometry,
MeshBasicNodeMaterial,
Object3DEventMap
>,
options?: PlaceOptions
) => InstancedMesh<
PlaneGeometry,
MeshBasicNodeMaterial,
Object3DEventMap
>

Glues an object to the box so it follows layout and pose. Disposed with the instance.

place
(
const sparks: InstancedMesh<
PlaneGeometry,
MeshBasicNodeMaterial,
Object3DEventMap
>
sparks
, {
PlaceOptions.fit?:
| "none"
| "box"
| "contain"
| undefined

'box' (default) stretches a 1x1 object to the box, 'contain' scales it by min(width, height), 'none' keeps px units.

fit
: 'none' })
},
)

A rebuild runs in two phases: the old setup keeps drawing until the new one has compiled, so the element does not flash. It happens only while the element is visible, and changes in between merge into one. Rebuilds are not for tweens: a param that rebuilds more than 3 times a second gets a warning in development builds.

Know what happens on failure

A broken effect never breaks the page. Its element goes back to plain HTML, the other effects keep running, and the app reports an error with a code.

What failsCodeWhat you see
The definition: a bad param name or defaultEFFECT_DEFINITIONeffect() throws when the module loads
An unknown name in data-dioEFFECT_UNKNOWNThe element waits; a later register() picks it up
A param value of the wrong typeEFFECT_PARAMA warning; the value falls back to the layer below
Setup throws or rejectsEFFECT_SETUPPlain HTML, data-dio-state="failed"
update throwsEFFECT_UPDATEPlain HTML from the first throw
The shader does not compileEFFECT_COMPILEThe last good material stays, or plain HTML
The image does not decode or is cross-originEFFECT_MEDIA, EFFECT_CORSPlain HTML until the image loads a new source or crossorigin changes

A failed instance stays failed until it gets a new start: remove the name from data-dio and add it back, or save the file in development and the edit replaces it. To react in code, listen to status changes.

src/main.ts
import {
function start(options?: StartOptions): App

Starts Diorama on the page and returns the app at once. It finishes starting in the background. A second call returns the same app, so a re-run module doesn't make another.

import { start } from '@dioramajs/diorama'
export const app = start()

start
} from '@dioramajs/diorama'
export const
const app: App
app
=
function start(options?: StartOptions): App

Starts Diorama on the page and returns the app at once. It finishes starting in the background. A second call returns the same app, so a re-run module doesn't make another.

import { start } from '@dioramajs/diorama'
export const app = start()

start
()
const app: App
app
.
AppCore<Services & Empty, string>.on<"effects:status">(
event: "effects:status",
listener: Listener<"effects:status">
): Off

Listens to an event. Returns Off.

on
('effects:status', ({
element: Element
element
,
effect: string
effect
,
status: InstanceStatus
status
,
error: unknown
error
}) => {
if (
status: InstanceStatus
status
=== 'failed')
var console: Console
console
.
Console.warn(...data: any[]): void

The console.warn() static method outputs a warning message to the console at the "warning" log level. The message is only displayed to the user if the console is configured to display warning output. In most cases, the log level is configured within the console UI. The message may receive special formatting, such as yellow colors and a warning icon.

MDN Reference

warn
(
effect: string
effect
,
element: Element
element
,
error: unknown
error
)
})

app.effects.get(element, name) returns the instance of one element with its status signal and error.

The whole file

src/effects/torch.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 smoothstep: Smoothstep
smoothstep
,
const vec2: Vec2Function
vec2
,
const vec4: Vec4Function
vec4
} from 'three/tsl'
import type {
type Node<TNodeType = unknown> = NodeClass<NodeClassEventMap>
& NodeElements
& (unknown extends TNodeType ? {} : NodeExtensions<TNodeType>)
& NodeExtras[TNodeType & string]
& { __TypeScript_NODE_TYPE__: TNodeType; }
const Node: {
new <TNodeType>(nodeType?: TNodeType | null): Node<TNodeType>;
new (nodeType?: string | null): Node;
captureStackTrace: boolean;
}
Node
} from 'three/webgpu'
export default
effect<P>(
params: P & CheckSpec<P>,
options: EffectOptions<ColorsIn<P>>,
setup: EffectSetup<ColorsIn<P>>
): Effect<...> (+2 overloads)
type P = {
readonly radius: 0.35;
readonly strength: 0.6;
readonly zoom: 0.2;
readonly tint: "#ffd27a";
}

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
(
{
radius: 0.35
radius
: 0.35,
strength: 0.6
strength
: 0.6,
zoom: 0.2
zoom
: 0.2,
tint: "#ffd27a"
tint
: '#ffd27a' },
{
when?: When | undefined

Media query the page must match or the instance is off. 'hover' means '(hover: hover)'.

when
: 'hover' },
({
surface: Surface

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

surface
,
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly zoom: 0.2;
readonly tint: "#ffd27a";
}
>
>

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

u
}) => {
const
const aspect: JoinNode<"vec2">
aspect
=
function vec2(
x: Scalar,
y: Scalar
): JoinNode<"vec2"> (+4 overloads)
vec2
(
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly zoom: 0.2;
readonly tint: "#ffd27a";
}
>
>

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

u
.
InputUniforms.size: UniformNode<"vec2">

Box size in CSS px.

size
.
Swizzle1From1<"float">.x: Node<"float">
x
.
NumExtensions<"float">.div: AddSubMulDivNumberVecNumExtensions
(
b: NumberParameterForNumType<"float">,
...params: NumberParameterForNumType<"float">[]
) => Node<"float"> (+3 overloads)
div
(
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly zoom: 0.2;
readonly tint: "#ffd27a";
}
>
>

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

u
.
InputUniforms.size: UniformNode<"vec2">

Box size in CSS px.

size
.
Swizzle1From2<"float">.y: Node<"float">
y
), 1)
const
const inside: (uv: Node<"vec2">) => Node<"float">
inside
= (
uv: Node<"vec2">
uv
:
type Node<TNodeType = unknown> = NodeClass<NodeClassEventMap>
& NodeElements
& (unknown extends TNodeType ? {} : NodeExtensions<TNodeType>)
& NodeExtras[TNodeType & string]
& { __TypeScript_NODE_TYPE__: TNodeType; }
Node
<'vec2'>) => {
const
const distance: Node<"float">
distance
=
uv: Node<"vec2">
uv
.
NumVec2Extensions<"float">.sub: AddSubMulDivNumberVecVec2Extensions
(
b: Vec2OrLessOrNumberParameterForNumType<"float">,
...params: Vec2OrLessOrNumberParameterForNumType<"float">[]
) => Node<"vec2"> (+2 overloads)
sub
(
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly zoom: 0.2;
readonly tint: "#ffd27a";
}
>
>

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

u
.
InputUniforms.pointer: UniformNode<"vec2">

Pointer in element UV, y up like uv(). 0..1 inside the box, not clamped outside.

pointer
).
mul: (
b: Vec2OrLessOrNumberParameterForNumType<"float">,
...params: Vec2OrLessOrNumberParameterForNumType<"float">[]
) => Node<"vec2"> (+6 overloads)
mul
(
const aspect: JoinNode<"vec2">
aspect
).
FloatOrVecExtensions<"vec2">.length: () => Node<"float">
length
()
const
const edge: Node<"float">
edge
=
function smoothstep(
low: FloatOrNumber,
high: FloatOrNumber,
x: FloatOrNumber
): Node<"float"> (+3 overloads)
smoothstep
(0,
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly zoom: 0.2;
readonly tint: "#ffd27a";
}
>
>

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

u
.
radius: UniformNode<"float">
radius
,
const distance: Node<"float">
distance
)
return
const edge: Node<"float">
edge
.
FloatOrVecExtensions<"float">.oneMinus: () => Node<"float">
oneMinus
().
mul: (
b: NumberParameterForNumType<"float">,
...params: NumberParameterForNumType<"float">[]
) => Node<"float"> (+7 overloads)
mul
(
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly zoom: 0.2;
readonly tint: "#ffd27a";
}
>
>

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

u
.
InputUniforms.hover: UniformNode<"float">

Damped 0..1 while the pointer is over the box. 0 under reduced motion.

hover
)
}
surface: Surface

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

surface
.
Surface.uv(transform: (uv: Node<"vec2">) => Node): Off

Bends box UV (0..1, y up) before sampling.

uv
((
uv: Node<"vec2">
uv
) => {
const
const pull: Node<"float">
pull
=
const inside: (uv: Node<"vec2">) => Node<"float">
inside
(
uv: Node<"vec2">
uv
).
mul: (
b: NumberParameterForNumType<"float">,
...params: NumberParameterForNumType<"float">[]
) => Node<"float"> (+7 overloads)
mul
(
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly zoom: 0.2;
readonly tint: "#ffd27a";
}
>
>

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

u
.
zoom: UniformNode<"float">
zoom
).
FloatOrVecExtensions<"float">.oneMinus: () => Node<"float">
oneMinus
()
return
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly zoom: 0.2;
readonly tint: "#ffd27a";
}
>
>

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

u
.
InputUniforms.pointer: UniformNode<"vec2">

Pointer in element UV, y up like uv(). 0..1 inside the box, not clamped outside.

pointer
.
add: (
b: Vec2OrLessOrNumberParameterForNumType<"float">,
...params: Vec2OrLessOrNumberParameterForNumType<"float">[]
) => Node<"vec2"> (+3 overloads)
add
(
uv: Node<"vec2">
uv
.
NumVec2Extensions<"float">.sub: AddSubMulDivNumberVecVec2Extensions
(
b: Vec2OrLessOrNumberParameterForNumType<"float">,
...params: Vec2OrLessOrNumberParameterForNumType<"float">[]
) => Node<"vec2"> (+2 overloads)
sub
(
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly zoom: 0.2;
readonly tint: "#ffd27a";
}
>
>

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

u
.
InputUniforms.pointer: UniformNode<"vec2">

Pointer in element UV, y up like uv(). 0..1 inside the box, not clamped outside.

pointer
).
mul: (
b: Vec2OrLessOrNumberParameterForNumType<"float">,
...params: Vec2OrLessOrNumberParameterForNumType<"float">[]
) => Node<"vec2"> (+6 overloads)
mul
(
const pull: Node<"float">
pull
))
})
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
, {
box: Node<"vec2">

Box UV before transforms.

box
}) => {
const
const light: Node<"float">
light
=
const inside: (uv: Node<"vec2">) => Node<"float">
inside
(
box: Node<"vec2">

Box UV before transforms.

box
).
mul: (
b: NumberParameterForNumType<"float">,
...params: NumberParameterForNumType<"float">[]
) => Node<"float"> (+7 overloads)
mul
(
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly zoom: 0.2;
readonly tint: "#ffd27a";
}
>
>

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

u
.
strength: UniformNode<"float">
strength
)
return
function vec4(
xyz:
| Node<"vec3">
| Node<"color">
| Vector3
| Color
| Node<"vec4">,
w: Scalar
): JoinNode<"vec4"> (+10 overloads)
vec4
(
color: Node<"vec4">
color
.
Swizzle3From3<"float">.rgb: Node<"vec3">
rgb
.
add: (
b: Vec3OrLessOrNumberParameterForNumType<"float">,
...params: Vec3OrLessOrNumberParameterForNumType<"float">[]
) => Node<"vec3"> (+2 overloads)
add
(
u: Uniforms<
ColorsIn<
{
readonly radius: 0.35;
readonly strength: 0.6;
readonly zoom: 0.2;
readonly tint: "#ffd27a";
}
>
>

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

u
.
tint: UniformNode<"color">
tint
.
ColorExtensions.mul: (
b: Number<"float">
) => Node<"vec3">
mul
(
const light: Node<"float">
light
)),
color: Node<"vec4">
color
.
Swizzle1From4<"float">.a: Node<"float">
a
)
})
},
)
index.html
<img src="/cover.jpg" data-dio="torch" />

effect() is the short form of defineEffect({ name, params, setup, ...options }), which packages use to set the name explicitly.