Parameters
Name | Type | Description |
---|---|---|
position | vec3<f32> | 3D position to transform. |
angle | f32 | Bend angle in radians. |
axis | vec3<f32> | Axis normal to the bending plane. |
center | vec3<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 axislet axisNorm = normalize(axis);// Translate position relative to bend centerlet localPos = position - center;// Find perpendicular vectors to the bend axis to define the bend planevar 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 vectorslet proj1 = dot(localPos, perpVec1);let proj2 = dot(localPos, perpVec2);let axisProj = dot(localPos, axisNorm);// Calculate radius for the bendlet radius = proj1;// Calculate the angle based on the distance along the bend directionlet bendAngle = proj2 * angle;// Calculate the bent position using polar coordinateslet c = cos(bendAngle);let s = sin(bendAngle);// Apply the transformationlet bentPos = center +axisNorm * axisProj +perpVec1 * (c * radius) +perpVec2 * (s * radius);return bentPos;}