Clicking while cursor is locked fires a red bullet in the camera's look direction. Bullets spawn slightly behind and below the camera for an FPS feel, travel at 50 units/s, fade to transparent over 5 seconds via a new per-object opacity field in ModelUniform, then get removed. A CSS crosshair reticule appears when the cursor is locked.
60 lines
1.7 KiB
WebGPU Shading Language
60 lines
1.7 KiB
WebGPU Shading Language
// Vertex input from the mesh buffers
|
|
struct VertexInput {
|
|
@location(0) position: vec3<f32>,
|
|
@location(1) normal: vec3<f32>,
|
|
@location(2) color: vec3<f32>,
|
|
};
|
|
|
|
struct VertexOutput {
|
|
@builtin(position) clip_position: vec4<f32>,
|
|
@location(0) world_normal: vec3<f32>,
|
|
@location(1) color: vec3<f32>,
|
|
@location(2) world_position: vec3<f32>,
|
|
};
|
|
|
|
struct Camera {
|
|
view_proj: mat4x4<f32>,
|
|
eye_pos: vec3<f32>,
|
|
_padding: f32,
|
|
};
|
|
|
|
struct Model {
|
|
transform: mat4x4<f32>,
|
|
opacity: f32,
|
|
};
|
|
|
|
@group(0) @binding(0) var<uniform> camera: Camera;
|
|
@group(1) @binding(0) var<uniform> model: Model;
|
|
|
|
@vertex
|
|
fn vs_main(in: VertexInput) -> VertexOutput {
|
|
var out: VertexOutput;
|
|
let world_pos = model.transform * vec4<f32>(in.position, 1.0);
|
|
out.clip_position = camera.view_proj * world_pos;
|
|
// Transform normal (assuming uniform scale, so we can skip inverse-transpose)
|
|
out.world_normal = normalize((model.transform * vec4<f32>(in.normal, 0.0)).xyz);
|
|
out.color = in.color;
|
|
out.world_position = world_pos.xyz;
|
|
return out;
|
|
}
|
|
|
|
@fragment
|
|
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
|
|
let normal = normalize(in.world_normal);
|
|
|
|
// Simple directional light + ambient for a flat-shaded look
|
|
let light_dir = normalize(vec3<f32>(0.4, 0.6, 1.0));
|
|
let ambient = 0.15;
|
|
let diffuse = max(dot(normal, light_dir), 0.0);
|
|
|
|
// Slight specular highlight
|
|
let view_dir = normalize(camera.eye_pos - in.world_position);
|
|
let half_dir = normalize(light_dir + view_dir);
|
|
let specular = pow(max(dot(normal, half_dir), 0.0), 32.0) * 0.3;
|
|
|
|
let brightness = ambient + diffuse * 0.75 + specular;
|
|
let final_color = in.color * brightness;
|
|
|
|
return vec4<f32>(final_color, model.opacity);
|
|
}
|