Fix shader warning and tidy up code.

This commit is contained in:
Robin KAY 2022-08-08 19:25:33 +01:00
parent 5c5c4dc5d9
commit 19d193584a
3 changed files with 11 additions and 21 deletions

View File

@ -5,11 +5,11 @@
@group(1) @binding(0) @group(1) @binding(0)
var<uniform> mesh: Mesh; var<uniform> mesh: Mesh;
fn model_origin_z() -> f32 { fn model_origin_z(model: mat4x4<f32>, view_proj: mat4x4<f32>) -> f32 {
var origin = mesh.model[3]; var origin = model[3];
var proj_zw = mat4x2<f32>( var proj_zw = mat4x2<f32>(
view.view_proj[0].zw, view.view_proj[1].zw, view_proj[0].zw, view_proj[1].zw,
view.view_proj[2].zw, view.view_proj[3].zw); view_proj[2].zw, view_proj[3].zw);
var zw = proj_zw * origin; var zw = proj_zw * origin;
return zw.x / zw.y; return zw.x / zw.y;
} }

View File

@ -5,10 +5,6 @@ struct VertexInput {
@location(1) normal: vec3<f32>, @location(1) normal: vec3<f32>,
}; };
struct VertexOutput {
@builtin(position) clip_position: vec4<f32>,
};
struct OutlineViewUniform { struct OutlineViewUniform {
scale: vec2<f32>, scale: vec2<f32>,
}; };
@ -37,13 +33,12 @@ fn mat4to3(m: mat4x4<f32>) -> mat3x3<f32> {
} }
@vertex @vertex
fn vertex(vertex: VertexInput) -> VertexOutput { fn vertex(vertex: VertexInput) -> @builtin(position) vec4<f32> {
var out: VertexOutput;
var clip_pos = view.view_proj * (mesh.model * vec4<f32>(vertex.position, 1.0)); var clip_pos = view.view_proj * (mesh.model * vec4<f32>(vertex.position, 1.0));
var clip_norm = mat4to3(view.view_proj) * (mat4to3(mesh.model) * vertex.normal); var clip_norm = mat4to3(view.view_proj) * (mat4to3(mesh.model) * vertex.normal);
var clip_delta = vec2<f32>(vstage.width * normalize(clip_norm.xy) * clip_pos.w * view_uniform.scale); var ndc_pos = clip_pos.xy / clip_pos.w;
out.clip_position = vec4<f32>((clip_pos.xy + clip_delta) / clip_pos.w, model_origin_z(), 1.0); var ndc_delta = vstage.width * normalize(clip_norm.xy) * view_uniform.scale;
return out; return vec4<f32>(ndc_pos + ndc_delta, model_origin_z(mesh.model, view.view_proj), 1.0);
} }
@fragment @fragment

View File

@ -4,16 +4,11 @@ struct VertexInput {
@location(0) position: vec3<f32>, @location(0) position: vec3<f32>,
}; };
struct VertexOutput {
@builtin(position) clip_position: vec4<f32>,
};
@vertex @vertex
fn vertex(vertex: VertexInput) -> VertexOutput { fn vertex(vertex: VertexInput) -> @builtin(position) vec4<f32> {
var out: VertexOutput;
var clip_pos = view.view_proj * (mesh.model * vec4<f32>(vertex.position, 1.0)); var clip_pos = view.view_proj * (mesh.model * vec4<f32>(vertex.position, 1.0));
out.clip_position = vec4<f32>(clip_pos.xy / clip_pos.w, model_origin_z(), 1.0); var ndc_pos = clip_pos.xy / clip_pos.w;
return out; return vec4<f32>(ndc_pos, model_origin_z(mesh.model, view.view_proj), 1.0);
} }
@fragment @fragment