sdfSphericalRepeat

spherical coordinate repetition.

110 total functionsUpdated: 30/07/2026

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)
  );
}

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