Debian Codegraph @

drivers/gles3/shaders/particles.glsl

raw
1 /* clang-format off */
2 #[modes]
3
4 mode_default =
5
6 #[specializations]
7
8 MODE_3D = false
9 USERDATA1_USED = false
10 USERDATA2_USED = false
11 USERDATA3_USED = false
12 USERDATA4_USED = false
13 USERDATA5_USED = false
14 USERDATA6_USED = false
15
16 #[vertex]
17
18 #define SDF_MAX_LENGTH 16384.0
19
20 layout(std140) uniform GlobalShaderUniformData { //ubo:1
21 vec4 global_shader_uniforms[MAX_GLOBAL_SHADER_UNIFORMS];
22 };
23
24 // This needs to be outside clang-format so the ubo comment is in the right place
25 #ifdef MATERIAL_UNIFORMS_USED
26 layout(std140) uniform MaterialUniforms{ //ubo:2
27
28 #MATERIAL_UNIFORMS
29
30 };
31 #endif
32
33 /* clang-format on */
34
35 #define MAX_ATTRACTORS 32
36
37 #define ATTRACTOR_TYPE_SPHERE uint(0)
38 #define ATTRACTOR_TYPE_BOX uint(1)
39 #define ATTRACTOR_TYPE_VECTOR_FIELD uint(2)
40
41 struct Attractor {
42 mat4 transform;
43 vec4 extents; // Extents or radius. w-channel is padding.
44
45 uint type;
46 float strength;
47 float attenuation;
48 float directionality;
49 };
50
51 #define MAX_COLLIDERS 32
52
53 #define COLLIDER_TYPE_SPHERE uint(0)
54 #define COLLIDER_TYPE_BOX uint(1)
55 #define COLLIDER_TYPE_SDF uint(2)
56 #define COLLIDER_TYPE_HEIGHT_FIELD uint(3)
57 #define COLLIDER_TYPE_2D_SDF uint(4)
58
59 struct Collider {
60 mat4 transform;
61 vec4 extents; // Extents or radius. w-channel is padding.
62
63 uint type;
64 float scale;
65 float pad0;
66 float pad1;
67 };
68
69 layout(std140) uniform FrameData { //ubo:0
70 bool emitting;
71 uint cycle;
72 float system_phase;
73 float prev_system_phase;
74
75 float explosiveness;
76 float randomness;
77 float time;
78 float delta;
79
80 float particle_size;
81 float amount_ratio;
82 float pad1;
83 float pad2;
84
85 uint random_seed;
86 uint attractor_count;
87 uint collider_count;
88 uint frame;
89
90 mat4 emission_transform;
91
92 vec3 emitter_velocity;
93 float interp_to_end;
94
95 Attractor attractors[MAX_ATTRACTORS];
96 Collider colliders[MAX_COLLIDERS];
97 };
98
99 #define PARTICLE_FLAG_ACTIVE uint(1)
100 #define PARTICLE_FLAG_STARTED uint(2)
101 #define PARTICLE_FLAG_TRAILED uint(4)
102 #define PARTICLE_FRAME_MASK uint(0xFFFF)
103 #define PARTICLE_FRAME_SHIFT uint(16)
104
105 // ParticleData
106 layout(location = 0) in highp vec4 color;
107 layout(location = 1) in highp vec4 velocity_flags;
108 layout(location = 2) in highp vec4 custom;
109 layout(location = 3) in highp vec4 xform_1;
110 layout(location = 4) in highp vec4 xform_2;
111 #ifdef MODE_3D
112 layout(location = 5) in highp vec4 xform_3;
113 #endif
114 #ifdef USERDATA1_USED
115 in highp vec4 userdata1;
116 #endif
117 #ifdef USERDATA2_USED
118 in highp vec4 userdata2;
119 #endif
120 #ifdef USERDATA3_USED
121 in highp vec4 userdata3;
122 #endif
123 #ifdef USERDATA4_USED
124 in highp vec4 userdata4;
125 #endif
126 #ifdef USERDATA5_USED
127 in highp vec4 userdata5;
128 #endif
129 #ifdef USERDATA6_USED
130 in highp vec4 userdata6;
131 #endif
132
133 out highp vec4 out_color; //tfb:
134 out highp vec4 out_velocity_flags; //tfb:
135 out highp vec4 out_custom; //tfb:
136 out highp vec4 out_xform_1; //tfb:
137 out highp vec4 out_xform_2; //tfb:
138 #ifdef MODE_3D
139 out highp vec4 out_xform_3; //tfb:MODE_3D
140 #endif
141 #ifdef USERDATA1_USED
142 out highp vec4 out_userdata1; //tfb:USERDATA1_USED
143 #endif
144 #ifdef USERDATA2_USED
145 out highp vec4 out_userdata2; //tfb:USERDATA2_USED
146 #endif
147 #ifdef USERDATA3_USED
148 out highp vec4 out_userdata3; //tfb:USERDATA3_USED
149 #endif
150 #ifdef USERDATA4_USED
151 out highp vec4 out_userdata4; //tfb:USERDATA4_USED
152 #endif
153 #ifdef USERDATA5_USED
154 out highp vec4 out_userdata5; //tfb:USERDATA5_USED
155 #endif
156 #ifdef USERDATA6_USED
157 out highp vec4 out_userdata6; //tfb:USERDATA6_USED
158 #endif
159
160 uniform sampler2D height_field_texture; //texunit:0
161
162 uniform float lifetime;
163 uniform bool clear;
164 uniform uint total_particles;
165 uniform bool use_fractional_delta;
166
167 uint hash(uint x) {
168 x = ((x >> uint(16)) ^ x) * uint(0x45d9f3b);
169 x = ((x >> uint(16)) ^ x) * uint(0x45d9f3b);
170 x = (x >> uint(16)) ^ x;
171 return x;
172 }
173
174 vec3 safe_normalize(vec3 direction) {
175 const float EPSILON = 0.001;
176 if (length(direction) < EPSILON) {
177 return vec3(0.0);
178 }
179 return normalize(direction);
180 }
181
182 // Needed whenever 2D sdf texture is read from as it is packed in RGBA8.
183 float vec4_to_float(vec4 p_vec) {
184 return dot(p_vec, vec4(1.0 / (255.0 * 255.0 * 255.0), 1.0 / (255.0 * 255.0), 1.0 / 255.0, 1.0)) * 2.0 - 1.0;
185 }
186
187 #GLOBALS
188
189 void main() {
190 bool apply_forces = true;
191 bool apply_velocity = true;
192 float local_delta = delta;
193
194 float mass = 1.0;
195
196 bool restart = false;
197
198 bool restart_position = false;
199 bool restart_rotation_scale = false;
200 bool restart_velocity = false;
201 bool restart_color = false;
202 bool restart_custom = false;
203
204 mat4 xform = mat4(1.0);
205 uint flags = 0u;
206
207 if (clear) {
208 out_color = vec4(1.0);
209 out_custom = vec4(0.0);
210 out_velocity_flags = vec4(0.0);
211 } else {
212 out_color = color;
213 out_velocity_flags = velocity_flags;
214 out_custom = custom;
215 xform[0] = xform_1;
216 xform[1] = xform_2;
217 #ifdef MODE_3D
218 xform[2] = xform_3;
219 #endif
220 xform = transpose(xform);
221 flags = floatBitsToUint(velocity_flags.w);
222 #ifdef USERDATA1_USED
223 out_userdata1 = userdata1;
224 #endif
225 #ifdef USERDATA2_USED
226 out_userdata2 = userdata2;
227 #endif
228 #ifdef USERDATA3_USED
229 out_userdata3 = userdata3;
230 #endif
231 #ifdef USERDATA4_USED
232 out_userdata4 = userdata4;
233 #endif
234 #ifdef USERDATA5_USED
235 out_userdata5 = userdata5;
236 #endif
237 #ifdef USERDATA6_USED
238 out_userdata6 = userdata6;
239 #endif
240 }
241
242 //clear started flag if set
243 flags &= ~PARTICLE_FLAG_STARTED;
244
245 bool collided = false;
246 vec3 collision_normal = vec3(0.0);
247 float collision_depth = 0.0;
248
249 vec3 attractor_force = vec3(0.0);
250
251 #if !defined(DISABLE_VELOCITY)
252
253 if (bool(flags & PARTICLE_FLAG_ACTIVE)) {
254 xform[3].xyz += out_velocity_flags.xyz * local_delta;
255 }
256 #endif
257 uint index = uint(gl_VertexID);
258 if (emitting) {
259 float restart_phase = float(index) / float(total_particles);
260
261 if (randomness > 0.0) {
262 uint seed = cycle;
263 if (restart_phase >= system_phase) {
264 seed -= uint(1);
265 }
266 seed *= uint(total_particles);
267 seed += index;
268 float random = float(hash(seed) % uint(65536)) / 65536.0;
269 restart_phase += randomness * random * 1.0 / float(total_particles);
270 }
271
272 restart_phase *= (1.0 - explosiveness);
273
274 if (system_phase > prev_system_phase) {
275 // restart_phase >= prev_system_phase is used so particles emit in the first frame they are processed
276
277 if (restart_phase >= prev_system_phase && restart_phase < system_phase) {
278 restart = true;
279 if (use_fractional_delta) {
280 local_delta = (system_phase - restart_phase) * lifetime;
281 }
282 }
283
284 } else if (delta > 0.0) {
285 if (restart_phase >= prev_system_phase) {
286 restart = true;
287 if (use_fractional_delta) {
288 local_delta = (1.0 - restart_phase + system_phase) * lifetime;
289 }
290
291 } else if (restart_phase < system_phase) {
292 restart = true;
293 if (use_fractional_delta) {
294 local_delta = (system_phase - restart_phase) * lifetime;
295 }
296 }
297 }
298
299 if (restart) {
300 flags = emitting ? (PARTICLE_FLAG_ACTIVE | PARTICLE_FLAG_STARTED | (cycle << PARTICLE_FRAME_SHIFT)) : 0u;
301 restart_position = true;
302 restart_rotation_scale = true;
303 restart_velocity = true;
304 restart_color = true;
305 restart_custom = true;
306 }
307 }
308
309 bool particle_active = bool(flags & PARTICLE_FLAG_ACTIVE);
310
311 uint particle_number = (flags >> PARTICLE_FRAME_SHIFT) * uint(total_particles) + index;
312
313 if (restart && particle_active) {
314 #CODE : START
315 }
316
317 if (particle_active) {
318 for (uint i = 0u; i < attractor_count; i++) {
319 vec3 dir;
320 float amount;
321 vec3 rel_vec = xform[3].xyz - attractors[i].transform[3].xyz;
322 vec3 local_pos = rel_vec * mat3(attractors[i].transform);
323
324 if (attractors[i].type == ATTRACTOR_TYPE_SPHERE) {
325 dir = safe_normalize(rel_vec);
326 float d = length(local_pos) / attractors[i].extents.x;
327 if (d > 1.0) {
328 continue;
329 }
330 amount = max(0.0, 1.0 - d);
331 } else if (attractors[i].type == ATTRACTOR_TYPE_BOX) {
332 dir = safe_normalize(rel_vec);
333
334 vec3 abs_pos = abs(local_pos / attractors[i].extents.xyz);
335 float d = max(abs_pos.x, max(abs_pos.y, abs_pos.z));
336 if (d > 1.0) {
337 continue;
338 }
339 amount = max(0.0, 1.0 - d);
340 } else if (attractors[i].type == ATTRACTOR_TYPE_VECTOR_FIELD) {
341 }
342 mediump float attractor_attenuation = attractors[i].attenuation;
343 amount = pow(amount, attractor_attenuation);
344 dir = safe_normalize(mix(dir, attractors[i].transform[2].xyz, attractors[i].directionality));
345 attractor_force -= mass * amount * dir * attractors[i].strength;
346 }
347
348 float particle_size = particle_size;
349
350 #ifdef USE_COLLISION_SCALE
351
352 particle_size *= dot(vec3(length(xform[0].xyz), length(xform[1].xyz), length(xform[2].xyz)), vec3(0.33333333333));
353
354 #endif
355
356 if (collider_count == 1u && colliders[0].type == COLLIDER_TYPE_2D_SDF) {
357 //2D collision
358
359 vec2 pos = xform[3].xy;
360 vec4 to_sdf_x = colliders[0].transform[0];
361 vec4 to_sdf_y = colliders[0].transform[1];
362 vec2 sdf_pos = vec2(dot(vec4(pos, 0, 1), to_sdf_x), dot(vec4(pos, 0, 1), to_sdf_y));
363
364 vec4 sdf_to_screen = vec4(colliders[0].extents.xyz, colliders[0].scale);
365
366 vec2 uv_pos = sdf_pos * sdf_to_screen.xy + sdf_to_screen.zw;
367
368 if (all(greaterThan(uv_pos, vec2(0.0))) && all(lessThan(uv_pos, vec2(1.0)))) {
369 vec2 pos2 = pos + vec2(0, particle_size);
370 vec2 sdf_pos2 = vec2(dot(vec4(pos2, 0, 1), to_sdf_x), dot(vec4(pos2, 0, 1), to_sdf_y));
371 float sdf_particle_size = distance(sdf_pos, sdf_pos2);
372
373 float d = vec4_to_float(texture(height_field_texture, uv_pos)) * SDF_MAX_LENGTH;
374
375 // Allowing for a small epsilon to allow particle just touching colliders to count as collided
376 const float EPSILON = 0.001;
377 d -= sdf_particle_size;
378 if (d < EPSILON) {
379 vec2 n = normalize(vec2(
380 vec4_to_float(texture(height_field_texture, uv_pos + vec2(EPSILON, 0.0))) - vec4_to_float(texture(height_field_texture, uv_pos - vec2(EPSILON, 0.0))),
381 vec4_to_float(texture(height_field_texture, uv_pos + vec2(0.0, EPSILON))) - vec4_to_float(texture(height_field_texture, uv_pos - vec2(0.0, EPSILON)))));
382
383 collided = true;
384 sdf_pos2 = sdf_pos + n * d;
385 pos2 = vec2(dot(vec4(sdf_pos2, 0, 1), colliders[0].transform[2]), dot(vec4(sdf_pos2, 0, 1), colliders[0].transform[3]));
386
387 n = pos - pos2;
388
389 collision_normal = normalize(vec3(n, 0.0));
390 collision_depth = length(n);
391 }
392 }
393
394 } else {
395 for (uint i = 0u; i < collider_count; i++) {
396 vec3 normal;
397 float depth;
398 bool col = false;
399
400 vec3 rel_vec = xform[3].xyz - colliders[i].transform[3].xyz;
401 vec3 local_pos = rel_vec * mat3(colliders[i].transform);
402
403 // Allowing for a small epsilon to allow particle just touching colliders to count as collided
404 const float EPSILON = 0.001;
405 if (colliders[i].type == COLLIDER_TYPE_SPHERE) {
406 float d = length(rel_vec) - (particle_size + colliders[i].extents.x);
407
408 if (d < EPSILON) {
409 col = true;
410 depth = -d;
411 normal = normalize(rel_vec);
412 }
413 } else if (colliders[i].type == COLLIDER_TYPE_BOX) {
414 vec3 abs_pos = abs(local_pos);
415 vec3 sgn_pos = sign(local_pos);
416
417 if (any(greaterThan(abs_pos, colliders[i].extents.xyz))) {
418 //point outside box
419
420 vec3 closest = min(abs_pos, colliders[i].extents.xyz);
421 vec3 rel = abs_pos - closest;
422 depth = length(rel) - particle_size;
423 if (depth < EPSILON) {
424 col = true;
425 normal = mat3(colliders[i].transform) * (normalize(rel) * sgn_pos);
426 depth = -depth;
427 }
428 } else {
429 //point inside box
430 vec3 axis_len = colliders[i].extents.xyz - abs_pos;
431 // there has to be a faster way to do this?
432 if (all(lessThan(axis_len.xx, axis_len.yz))) {
433 normal = vec3(1, 0, 0);
434 } else if (all(lessThan(axis_len.yy, axis_len.xz))) {
435 normal = vec3(0, 1, 0);
436 } else {
437 normal = vec3(0, 0, 1);
438 }
439
440 col = true;
441 depth = dot(normal * axis_len, vec3(1)) + particle_size;
442 normal = mat3(colliders[i].transform) * (normal * sgn_pos);
443 }
444 } else if (colliders[i].type == COLLIDER_TYPE_SDF) {
445 } else if (colliders[i].type == COLLIDER_TYPE_HEIGHT_FIELD) {
446 vec3 local_pos_bottom = local_pos;
447 local_pos_bottom.y -= particle_size;
448
449 if (any(greaterThan(abs(local_pos_bottom), colliders[i].extents.xyz))) {
450 continue;
451 }
452 const float DELTA = 1.0 / 8192.0;
453
454 vec3 uvw_pos = vec3(local_pos_bottom / colliders[i].extents.xyz) * 0.5 + 0.5;
455
456 float y = texture(height_field_texture, uvw_pos.xz).r;
457
458 if (y + EPSILON > uvw_pos.y) {
459 //inside heightfield
460
461 vec3 pos1 = (vec3(uvw_pos.x, y, uvw_pos.z) * 2.0 - 1.0) * colliders[i].extents.xyz;
462 vec3 pos2 = (vec3(uvw_pos.x + DELTA, texture(height_field_texture, uvw_pos.xz + vec2(DELTA, 0)).r, uvw_pos.z) * 2.0 - 1.0) * colliders[i].extents.xyz;
463 vec3 pos3 = (vec3(uvw_pos.x, texture(height_field_texture, uvw_pos.xz + vec2(0, DELTA)).r, uvw_pos.z + DELTA) * 2.0 - 1.0) * colliders[i].extents.xyz;
464
465 normal = normalize(cross(pos1 - pos2, pos1 - pos3));
466 float local_y = (vec3(local_pos / colliders[i].extents.xyz) * 0.5 + 0.5).y;
467
468 col = true;
469 depth = dot(normal, pos1) - dot(normal, local_pos_bottom);
470 }
471 }
472
473 if (col) {
474 if (!collided) {
475 collided = true;
476 collision_normal = normal;
477 collision_depth = depth;
478 } else {
479 vec3 c = collision_normal * collision_depth;
480 c += normal * max(0.0, depth - dot(normal, c));
481 collision_normal = normalize(c);
482 collision_depth = length(c);
483 }
484 }
485 }
486 }
487 }
488
489 if (particle_active) {
490 #CODE : PROCESS
491 }
492
493 flags &= ~PARTICLE_FLAG_ACTIVE;
494 if (particle_active) {
495 flags |= PARTICLE_FLAG_ACTIVE;
496 }
497
498 xform = transpose(xform);
499 out_xform_1 = xform[0];
500 out_xform_2 = xform[1];
501 #ifdef MODE_3D
502 out_xform_3 = xform[2];
503 #endif
504 out_velocity_flags.w = uintBitsToFloat(flags);
505 }
506
507 /* clang-format off */
508 #[fragment]
509
510 void main() {
511 }
512 /* clang-format on */