Skip to content

Instantly share code, notes, and snippets.

@antoineMoPa
Last active January 6, 2024 18:53
Show Gist options
  • Save antoineMoPa/f849fe60ab7f97864abe761884affd67 to your computer and use it in GitHub Desktop.
Save antoineMoPa/f849fe60ab7f97864abe761884affd67 to your computer and use it in GitHub Desktop.
WGSL vector math utils

WGSL SDF/vector math utils

Please report any issues

Vector Projection

fn position_in_camera_plane(p: vec3<f32>) -> vec2<f32> {
    return vec2(dot(p, camera_right.xyz), dot(p, camera_up.xyz));
}

Signed distance fields

fn sphere_sdf(p: vec3<f32>, r: f32) -> f32 {
    return length(p) - r;
}
fn max_vec3(p: vec3<f32>, value: f32) -> vec3<f32> {
    return vec3(max(p.x, value), max(p.y, value), max(p.z, value));
}
fn min_vec3(p: vec3<f32>, value: f32) -> vec3<f32> {
    return vec3(min(p.x, value), min(p.y, value), min(p.z, value));
}
fn box_sdf(p: vec3<f32>, b: vec3<f32>) -> f32 {
    let q: vec3<f32> = abs(p) - b;
    return length(max_vec3(q, 0.0)) + min(max(q.x,max(q.y, q.z)), 0.0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment