11#![ no_std]
22
3+ use spirv_std:: glam:: { IVec2 , Mat4 , UVec3 , Vec2 , Vec3 , Vec4 , Vec4Swizzles } ;
4+ use spirv_std:: num_traits:: Float ;
35use spirv_std:: spirv;
4- use spirv_std:: glam:: { IVec2 , UVec3 , Vec2 , Vec3 , Vec4 , Vec4Swizzles , Mat4 } ;
56use spirv_std:: { Image , Sampler } ;
6- use spirv_std:: num_traits:: Float ;
77
88#[ repr( C ) ]
99#[ derive( Copy , Clone ) ]
@@ -60,59 +60,99 @@ pub fn main_cs(
6060 if index >= particle_count_x * particle_count_y {
6161 return ;
6262 }
63-
63+
6464 // Initial force from gravity
6565 let mut force = ubo. gravity . xyz ( ) * ubo. particle_mass ;
66-
66+
6767 let idx = index as usize ;
6868 let pos = particle_in[ idx] . pos . xyz ( ) ;
6969 let vel = particle_in[ idx] . vel . xyz ( ) ;
70-
70+
7171 // Spring forces from neighboring particles
7272 // left
7373 if id. x > 0 {
74- force += spring_force ( particle_in[ idx - 1 ] . pos . xyz ( ) , pos, ubo. rest_dist_h , ubo. spring_stiffness ) ;
74+ force += spring_force (
75+ particle_in[ idx - 1 ] . pos . xyz ( ) ,
76+ pos,
77+ ubo. rest_dist_h ,
78+ ubo. spring_stiffness ,
79+ ) ;
7580 }
7681 // right
7782 if id. x < particle_count_x - 1 {
78- force += spring_force ( particle_in[ idx + 1 ] . pos . xyz ( ) , pos, ubo. rest_dist_h , ubo. spring_stiffness ) ;
83+ force += spring_force (
84+ particle_in[ idx + 1 ] . pos . xyz ( ) ,
85+ pos,
86+ ubo. rest_dist_h ,
87+ ubo. spring_stiffness ,
88+ ) ;
7989 }
8090 // upper
8191 if id. y < particle_count_y - 1 {
82- force += spring_force ( particle_in[ idx + particle_count_x as usize ] . pos . xyz ( ) , pos, ubo. rest_dist_v , ubo. spring_stiffness ) ;
92+ force += spring_force (
93+ particle_in[ idx + particle_count_x as usize ] . pos . xyz ( ) ,
94+ pos,
95+ ubo. rest_dist_v ,
96+ ubo. spring_stiffness ,
97+ ) ;
8398 }
8499 // lower
85100 if id. y > 0 {
86- force += spring_force ( particle_in[ idx - particle_count_x as usize ] . pos . xyz ( ) , pos, ubo. rest_dist_v , ubo. spring_stiffness ) ;
101+ force += spring_force (
102+ particle_in[ idx - particle_count_x as usize ] . pos . xyz ( ) ,
103+ pos,
104+ ubo. rest_dist_v ,
105+ ubo. spring_stiffness ,
106+ ) ;
87107 }
88108 // upper-left
89109 if id. x > 0 && id. y < particle_count_y - 1 {
90- force += spring_force ( particle_in[ idx + particle_count_x as usize - 1 ] . pos . xyz ( ) , pos, ubo. rest_dist_d , ubo. spring_stiffness ) ;
110+ force += spring_force (
111+ particle_in[ idx + particle_count_x as usize - 1 ] . pos . xyz ( ) ,
112+ pos,
113+ ubo. rest_dist_d ,
114+ ubo. spring_stiffness ,
115+ ) ;
91116 }
92117 // lower-left
93118 if id. x > 0 && id. y > 0 {
94- force += spring_force ( particle_in[ idx - particle_count_x as usize - 1 ] . pos . xyz ( ) , pos, ubo. rest_dist_d , ubo. spring_stiffness ) ;
119+ force += spring_force (
120+ particle_in[ idx - particle_count_x as usize - 1 ] . pos . xyz ( ) ,
121+ pos,
122+ ubo. rest_dist_d ,
123+ ubo. spring_stiffness ,
124+ ) ;
95125 }
96126 // upper-right
97127 if id. x < particle_count_x - 1 && id. y < particle_count_y - 1 {
98- force += spring_force ( particle_in[ idx + particle_count_x as usize + 1 ] . pos . xyz ( ) , pos, ubo. rest_dist_d , ubo. spring_stiffness ) ;
128+ force += spring_force (
129+ particle_in[ idx + particle_count_x as usize + 1 ] . pos . xyz ( ) ,
130+ pos,
131+ ubo. rest_dist_d ,
132+ ubo. spring_stiffness ,
133+ ) ;
99134 }
100135 // lower-right
101136 if id. x < particle_count_x - 1 && id. y > 0 {
102- force += spring_force ( particle_in[ idx - particle_count_x as usize + 1 ] . pos . xyz ( ) , pos, ubo. rest_dist_d , ubo. spring_stiffness ) ;
137+ force += spring_force (
138+ particle_in[ idx - particle_count_x as usize + 1 ] . pos . xyz ( ) ,
139+ pos,
140+ ubo. rest_dist_d ,
141+ ubo. spring_stiffness ,
142+ ) ;
103143 }
104-
144+
105145 // Damping
106146 force += -ubo. damping * vel;
107-
147+
108148 // Integrate
109149 let f = force * ( 1.0 / ubo. particle_mass ) ;
110150 let new_pos = pos + vel * ubo. delta_t + 0.5 * f * ubo. delta_t * ubo. delta_t ;
111151 let new_vel = vel + f * ubo. delta_t ;
112-
152+
113153 particle_out[ idx] . pos = Vec4 :: new ( new_pos. x , new_pos. y , new_pos. z , 1.0 ) ;
114154 particle_out[ idx] . vel = Vec4 :: new ( new_vel. x , new_vel. y , new_vel. z , 0.0 ) ;
115-
155+
116156 // Sphere collision
117157 let sphere_dist = new_pos - ubo. sphere_pos . xyz ( ) ;
118158 if sphere_dist. length ( ) < ubo. sphere_radius + 0.01 {
@@ -122,11 +162,11 @@ pub fn main_cs(
122162 // Cancel out velocity
123163 particle_out[ idx] . vel = Vec4 :: ZERO ;
124164 }
125-
165+
126166 // Calculate normals
127167 if push_consts. calculate_normals == 1 {
128168 let mut normal = Vec3 :: ZERO ;
129-
169+
130170 let stride = particle_count_x as usize ;
131171 if id. y > 0 {
132172 if id. x > 0 {
@@ -156,13 +196,13 @@ pub fn main_cs(
156196 normal += a. cross ( b) + b. cross ( c) ;
157197 }
158198 }
159-
199+
160200 if normal. length ( ) > 0.0 {
161201 normal = normal. normalize ( ) ;
162202 }
163203 particle_out[ idx] . normal = Vec4 :: new ( normal. x , normal. y , normal. z , 0.0 ) ;
164204 }
165-
205+
166206 // Copy UV coordinates
167207 particle_out[ idx] . uv = particle_in[ idx] . uv ;
168208}
@@ -221,6 +261,6 @@ pub fn main_fs(
221261 diffuse. x * color. x + specular. x ,
222262 diffuse. y * color. y + specular. y ,
223263 diffuse. z * color. z + specular. z ,
224- 1.0
264+ 1.0 ,
225265 ) ;
226- }
266+ }
0 commit comments