component
StreetLamp
A street lamp on an arm: a pole, a hot lens at the world's lamp luminance level, a haze cone under the head and a soft pool of light on the street, every lamp's cone and pool sharing two instanced draws. The real light comes from the world's light pool. One thin collider for the pole.
⌖ Explore the library in 3D → · View as Markdown
Add it
npx @runek/cli add street-lamp
Pulls @react-three/fiber@^9.6.1, @react-three/rapier@^2.2.0, @runek/core@^0.18.0, three@^0.184.0.
Use it
import { StreetLamp } from './runek/StreetLamp'
<StreetLamp position={[0, 0, 0]} height={6} arm={1.8} color="#ffa64d" />
Or as a node in a world file (worlds as data):
{ "type": "StreetLamp", "props": { "position": [0, 0, 0], "height": 6, "arm": 1.8, "color": "#ffa64d" } }
Props
| Prop | Type | Default | Description |
|---|---|---|---|
position |
[x, y, z] |
[0, 0, 0] |
Base of the pole [x, y, z] in units. The arm reaches along local +X. |
rotation |
[x, y, z] |
[0, 0, 0] |
Rotation [x, y, z] in radians. |
seed |
number |
Accepted for the component contract; this component has no seeded variation. | |
height |
number |
8 |
Pole height to the arm, in units. |
arm |
number |
2.4 |
How far the arm reaches out from the pole, in units. |
color |
color |
"#dfe8ff" |
Lamp color. A cool white by default: the city lights its streets like day. |
poleColor |
color |
Pole, arm and head color. Defaults to the world palette’s metal. |
|
intensity |
number |
350 |
Candela toward the street, when the world lends this lamp a real light. |
light |
boolean |
true |
Ask the world’s light pool for a real light. The lens, cone and pool glow either way. |
cone |
boolean |
true |
Draw the haze cone under the head. |
haze |
number |
0.06 |
Peak luminance of the haze cone (scene-linear), times the world’s lamp level. |
pool |
boolean |
true |
Draw the soft pool on the street under the head, for lamps the pool has no light for. |
poolRadius |
number |
6 |
Radius of that pool, in units. |
poolOn |
"ground" | "base" |
"ground" |
What the pool lies on. ground is the world’s ground, for a lamp on a pavement whose arm reaches out over the street below it. base is whatever the lamp itself stands on: a plaza, a lawn, a roof. |
Registry manifest
registry manifest /r/components/street-lamp.json Self-contained JSON: inlined source plus resolved dependencies, exactly whatrunek add street-lamp fetches.
Browse the whole catalog in the gallery →.
Source (5 files)
StreetLamp.tsx
import { CuboidCollider, RigidBody } from '@react-three/rapier'
import {
lazyModule,
useLight,
useLuminance,
useMaterial,
useNodeRenderer,
useWorld,
type Vec3,
} from '@runek/core'
import { useMemo } from 'react'
import { BoxGeometry, CylinderGeometry, Matrix4, PlaneGeometry } from 'three'
import { emissiveColor, glowMaterial, mid, useLadder } from './city/glow'
import { coneMaterial, poolMaterial } from './city/haze'
import { useInstances } from './city/instances'
// The haze's node (TSL) materials, loaded only under a node renderer.
const hazeNodes = lazyModule(() => import('./nodes/haze'))
export interface StreetLampProps {
/** Base of the pole [x, y, z] in units. The arm reaches along local +X. */
position?: Vec3
/** Rotation [x, y, z] in radians. */
rotation?: Vec3
/** Accepted for the component contract; this component has no seeded variation. */
seed?: number
/** Pole height to the arm, in units. */
height?: number
/** How far the arm reaches out from the pole, in units. */
arm?: number
/** Lamp color. A cool white by default: the city lights its streets like day. */
color?: string
/** Pole, arm and head color. Defaults to the world palette's `metal`. */
poleColor?: string
/** Candela toward the street, when the world lends this lamp a real light. */
intensity?: number
/** Ask the world's light pool for a real light. The lens, cone and pool glow either way. */
light?: boolean
/** Draw the haze cone under the head. */
cone?: boolean
/** Peak luminance of the haze cone (scene-linear), times the world's `lamp` level. */
haze?: number
/** Draw the soft pool on the street under the head, for lamps the pool has no light for. */
pool?: boolean
/** Radius of that pool, in units. */
poolRadius?: number
/** What the pool lies on. `ground` is the world's ground, for a lamp on a pavement whose arm
* reaches out over the street below it. `base` is whatever the lamp itself stands on: a plaza,
* a lawn, a roof. */
poolOn?: 'ground' | 'base'
}
/**
* A street lamp on an arm: a pole, a hot lens at the world's `lamp` luminance level, a haze cone
* under the head, and a soft pool on the street. The real light comes only from the world's
* light pool (`useLight`), which lends it to the lamps that matter most to the view; every other
* lamp still reads through its lens, cone, pool, bloom and fog. The pool fades in with distance
* so it does not double the real light on the nearest lamps. One thin collider for the pole.
*/
export function StreetLamp({
position = [0, 0, 0],
rotation = [0, 0, 0],
height = 8,
arm = 2.4,
color = '#dfe8ff',
poleColor,
intensity = 350,
light = true,
cone = true,
haze = 0.06,
pool = true,
poolRadius = 6,
poolOn = 'ground',
}: StreetLampProps) {
const { unit, palette, ground } = useWorld()
const ladder = useLadder()
const lamp = useLuminance().lamp
const nodes = useNodeRenderer() ? hazeNodes() : null
const h = height * unit
const a = arm * unit
const metal = useMaterial({
slot: 'metal.painted',
color: poleColor ?? palette.metal,
roughness: 0.55,
metalness: 0.15,
})
const lens = glowMaterial(color, mid(ladder.lampLens))
const lit = haze * lamp
const coneMat = useMemo(() => {
const c = emissiveColor(color, lit)
return nodes ? nodes.coneNodeMaterial(c) : coneMaterial(c)
}, [color, lit, nodes])
const poolMat = useMemo(() => {
const c = emissiveColor(color, ladder.streetUnderSign[0])
return nodes
? nodes.poolNodeMaterial(c, 20 * unit, 45 * unit)
: poolMaterial(c, 20 * unit, 45 * unit)
}, [color, ladder.streetUnderSign, unit, nodes])
const emitter = useLight({ color, intensity, distance: 26 * unit, decay: 2 }, light)
const coneH = h - 0.3 * unit
// The pool lies a little above the world's ground (the street), whatever the base height, or
// a little above what the lamp stands on.
const poolY = poolOn === 'base' ? 0.045 * unit : ground + 0.045 * unit - position[1]
// Every lamp's cone is an instance in one mesh and every pool in another (`useInstances`):
// both are see-through, so static batching leaves them alone, and a city lit by a hundred
// lamps would be two hundred draws of haze.
const coneAt = useMemo(
() => (cone ? [new Matrix4().makeTranslation(a, h - 0.25 * unit - coneH / 2, 0)] : []),
[cone, a, h, coneH, unit],
)
const poolAt = useMemo(
() => (pool ? [new Matrix4().makeRotationX(-Math.PI / 2).setPosition(a, poolY, 0)] : []),
[pool, a, poolY],
)
const place = [position, rotation]
const root = useInstances(
`lamp-cone:${coneMat.name}:${coneH}`,
() => ({ geometry: coneGeometry(coneH, unit), material: coneMat, renderOrder: 2 }),
coneAt,
null,
place,
)
const pools = useInstances(
`lamp-pool:${poolMat.name}:${poolRadius}`,
() => ({ geometry: plane(poolRadius * 2 * unit), material: poolMat, renderOrder: 1 }),
poolAt,
null,
place,
)
return (
<group ref={root} position={position} rotation={rotation}>
<group ref={pools} />
<RigidBody type="fixed" colliders={false}>
<CuboidCollider args={[0.1 * unit, h / 2, 0.1 * unit]} position={[0, h / 2, 0]} />
</RigidBody>
<mesh position={[0, h / 2, 0]} material={metal} geometry={poleGeometry(h, unit)} />
<mesh
position={[a / 2, h - 0.1 * unit, 0]}
material={metal}
geometry={box(a, 0.1 * unit, 0.12 * unit)}
/>
<mesh
position={[a, h - 0.12 * unit, 0]}
material={metal}
geometry={box(0.8 * unit, 0.16 * unit, 0.36 * unit)}
/>
<mesh
position={[a, h - 0.21 * unit, 0]}
material={lens}
geometry={box(0.62 * unit, 0.02 * unit, 0.24 * unit)}
/>
<object3D ref={emitter.ref} position={[a, h - 0.45 * unit, 0]} />
</group>
)
}
// Shared geometry: lamps along a street repeat the same few shapes.
const geometries = new Map<string, BoxGeometry | CylinderGeometry | PlaneGeometry>()
function cached<G extends BoxGeometry | CylinderGeometry | PlaneGeometry>(
key: string,
make: () => G,
): G {
let g = geometries.get(key) as G | undefined
if (!g) {
g = make()
geometries.set(key, g)
}
return g
}
const box = (w: number, h: number, d: number) =>
cached(`box:${w}:${h}:${d}`, () => new BoxGeometry(w, h, d))
const poleGeometry = (h: number, unit: number) =>
cached(`pole:${h}:${unit}`, () => new CylinderGeometry(0.07 * unit, 0.11 * unit, h, 12))
const coneGeometry = (h: number, unit: number) =>
cached(`cone:${h}:${unit}`, () => new CylinderGeometry(0.25 * unit, 3.4 * unit, h, 24, 1, true))
const plane = (size: number) => cached(`plane:${size}`, () => new PlaneGeometry(size, size))
StreetLamp.groundSitting = true 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/haze.ts
// Additive light that is not a real light: a lamp's haze cone and the pool it leaves on the
// street. One shared material per color and strength: a GLSL ShaderMaterial here, and its TSL
// twin for WebGPURenderer in nodes/haze.ts. Both work on instanced and merged geometry: they read
// only `uv`, normals and world positions.
//
// Additive light must fade into fog rather than mix toward it (mixing would add the fog color
// wherever the cone is), so they attenuate by the scene's linear fog range themselves.
import {
AdditiveBlending,
type Color,
DoubleSide,
type Material,
ShaderMaterial,
UniformsLib,
UniformsUtils,
} from 'three'
const HAZE_VERT = /* glsl */ `
varying vec3 vN;
varying vec3 vV;
varying vec2 vUv;
varying vec3 vWorld;
void main() {
// Lamps share one instanced mesh for their cones and one for their pools (StreetLamp.tsx), so
// each instance brings its own place and turn.
#ifdef USE_INSTANCING
vec4 world = modelMatrix * instanceMatrix * vec4(position, 1.0);
vec3 n = mat3(instanceMatrix) * normal;
#else
vec4 world = modelMatrix * vec4(position, 1.0);
vec3 n = normal;
#endif
vec4 mv = viewMatrix * world;
vN = normalize(normalMatrix * n);
vV = normalize(-mv.xyz);
vUv = uv;
vWorld = world.xyz;
gl_Position = projectionMatrix * mv;
}`
// The scene's linear fog range sets how far additive light carries, whatever fog chunk is in use.
export const FOG_FADE = /* glsl */ `
uniform float fogNear;
uniform float fogFar;
float hazeFog(vec3 world) {
#ifdef USE_FOG
return 1.0 - 0.94 * smoothstep(fogNear, fogFar, distance(world, cameraPosition));
#else
return 1.0;
#endif
}`
// Brightest where the cone is seen side on through its middle, falling to nothing at the top.
const CONE_FRAG = /* glsl */ `
uniform vec3 uColor;
varying vec3 vN;
varying vec3 vV;
varying vec2 vUv;
varying vec3 vWorld;
${FOG_FADE}
void main() {
float facing = pow(abs(dot(normalize(vN), normalize(vV))), 1.5);
float fall = vUv.y * vUv.y;
gl_FragColor = vec4(uColor * facing * fall * hazeFog(vWorld), 1.0);
}`
// A soft disc on the ground. It doubles in strength with distance, because near lamps usually
// hold a real light from the pool, which lights the street itself.
const POOL_FRAG = /* glsl */ `
uniform vec3 uColor;
uniform vec2 uFade;
varying vec2 vUv;
varying vec3 vWorld;
${FOG_FADE}
void main() {
float r = length(vUv - 0.5) * 2.0;
float disc = (1.0 - smoothstep(0.0, 1.0, r));
disc *= disc;
float far = mix(0.5, 1.0, smoothstep(uFade.x, uFade.y, distance(vWorld, cameraPosition)));
gl_FragColor = vec4(uColor * disc * far * hazeFog(vWorld), 1.0);
}`
const cache = new Map<string, Material>()
export function additive<M extends Material>(m: M): M {
m.transparent = true
m.blending = AdditiveBlending
m.depthWrite = false
return m
}
/** The haze cone under a lamp head, in `color` (linear, already scaled to its luminance). */
export function coneMaterial(color: Color): Material {
const key = `cone:${color.r.toFixed(4)},${color.g.toFixed(4)},${color.b.toFixed(4)}`
let m = cache.get(key)
if (!m) {
m = new ShaderMaterial({
vertexShader: HAZE_VERT,
fragmentShader: CONE_FRAG,
uniforms: UniformsUtils.merge([UniformsLib.fog, { uColor: { value: color.clone() } }]),
side: DoubleSide,
fog: true,
})
m.name = `runek:haze:${key}`
cache.set(key, additive(m))
}
return m
}
/** The pool a lamp leaves on the street, faded in between `near` and `far` meters from the eye. */
export function poolMaterial(color: Color, near: number, far: number): Material {
const key = `pool:${color.r.toFixed(4)},${color.g.toFixed(4)},${color.b.toFixed(4)}:${near}:${far}`
let m = cache.get(key)
if (!m) {
m = new ShaderMaterial({
vertexShader: HAZE_VERT,
fragmentShader: POOL_FRAG,
uniforms: UniformsUtils.merge([
UniformsLib.fog,
{ uColor: { value: color.clone() }, uFade: { value: [near, far] } },
]),
fog: true,
})
m.polygonOffset = true
m.polygonOffsetFactor = -2
m.polygonOffsetUnits = -4
m.name = `runek:haze:${key}`
cache.set(key, additive(m))
}
return m
} city/instances.ts
// Shared instanced meshes for small parts a world repeats hundreds of times and that static
// batching cannot merge: anything see-through. Runek's batcher leaves transparent meshes alone,
// so every window pane and every soft shell round a neon tube was a draw of its own.
//
// A pool is one `InstancedMesh` in the scene for one kind of part (one geometry, one material).
// Components hand it the places of their parts in the world and take them back when they
// unmount. Parts are still: a component gives its places again when it moves. However many
// parts there are, a pool is one draw, and it is never frustum culled.
import { useThree } from '@react-three/fiber'
import { skipMirrors } from '@runek/core'
import { type RefObject, useLayoutEffect, useMemo, useRef } from 'react'
import {
type BufferGeometry,
type Color,
type Group,
InstancedMesh,
type Material,
Matrix4,
type Scene,
} from 'three'
interface Parts {
matrices: Matrix4[]
/** One color for every part of this owner, when the pool colors its instances. */
color: Color | null
}
/** What a pool is made of, asked for once when the first part of its kind turns up. */
export interface PoolLook {
geometry: BufferGeometry
material: Material
/** Draw order among see-through things. */
renderOrder?: number
/** Leave the pool out of the world's mirrors (`skipMirrors`). */
skipMirrors?: boolean
}
class Pool {
private mesh: InstancedMesh | null = null
private unskip: (() => void) | null = null
private readonly owners = new Map<object, Parts>()
private queued = false
constructor(
private readonly scene: Scene,
private readonly name: string,
private readonly look: PoolLook,
) {}
/** Give `owner`'s parts their places in the world, replacing any it had. */
set(owner: object, matrices: Matrix4[], color: Color | null) {
this.owners.set(owner, { matrices, color })
this.queue()
}
drop(owner: object) {
if (this.owners.delete(owner)) this.queue()
}
/** Many components mount in one commit: write the buffers once, after all of them. */
private queue() {
if (this.queued) return
this.queued = true
queueMicrotask(() => {
this.queued = false
this.write()
})
}
private write() {
let total = 0
for (const parts of this.owners.values()) total += parts.matrices.length
let mesh = this.mesh
if (!mesh || mesh.instanceMatrix.count < total) {
// Room for half as many again, so a few more parts do not mean a new buffer.
if (mesh) {
this.unskip?.()
this.scene.remove(mesh)
mesh.dispose()
}
mesh = new InstancedMesh(
this.look.geometry,
this.look.material,
Math.max(16, Math.ceil(total * 1.5)),
)
mesh.name = `runek:pool:${this.name}`
mesh.frustumCulled = false
mesh.renderOrder = this.look.renderOrder ?? 0
this.unskip = this.look.skipMirrors ? skipMirrors(mesh) : null
this.scene.add(mesh)
this.mesh = mesh
}
let i = 0
for (const parts of this.owners.values()) {
for (const matrix of parts.matrices) {
mesh.setMatrixAt(i, matrix)
if (parts.color) mesh.setColorAt(i, parts.color)
i++
}
}
mesh.count = total
mesh.instanceMatrix.needsUpdate = true
if (mesh.instanceColor) mesh.instanceColor.needsUpdate = true
}
}
const pools = new WeakMap<Scene, Map<string, Pool>>()
/** The pool named `name` in `scene`, made from `look()` the first time it is asked for. */
function poolFor(scene: Scene, name: string, look: () => PoolLook): Pool {
let named = pools.get(scene)
if (!named) {
named = new Map()
pools.set(scene, named)
}
let pool = named.get(name)
if (!pool) {
pool = new Pool(scene, name, look())
named.set(name, pool)
}
return pool
}
/**
* Put a component's parts in the pool named `name`. `local` is each part's matrix in the
* component's own frame and must be memoized. Attach the returned ref to a group at the
* component's origin: its world matrix places the parts. `place` is any value that changes when
* that group moves (its position and rotation props). `color` tints every part of this
* component, for a pool whose material is white.
*/
export function useInstances(
name: string,
look: () => PoolLook,
local: Matrix4[],
color: Color | null,
place: unknown,
): RefObject<Group | null> {
const ref = useRef<Group>(null)
const scene = useThree((s) => s.scene)
const owner = useMemo(() => ({}), [])
useLayoutEffect(() => {
const group = ref.current
if (!group || !local.length) return
const pool = poolFor(scene, name, look)
group.updateWorldMatrix(true, false)
pool.set(
owner,
local.map((m) => new Matrix4().multiplyMatrices(group.matrixWorld, m)),
color,
)
return () => pool.drop(owner)
// `look` is only read when the pool is first made.
}, [scene, name, local, color, place, owner])
return ref
} nodes/haze.ts
// The TSL twins of city/haze.ts, for WebGPURenderer: a lamp's haze cone and the pool it leaves
// on the street, additive and faded out by distance instead of fog.
import { type Color, DoubleSide, type Material } from 'three'
import {
abs,
cameraPosition,
distance,
float,
mix,
normalView,
positionViewDirection,
positionWorld,
pow,
smoothstep,
uniform,
uv,
vec2,
} from 'three/tsl'
import { MeshBasicNodeMaterial } from 'three/webgpu'
import { additive } from '../city/haze'
const cache = new Map<string, Material>()
/** The haze cone under a lamp head, in `color` (linear, already scaled to its luminance). */
export function coneNodeMaterial(color: Color): Material {
const key = `cone:${color.r.toFixed(4)},${color.g.toFixed(4)},${color.b.toFixed(4)}`
let m = cache.get(key)
if (!m) {
const n = new MeshBasicNodeMaterial()
const facing = pow(abs(normalView.dot(positionViewDirection)), 1.5)
n.colorNode = uniform(color.clone()).mul(facing).mul(uv().y.mul(uv().y))
n.fog = false
n.side = DoubleSide
n.name = `runek:haze:${key}`
m = additive(n)
cache.set(key, m)
}
return m
}
/** The pool a lamp leaves on the street, faded in between `near` and `far` meters from the eye. */
export function poolNodeMaterial(color: Color, near: number, far: number): Material {
const key = `pool:${color.r.toFixed(4)},${color.g.toFixed(4)},${color.b.toFixed(4)}:${near}:${far}`
let m = cache.get(key)
if (!m) {
const n = new MeshBasicNodeMaterial()
const r = uv().sub(vec2(0.5)).length().mul(2)
const disc = float(1).sub(smoothstep(0, 1, r))
const fade = mix(0.5, 1, smoothstep(near, far, distance(positionWorld, cameraPosition)))
n.colorNode = uniform(color.clone()).mul(disc.mul(disc)).mul(fade)
n.fog = false
n.polygonOffset = true
n.polygonOffsetFactor = -2
n.polygonOffsetUnits = -4
n.name = `runek:haze:${key}`
m = additive(n)
cache.set(key, m)
}
return m
}