component
SteamVent
Steam off a manhole or out of a wall vent: soft puffs that rise, spread, lean and thin away, each a pure function of the world clock and `seed`. One instanced draw, plus the lid. Decorative: no collider.
⌖ Explore the library in 3D → · View as Markdown
Add it
npx @runek/cli add steam-vent
Pulls @react-three/fiber@^9.6.1, @runek/core@^0.18.0, three@^0.184.0.
Use it
import { SteamVent } from './runek/SteamVent'
<SteamVent position={[0, 0, 0]} puffs={20} rise={4} seed={1} />
Or as a node in a world file (worlds as data):
{ "type": "SteamVent", "props": { "position": [0, 0, 0], "puffs": 20, "rise": 4, "seed": 1 } }
Props
| Prop | Type | Default | Description |
|---|---|---|---|
position |
[x, y, z] |
[0, 0, 0] |
Where the steam comes out [x, y, z] in units: the middle of a manhole, or the mouth of a wall vent. |
rotation |
[x, y, z] |
[0, 0, 0] |
Rotation [x, y, z] in radians. |
seed |
number |
1 |
Sets each puff apart from the next, and this vent from every other. |
puffs |
number |
14 |
How many puffs are in the air at once. |
rise |
number |
3.2 |
How high a puff rises before it is gone, in units. |
spread |
number |
1.1 |
How far a puff has spread by then, in units. |
period |
number |
4.5 |
Seconds a puff takes to rise and thin out. |
color |
color |
"#c9d2e6" |
Steam color: it takes the color of the light around it. |
strength |
number |
1 |
How thick it is, 0 (nothing) to 1. |
grate |
boolean |
true |
Draw the manhole it comes from: a dark lid with a rim, radius across. |
radius |
number |
0.42 |
Radius of the lid and of the column of steam at its foot, in units. |
Registry manifest
registry manifest /r/components/steam-vent.json Self-contained JSON: inlined source plus resolved dependencies, exactly whatrunek add steam-vent fetches.
Browse the whole catalog in the gallery →.
Source (3 files)
SteamVent.tsx
import { useFrame } from '@react-three/fiber'
import { Dynamic, skipMirrors, useMaterial, useWorld, type Vec3 } from '@runek/core'
import { useEffect, useMemo, useRef } from 'react'
import {
AdditiveBlending,
Color,
CylinderGeometry,
DataTexture,
type InstancedMesh,
LinearFilter,
MeshBasicMaterial,
Object3D,
PlaneGeometry,
RGBAFormat,
TorusGeometry,
} from 'three'
import { emissiveColor, useLadder } from './city/glow'
import { hash2 } from './city/hash'
export interface SteamVentProps {
/** Where the steam comes out [x, y, z] in units: the middle of a manhole, or the mouth of a wall vent. */
position?: Vec3
/** Rotation [x, y, z] in radians. */
rotation?: Vec3
/** Sets each puff apart from the next, and this vent from every other. */
seed?: number
/** How many puffs are in the air at once. */
puffs?: number
/** How high a puff rises before it is gone, in units. */
rise?: number
/** How far a puff has spread by then, in units. */
spread?: number
/** Seconds a puff takes to rise and thin out. */
period?: number
/** Steam color: it takes the color of the light around it. */
color?: string
/** How thick it is, 0 (nothing) to 1. */
strength?: number
/** Draw the manhole it comes from: a dark lid with a rim, `radius` across. */
grate?: boolean
/** Radius of the lid and of the column of steam at its foot, in units. */
radius?: number
}
let soft: DataTexture | null = null
/** One soft round puff, drawn in code: white, with alpha falling off from the middle. */
function puffTexture(): DataTexture {
if (soft) return soft
const N = 64
const data = new Uint8Array(N * N * 4)
for (let y = 0; y < N; y++) {
for (let x = 0; x < N; x++) {
const r = Math.hypot(x - (N - 1) / 2, y - (N - 1) / 2) / (N / 2)
const a = Math.max(0, 1 - r)
const v = Math.round(255 * a * a * (3 - 2 * a))
data.set([v, v, v, 255], (y * N + x) * 4)
}
}
soft = new DataTexture(data, N, N, RGBAFormat)
soft.magFilter = soft.minFilter = LinearFilter
soft.needsUpdate = true
return soft
}
/**
* Steam off a manhole or out of a kitchen vent: a column of soft puffs that rise, spread, lean a
* little and thin away. Each puff's place is a pure function of the world clock and `seed`, so
* every player sees the same plume and nothing is simulated.
*
* One instanced draw of quads that face the camera, left out of the world's mirrors, plus the lid
* when `grate` is set. The steam is added light a little brighter than an unlit wall, so it shows
* where a lamp or a sign is behind it and barely at all against the dark. No collider.
*/
export function SteamVent({
position = [0, 0, 0],
rotation = [0, 0, 0],
seed = 1,
puffs = 14,
rise = 3.2,
spread = 1.1,
period = 4.5,
color = '#c9d2e6',
strength = 1,
grate = true,
radius = 0.42,
}: SteamVentProps) {
const { unit, palette, now = Date.now } = useWorld()
const ladder = useLadder()
const mesh = useRef<InstancedMesh>(null)
const count = Math.max(1, Math.round(puffs))
const quad = useMemo(() => new PlaneGeometry(1, 1), [])
const material = useMemo(
() =>
new MeshBasicMaterial({
map: puffTexture(),
transparent: true,
blending: AdditiveBlending,
depthWrite: false,
toneMapped: false,
}),
[],
)
useEffect(
() => () => {
quad.dispose()
material.dispose()
},
[quad, material],
)
const lid = useMaterial({
slot: 'metal.bare',
color: palette.metal,
roughness: 0.7,
metalness: 0.4,
})
// A puff at its thickest: a little over an unlit wall, so a column of them reads as haze.
const tone = useMemo(
() => emissiveColor(color, ladder.unlitWall[1] * 1.0 * Math.min(1, Math.max(0, strength))),
[color, ladder.unlitWall, strength],
)
useEffect(() => skipMirrors(mesh.current), [])
const at = useMemo(() => new Object3D(), [])
const tint = useMemo(() => new Color(), [])
useFrame(({ camera }) => {
const m = mesh.current
if (!m) return
const seconds = now() / 1000
const parent = m.parent
for (let i = 0; i < count; i++) {
const life = (((seconds / period + hash2(seed, i)) % 1) + 1) % 1
// Which time round this puff is on, so each one takes a path of its own.
const round = Math.floor(seconds / period + hash2(seed, i))
const lean = (hash2(seed * 7 + i, round) - 0.5) * 2
const turn = hash2(seed * 11 + i, round) * Math.PI * 2
const out = (radius * 0.6 + spread * 0.5 * life) * unit
at.position.set(
Math.cos(turn) * out * 0.5 + lean * life * life * 0.5 * unit,
rise * unit * life ** 0.8,
Math.sin(turn) * out * 0.5,
)
// Face the camera, whatever the vent's own turn.
at.quaternion.copy(camera.quaternion)
if (parent) at.quaternion.premultiply(parent.getWorldQuaternion(scratch).invert())
at.scale.setScalar((radius * 1.6 + spread * life) * unit)
at.updateMatrix()
m.setMatrixAt(i, at.matrix)
// Thin at the mouth, thickest a third of the way up, gone at the top.
const fade = Math.min(1, life * 5) * (1 - life) ** 1.5
m.setColorAt(i, tint.copy(tone).multiplyScalar(fade))
}
m.instanceMatrix.needsUpdate = true
if (m.instanceColor) m.instanceColor.needsUpdate = true
})
return (
<group position={position} rotation={rotation}>
{grate && (
<>
<mesh
position={[0, 0.012 * unit, 0]}
material={lid}
geometry={lidGeometry(radius * unit, unit)}
receiveShadow
/>
<mesh
position={[0, 0.02 * unit, 0]}
rotation={[Math.PI / 2, 0, 0]}
material={lid}
geometry={rimGeometry(radius * unit, unit)}
/>
</>
)}
<Dynamic>
<instancedMesh
ref={mesh}
args={[quad, material, count]}
frustumCulled={false}
renderOrder={3}
/>
</Dynamic>
</group>
)
}
const scratch = new Object3D().quaternion
const lids = new Map<string, CylinderGeometry>()
function lidGeometry(radius: number, unit: number) {
const key = `${radius.toFixed(3)}:${unit}`
let g = lids.get(key)
if (!g) {
g = new CylinderGeometry(radius, radius, 0.024 * unit, 24)
lids.set(key, g)
}
return g
}
const rims = new Map<string, TorusGeometry>()
function rimGeometry(radius: number, unit: number) {
const key = `${radius.toFixed(3)}:${unit}`
let g = rims.get(key)
if (!g) {
g = new TorusGeometry(radius * 1.06, 0.03 * unit, 6, 28)
rims.set(key, g)
}
return g
} city/glow.ts
// How bright each kind of city emitter is, as a ladder of rungs, each the lowest and highest
// luminance that kind may have, so a sign face is a sign face whatever its hue and a lamp lens
// always outshines it. The rungs are face values scaled by the world's luminance levels
// (`useLuminance()`): sign faces by `sign`, tube cores by `tube`, lamp lenses by `lamp`, and lit
// glass (windows, shopfronts, ad boards, and the light they leave on the pavement) by `screen`.
// At every level's default of 1 the brightest rungs sit around 1, so they read without bloom; a
// world with bloom raises `sign`, `tube` and `lamp` to make them bloom.
//
// Emissive colors are normalized to unit luminance and then scaled onto a rung, so the hue of a
// color never changes how bright it reads.
import {
DEFAULT_LUMINANCE,
glowLook,
type MaterialLook,
materialKey,
retainMaterial,
sharedMaterial,
useLuminance,
type WorldLuminance,
} from '@runek/core'
import { useMemo } from 'react'
import { Color, type Material } from 'three'
/** A rung of the ladder: the lowest and highest luminance that kind of emitter may have. */
export type Rung = readonly [number, number]
export interface Ladder {
/** A night sky dome lit from below by a city. Face value. */
skyDome: Rung
/** A wall with no lamp on it. Face value. */
unlitWall: Rung
/** Pavement lit by the glass and signs above it. */
streetUnderSign: Rung
/** A lit room seen through its window from the street. */
windowLit: Rung
/** Lit shopfront glass, and the white of an ad board. */
shopGlass: Rung
/** Sign faces and lit letters. */
signFace: Rung
/** The core of a neon tube. */
tubeCore: Rung
/** A street lamp's lens seen from below. */
lampLens: Rung
}
const scale = (r: Rung, k: number): Rung => [r[0] * k, r[1] * k]
/** The rungs at every luminance level's face value (1). */
export const FACE_LADDER: Ladder = {
skyDome: [0.018, 0.055],
unlitWall: [0.02, 0.05],
streetUnderSign: [0.1, 0.5],
windowLit: [0.15, 0.6],
shopGlass: [0.4, 1.2],
signFace: [0.6, 1.6],
tubeCore: [0.6, 1.7],
lampLens: [0.67, 1.5],
}
/** The ladder for a world's luminance levels. */
export function ladderFor(l: WorldLuminance): Ladder {
const f = FACE_LADDER
return {
skyDome: f.skyDome,
unlitWall: f.unlitWall,
streetUnderSign: scale(f.streetUnderSign, l.screen),
windowLit: scale(f.windowLit, l.screen),
shopGlass: scale(f.shopGlass, l.screen),
signFace: scale(f.signFace, l.sign),
tubeCore: scale(f.tubeCore, l.tube),
lampLens: scale(f.lampLens, l.lamp),
}
}
/** The ladder at the default levels. */
export const DEFAULT_LADDER: Ladder = ladderFor(DEFAULT_LUMINANCE)
/** The world's ladder, from its luminance levels. */
export function useLadder(): Ladder {
const { screen, sign, tube, lamp, flame } = useLuminance()
return useMemo(
() => ladderFor({ screen, sign, tube, lamp, flame }),
[screen, sign, tube, lamp, flame],
)
}
/** The typical value of a rung: the geometric middle, since luminance is perceived on a log scale. */
export function mid(rung: Rung): number {
return Math.sqrt(rung[0] * rung[1])
}
/** A point along a rung, 0 at its bottom and 1 at its top, spaced evenly on a log scale. */
export function along(rung: Rung, t: number): number {
return rung[0] * (rung[1] / rung[0]) ** t
}
/** Relative luminance (Rec. 709) of a linear color. */
export function luminanceOf(c: Color): number {
return 0.2126 * c.r + 0.7152 * c.g + 0.0722 * c.b
}
/** `hex` at unit luminance times `luminance`, as a linear (HDR) color. */
export function emissiveColor(hex: string, luminance: number): Color {
const c = new Color(hex)
return c.multiplyScalar(luminance / Math.max(1e-4, luminanceOf(c)))
}
/** A shared look's material, kept for the life of the page: city emitters come back often. */
function kept(look: MaterialLook): Material {
const key = materialKey(look)
const material = sharedMaterial(look, key)
if (!material.userData.kept) {
material.userData.kept = true
retainMaterial(key)
}
return material
}
/**
* The shared unlit material for an emitter of color `hex` at `luminance`, through core's
* `glowLook`, so it batches with every other part of that look. Read-only.
*/
export function glowMaterial(hex: string, luminance: number, opacity = 1): Material {
return kept(
glowLook(
emissiveColor(hex, 1),
luminance,
opacity < 1 ? { transparent: true, opacity, depthWrite: false } : undefined,
),
)
}
/**
* The shared unlit material for picture content on a lit board: `hex` keeps its own value and
* is scaled so that white would shine at `white`. Dark backgrounds stay dark, bright text and
* shapes glow.
*/
export function screenMaterial(hex: string, white: number): Material {
return kept(glowLook(hex, white))
} city/hash.ts
/** A number in [0, 1) from two integers: the same on every machine, with no state. */
export function hash2(a: number, b: number): number {
let h = Math.imul(a | 0, 0x9e3779b1) ^ Math.imul(b | 0, 0x85ebca6b)
h = Math.imul(h ^ (h >>> 15), 0x2c1b3c6d)
h = Math.imul(h ^ (h >>> 12), 0x297a2d39)
return ((h ^ (h >>> 15)) >>> 0) / 4294967296
}