diff --git a/src/bin/wireframe/main.rs b/src/bin/wireframe/main.rs index 23a3b58..2aff6e7 100644 --- a/src/bin/wireframe/main.rs +++ b/src/bin/wireframe/main.rs @@ -247,7 +247,7 @@ impl<'a> State<'a> { wgpu::MultisampleState { count: viewport.sample_count(), mask: !0, - alpha_to_coverage_enabled: true, + alpha_to_coverage_enabled: false, }, ); diff --git a/src/bin/wireframe/mesh.wgsl b/src/bin/wireframe/mesh.wgsl index 9e92181..0de69b4 100644 --- a/src/bin/wireframe/mesh.wgsl +++ b/src/bin/wireframe/mesh.wgsl @@ -20,15 +20,44 @@ struct VertexOutput { @location(0) vertex_color: vec4, } +struct FragmentOutput { + @location(0) color: vec4, + @builtin(sample_mask) mask: u32, +} + +fn hash(key : u32) -> u32 { + var v = key; + v *= 0xb384af1bu; + v ^= v >> 15u; + return v; +} + @vertex fn vs_main(ver: VertexInput) -> VertexOutput { var out: VertexOutput; - out.vertex_color = mesh.color; + var light = dot(ver.normal, normalize(vec3(1., 1., 1.))); + light = .7 + .3 * light; + out.vertex_color = light * mesh.color; out.clip_position = camera.mvp * vec4(ver.position, 1.); return out; } @fragment -fn fs_main(in: VertexOutput) -> @location(0) vec4 { - return in.vertex_color; +fn fs_main(in: VertexOutput) -> FragmentOutput { + var out: FragmentOutput; + out.color = vec4(in.vertex_color.xyz, 1.); + var x = bitcast(in.clip_position.x); + var y = bitcast(in.clip_position.y); + var z = bitcast(in.clip_position.z); + var alpha = in.vertex_color.w; + var seed = hash(hash(hash(x) ^ y) ^ z); + var mask = 0u; + for (var sample = 0u; sample < 8u; sample++) { + var threshold = f32(hash(seed ^ sample)) / 0x1p32; + if (alpha > threshold) { + mask |= 1u << sample; + } + } + out.mask = mask; + return out; }