1+ #![ cfg_attr( target_arch = "spirv" , no_std) ]
2+ #![ allow( clippy:: missing_safety_doc) ]
3+
4+ use spirv_std:: { spirv, glam:: { vec3, vec4, Vec3 , Vec4 } , num_traits:: Float } ;
5+
6+ #[ spirv( fragment) ]
7+ pub fn main_fs (
8+ in_normal : Vec3 ,
9+ in_color : Vec3 ,
10+ in_view_vec : Vec3 ,
11+ in_light_vec : Vec3 ,
12+ #[ spirv( flat) ] _in_flat_normal : Vec3 ,
13+ #[ spirv( spec_constant( id = 0 , default = 0 ) ) ] lighting_model : u32 ,
14+ out_frag_color : & mut Vec4 ,
15+ ) {
16+ let mut color = match lighting_model {
17+ 0 => { // Phong
18+ let ambient = in_color * vec3 ( 0.25 , 0.25 , 0.25 ) ;
19+ let n = in_normal. normalize ( ) ;
20+ let l = in_light_vec. normalize ( ) ;
21+ let v = in_view_vec. normalize ( ) ;
22+ let r = ( -l) . reflect ( n) ;
23+ let diffuse = n. dot ( l) . max ( 0.0 ) * in_color;
24+ let specular = r. dot ( v) . max ( 0.0 ) . powf ( 32.0 ) * vec3 ( 0.75 , 0.75 , 0.75 ) ;
25+ ambient + diffuse * 1.75 + specular
26+ }
27+ 1 => { // Toon
28+ let n = in_normal. normalize ( ) ;
29+ let l = in_light_vec. normalize ( ) ;
30+ let intensity = n. dot ( l) ;
31+ if intensity > 0.98 {
32+ in_color * 1.5
33+ } else if intensity > 0.9 {
34+ in_color * 1.0
35+ } else if intensity > 0.5 {
36+ in_color * 0.6
37+ } else if intensity > 0.25 {
38+ in_color * 0.4
39+ } else {
40+ in_color * 0.2
41+ }
42+ }
43+ 2 => { // No shading
44+ in_color
45+ }
46+ 3 => { // Greyscale
47+ let grey = in_color. x * 0.299 + in_color. y * 0.587 + in_color. z * 0.114 ;
48+ vec3 ( grey, grey, grey)
49+ }
50+ _ => in_color, // Default case
51+ } ;
52+
53+ // Scene is dark, brighten up a bit
54+ color *= 1.25 ;
55+
56+ * out_frag_color = vec4 ( color. x , color. y , color. z , 1.0 ) ;
57+ }
0 commit comments