Parameters
Name | Type | Description |
---|---|---|
position | vec3<f32> | 3D position to transform. |
angle | f32 | Twist angle in radians per unit distance. |
axis | vec3<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 axislet axisNorm = normalize(axis);// Project position onto the twist axislet proj = dot(position, axisNorm);// Calculate twist angle based on projection along axislet twistAngle = proj * angle;// Get sin and cos of the twist anglelet 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 rotationlet basis1 = normalize(fromAxis);let basis2 = cross(axisNorm, basis1);// Rotate using the basis vectorslet rotated = axisProj +basis1 * length(fromAxis) * c +basis2 * length(fromAxis) * s;return rotated;}