elasticWave

an elastic wave with exponential decay and sinusoidal oscillation.

Parameters

NameTypeDescription
xf32Input position along the wave.
amplitudef32Wave amplitude multiplier.
frequencyf32Wave frequency.
decayf32Exponential decay factor.
phasef32Phase offset for the wave.

Returns

f32wave value.

WGSL Code

fn elasticWave(x: f32, amplitude: f32, frequency: f32, decay: f32, phase: f32) -> f32 {
let d = max(0.001, decay);
let decayTerm = exp(-d * x);
let oscTerm = sin(frequency * x * 6.28318 + phase);
return amplitude * decayTerm * oscTerm;
}
Showing 5/6 lines

exponentialRamp

an exponential ramp function with derivative.

Parameters

NameTypeDescription
xf32Input value.
basef32Exponential base.
scalef32Scale factor.
offsetf32Vertical offset.

Returns

vec2<f32>value and derivative.

WGSL Code

fn exponentialRamp(x: f32, base: f32, scale: f32, offset: f32) -> vec2<f32> {
// Ensure base is positive and not 1 (which would make it linear)
let b = select(base, 2.71828, abs(base - 1.0) < 0.001);
// Calculate the exponential function
let result = scale * pow(b, x) + offset;
// Calculate the derivative
let derivative = scale * pow(b, x) * log(b);
return vec2<f32>(result, derivative);
}
Showing 5/12 lines

logisticCurve

a logistic (S-curve) function with derivative.

Parameters

NameTypeDescription
xf32Input value.
midpointf32Curve midpoint (inflection point).
steepnessf32Curve steepness factor.
minf32Minimum output value.
maxf32Maximum output value.

Returns

vec2<f32>value and derivative.

WGSL Code

fn logisticCurve(x: f32, midpoint: f32, steepness: f32, minValue: f32, maxValue: f32) -> vec2<f32> {
// Scale factor for steepness
let k = max(0.001, steepness);
// Shift x relative to midpoint
let z = -k * (x - midpoint);
// Calculate the exponent
let expTerm = exp(z);
// Calculate the logistic function value
let logistic = 1.0 / (1.0 + expTerm);
// Scale to min-max range
let range = maxValue - minValue;
let value = minValue + range * logistic;
// Calculate the derivative
let derivative = range * k * expTerm / ((1.0 + expTerm) * (1.0 + expTerm));
return vec2<f32>(value, derivative);
}
Showing 5/22 lines

rotate2D

a 2D vector by a given angle.

Parameters

NameTypeDescription
vvec2<f32>Input 2D vector to rotate.
anglef32Rotation angle in radians.

Returns

vec2<f32>2D vector.

WGSL Code

fn rotate2D(v: vec2<f32>, angle: f32) -> vec2<f32> {
let c = cos(angle);
let s = sin(angle);
return vec2(v.x * c - v.y * s, v.x * s + v.y * c);
}

smoothStep

interpolation between two values with smooth acceleration and deceleration.

Parameters

NameTypeDescription
edge0f32Lower edge of interpolation range.
edge1f32Upper edge of interpolation range.
xf32Input value to interpolate.

Returns

f32interpolated value between 0 and 1.

WGSL Code

fn smoothStep(edge0: f32, edge1: f32, x: f32) -> f32 {
let t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
return t * t * (3.0 - 2.0 * t);
}

smoothStepVec2

interpolation between two vectors with smooth acceleration and deceleration.

Parameters

NameTypeDescription
edge0vec2<f32>Lower edge of interpolation range.
edge1vec2<f32>Upper edge of interpolation range.
xvec2<f32>Input vector to interpolate.

Returns

vec2<f32>interpolated vector between 0 and 1.

WGSL Code

fn smoothStepVec2(edge0: vec2f, edge1: vec2f, x: vec2f) -> vec2f {
let t = clamp((x - edge0) / (edge1 - edge0), vec2f(0.0), vec2f(1.0));
return t * t * (3.0 - 2.0 * t);
}

stepSequence

a stepped sequence with optional smoothing between steps.

Parameters

NameTypeDescription
xf32Input value.
stepsf32Number of steps in the sequence.
smoothingf32Smoothing factor between steps (0-1).
minValuef32Minimum output value.
maxValuef32Maximum output value.

Returns

vec2<f32>value and current step index.

WGSL Code

fn stepSequence(x: f32, steps: f32, smoothing: f32, minValue: f32, maxValue: f32) -> vec2<f32> {
// Ensure at least 1 step and positive smoothing
let numSteps = max(1.0, floor(steps));
let smoothFactor = max(0.0, smoothing);
// Normalize x to 0-1 range
let normalizedX = fract(x);
// Calculate the size of each step
let stepSize = 1.0 / numSteps;
// Calculate the current step (0 to numSteps-1)
let currentStep = floor(normalizedX * numSteps);
let nextStep = fract(currentStep + 1.0);
// Calculate progress within the current step
let stepProgress = fract(normalizedX * numSteps);
// Calculate the progress values for current and next steps
let currentStepValue = currentStep / (numSteps - 1.0);
// Prepare next step value, handle the last step case
var nextStepValue: f32 = 0.0;
if (currentStep >= numSteps - 1.0) {
nextStepValue = 1.0;
} else {
nextStepValue = nextStep / (numSteps - 1.0);
}
// Apply smoothing between steps if needed
var result: f32 = 0.0;
if (smoothFactor > 0.0 && stepProgress > (1.0 - smoothFactor) && numSteps > 1.0) {
// Calculate smoothing factor
let t = (stepProgress - (1.0 - smoothFactor)) / smoothFactor;
// Smoothstep for better transition
let smoothT = t * t * (3.0 - 2.0 * t);
// Interpolate between current and next step
result = mix(currentStepValue, nextStepValue, smoothT);
} else {
result = currentStepValue;
}
// Scale to min-max range
let range = maxValue - minValue;
let finalResult = minValue + result * range;
return vec2<f32>(finalResult, currentStep);
}
Showing 5/51 lines

taylorInvSqrt4

series inverse square root approximation for Perlin noise.

Parameters

NameTypeDescription
rvec4<f32>Input 4D vector.

Returns

vec4<f32>square root approximation.

WGSL Code

fn taylorInvSqrt4(r: vec4f) -> vec4f {
return 1.79284291400159 - 0.85373472095314 * r;
}