1+ #![ cfg_attr( target_arch = "spirv" , no_std) ]
2+ #![ allow( clippy:: missing_safety_doc) ]
3+
4+ use spirv_std:: { spirv, glam:: { vec3, vec4, Mat3 , Mat4 , Vec3 , Vec4 } , num_traits:: Float } ;
5+
6+ #[ repr( C ) ]
7+ #[ derive( Copy , Clone ) ]
8+ pub struct Ubo {
9+ pub projection : Mat4 ,
10+ pub view : Mat4 ,
11+ pub model : Mat4 ,
12+ }
13+
14+ #[ repr( C ) ]
15+ #[ derive( Copy , Clone ) ]
16+ pub struct Node {
17+ pub matrix : Mat4 ,
18+ }
19+
20+ #[ repr( C ) ]
21+ #[ derive( Copy , Clone ) ]
22+ pub struct PushBlock {
23+ pub base_color_factor : Vec4 ,
24+ }
25+
26+ #[ spirv( vertex) ]
27+ pub fn main_vs (
28+ in_pos : Vec3 ,
29+ in_normal : Vec3 ,
30+ _in_color : Vec3 ,
31+ #[ spirv( uniform, descriptor_set = 0 , binding = 0 ) ] ubo : & Ubo ,
32+ #[ spirv( uniform, descriptor_set = 1 , binding = 0 ) ] node : & Node ,
33+ #[ spirv( push_constant) ] material : & PushBlock ,
34+ #[ spirv( position) ] out_position : & mut Vec4 ,
35+ out_normal : & mut Vec3 ,
36+ out_color : & mut Vec3 ,
37+ out_view_vec : & mut Vec3 ,
38+ out_light_vec : & mut Vec3 ,
39+ ) {
40+ * out_normal = in_normal;
41+ * out_color = material. base_color_factor . truncate ( ) ;
42+ let pos = vec4 ( in_pos. x , in_pos. y , in_pos. z , 1.0 ) ;
43+ * out_position = ubo. projection * ubo. view * ubo. model * node. matrix * pos;
44+
45+ let normal_matrix = Mat3 :: from_mat4 ( ubo. view * ubo. model * node. matrix ) ;
46+ * out_normal = normal_matrix * in_normal;
47+
48+ let localpos = ubo. view * ubo. model * node. matrix * pos;
49+ let light_pos = vec3 ( 10.0 , -10.0 , 10.0 ) ;
50+ * out_light_vec = light_pos - localpos. truncate ( ) ;
51+ * out_view_vec = -localpos. truncate ( ) ;
52+ }
53+
54+ #[ spirv( fragment) ]
55+ pub fn main_fs (
56+ in_normal : Vec3 ,
57+ in_color : Vec3 ,
58+ in_view_vec : Vec3 ,
59+ in_light_vec : Vec3 ,
60+ out_frag_color : & mut Vec4 ,
61+ ) {
62+ let n = in_normal. normalize ( ) ;
63+ let l = in_light_vec. normalize ( ) ;
64+ let v = in_view_vec. normalize ( ) ;
65+ let r = ( -l) . reflect ( n) ;
66+ let ambient = vec3 ( 0.1 , 0.1 , 0.1 ) ;
67+ let diffuse = n. dot ( l) . max ( 0.0 ) * vec3 ( 1.0 , 1.0 , 1.0 ) ;
68+ let specular = r. dot ( v) . max ( 0.0 ) . powf ( 16.0 ) * vec3 ( 0.75 , 0.75 , 0.75 ) ;
69+ let final_color = ( ambient + diffuse) * in_color + specular;
70+ * out_frag_color = vec4 ( final_color. x , final_color. y , final_color. z , 1.0 ) ;
71+ }
0 commit comments