sdfCylindricalRepeat

cylindrical coordinate repetition.

Parameters

NameTypeDescription
positionvec3<f32>3D position to transform.
angleRepeatf32Angular repetition count.
heightRepeatf32Height repetition interval.
axisvec3<f32>Cylindrical axis (should be normalized).

Returns

vec3<f32>repeated position.

WGSL Code

fn sdfCylindricalRepeat(position: vec3<f32>, angleRepeat: f32, heightRepeat: f32, axis: vec3<f32>) -> vec3<f32> {
  let n = normalize(axis);
  
  // Project onto axis for height
  let h = dot(position, n);
  let radial = position - h * n;
  
  // Repeat in height
  let newH = h - heightRepeat * round(h / heightRepeat);
  
  // Repeat in angle
  let radius = length(radial);
  if (radius < 0.001) {
    return newH * n;
  }
  
  let angle = atan2(radial.y, radial.x);
  let sectorAngle = 6.28318530718 / angleRepeat;
  let newAngle = angle - sectorAngle * round(angle / sectorAngle);
  
  let newRadial = radius * vec2<f32>(cos(newAngle), sin(newAngle));
  
  // This assumes axis is along Z - for general axis, need proper basis vectors
  return newH * n + vec3<f32>(newRadial.x, newRadial.y, 0.0);
}
Showing 5/25 lines

sdfMirror

an SDF across a plane defined by a normal vector.

Parameters

NameTypeDescription
positionvec3<f32>3D position to transform.
normalvec3<f32>Normal vector of the mirror plane.
offsetf32Distance offset of the mirror plane.

Returns

vec3<f32>position.

WGSL Code

fn sdfMirror(position: vec3<f32>, normal: vec3<f32>, offset: f32) -> vec3<f32> {
  let n = normalize(normal);
  let d = dot(position, n) - offset;
  return position - 2.0 * max(0.0, d) * n;
}

sdfPolarRepeat

polar repetition around an axis.

Parameters

NameTypeDescription
positionvec3<f32>3D position to transform.
countf32Number of repetitions around the circle.
axisvec3<f32>Axis to repeat around (should be normalized).

Returns

vec3<f32>repeated position.

WGSL Code

fn sdfPolarRepeat(position: vec3<f32>, count: f32, axis: vec3<f32>) -> vec3<f32> {
  let n = normalize(axis);
  
  // Project position onto axis
  let axisProj = dot(position, n) * n;
  let radial = position - axisProj;
  
  // Get angle in the plane perpendicular to axis
  let radius = length(radial);
  if (radius < 0.001) {
    return position;
  }
  
  let angle = atan2(radial.y, radial.x);
  let sectorAngle = 6.28318530718 / count;
  let snappedAngle = round(angle / sectorAngle) * sectorAngle;
  
  // Reconstruct position with snapped angle
  let newRadial = radius * vec2<f32>(cos(snappedAngle), sin(snappedAngle));
  
  // This assumes axis is along Z - for general axis, need proper basis vectors
  return axisProj + vec3<f32>(newRadial.x, newRadial.y, 0.0);
}
Showing 5/23 lines

sdfRotate

an SDF around a pivot point with Euler angles.

Parameters

NameTypeDescription
positionvec3<f32>3D position to transform.
anglesvec3<f32>Rotation angles in radians (x, y, z).
pivotvec3<f32>Point to rotate around.

Returns

vec3<f32>position.

WGSL Code

fn sdfRotate(position: vec3<f32>, angles: vec3<f32>, pivot: vec3<f32>) -> vec3<f32> {
  // First translate to origin relative to pivot point
  let centered = position - pivot;
  
  // Create rotation matrices (inverse rotation = negative angles)
  let cx = cos(-angles.x);
  let sx = sin(-angles.x);
  let cy = cos(-angles.y);
  let sy = sin(-angles.y);
  let cz = cos(-angles.z);
  let sz = sin(-angles.z);
  
  // Rotate around X axis
  let rx = vec3<f32>(
    centered.x,
    centered.y * cx - centered.z * sx,
    centered.y * sx + centered.z * cx
  );
  
  // Rotate around Y axis
  let ry = vec3<f32>(
    rx.x * cy + rx.z * sy,
    rx.y,
    -rx.x * sy + rx.z * cy
  );
  
  // Rotate around Z axis
  let rz = vec3<f32>(
    ry.x * cz - ry.y * sz,
    ry.x * sz + ry.y * cz,
    ry.z
  );
  
  // Translate back from pivot point
  return rz + pivot;
}
Showing 5/36 lines

sdfScale

an SDF uniformly or non-uniformly.

Parameters

NameTypeDescription
positionvec3<f32>3D position to transform.
scalevec3<f32>Scale factors for each axis.

Returns

vec3<f32>position.

WGSL Code

fn sdfScale(position: vec3<f32>, scale: vec3<f32>) -> vec3<f32> {
  return position / scale;
}

sdfSphericalRepeat

spherical coordinate repetition.

Parameters

NameTypeDescription
positionvec3<f32>3D position to transform.
phiRepeatf32Azimuthal angle repetition count.
thetaRepeatf32Polar angle repetition count.

Returns

vec3<f32>repeated position.

WGSL Code

fn sdfSphericalRepeat(position: vec3<f32>, phiRepeat: f32, thetaRepeat: f32) -> vec3<f32> {
  let radius = length(position);
  if (radius < 0.001) {
    return position;
  }
  
  // Convert to spherical coordinates
  let theta = acos(clamp(position.z / radius, -1.0, 1.0));
  let phi = atan2(position.y, position.x);
  
  // Repeat in spherical coordinates
  let phiSector = 6.28318530718 / phiRepeat;
  let thetaSector = 3.14159265359 / thetaRepeat;
  
  let newPhi = phi - phiSector * round(phi / phiSector);
  let newTheta = theta - thetaSector * round(theta / thetaSector);
  
  // Convert back to Cartesian
  let sinTheta = sin(newTheta);
  return radius * vec3<f32>(
    sinTheta * cos(newPhi),
    sinTheta * sin(newPhi),
    cos(newTheta)
  );
}
Showing 5/25 lines

sdfTranslate

an SDF by moving its position.

Parameters

NameTypeDescription
positionvec3<f32>3D position to transform.
offsetvec3<f32>Translation offset.

Returns

vec3<f32>position.

WGSL Code

fn sdfTranslate(position: vec3<f32>, offset: vec3<f32>) -> vec3<f32> {
  return position - offset;
}