sdfPyramid

a signed distance field for a pyramid.

110 total functionsUpdated: Invalid Date

sdfPyramid

a signed distance field for a pyramid.

Parameters

NameTypeDescription
positionvec3<f32>3D position to evaluate.
sizef32Base size of the pyramid.
heightf32Height of the pyramid.

Returns

f32distance to the pyramid surface.

WGSL Code

fn sdfPyramid(position: vec3<f32>, size: f32, height: f32) -> f32 {
// Normalize position
var p = position;
let h = height;
let m2 = h * h + size * size;
// Project into 2D
let q = abs(p);
p.y -= h;
p.y = max(p.y, 0.0);
// Distance calculation
var d: f32;
if (max(q.x, q.z) < size) {
d = length(vec2<f32>(length(p.xz), p.y)) - sqrt(m2);
} else {
d = length(vec2<f32>(length(max(abs(p.xz) - vec2<f32>(size), vec2<f32>(0.0))), p.y));
}
// Account for position below base
d = select(d, length(p) - sqrt(m2), p.y < 0.0);
return d;
}

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 { sdfPyramid, getFns } from 'wgsl-fns'
Usage:getFns(['sdfPyramid'])