@typegpu/sdf
The @typegpu/sdf package provides signed distance functions and small SDF helpers for TypeGPU and WebGPU projects.
It is meant for shader-side shape logic: UI masks, ray-marched scenes, collision-ish distance checks, and texture-based SDF generation.
A signed distance function returns:
- a negative value inside the shape,
0on the boundary,- a positive value outside the shape.
Basic usage
Section titled “Basic usage”Call SDF helpers from TypeGPU functions just like any other TypeGPU resource. Resolving the function includes the SDF code and its dependencies automatically.

import const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu, { import d
d, import std
std } from 'typegpu';import * as import sdf
sdf from '@typegpu/sdf';
const const renderRoundedBox: TgpuFn<(uv: d.Vec2f) => d.Vec4f>
renderRoundedBox = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.fn: <[d.Vec2f], d.Vec4f>(argTypes: [d.Vec2f], returnType: d.Vec4f) => TgpuFnShell<[d.Vec2f], d.Vec4f> (+2 overloads)
fn([import d
d.const vec2f: d.Vec2fexport vec2f
Schema representing vec2f - a vector with 2 elements of type f32.
Also a constructor function for this vector value.
vec2f], import d
d.const vec4f: d.Vec4fexport vec4f
Schema representing vec4f - a vector with 4 elements of type f32.
Also a constructor function for this vector value.
vec4f)((uv: d.v2f
uv) => { 'use gpu'; const const p: d.v2f
p = uv: d.v2f
uv - 0.5; const const dist: number
dist = import sdf
sdf.function sdRoundedBox2d(point: d.v2f, size: d.v2f, cornerRadius: number): numberexport sdRoundedBox2d
Signed distance function for a rounded 2d box
sdRoundedBox2d(const p: d.v2f
p, import d
d.function vec2f(x: number, y: number): d.v2f (+3 overloads)export vec2f
Schema representing vec2f - a vector with 2 elements of type f32.
Also a constructor function for this vector value.
vec2f(0.26, 0.12), 0.04); const const edge: number
edge = import std
std.function max(fst: number, ...rest: number[]): number (+1 overload)export max
max(import std
std.function fwidth(value: number): number (+1 overload)export fwidth
fwidth(const dist: number
dist), 0.001); const const alpha: number
alpha = 1 - import std
std.function smoothstep(edge0: number, edge1: number, x: number): number (+1 overload)export smoothstep
smoothstep(-const edge: number
edge, const edge: number
edge, const dist: number
dist); const const glow: number
glow = import std
std.function exp(value: number): number (+1 overload)export exp
exp(-import std
std.function abs(value: number): number (+1 overload)export abs
abs(const dist: number
dist) * 28) * 0.2;
const const bg: d.v3f
bg = import std
std.mix<d.v3f>(e1: d.v3f, e2: d.v3f, e3: number): d.v3f (+2 overloads)export mix
mix(import d
d.function vec3f(x: number, y: number, z: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(0.93, 0.95, 0.97), import d
d.function vec3f(x: number, y: number, z: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(0.74, 0.82, 0.95), uv: d.v2f
uv.v2f.y: number
y); const const fill: d.v3f
fill = import d
d.function vec3f(x: number, y: number, z: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(0.08, 0.08, 0.1); const const color: d.v3f
color = import std
std.mix<d.v3f>(e1: d.v3f, e2: d.v3f, e3: number): d.v3f (+2 overloads)export mix
mix(const bg: d.v3f
bg, const fill: d.v3f
fill, const alpha: number
alpha) + import d
d.function vec3f(x: number, y: number, z: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(1, 0.55, 0.28) * const glow: number
glow;
return import d
d.function vec4f(v0: AnyNumericVec3Instance, w: number): d.v4f (+9 overloads)export vec4f
Schema representing vec4f - a vector with 4 elements of type f32.
Also a constructor function for this vector value.
vec4f(import std
std.min<d.v3f>(fst: d.v3f, ...rest: d.v3f[]): d.v3f (+1 overload)export min
min(const color: d.v3f
color, import d
d.function vec3f(xyz: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(1)), 1);});Composing shapes
Section titled “Composing shapes”The package includes a few common operators. They take distances as input, so you can combine any primitive or custom SDF.

import const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu, { import d
d, import std
std } from 'typegpu';import * as import sdf
sdf from '@typegpu/sdf';
const const sceneSdf: TgpuFn<(p: d.Vec2f) => d.F32>
sceneSdf = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.fn: <[d.Vec2f], d.F32>(argTypes: [d.Vec2f], returnType: d.F32) => TgpuFnShell<[d.Vec2f], d.F32> (+2 overloads)
fn([import d
d.const vec2f: d.Vec2fexport vec2f
Schema representing vec2f - a vector with 2 elements of type f32.
Also a constructor function for this vector value.
vec2f], import d
d.const f32: d.F32export f32
A schema that represents a 32-bit float value. (equivalent to f32 in WGSL)
Can also be called to cast a value to an f32.
f32)((p: d.v2f
p) => { 'use gpu'; const const disk: number
disk = import sdf
sdf.function sdDisk(point: d.v2f, radius: number): numberexport sdDisk
Signed distance function for a disk (filled circle)
sdDisk(p: d.v2f
p - import d
d.function vec2f(x: number, y: number): d.v2f (+3 overloads)export vec2f
Schema representing vec2f - a vector with 2 elements of type f32.
Also a constructor function for this vector value.
vec2f(-0.18, 0), 0.22); const const box: number
box = import sdf
sdf.function sdRoundedBox2d(point: d.v2f, size: d.v2f, cornerRadius: number): numberexport sdRoundedBox2d
Signed distance function for a rounded 2d box
sdRoundedBox2d(p: d.v2f
p - import d
d.function vec2f(x: number, y: number): d.v2f (+3 overloads)export vec2f
Schema representing vec2f - a vector with 2 elements of type f32.
Also a constructor function for this vector value.
vec2f(0.18, 0), import d
d.function vec2f(xy: number): d.v2f (+3 overloads)export vec2f
Schema representing vec2f - a vector with 2 elements of type f32.
Also a constructor function for this vector value.
vec2f(0.18), 0.05);
return import sdf
sdf.function opSmoothUnion(d1: number, d2: number, k: number): numberexport opSmoothUnion
Smooth minimum operator for combining two SDFs with a smooth transition
opSmoothUnion(const disk: number
disk, const box: number
box, 0.12);});
const const renderSmoothUnion: TgpuFn<(uv: d.Vec2f) => d.Vec4f>
renderSmoothUnion = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.fn: <[d.Vec2f], d.Vec4f>(argTypes: [d.Vec2f], returnType: d.Vec4f) => TgpuFnShell<[d.Vec2f], d.Vec4f> (+2 overloads)
fn([import d
d.const vec2f: d.Vec2fexport vec2f
Schema representing vec2f - a vector with 2 elements of type f32.
Also a constructor function for this vector value.
vec2f], import d
d.const vec4f: d.Vec4fexport vec4f
Schema representing vec4f - a vector with 4 elements of type f32.
Also a constructor function for this vector value.
vec4f)((uv: d.v2f
uv) => { 'use gpu'; const const dist: number
dist = const sceneSdf: (p: d.v2f) => number
sceneSdf((uv: d.v2f
uv - 0.5) * 1.08); const const edge: number
edge = import std
std.function max(fst: number, ...rest: number[]): number (+1 overload)export max
max(import std
std.function fwidth(value: number): number (+1 overload)export fwidth
fwidth(const dist: number
dist), 0.001); const const alpha: number
alpha = 1 - import std
std.function smoothstep(edge0: number, edge1: number, x: number): number (+1 overload)export smoothstep
smoothstep(-const edge: number
edge, const edge: number
edge, const dist: number
dist); const const glow: number
glow = import std
std.function exp(value: number): number (+1 overload)export exp
exp(-import std
std.function abs(value: number): number (+1 overload)export abs
abs(const dist: number
dist) * 26) * 0.2;
const const bg: d.v3f
bg = import std
std.mix<d.v3f>(e1: d.v3f, e2: d.v3f, e3: number): d.v3f (+2 overloads)export mix
mix(import d
d.function vec3f(x: number, y: number, z: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(0.93, 0.95, 0.97), import d
d.function vec3f(x: number, y: number, z: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(0.74, 0.82, 0.95), uv: d.v2f
uv.v2f.y: number
y); const const fill: d.v3f
fill = import d
d.function vec3f(x: number, y: number, z: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(0.07, 0.075, 0.095); const const color: d.v3f
color = import std
std.mix<d.v3f>(e1: d.v3f, e2: d.v3f, e3: number): d.v3f (+2 overloads)export mix
mix(const bg: d.v3f
bg, const fill: d.v3f
fill, const alpha: number
alpha) + import d
d.function vec3f(x: number, y: number, z: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(1, 0.55, 0.28) * const glow: number
glow;
return import d
d.function vec4f(v0: AnyNumericVec3Instance, w: number): d.v4f (+9 overloads)export vec4f
Schema representing vec4f - a vector with 4 elements of type f32.
Also a constructor function for this vector value.
vec4f(import std
std.min<d.v3f>(fst: d.v3f, ...rest: d.v3f[]): d.v3f (+1 overload)export min
min(const color: d.v3f
color, import d
d.function vec3f(xyz: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(1)), 1);});For more on combining distance fields, see Inigo Quilez’s smooth minimum and distance functions articles.
3D SDFs
Section titled “3D SDFs”The 3D primitives follow the same rules as the 2D ones. They are useful for ray marching and for generating volumetric fields.

import const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu, { import d
d, import std
std } from 'typegpu';import * as import sdf
sdf from '@typegpu/sdf';
const const sceneSdf: TgpuFn<(p: d.Vec3f) => d.F32>
sceneSdf = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.fn: <[d.Vec3f], d.F32>(argTypes: [d.Vec3f], returnType: d.F32) => TgpuFnShell<[d.Vec3f], d.F32> (+2 overloads)
fn([import d
d.const vec3f: d.Vec3fexport vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f], import d
d.const f32: d.F32export f32
A schema that represents a 32-bit float value. (equivalent to f32 in WGSL)
Can also be called to cast a value to an f32.
f32)((p: d.v3f
p) => { 'use gpu'; const const sphere: number
sphere = import sdf
sdf.function sdSphere(point: d.v3f, radius: number): numberexport sdSphere
Signed distance function for a sphere
sdSphere(p: d.v3f
p - import d
d.function vec3f(x: number, y: number, z: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(-0.2, 0, 0), 0.32); const const box: number
box = import sdf
sdf.function sdRoundedBox3d(point: d.v3f, size: d.v3f, cornerRadius: number): numberexport sdRoundedBox3d
Signed distance function for a rounded 3d box
sdRoundedBox3d(p: d.v3f
p - import d
d.function vec3f(x: number, y: number, z: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(0.22, 0, 0), import d
d.function vec3f(xyz: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(0.22), 0.04);
return import sdf
sdf.function opSmoothUnion(d1: number, d2: number, k: number): numberexport opSmoothUnion
Smooth minimum operator for combining two SDFs with a smooth transition
opSmoothUnion(const sphere: number
sphere, const box: number
box, 0.08);});
const const normalAt: TgpuFn<(p: d.Vec3f) => d.Vec3f>
normalAt = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.fn: <[d.Vec3f], d.Vec3f>(argTypes: [d.Vec3f], returnType: d.Vec3f) => TgpuFnShell<[d.Vec3f], d.Vec3f> (+2 overloads)
fn([import d
d.const vec3f: d.Vec3fexport vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f], import d
d.const vec3f: d.Vec3fexport vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f)((p: d.v3f
p) => { 'use gpu'; const const e: 0.002
e = 0.002; return import std
std.normalize<d.v3f>(v: d.v3f): d.v3fexport normalize
normalize(import d
d.function vec3f(x: number, y: number, z: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f( const sceneSdf: (p: d.v3f) => number
sceneSdf(p: d.v3f
p + import d
d.function vec3f(x: number, y: number, z: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(const e: 0.002
e, 0, 0)) - const sceneSdf: (p: d.v3f) => number
sceneSdf(p: d.v3f
p - import d
d.function vec3f(x: number, y: number, z: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(const e: 0.002
e, 0, 0)), const sceneSdf: (p: d.v3f) => number
sceneSdf(p: d.v3f
p + import d
d.function vec3f(x: number, y: number, z: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(0, const e: 0.002
e, 0)) - const sceneSdf: (p: d.v3f) => number
sceneSdf(p: d.v3f
p - import d
d.function vec3f(x: number, y: number, z: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(0, const e: 0.002
e, 0)), const sceneSdf: (p: d.v3f) => number
sceneSdf(p: d.v3f
p + import d
d.function vec3f(x: number, y: number, z: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(0, 0, const e: 0.002
e)) - const sceneSdf: (p: d.v3f) => number
sceneSdf(p: d.v3f
p - import d
d.function vec3f(x: number, y: number, z: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(0, 0, const e: 0.002
e)), ));});
const const march: TgpuFn<(ro: d.Vec3f, rd: d.Vec3f) => d.Vec2f>
march = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.fn: <[d.Vec3f, d.Vec3f], d.Vec2f>(argTypes: [d.Vec3f, d.Vec3f], returnType: d.Vec2f) => TgpuFnShell<[d.Vec3f, d.Vec3f], d.Vec2f> (+2 overloads)
fn([import d
d.const vec3f: d.Vec3fexport vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f, import d
d.const vec3f: d.Vec3fexport vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f], import d
d.const vec2f: d.Vec2fexport vec2f
Schema representing vec2f - a vector with 2 elements of type f32.
Also a constructor function for this vector value.
vec2f)((ro: d.v3f
ro, rd: d.v3f
rd) => { 'use gpu'; let let t: number
t = import d
d.function f32(v?: number | boolean): numberexport f32
A schema that represents a 32-bit float value. (equivalent to f32 in WGSL)
Can also be called to cast a value to an f32.
f32(0); let let hit: number
hit = import d
d.function f32(v?: number | boolean): numberexport f32
A schema that represents a 32-bit float value. (equivalent to f32 in WGSL)
Can also be called to cast a value to an f32.
f32(0);
for (let let i: number
i = 0; let i: number
i < 96; let i: number
i++) { const const dist: number
dist = const sceneSdf: (p: d.v3f) => number
sceneSdf(ro: d.v3f
ro + rd: d.v3f
rd * let t: number
t); if (const dist: number
dist < 0.002) { let hit: number
hit = 1; break; } let t: number
t += const dist: number
dist; if (let t: number
t > 6) { break; } }
return import d
d.function vec2f(x: number, y: number): d.v2f (+3 overloads)export vec2f
Schema representing vec2f - a vector with 2 elements of type f32.
Also a constructor function for this vector value.
vec2f(let t: number
t, let hit: number
hit);});
const const renderRayMarching: TgpuFn<(uv: d.Vec2f) => d.Vec4f>
renderRayMarching = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.fn: <[d.Vec2f], d.Vec4f>(argTypes: [d.Vec2f], returnType: d.Vec4f) => TgpuFnShell<[d.Vec2f], d.Vec4f> (+2 overloads)
fn([import d
d.const vec2f: d.Vec2fexport vec2f
Schema representing vec2f - a vector with 2 elements of type f32.
Also a constructor function for this vector value.
vec2f], import d
d.const vec4f: d.Vec4fexport vec4f
Schema representing vec4f - a vector with 4 elements of type f32.
Also a constructor function for this vector value.
vec4f)((uv: d.v2f
uv) => { 'use gpu'; const const screen: d.v2f
screen = uv: d.v2f
uv * 2 - 1; const const ro: d.v3f
ro = import d
d.function vec3f(x: number, y: number, z: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(0, 0.05, -2.4); const const rd: d.v3f
rd = import std
std.normalize<d.v3f>(v: d.v3f): d.v3fexport normalize
normalize(import d
d.function vec3f(v0: AnyNumericVec2Instance, z: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(const screen: d.v2f
screen, 1.25)); const const result: d.v2f
result = const march: (ro: d.v3f, rd: d.v3f) => d.v2f
march(const ro: d.v3f
ro, const rd: d.v3f
rd);
const const bg: d.v3f
bg = import std
std.mix<d.v3f>(e1: d.v3f, e2: d.v3f, e3: number): d.v3f (+2 overloads)export mix
mix(import d
d.function vec3f(x: number, y: number, z: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(0.93, 0.95, 0.97), import d
d.function vec3f(x: number, y: number, z: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(0.74, 0.82, 0.95), uv: d.v2f
uv.v2f.y: number
y); if (const result: d.v2f
result.v2f.y: number
y < 0.5) { return import d
d.function vec4f(v0: AnyNumericVec3Instance, w: number): d.v4f (+9 overloads)export vec4f
Schema representing vec4f - a vector with 4 elements of type f32.
Also a constructor function for this vector value.
vec4f(const bg: d.v3f
bg, 1); }
const const p: d.v3f
p = const ro: d.v3f
ro + const rd: d.v3f
rd * const result: d.v2f
result.v2f.x: number
x; const const normal: d.v3f
normal = const normalAt: (p: d.v3f) => d.v3f
normalAt(const p: d.v3f
p); const const lightDir: d.v3f
lightDir = import std
std.normalize<d.v3f>(v: d.v3f): d.v3fexport normalize
normalize(import d
d.function vec3f(x: number, y: number, z: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(-0.35, 0.7, -0.55)); const const viewDir: d.v3f
viewDir = import std
std.normalize<d.v3f>(v: d.v3f): d.v3fexport normalize
normalize(const rd: d.v3f
rd * -1); const const diffuse: number
diffuse = import std
std.function max(fst: number, ...rest: number[]): number (+1 overload)export max
max(import std
std.dot<d.v3f>(lhs: d.v3f, rhs: d.v3f): numberexport dot
dot(const normal: d.v3f
normal, const lightDir: d.v3f
lightDir), 0); const const rim: number
rim = (1 - import std
std.function max(fst: number, ...rest: number[]): number (+1 overload)export max
max(import std
std.dot<d.v3f>(lhs: d.v3f, rhs: d.v3f): numberexport dot
dot(const normal: d.v3f
normal, const viewDir: d.v3f
viewDir), 0)) ** 2;
const const fill: d.v3f
fill = import std
std.mix<d.v3f>(e1: d.v3f, e2: d.v3f, e3: number): d.v3f (+2 overloads)export mix
mix(import d
d.function vec3f(x: number, y: number, z: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(0.04, 0.045, 0.06), import d
d.function vec3f(x: number, y: number, z: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(0.09, 0.1, 0.13), const p: d.v3f
p.v3f.x: number
x + 0.5); const const color: d.v3f
color = const fill: d.v3f
fill * (0.28 + const diffuse: number
diffuse * 0.9) + import d
d.function vec3f(x: number, y: number, z: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(1, 0.55, 0.28) * const rim: number
rim * 0.35;
return import d
d.function vec4f(v0: AnyNumericVec3Instance, w: number): d.v4f (+9 overloads)export vec4f
Schema representing vec4f - a vector with 4 elements of type f32.
Also a constructor function for this vector value.
vec4f(import std
std.min<d.v3f>(fst: d.v3f, ...rest: d.v3f[]): d.v3f (+1 overload)export min
min(const color: d.v3f
color, import d
d.function vec3f(xyz: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(1)), 1);});For 2D-to-3D shapes, use opExtrudeX, opExtrudeY, or opExtrudeZ with a 2D distance value and a half-height.
Jump flood textures
Section titled “Jump flood textures”createJumpFlood builds an executor for the Jump Flood Algorithm. It turns a shader-side classifier into two sampled output textures:
sdfOutput, anrgba16floattexture with the signed distance in the red channel,colorOutput, anrgba8unormtexture for data copied from the nearest inside pixel.
Here the source shape is a triangle mask baked into a texture first, then jump flooded into an SDF texture.

import const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu, { import d
d, import std
std } from 'typegpu';import * as import sdf
sdf from '@typegpu/sdf';
const const root: TgpuRoot
root = await const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.init: (options?: InitOptions) => Promise<TgpuRoot>
Requests a new GPU device and creates a root around it.
If a specific device should be used instead, use
init();
const const sourceTexture: TgpuTexture<{ size: [512, 512]; format: "rgba8unorm";}> & StorageFlag & SampledFlag
sourceTexture = const root: TgpuRoot
root .TgpuRoot.createTexture<number, number, number, [512, 512], "rgba8unorm", number, number, GPUTextureFormat[], GPUTextureDimension>(props: CreateTextureOptions<[512, 512], "rgba8unorm", number, number, GPUTextureFormat[], GPUTextureDimension>): TgpuTexture<{ size: [512, 512]; format: "rgba8unorm";}>
createTexture({ size: [512, 512]
The width, height, and depth or layer count of the texture.
size: [512, 512], format: "rgba8unorm"
The format of the texture.
format: 'rgba8unorm', }) .TgpuTexture<{ size: [512, 512]; format: "rgba8unorm"; }>.$usage<["storage", "sampled"]>(usages_0: "storage", usages_1: "sampled"): TgpuTexture<{ size: [512, 512]; format: "rgba8unorm";}> & StorageFlag & SampledFlag
$usage('storage', 'sampled');
const const sourceWriteView: TgpuTextureView<d.WgslStorageTexture2d<"rgba8unorm", "write-only">>
sourceWriteView = const sourceTexture: TgpuTexture<{ size: [512, 512]; format: "rgba8unorm";}> & StorageFlag & SampledFlag
sourceTexture.TgpuTexture<{ size: [512, 512]; format: "rgba8unorm"; }>.createView<d.WgslStorageTexture2d<"rgba8unorm", "write-only">>(schema: d.WgslStorageTexture2d<"rgba8unorm", "write-only">, viewDescriptor?: TgpuTextureViewDescriptor): TgpuTextureView<d.WgslStorageTexture2d<"rgba8unorm", "write-only">> (+3 overloads)
createView(import d
d.textureStorage2d<"rgba8unorm">(format: "rgba8unorm"): d.WgslStorageTexture2d<"rgba8unorm", "write-only"> (+1 overload)export textureStorage2d
textureStorage2d('rgba8unorm'));const const sourceReadView: TgpuTextureView<d.WgslTexture2d<d.F32>>
sourceReadView = const sourceTexture: TgpuTexture<{ size: [512, 512]; format: "rgba8unorm";}> & StorageFlag & SampledFlag
sourceTexture.TgpuTexture<{ size: [512, 512]; format: "rgba8unorm"; }>.createView(): TgpuTextureView<d.WgslTexture2d<d.F32>> (+3 overloads)
createView();
const const edge: (p: d.v2f, a: d.v2f, b: d.v2f) => number
edge = (p: d.v2f
p: import d
d.export v2f
Interface representing its WGSL vector type counterpart: vec2f or vec2.
A vector with 2 elements of type f32
v2f, a: d.v2f
a: import d
d.export v2f
Interface representing its WGSL vector type counterpart: vec2f or vec2.
A vector with 2 elements of type f32
v2f, b: d.v2f
b: import d
d.export v2f
Interface representing its WGSL vector type counterpart: vec2f or vec2.
A vector with 2 elements of type f32
v2f) => { 'use gpu'; const const ab: d.v2f
ab = b: d.v2f
b - a: d.v2f
a; const const ap: d.v2f
ap = p: d.v2f
p - a: d.v2f
a; return const ab: d.v2f
ab.v2f.x: number
x * const ap: d.v2f
ap.v2f.y: number
y - const ab: d.v2f
ab.v2f.y: number
y * const ap: d.v2f
ap.v2f.x: number
x;};
const const bakeTriangle: TgpuGuardedComputePipeline<[x: number, y: number]>
bakeTriangle = const root: TgpuRoot
root.WithBinding.createGuardedComputePipeline<[x: number, y: number]>(callback: (x: number, y: number) => void): TgpuGuardedComputePipeline<[x: number, y: number]>
Creates a compute pipeline that executes the given callback in an exact number of threads.
This is different from withCompute(...).createPipeline() in that it does a bounds check on the
thread id, where as regular pipelines do not and work in units of workgroups.
createGuardedComputePipeline((x: number
x, y: number
y) => { 'use gpu'; const const size: d.v2u
size = import std
std.textureDimensions<d.textureStorage2d<"rgba8unorm", "write-only">>(texture: d.textureStorage2d<"rgba8unorm", "write-only">): d.v2u (+5 overloads)export textureDimensions
textureDimensions(const sourceWriteView: TgpuTextureView<d.WgslStorageTexture2d<"rgba8unorm", "write-only">>
sourceWriteView.TgpuTextureView<WgslStorageTexture2d<"rgba8unorm", "write-only">>.$: d.textureStorage2d<"rgba8unorm", "write-only">
$); const const uv: d.v2f
uv = (import d
d.function vec2f(x: number, y: number): d.v2f (+3 overloads)export vec2f
Schema representing vec2f - a vector with 2 elements of type f32.
Also a constructor function for this vector value.
vec2f(x: number
x, y: number
y) + 0.5) / import d
d.function vec2f(v: AnyNumericVec2Instance): d.v2f (+3 overloads)export vec2f
Schema representing vec2f - a vector with 2 elements of type f32.
Also a constructor function for this vector value.
vec2f(const size: d.v2u
size);
const const a: d.v2f
a = import d
d.function vec2f(x: number, y: number): d.v2f (+3 overloads)export vec2f
Schema representing vec2f - a vector with 2 elements of type f32.
Also a constructor function for this vector value.
vec2f(0.33, 0.2); const const b: d.v2f
b = import d
d.function vec2f(x: number, y: number): d.v2f (+3 overloads)export vec2f
Schema representing vec2f - a vector with 2 elements of type f32.
Also a constructor function for this vector value.
vec2f(0.77, 0.52); const const c: d.v2f
c = import d
d.function vec2f(x: number, y: number): d.v2f (+3 overloads)export vec2f
Schema representing vec2f - a vector with 2 elements of type f32.
Also a constructor function for this vector value.
vec2f(0.35, 0.84);
let let color: d.v4f
color = import d
d.function vec4f(): d.v4f (+9 overloads)export vec4f
Schema representing vec4f - a vector with 4 elements of type f32.
Also a constructor function for this vector value.
vec4f(); if (const edge: (p: d.v2f, a: d.v2f, b: d.v2f) => number
edge(const uv: d.v2f
uv, const a: d.v2f
a, const b: d.v2f
b) >= 0 && const edge: (p: d.v2f, a: d.v2f, b: d.v2f) => number
edge(const uv: d.v2f
uv, const b: d.v2f
b, const c: d.v2f
c) >= 0 && const edge: (p: d.v2f, a: d.v2f, b: d.v2f) => number
edge(const uv: d.v2f
uv, const c: d.v2f
c, const a: d.v2f
a) >= 0) { let color: d.v4f
color = import d
d.function vec4f(x: number, y: number, z: number, w: number): d.v4f (+9 overloads)export vec4f
Schema representing vec4f - a vector with 4 elements of type f32.
Also a constructor function for this vector value.
vec4f(1, 0.82, 0.42, 1); }
import std
std.textureStore<d.textureStorage2d<"rgba8unorm", "write-only">>(texture: d.textureStorage2d<"rgba8unorm", "write-only">, coords: d.v2i | d.v2u, value: d.v4f): void (+3 overloads)export textureStore
textureStore(const sourceWriteView: TgpuTextureView<d.WgslStorageTexture2d<"rgba8unorm", "write-only">>
sourceWriteView.TgpuTextureView<WgslStorageTexture2d<"rgba8unorm", "write-only">>.$: d.textureStorage2d<"rgba8unorm", "write-only">
$, import d
d.function vec2u(x: number, y: number): d.v2u (+3 overloads)export vec2u
Schema representing vec2u - a vector with 2 elements of type u32.
Also a constructor function for this vector value.
vec2u(x: number
x, y: number
y), let color: d.v4f
color);});
const bakeTriangle: TgpuGuardedComputePipeline<[x: number, y: number]>
bakeTriangle.TgpuGuardedComputePipeline<[x: number, y: number]>.dispatchThreads(x: number, y: number): void
Dispatches the pipeline.
Unlike TgpuComputePipeline.dispatchWorkgroups(), this method takes in the
number of threads to run in each dimension.
Under the hood, the number of expected threads is sent as a uniform, and
"guarded" by a bounds check.
dispatchThreads(512, 512);
const const sourceLayout: TgpuBindGroupLayout<{ source: { texture: d.WgslTexture2d<d.F32>; };}>
sourceLayout = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.bindGroupLayout: <{ source: { texture: d.WgslTexture2d<d.F32>; };}>(entries: { source: { texture: d.WgslTexture2d<d.F32>; };}) => TgpuBindGroupLayout<{ source: { texture: d.WgslTexture2d<d.F32>; };}> (+1 overload)
bindGroupLayout({ source: { texture: d.WgslTexture2d<d.F32>;}
source: { texture: d.WgslTexture2d<d.F32>
texture: import d
d.function texture2d(): d.WgslTexture2d<d.F32> (+1 overload)export texture2d
texture2d() },});const const sourceBindGroup: TgpuBindGroup<{ source: { texture: d.WgslTexture2d<d.F32>; };}>
sourceBindGroup = const root: TgpuRoot
root.TgpuRoot.createBindGroup<{ source: { texture: d.WgslTexture2d<d.F32>; };}>(layout: TgpuBindGroupLayout<{ source: { texture: d.WgslTexture2d<d.F32>; };}>, entries: ExtractBindGroupInputFromLayout<{ source: { texture: d.WgslTexture2d<d.F32>; };}>): TgpuBindGroup<{ source: { texture: d.WgslTexture2d<d.F32>; };}>
Creates a group of resources that can be bound to a shader based on a specified layout.
createBindGroup(const sourceLayout: TgpuBindGroupLayout<{ source: { texture: d.WgslTexture2d<d.F32>; };}>
sourceLayout, { source: GPUTextureView | GPUTexture | (SampledFlag & TgpuTexture<{ size: readonly number[]; format: GPUTextureFormat; dimension?: "2d"; sampleCount?: 1;}>) | TgpuTextureView<d.WgslTexture<WgslTextureProps>>
source: const sourceReadView: TgpuTextureView<d.WgslTexture2d<d.F32>>
sourceReadView,});
const const runner: sdf.JumpFlood.Executor
runner = import sdf
sdf .function createJumpFlood(options: JumpFloodOptions): sdf.JumpFlood.Executorexport createJumpFlood
Create a Jump Flood Algorithm executor with separate SDF and color output textures.
createJumpFlood({ root: TgpuRoot
root, size: { width: number; height: number;}
size: { width: number
width: 512, height: number
height: 512 }, classify: (coord: d.v2u, size: d.v2u) => boolean
Classify function that determines which pixels are "inside" for the SDF.
Returns true if the pixel is inside, false if outside.
classify: (coord: d.v2u
coord) => { 'use gpu'; const const source: d.v4f
source = import std
std.textureLoad<d.texture2d<d.F32>>(texture: d.texture2d<d.F32>, coords: d.v2i | d.v2u, level: number): d.v4f (+12 overloads)export textureLoad
textureLoad(const sourceLayout: TgpuBindGroupLayout<{ source: { texture: d.WgslTexture2d<d.F32>; };}>
sourceLayout.TgpuBindGroupLayout<{ source: { texture: WgslTexture2d<F32>; }; }>.$: { source: d.texture2d<d.F32>;}
$.source: d.texture2d<d.F32>
source, coord: d.v2u
coord, 0); return const source: d.v4f
source.v4f.w: number
w > 0.5; }, getSdf: (coord: d.v2u, size: d.v2u, signedDist: number, insidePx: d.v2u, outsidePx: d.v2u) => number
Get the SDF value to store. Receives signed distance in pixels.
getSdf: (_coord: d.v2u
_coord, size: d.v2u
size, signedDist: number
signedDist) => { 'use gpu'; const const minDim: number
minDim = import std
std.function min(fst: number, ...rest: number[]): number (+1 overload)export min
min(size: d.v2u
size.v2u.x: number
x, size: d.v2u
size.v2u.y: number
y); return signedDist: number
signedDist / import d
d.function f32(v?: number | boolean): numberexport f32
A schema that represents a 32-bit float value. (equivalent to f32 in WGSL)
Can also be called to cast a value to an f32.
f32(const minDim: number
minDim); }, getColor: (coord: d.v2u, size: d.v2u, signedDist: number, insidePx: d.v2u, outsidePx: d.v2u) => d.v4f
Get the color value to store.
getColor: (_coord: d.v2u
_coord, _size: d.v2u
_size, _signedDist: number
_signedDist, insidePx: d.v2u
insidePx) => { 'use gpu'; const const source: d.v4f
source = import std
std.textureLoad<d.texture2d<d.F32>>(texture: d.texture2d<d.F32>, coords: d.v2i | d.v2u, level: number): d.v4f (+12 overloads)export textureLoad
textureLoad(const sourceLayout: TgpuBindGroupLayout<{ source: { texture: d.WgslTexture2d<d.F32>; };}>
sourceLayout.TgpuBindGroupLayout<{ source: { texture: WgslTexture2d<F32>; }; }>.$: { source: d.texture2d<d.F32>;}
$.source: d.texture2d<d.F32>
source, insidePx: d.v2u
insidePx, 0); return import d
d.function vec4f(v0: AnyNumericVec3Instance, w: number): d.v4f (+9 overloads)export vec4f
Schema representing vec4f - a vector with 4 elements of type f32.
Also a constructor function for this vector value.
vec4f(const source: d.v4f
source.xyz: d.v3f
xyz, 1); }, }) .function with(bindGroup: TgpuBindGroup): sdf.JumpFlood.Executor
Returns a new executor with the additional bind group attached.
Use this to pass resources needed by custom classify or getter functions.
with(const sourceBindGroup: TgpuBindGroup<{ source: { texture: d.WgslTexture2d<d.F32>; };}>
sourceBindGroup);
const runner: sdf.JumpFlood.Executor
runner.function run(): void
Run the jump flood algorithm.
run();
const const sdfView: TgpuTextureView<d.WgslTexture2d<d.F32>>
sdfView = const runner: sdf.JumpFlood.Executor
runner.sdfOutput: sdf.JumpFlood.SdfTexture
The SDF output texture (rgba16float).
sdfOutput.TgpuTexture<{ size: [number, number]; format: "rgba16float"; }>.createView(): TgpuTextureView<d.WgslTexture2d<d.F32>> (+3 overloads)
createView();const const colorView: TgpuTextureView<d.WgslTexture2d<d.F32>>
colorView = const runner: sdf.JumpFlood.Executor
runner.colorOutput: sdf.JumpFlood.ColorTexture
The color output texture (rgba8unorm).
colorOutput.TgpuTexture<{ size: [number, number]; format: "rgba8unorm"; }>.createView(): TgpuTextureView<d.WgslTexture2d<d.F32>> (+3 overloads)
createView();const const sampler: TgpuFixedSampler
sampler = const root: TgpuRoot
root.TgpuRoot.createSampler(props: WgslSamplerProps): TgpuFixedSampler
createSampler({ WgslSamplerProps.magFilter?: GPUFilterMode
Specifies the sampling behavior when the sample footprint is smaller than or equal to one
texel.
magFilter: 'linear', WgslSamplerProps.minFilter?: GPUFilterMode
Specifies the sampling behavior when the sample footprint is larger than one texel.
minFilter: 'linear' });
const const renderJumpFlood: TgpuFn<(uv: d.Vec2f) => d.Vec4f>
renderJumpFlood = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.fn: <[d.Vec2f], d.Vec4f>(argTypes: [d.Vec2f], returnType: d.Vec4f) => TgpuFnShell<[d.Vec2f], d.Vec4f> (+2 overloads)
fn([import d
d.const vec2f: d.Vec2fexport vec2f
Schema representing vec2f - a vector with 2 elements of type f32.
Also a constructor function for this vector value.
vec2f], import d
d.const vec4f: d.Vec4fexport vec4f
Schema representing vec4f - a vector with 4 elements of type f32.
Also a constructor function for this vector value.
vec4f)((uv: d.v2f
uv) => { 'use gpu'; const const dist: number
dist = import std
std.textureSampleLevel<d.texture2d<d.F32>>(texture: d.texture2d<d.F32>, sampler: d.sampler, coords: d.v2f, level: number): d.v4f (+13 overloads)export textureSampleLevel
textureSampleLevel(const sdfView: TgpuTextureView<d.WgslTexture2d<d.F32>>
sdfView.TgpuTextureView<WgslTexture2d<F32>>.$: d.texture2d<d.F32>
$, const sampler: TgpuFixedSampler
sampler.TgpuSampler.$: d.sampler
$, uv: d.v2f
uv, 0).v4f.x: number
x; const const seed: d.v3f
seed = import std
std.textureSampleLevel<d.texture2d<d.F32>>(texture: d.texture2d<d.F32>, sampler: d.sampler, coords: d.v2f, level: number): d.v4f (+13 overloads)export textureSampleLevel
textureSampleLevel(const colorView: TgpuTextureView<d.WgslTexture2d<d.F32>>
colorView.TgpuTextureView<WgslTexture2d<F32>>.$: d.texture2d<d.F32>
$, const sampler: TgpuFixedSampler
sampler.TgpuSampler.$: d.sampler
$, uv: d.v2f
uv, 0).xyz: d.v3f
xyz; const const edge: number
edge = import std
std.function max(fst: number, ...rest: number[]): number (+1 overload)export max
max(import std
std.function fwidth(value: number): number (+1 overload)export fwidth
fwidth(const dist: number
dist), 0.0015); const const alpha: number
alpha = 1 - import std
std.function smoothstep(edge0: number, edge1: number, x: number): number (+1 overload)export smoothstep
smoothstep(-const edge: number
edge, const edge: number
edge, const dist: number
dist); const const glow: number
glow = import std
std.function exp(value: number): number (+1 overload)export exp
exp(-import std
std.function abs(value: number): number (+1 overload)export abs
abs(const dist: number
dist) * 28) * 0.18; const const band: number
band = import std
std.function abs(value: number): number (+1 overload)export abs
abs(import std
std.function fract(value: number): number (+1 overload)export fract
fract(import std
std.function abs(value: number): number (+1 overload)export abs
abs(const dist: number
dist) * 22) - 0.5); const const contour: number
contour = (1 - import std
std.function smoothstep(edge0: number, edge1: number, x: number): number (+1 overload)export smoothstep
smoothstep(0.46, 0.5, const band: number
band)) * import std
std.function smoothstep(edge0: number, edge1: number, x: number): number (+1 overload)export smoothstep
smoothstep(0.012, 0.04, import std
std.function abs(value: number): number (+1 overload)export abs
abs(const dist: number
dist)) * 0.12;
const const bg: d.v3f
bg = import std
std.mix<d.v3f>(e1: d.v3f, e2: d.v3f, e3: number): d.v3f (+2 overloads)export mix
mix(import d
d.function vec3f(x: number, y: number, z: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(0.93, 0.95, 0.97), import d
d.function vec3f(x: number, y: number, z: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(0.74, 0.82, 0.95), uv: d.v2f
uv.v2f.y: number
y); const const field: d.v3f
field = const bg: d.v3f
bg + import d
d.function vec3f(x: number, y: number, z: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(0.36, 0.5, 0.76) * const contour: number
contour; const const fill: d.v3f
fill = import d
d.function vec3f(x: number, y: number, z: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(0.07, 0.075, 0.095) + const seed: d.v3f
seed * 0.045; const const color: d.v3f
color = import std
std.mix<d.v3f>(e1: d.v3f, e2: d.v3f, e3: number): d.v3f (+2 overloads)export mix
mix(const field: d.v3f
field, const fill: d.v3f
fill, const alpha: number
alpha) + import d
d.function vec3f(x: number, y: number, z: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(1, 0.55, 0.28) * const glow: number
glow;
return import d
d.function vec4f(v0: AnyNumericVec3Instance, w: number): d.v4f (+9 overloads)export vec4f
Schema representing vec4f - a vector with 4 elements of type f32.
Also a constructor function for this vector value.
vec4f(import std
std.min<d.v3f>(fst: d.v3f, ...rest: d.v3f[]): d.v3f (+1 overload)export min
min(const color: d.v3f
color, import d
d.function vec3f(xyz: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(1)), 1);});If your classifier or output getters need extra textures, buffers, or samplers, create a bind group for them and attach it with runner.with(bindGroup).
Call runner.destroy() when the generated textures are no longer needed.
Available utilities
Section titled “Available utilities”| Group | Exports |
|---|---|
| 2D primitives | sdDisk, sdBox2d, sdRoundedBox2d, sdLine, sdBezier, sdBezierApprox, sdPie |
| 3D primitives | sdSphere, sdBox3d, sdRoundedBox3d, sdBoxFrame3d, sdLine3d, sdPlane, sdCapsule |
| Operators | opUnion, opSmoothUnion, opSmoothDifference, opExtrudeX, opExtrudeY, opExtrudeZ |
| Texture generation | createJumpFlood, classifySlot, JumpFlood |
For parameter details, hover the functions in your editor or read the JSDoc in the package source.
Examples
Section titled “Examples”- Jelly Switch uses SDFs for a soft 3D interaction.
- Suika SDF mixes texture SDFs with a physics-driven scene.
- Radiance Cascades (with drawing) uses
createJumpFloodas input for global illumination.