sdfBend

geometry along a specified axis creating a smooth curve.

Parameters

NameTypeDescription
positionvec3<f32>3D position to transform.
anglef32Bend angle in radians.
axisvec3<f32>Axis normal to the bending plane.
centervec3<f32>Center of the bend.

Returns

vec3<f32>position.

WGSL Code

fn sdfBend(position: vec3<f32>, angle: f32, axis: vec3<f32>, center: vec3<f32>) -> vec3<f32> {
  // Normalize the bend axis
  let axisNorm = normalize(axis);
  
  // Translate position relative to bend center
  let localPos = position - center;
  
  // Find perpendicular vectors to the bend axis to define the bend plane
  var perpVec1: vec3<f32>;
  if (abs(axisNorm.y) < 0.999) {
    perpVec1 = normalize(cross(vec3<f32>(0.0, 1.0, 0.0), axisNorm));
  } else {
    perpVec1 = normalize(cross(vec3<f32>(1.0, 0.0, 0.0), axisNorm));
  }
  let perpVec2 = normalize(cross(axisNorm, perpVec1));
  
  // Project the position onto the perpendicular vectors
  let proj1 = dot(localPos, perpVec1);
  let proj2 = dot(localPos, perpVec2);
  let axisProj = dot(localPos, axisNorm);
  
  // Calculate radius for the bend
  let radius = proj1;
  
  // Calculate the angle based on the distance along the bend direction
  let bendAngle = proj2 * angle;
  
  // Calculate the bent position using polar coordinates
  let c = cos(bendAngle);
  let s = sin(bendAngle);
  
  // Apply the transformation
  let bentPos = center + 
                axisNorm * axisProj +
                perpVec1 * (c * radius) +
                perpVec2 * (s * radius);
  
  return bentPos;
}
Showing 5/39 lines

sdfDisplace

SDF using noise or other displacement functions.

Parameters

NameTypeDescription
positionvec3<f32>3D position to displace.
amountf32Displacement amount.
frequencyf32Displacement frequency.
seedf32Random seed for displacement.

Returns

vec3<f32>position.

Dependencies

WGSL Code

//! requires hash3D
fn sdfDisplace(position: vec3<f32>, amount: f32, frequency: f32, seed: f32) -> vec3<f32> {
  let noisePos = position * frequency + seed;
  let displacement = hash3D(noisePos) * amount;
  
  return position + displacement;
}
Showing 5/7 lines

sdfDomainRepeat

domain repetition with optional warping for complex patterns.

Parameters

NameTypeDescription
positionvec3<f32>3D position to transform.
cellSizevec3<f32>Size of each repetition cell.
warpAmountf32Amount of warping to apply.
warpScalef32Scale of the warping effect.
seedf32Random seed for warping.

Returns

vec3<f32>repeated position.

Dependencies

WGSL Code

//! requires warpNoise3D
fn sdfDomainRepeat(position: vec3<f32>, cellSize: vec3<f32>, warpAmount: f32, warpScale: f32, seed: f32) -> vec3<f32> {
  // Calculate warping for position
  let warp = warpNoise3D(position * warpScale, seed) * warpAmount;
  
  // Apply warping to position
  let warpedPos = position + warp;
  
  // Calculate repetition
  return warpedPos - cellSize * round(warpedPos / cellSize);
}
Showing 5/11 lines

sdfFiniteRepeat

finite repetition with specified count along each axis.

Parameters

NameTypeDescription
positionvec3<f32>3D position to transform.
spacingvec3<f32>Spacing between repetitions.
countvec3<f32>Number of repetitions along each axis.

Returns

vec3<f32>repeated position.

WGSL Code

fn sdfFiniteRepeat(position: vec3<f32>, spacing: vec3<f32>, count: vec3<f32>) -> vec3<f32> {
  let id = clamp(round(position / spacing), -count * 0.5, count * 0.5);
  return position - spacing * id;
}

sdfInfiniteRepeat

infinite repetition along all axes.

Parameters

NameTypeDescription
positionvec3<f32>3D position to transform.
spacingvec3<f32>Spacing between repetitions.

Returns

vec3<f32>repeated position.

WGSL Code

fn sdfInfiniteRepeat(position: vec3<f32>, spacing: vec3<f32>) -> vec3<f32> {
  return position - spacing * round(position / spacing);
}

sdfTaper

a linear taper effect along an axis.

Parameters

NameTypeDescription
positionvec3<f32>3D position to transform.
amountf32Taper amount (0 = no taper, 1 = full taper).
axisvec3<f32>Taper axis direction.
heightf32Height over which to apply the taper.
offsetf32Offset along the taper axis.

Returns

vec3<f32>position.

WGSL Code

fn sdfTaper(position: vec3<f32>, amount: f32, axis: vec3<f32>, height: f32, offset: f32) -> vec3<f32> {
  let axisNorm = normalize(axis);
  
  // Project position onto the taper axis
  let axisPos = dot(position, axisNorm) - offset;
  
  // Calculate taper factor based on position along axis
  let t = clamp(axisPos / height, 0.0, 1.0);
  let taperFactor = 1.0 - amount * t;
  
  // Apply taper to the perpendicular components
  let axisComponent = axisPos * axisNorm;
  let perpComponent = position - dot(position, axisNorm) * axisNorm;
  
  return axisComponent + perpComponent * taperFactor;
}
Showing 5/16 lines

sdfTwist

a continuous rotation around an axis proportional to distance along that axis.

Parameters

NameTypeDescription
positionvec3<f32>3D position to transform.
anglef32Twist angle in radians per unit distance.
axisvec3<f32>Axis to twist around (should be normalized).

Returns

vec3<f32>position.

WGSL Code

fn sdfTwist(position: vec3<f32>, angle: f32, axis: vec3<f32>) -> vec3<f32> {
  // Normalize the axis
  let axisNorm = normalize(axis);
  
  // Project position onto the twist axis
  let proj = dot(position, axisNorm);
  
  // Calculate twist angle based on projection along axis
  let twistAngle = proj * angle;
  
  // Get sin and cos of the twist angle
  let s = sin(twistAngle);
  let c = cos(twistAngle);
  
  // Calculate vector from axis (the part that will be rotated)
  let axisProj = proj * axisNorm;
  let fromAxis = position - axisProj;
  
  // Find a perpendicular vector for the rotation
  let basis1 = normalize(fromAxis);
  let basis2 = cross(axisNorm, basis1);
  
  // Rotate using the basis vectors
  let rotated = axisProj + 
                basis1 * length(fromAxis) * c + 
                basis2 * length(fromAxis) * s;
  
  return rotated;
}
Showing 5/29 lines