runmat_plot/gpu/shaders/vertex/
triangle.rs1pub const SHADER: &str = r#"// Triangle vertex shader for surfaces and filled plots
2
3struct Uniforms {
4 view_proj: mat4x4<f32>,
5 model: mat4x4<f32>,
6 normal_matrix: mat3x4<f32>,
7}
8
9@group(0) @binding(0)
10var<uniform> uniforms: Uniforms;
11
12struct VertexInput {
13 @location(0) position: vec3<f32>,
14 @location(1) color: vec4<f32>,
15 @location(2) normal: vec3<f32>,
16 @location(3) tex_coords: vec2<f32>,
17}
18
19struct VertexOutput {
20 @builtin(position) clip_position: vec4<f32>,
21 @location(0) color: vec4<f32>,
22 @location(1) world_position: vec3<f32>,
23 @location(2) normal: vec3<f32>,
24 @location(3) tex_coords: vec2<f32>,
25}
26
27@vertex
28fn vs_main(input: VertexInput) -> VertexOutput {
29 var out: VertexOutput;
30
31 let world_position = uniforms.model * vec4<f32>(input.position, 1.0);
32 out.clip_position = uniforms.view_proj * world_position;
33 out.world_position = world_position.xyz;
34 out.color = input.color;
35 // normal_matrix is mat3x4 for alignment (see Rust side). In WGSL, mat3x4 * vec3 -> vec4.
36 out.normal = normalize((uniforms.normal_matrix * input.normal).xyz);
37 out.tex_coords = input.tex_coords;
38
39 return out;
40}
41
42@fragment
43fn fs_main(input: VertexOutput) -> @location(0) vec4<f32> {
44 let normal_len = length(input.normal);
45 if normal_len < 0.0001 {
46 return input.color;
47 }
48
49 let normal = normalize(input.normal);
50 let light_dir = normalize(vec3<f32>(0.36, 0.48, 0.80));
51 let fill_dir = normalize(vec3<f32>(-0.58, -0.26, 0.78));
52 let diffuse = max(dot(normal, light_dir), 0.0);
53 let fill = max(dot(normal, fill_dir), 0.0);
54 let rim = pow(1.0 - abs(normal.z), 2.0);
55 let lighting = clamp(0.56 + diffuse * 0.36 + fill * 0.10 + rim * 0.10, 0.46, 1.12);
56 let highlight = vec3<f32>(0.08, 0.09, 0.10) * max(diffuse - 0.72, 0.0);
57 return vec4<f32>(clamp(input.color.rgb * lighting + highlight, vec3<f32>(0.0), vec3<f32>(1.0)), input.color.a);
58}
59"#;