1
1
#![ no_std]
2
2
3
+ use spirv_std:: glam:: { IVec2 , Mat4 , UVec3 , Vec2 , Vec3 , Vec4 , Vec4Swizzles } ;
4
+ use spirv_std:: num_traits:: Float ;
3
5
use spirv_std:: spirv;
4
- use spirv_std:: glam:: { IVec2 , UVec3 , Vec2 , Vec3 , Vec4 , Vec4Swizzles , Mat4 } ;
5
6
use spirv_std:: { Image , Sampler } ;
6
- use spirv_std:: num_traits:: Float ;
7
7
8
8
#[ repr( C ) ]
9
9
#[ derive( Copy , Clone ) ]
@@ -60,59 +60,99 @@ pub fn main_cs(
60
60
if index >= particle_count_x * particle_count_y {
61
61
return ;
62
62
}
63
-
63
+
64
64
// Initial force from gravity
65
65
let mut force = ubo. gravity . xyz ( ) * ubo. particle_mass ;
66
-
66
+
67
67
let idx = index as usize ;
68
68
let pos = particle_in[ idx] . pos . xyz ( ) ;
69
69
let vel = particle_in[ idx] . vel . xyz ( ) ;
70
-
70
+
71
71
// Spring forces from neighboring particles
72
72
// left
73
73
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
+ ) ;
75
80
}
76
81
// right
77
82
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
+ ) ;
79
89
}
80
90
// upper
81
91
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
+ ) ;
83
98
}
84
99
// lower
85
100
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
+ ) ;
87
107
}
88
108
// upper-left
89
109
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
+ ) ;
91
116
}
92
117
// lower-left
93
118
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
+ ) ;
95
125
}
96
126
// upper-right
97
127
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
+ ) ;
99
134
}
100
135
// lower-right
101
136
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
+ ) ;
103
143
}
104
-
144
+
105
145
// Damping
106
146
force += -ubo. damping * vel;
107
-
147
+
108
148
// Integrate
109
149
let f = force * ( 1.0 / ubo. particle_mass ) ;
110
150
let new_pos = pos + vel * ubo. delta_t + 0.5 * f * ubo. delta_t * ubo. delta_t ;
111
151
let new_vel = vel + f * ubo. delta_t ;
112
-
152
+
113
153
particle_out[ idx] . pos = Vec4 :: new ( new_pos. x , new_pos. y , new_pos. z , 1.0 ) ;
114
154
particle_out[ idx] . vel = Vec4 :: new ( new_vel. x , new_vel. y , new_vel. z , 0.0 ) ;
115
-
155
+
116
156
// Sphere collision
117
157
let sphere_dist = new_pos - ubo. sphere_pos . xyz ( ) ;
118
158
if sphere_dist. length ( ) < ubo. sphere_radius + 0.01 {
@@ -122,11 +162,11 @@ pub fn main_cs(
122
162
// Cancel out velocity
123
163
particle_out[ idx] . vel = Vec4 :: ZERO ;
124
164
}
125
-
165
+
126
166
// Calculate normals
127
167
if push_consts. calculate_normals == 1 {
128
168
let mut normal = Vec3 :: ZERO ;
129
-
169
+
130
170
let stride = particle_count_x as usize ;
131
171
if id. y > 0 {
132
172
if id. x > 0 {
@@ -156,13 +196,13 @@ pub fn main_cs(
156
196
normal += a. cross ( b) + b. cross ( c) ;
157
197
}
158
198
}
159
-
199
+
160
200
if normal. length ( ) > 0.0 {
161
201
normal = normal. normalize ( ) ;
162
202
}
163
203
particle_out[ idx] . normal = Vec4 :: new ( normal. x , normal. y , normal. z , 0.0 ) ;
164
204
}
165
-
205
+
166
206
// Copy UV coordinates
167
207
particle_out[ idx] . uv = particle_in[ idx] . uv ;
168
208
}
@@ -221,6 +261,6 @@ pub fn main_fs(
221
261
diffuse. x * color. x + specular. x ,
222
262
diffuse. y * color. y + specular. y ,
223
263
diffuse. z * color. z + specular. z ,
224
- 1.0
264
+ 1.0 ,
225
265
) ;
226
- }
266
+ }
0 commit comments