polyscope-render 0.5.10

Rendering backend for polyscope-rs: wgpu engine, shaders, and materials
Documentation
// Reflected point sphere impostor shader
// Renders point cloud spheres with reflection matrix applied

struct CameraUniforms {
    view: mat4x4<f32>,
    proj: mat4x4<f32>,
    view_proj: mat4x4<f32>,
    inv_proj: mat4x4<f32>,
    camera_pos: vec3<f32>,
    _padding: f32,
}

struct PointUniforms {
    model: mat4x4<f32>,
    point_radius: f32,
    use_per_point_color: u32,
    _padding: vec2<f32>,
    base_color: vec4<f32>,
}

struct ReflectionUniforms {
    reflection_matrix: mat4x4<f32>,
    intensity: f32,
    ground_height: f32,
    _padding: vec2<f32>,
}

@group(0) @binding(0) var<uniform> camera: CameraUniforms;
@group(0) @binding(1) var<uniform> point_uniforms: PointUniforms;
@group(0) @binding(2) var<storage, read> point_positions: array<vec3<f32>>;
@group(0) @binding(3) var<storage, read> point_colors: array<vec4<f32>>;
// Slice plane uniforms for fragment-level slicing
struct SlicePlaneUniforms {
    origin: vec3<f32>,
    enabled: f32,
    normal: vec3<f32>,
    _padding: f32,
}

struct SlicePlanesArray {
    planes: array<SlicePlaneUniforms, 4>,
}

@group(1) @binding(0) var<uniform> reflection: ReflectionUniforms;

// Matcap textures (Group 2)
@group(2) @binding(0) var matcap_r: texture_2d<f32>;
@group(2) @binding(1) var matcap_g: texture_2d<f32>;
@group(2) @binding(2) var matcap_b: texture_2d<f32>;
@group(2) @binding(3) var matcap_k: texture_2d<f32>;
@group(2) @binding(4) var matcap_sampler: sampler;

// Slice planes (Group 3)
@group(3) @binding(0) var<uniform> slice_planes: SlicePlanesArray;

fn light_surface_matcap(normal: vec3<f32>, color: vec3<f32>) -> vec3<f32> {
    var n = normalize(normal);
    n.y = -n.y;
    n = n * 0.98;
    let uv = n.xy * 0.5 + vec2<f32>(0.5);
    let mat_r = textureSample(matcap_r, matcap_sampler, uv).rgb;
    let mat_g = textureSample(matcap_g, matcap_sampler, uv).rgb;
    let mat_b = textureSample(matcap_b, matcap_sampler, uv).rgb;
    let mat_k = textureSample(matcap_k, matcap_sampler, uv).rgb;
    return color.r * mat_r + color.g * mat_g
         + color.b * mat_b + (1.0 - color.r - color.g - color.b) * mat_k;
}

struct VertexOutput {
    @builtin(position) clip_position: vec4<f32>,
    @location(0) sphere_center_view: vec3<f32>,
    @location(1) quad_pos: vec2<f32>,
    @location(2) point_color: vec3<f32>,
    @location(3) point_radius: f32,
    @location(4) sphere_center_world: vec3<f32>,
    @location(5) original_world_position: vec3<f32>,
}

// Billboard quad vertices (two triangles)
const QUAD_VERTICES: array<vec2<f32>, 6> = array<vec2<f32>, 6>(
    vec2<f32>(-1.0, -1.0),
    vec2<f32>( 1.0, -1.0),
    vec2<f32>( 1.0,  1.0),
    vec2<f32>(-1.0, -1.0),
    vec2<f32>( 1.0,  1.0),
    vec2<f32>(-1.0,  1.0),
);

@vertex
fn vs_main(
    @builtin(vertex_index) vertex_index: u32,
    @builtin(instance_index) instance_index: u32,
) -> VertexOutput {
    var out: VertexOutput;

    // Get point position and apply model transform
    let local_pos = point_positions[instance_index];
    let world_pos = (point_uniforms.model * vec4<f32>(local_pos, 1.0)).xyz;

    // Apply reflection matrix
    let reflected_pos = (reflection.reflection_matrix * vec4<f32>(world_pos, 1.0)).xyz;

    let view_pos = (camera.view * vec4<f32>(reflected_pos, 1.0)).xyz;

    // Get quad vertex
    let quad_pos = QUAD_VERTICES[vertex_index];

    // Compute billboard offset in view space (always facing camera)
    let radius = point_uniforms.point_radius;
    let offset = vec3<f32>(quad_pos * radius, 0.0);
    let billboard_pos_view = view_pos + offset;

    // Project to clip space
    out.clip_position = camera.proj * vec4<f32>(billboard_pos_view, 1.0);
    out.sphere_center_view = view_pos;
    out.sphere_center_world = reflected_pos;
    out.quad_pos = quad_pos;
    out.point_radius = radius;

    // Get color
    if (point_uniforms.use_per_point_color == 1u) {
        out.point_color = point_colors[instance_index].xyz;
    } else {
        out.point_color = point_uniforms.base_color.rgb;
    }

    out.original_world_position = world_pos;

    return out;
}

@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
    // Clip pixels above ground plane
    if (in.sphere_center_world.y > reflection.ground_height) {
        discard;
    }

    // Slice plane culling — test against original (pre-reflection) world position
    for (var i = 0u; i < 4u; i = i + 1u) {
        let plane = slice_planes.planes[i];
        if (plane.enabled > 0.5) {
            let dist = dot(in.original_world_position - plane.origin, plane.normal);
            if (dist < 0.0) {
                discard;
            }
        }
    }

    // Ray-sphere intersection in view space
    let ray_origin = vec3<f32>(
        in.sphere_center_view.xy + in.quad_pos * in.point_radius,
        in.sphere_center_view.z
    );
    let ray_dir = vec3<f32>(0.0, 0.0, -1.0);

    // Sphere intersection
    let oc = ray_origin - in.sphere_center_view;
    let a = dot(ray_dir, ray_dir);
    let b = 2.0 * dot(oc, ray_dir);
    let c = dot(oc, oc) - in.point_radius * in.point_radius;
    let discriminant = b * b - 4.0 * a * c;

    if (discriminant < 0.0) {
        discard;
    }

    let t = (-b - sqrt(discriminant)) / (2.0 * a);
    let hit_point = ray_origin + t * ray_dir;
    let normal = normalize(hit_point - in.sphere_center_view);

    // Matcap lighting: normal is already in view space from ray-sphere intersection
    let color = light_surface_matcap(normal, in.point_color);

    // Output with reflection intensity as alpha
    return vec4<f32>(color, reflection.intensity);
}