sdfCapsule

a signed distance field for a capsule (cylinder with rounded caps).

110 total functionsUpdated: Invalid Date

sdfCapsule

a signed distance field for a capsule (cylinder with rounded caps).

Parameters

NameTypeDescription
positionvec3<f32>3D position to evaluate.
radiusf32Radius of the capsule.
heightf32Height of the cylindrical portion.

Returns

f32distance to the capsule surface.

WGSL Code

fn sdfCapsule(position: vec3<f32>, radius: f32, height: f32) -> f32 {
let d = abs(length(position.xz)) - radius;
let p = vec2<f32>(d, abs(position.y) - height * 0.5);
return length(max(p, vec2<f32>(0.0))) + min(max(p.x, p.y), 0.0) - radius;
}

About wgsl-fns

A JavaScript library providing WGSL utility functions as strings for WebGPU shader development. Includes mathematical utilities, noise generation, signed distance fields, and color manipulation functions.

Install:npm install wgsl-fns
Import:import { sdfCapsule, getFns } from 'wgsl-fns'
Usage:getFns(['sdfCapsule'])