sdfRoundedCone

a signed distance field for a rounded cone.

110 total functionsUpdated: Invalid Date

sdfRoundedCone

a signed distance field for a rounded cone.

Parameters

NameTypeDescription
positionvec3<f32>3D position to evaluate.
radius1f32Bottom radius of the cone.
radius2f32Top radius of the cone.
heightf32Height of the cone.
roundnessf32Rounding factor for the edges.

Returns

f32distance to the rounded cone surface.

WGSL Code

fn sdfRoundedCone(position: vec3<f32>, radius1: f32, radius2: f32, height: f32, roundness: f32) -> f32 {
// Calculate distances
let p = position;
let r1 = radius1 - roundness;
let r2 = radius2 - roundness;
let h = height;
// Squared distance from axis
let q = length(p.xz);
// Project into 2D space
let k1 = (r2 - r1) / h;
let k2 = h / (r1 - r2);
let projected = vec2<f32>(q - r1 + r1 * (p.y / h) * (r1 - r2) / r1, p.y - h);
let ca = p.y * k1 - q;
let cb = p.y - r1 * k2 + q * k2;
var s: f32;
if (ca < 0.0 && projected.y < 0.0) {
s = length(projected) - roundness;
} else if (ca > 0.0 && cb < 0.0) {
s = -ca - roundness;
} else {
s = length(vec2<f32>(max(ca, 0.0), max(projected.y, 0.0))) - roundness;
}
return s;
}

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