outline.wgsl (1242B)
1 // Outline shader: inverted hull method. 2 // Renders back faces of a slightly inflated mesh in a solid color 3 // to produce a visible outline around the selected object. 4 5 struct Globals { 6 time: f32, 7 _pad0: f32, 8 resolution: vec2<f32>, 9 10 cam_pos: vec3<f32>, 11 _pad3: f32, 12 13 light_dir: vec3<f32>, 14 _pad1: f32, 15 16 light_color: vec3<f32>, 17 _pad2: f32, 18 19 fill_light_dir: vec3<f32>, 20 _pad4: f32, 21 22 fill_light_color: vec3<f32>, 23 _pad5: f32, 24 25 view_proj: mat4x4<f32>, 26 inv_view_proj: mat4x4<f32>, 27 light_view_proj: mat4x4<f32>, 28 }; 29 30 struct Object { 31 model: mat4x4<f32>, 32 normal: mat4x4<f32>, 33 }; 34 35 @group(0) @binding(0) var<uniform> globals: Globals; 36 @group(1) @binding(0) var<uniform> object: Object; 37 38 struct VSIn { 39 @location(0) pos: vec3<f32>, 40 @location(1) normal: vec3<f32>, 41 @location(2) uv: vec2<f32>, 42 @location(3) tangent: vec4<f32>, 43 }; 44 45 @vertex 46 fn vs_main(v: VSIn) -> @builtin(position) vec4<f32> { 47 let outline_width = 0.012; 48 let inflated = v.pos + v.normal * outline_width; 49 let world4 = object.model * vec4<f32>(inflated, 1.0); 50 return globals.view_proj * world4; 51 } 52 53 @fragment 54 fn fs_main() -> @location(0) vec4<f32> { 55 return vec4<f32>(1.0, 0.6, 0.15, 1.0); 56 }