Skip to main content

ROLL_WGSL

Constant ROLL_WGSL 

Source
pub const ROLL_WGSL: &str = r#"
struct Rolled {
    position: vec3<f32>,
    tangent: vec3<f32>,
    normal: vec3<f32>,
}

fn roll_outer(core: f32, thickness: f32, wound: f32) -> f32 {
    return sqrt(core * core + max(wound, 0.0) * thickness / 3.14159265);
}

fn roll_place(rest: vec3<f32>, at: f32, outer: f32, thickness: f32) -> Rolled {
    var out: Rolled;
    let s = rest.x - at;
    if (s <= 0.0) {
        out.position = vec3<f32>(rest.x, 0.0, rest.z);
        out.tangent = vec3<f32>(1.0, 0.0, 0.0);
        out.normal = vec3<f32>(0.0, 1.0, 0.0);
        return out;
    }
    var theta = s / outer;
    if (thickness > 1e-7) {
        let inside = max(outer * outer - thickness * s / 3.14159265, 0.0);
        theta = (outer - sqrt(inside)) * 6.28318531 / thickness;
    }
    let shrink = thickness / 6.28318531;
    let r = outer - shrink * theta;
    let around = vec2<f32>(sin(theta), -cos(theta));
    let along = vec2<f32>(cos(theta), sin(theta));
    let flat = -shrink * around + r * along;
    let t = normalize(flat);
    out.position = vec3<f32>(at + r * around.x, outer + r * around.y, rest.z);
    out.tangent = vec3<f32>(t.x, t.y, 0.0);
    out.normal = vec3<f32>(-t.y, t.x, 0.0);
    return out;
}
"#;