1+ #![ cfg_attr( target_arch = "spirv" , no_std) ]
2+ #![ allow( clippy:: missing_safety_doc) ]
3+
4+ use spirv_std:: { spirv, glam:: { mat3, vec3, vec4, Mat4 , Vec3 , Vec4 } , Image , num_traits:: Float } ;
5+ use spirv_std:: image:: SampledImage ;
6+
7+ #[ repr( C ) ]
8+ #[ derive( Copy , Clone ) ]
9+ pub struct Ubo {
10+ pub projection : Mat4 ,
11+ pub model : Mat4 ,
12+ pub light_pos : Vec4 ,
13+ }
14+
15+ #[ spirv( vertex) ]
16+ pub fn main_vs (
17+ in_pos : Vec3 ,
18+ in_normal : Vec3 ,
19+ in_color : Vec3 ,
20+ #[ spirv( uniform, descriptor_set = 0 , binding = 0 ) ] ubo : & Ubo ,
21+ #[ spirv( position) ] out_position : & mut Vec4 ,
22+ out_normal : & mut Vec3 ,
23+ out_color : & mut Vec3 ,
24+ out_view_vec : & mut Vec3 ,
25+ out_light_vec : & mut Vec3 ,
26+ ) {
27+ * out_normal = in_normal;
28+ * out_color = in_color;
29+ * out_position = ubo. projection * ubo. model * vec4 ( in_pos. x , in_pos. y , in_pos. z , 1.0 ) ;
30+
31+ let pos = ubo. model * vec4 ( in_pos. x , in_pos. y , in_pos. z , 1.0 ) ;
32+ let model_mat3 = mat3 (
33+ ubo. model . x_axis . truncate ( ) ,
34+ ubo. model . y_axis . truncate ( ) ,
35+ ubo. model . z_axis . truncate ( ) ,
36+ ) ;
37+ * out_normal = model_mat3 * in_normal;
38+ let l_pos = model_mat3 * ubo. light_pos . truncate ( ) ;
39+ * out_light_vec = l_pos - pos. truncate ( ) ;
40+ * out_view_vec = -pos. truncate ( ) ;
41+ }
42+
43+ #[ spirv( fragment) ]
44+ pub fn main_fs (
45+ in_normal : Vec3 ,
46+ in_color : Vec3 ,
47+ in_view_vec : Vec3 ,
48+ in_light_vec : Vec3 ,
49+ #[ spirv( descriptor_set = 0 , binding = 1 ) ] _sampler_color_map : & SampledImage < Image ! ( 2 D , type =f32 , sampled) > ,
50+ out_frag_color : & mut Vec4 ,
51+ ) {
52+ // Desaturate color
53+ let gray = in_color. dot ( vec3 ( 0.2126 , 0.7152 , 0.0722 ) ) ;
54+ let color = in_color. lerp ( vec3 ( gray, gray, gray) , 0.65 ) ;
55+
56+ // High ambient colors because mesh materials are pretty dark
57+ let ambient = color * vec3 ( 1.0 , 1.0 , 1.0 ) ;
58+ let n = in_normal. normalize ( ) ;
59+ let l = in_light_vec. normalize ( ) ;
60+ let v = in_view_vec. normalize ( ) ;
61+ let r = ( -l) . reflect ( n) ;
62+ let diffuse = n. dot ( l) . max ( 0.0 ) * color;
63+ let specular = r. dot ( v) . max ( 0.0 ) . powf ( 32.0 ) * vec3 ( 0.35 , 0.35 , 0.35 ) ;
64+ let final_color = ambient + diffuse * 1.75 + specular;
65+ * out_frag_color = vec4 ( final_color. x , final_color. y , final_color. z , 1.0 ) ;
66+ }
0 commit comments