sdfHexagonalPrism

a signed distance field for a hexagonal prism.

110 total functionsUpdated: Invalid Date

sdfHexagonalPrism

a signed distance field for a hexagonal prism.

Parameters

NameTypeDescription
positionvec3<f32>3D position to evaluate.
radiusf32Radius of the hexagon.
heightf32Height of the prism.

Returns

f32distance to the hexagonal prism surface.

WGSL Code

fn sdfHexagonalPrism(position: vec3<f32>, radius: f32, height: f32) -> f32 {
// Project into 2D
var p = abs(position);
let k = vec3<f32>(-0.866025404, 0.5, 0.577350269);
// Hexagon in xy-plane
p = vec3<f32>(p.x + p.y * k.x, p.y * k.y, p.z);
p = vec3<f32>(p.x - min(p.x, p.y), p.y, p.z);
let d = vec2<f32>(length(vec2<f32>(p.x, p.y - radius * k.z)) - radius, abs(p.z) - height * 0.5);
return min(max(d.x, d.y), 0.0) + length(max(d, vec2<f32>(0.0)));
}

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