| 1 |
#[compute] |
| 2 |
|
| 3 |
#version 450 |
| 4 |
|
| 5 |
#VERSION_DEFINES |
| 6 |
|
| 7 |
layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in; |
| 8 |
|
| 9 |
#define SDF_MAX_LENGTH 16384.0 |
| 10 |
|
| 11 |
/* SET 0: GLOBAL DATA */ |
| 12 |
|
| 13 |
#include "samplers_inc.glsl" |
| 14 |
|
| 15 |
layout(set = 0, binding = 2, std430) restrict readonly buffer GlobalShaderUniformData { |
| 16 |
vec4 data[]; |
| 17 |
} |
| 18 |
global_shader_uniforms; |
| 19 |
|
| 20 |
/* Set 1: FRAME AND PARTICLE DATA */ |
| 21 |
|
| 22 |
// a frame history is kept for trail deterministic behavior |
| 23 |
|
| 24 |
#define MAX_ATTRACTORS 32 |
| 25 |
|
| 26 |
#define ATTRACTOR_TYPE_SPHERE 0 |
| 27 |
#define ATTRACTOR_TYPE_BOX 1 |
| 28 |
#define ATTRACTOR_TYPE_VECTOR_FIELD 2 |
| 29 |
|
| 30 |
struct Attractor { |
| 31 |
mat4 transform; |
| 32 |
vec3 extents; //exents or radius |
| 33 |
uint type; |
| 34 |
uint texture_index; //texture index for vector field |
| 35 |
float strength; |
| 36 |
float attenuation; |
| 37 |
float directionality; |
| 38 |
}; |
| 39 |
|
| 40 |
#define MAX_COLLIDERS 32 |
| 41 |
|
| 42 |
#define COLLIDER_TYPE_SPHERE 0 |
| 43 |
#define COLLIDER_TYPE_BOX 1 |
| 44 |
#define COLLIDER_TYPE_SDF 2 |
| 45 |
#define COLLIDER_TYPE_HEIGHT_FIELD 3 |
| 46 |
#define COLLIDER_TYPE_2D_SDF 4 |
| 47 |
|
| 48 |
struct Collider { |
| 49 |
mat4 transform; |
| 50 |
vec3 extents; //exents or radius |
| 51 |
uint type; |
| 52 |
|
| 53 |
uint texture_index; //texture index for vector field |
| 54 |
float scale; |
| 55 |
uint pad[2]; |
| 56 |
}; |
| 57 |
|
| 58 |
struct FrameParams { |
| 59 |
bool emitting; |
| 60 |
float system_phase; |
| 61 |
float prev_system_phase; |
| 62 |
uint cycle; |
| 63 |
|
| 64 |
float explosiveness; |
| 65 |
float randomness; |
| 66 |
float time; |
| 67 |
float delta; |
| 68 |
|
| 69 |
uint frame; |
| 70 |
float amount_ratio; |
| 71 |
uint pad1; |
| 72 |
uint pad2; |
| 73 |
|
| 74 |
uint random_seed; |
| 75 |
uint attractor_count; |
| 76 |
uint collider_count; |
| 77 |
float particle_size; |
| 78 |
|
| 79 |
mat4 emission_transform; |
| 80 |
vec3 emitter_velocity; |
| 81 |
float interp_to_end; |
| 82 |
|
| 83 |
Attractor attractors[MAX_ATTRACTORS]; |
| 84 |
Collider colliders[MAX_COLLIDERS]; |
| 85 |
}; |
| 86 |
|
| 87 |
layout(set = 1, binding = 0, std430) restrict buffer FrameHistory { |
| 88 |
FrameParams data[]; |
| 89 |
} |
| 90 |
frame_history; |
| 91 |
|
| 92 |
#define PARTICLE_FLAG_ACTIVE uint(1) |
| 93 |
#define PARTICLE_FLAG_STARTED uint(2) |
| 94 |
#define PARTICLE_FLAG_TRAILED uint(4) |
| 95 |
#define PARTICLE_FRAME_MASK uint(0xFFFF) |
| 96 |
#define PARTICLE_FRAME_SHIFT uint(16) |
| 97 |
|
| 98 |
struct ParticleData { |
| 99 |
mat4 xform; |
| 100 |
vec3 velocity; |
| 101 |
uint flags; |
| 102 |
vec4 color; |
| 103 |
vec4 custom; |
| 104 |
#ifdef USERDATA1_USED |
| 105 |
vec4 userdata1; |
| 106 |
#endif |
| 107 |
#ifdef USERDATA2_USED |
| 108 |
vec4 userdata2; |
| 109 |
#endif |
| 110 |
#ifdef USERDATA3_USED |
| 111 |
vec4 userdata3; |
| 112 |
#endif |
| 113 |
#ifdef USERDATA4_USED |
| 114 |
vec4 userdata4; |
| 115 |
#endif |
| 116 |
#ifdef USERDATA5_USED |
| 117 |
vec4 userdata5; |
| 118 |
#endif |
| 119 |
#ifdef USERDATA6_USED |
| 120 |
vec4 userdata6; |
| 121 |
#endif |
| 122 |
}; |
| 123 |
|
| 124 |
layout(set = 1, binding = 1, std430) restrict buffer Particles { |
| 125 |
ParticleData data[]; |
| 126 |
} |
| 127 |
particles; |
| 128 |
|
| 129 |
#define EMISSION_FLAG_HAS_POSITION 1 |
| 130 |
#define EMISSION_FLAG_HAS_ROTATION_SCALE 2 |
| 131 |
#define EMISSION_FLAG_HAS_VELOCITY 4 |
| 132 |
#define EMISSION_FLAG_HAS_COLOR 8 |
| 133 |
#define EMISSION_FLAG_HAS_CUSTOM 16 |
| 134 |
|
| 135 |
struct ParticleEmission { |
| 136 |
mat4 xform; |
| 137 |
vec3 velocity; |
| 138 |
uint flags; |
| 139 |
vec4 color; |
| 140 |
vec4 custom; |
| 141 |
}; |
| 142 |
|
| 143 |
layout(set = 1, binding = 2, std430) restrict buffer SourceEmission { |
| 144 |
int particle_count; |
| 145 |
uint pad0; |
| 146 |
uint pad1; |
| 147 |
uint pad2; |
| 148 |
ParticleEmission data[]; |
| 149 |
} |
| 150 |
src_particles; |
| 151 |
|
| 152 |
layout(set = 1, binding = 3, std430) restrict buffer DestEmission { |
| 153 |
int particle_count; |
| 154 |
int particle_max; |
| 155 |
uint pad1; |
| 156 |
uint pad2; |
| 157 |
ParticleEmission data[]; |
| 158 |
} |
| 159 |
dst_particles; |
| 160 |
|
| 161 |
/* SET 2: COLLIDER/ATTRACTOR TEXTURES */ |
| 162 |
|
| 163 |
#define MAX_3D_TEXTURES 7 |
| 164 |
|
| 165 |
layout(set = 2, binding = 0) uniform texture3D sdf_vec_textures[MAX_3D_TEXTURES]; |
| 166 |
layout(set = 2, binding = 1) uniform texture2D height_field_texture; |
| 167 |
|
| 168 |
/* SET 3: MATERIAL */ |
| 169 |
|
| 170 |
#ifdef MATERIAL_UNIFORMS_USED |
| 171 |
/* clang-format off */ |
| 172 |
layout(set = 3, binding = 0, std140) uniform MaterialUniforms { |
| 173 |
#MATERIAL_UNIFORMS |
| 174 |
} material; |
| 175 |
/* clang-format on */ |
| 176 |
#endif |
| 177 |
|
| 178 |
layout(push_constant, std430) uniform Params { |
| 179 |
float lifetime; |
| 180 |
bool clear; |
| 181 |
uint total_particles; |
| 182 |
uint trail_size; |
| 183 |
bool use_fractional_delta; |
| 184 |
bool sub_emitter_mode; |
| 185 |
bool can_emit; |
| 186 |
bool trail_pass; |
| 187 |
} |
| 188 |
params; |
| 189 |
|
| 190 |
uint hash(uint x) { |
| 191 |
x = ((x >> uint(16)) ^ x) * uint(0x45d9f3b); |
| 192 |
x = ((x >> uint(16)) ^ x) * uint(0x45d9f3b); |
| 193 |
x = (x >> uint(16)) ^ x; |
| 194 |
return x; |
| 195 |
} |
| 196 |
|
| 197 |
bool emit_subparticle(mat4 p_xform, vec3 p_velocity, vec4 p_color, vec4 p_custom, uint p_flags) { |
| 198 |
if (!params.can_emit) { |
| 199 |
return false; |
| 200 |
} |
| 201 |
|
| 202 |
bool valid = false; |
| 203 |
|
| 204 |
int dst_index = atomicAdd(dst_particles.particle_count, 1); |
| 205 |
|
| 206 |
if (dst_index >= dst_particles.particle_max) { |
| 207 |
atomicAdd(dst_particles.particle_count, -1); |
| 208 |
return false; |
| 209 |
} |
| 210 |
|
| 211 |
dst_particles.data[dst_index].xform = p_xform; |
| 212 |
dst_particles.data[dst_index].velocity = p_velocity; |
| 213 |
dst_particles.data[dst_index].color = p_color; |
| 214 |
dst_particles.data[dst_index].custom = p_custom; |
| 215 |
dst_particles.data[dst_index].flags = p_flags; |
| 216 |
|
| 217 |
return true; |
| 218 |
} |
| 219 |
|
| 220 |
vec3 safe_normalize(vec3 direction) { |
| 221 |
const float EPSILON = 0.001; |
| 222 |
if (length(direction) < EPSILON) { |
| 223 |
return vec3(0.0); |
| 224 |
} |
| 225 |
return normalize(direction); |
| 226 |
} |
| 227 |
|
| 228 |
#GLOBALS |
| 229 |
|
| 230 |
void main() { |
| 231 |
uint particle = gl_GlobalInvocationID.x; |
| 232 |
|
| 233 |
if (params.trail_size > 1) { |
| 234 |
if (params.trail_pass) { |
| 235 |
if (particle >= params.total_particles * (params.trail_size - 1)) { |
| 236 |
return; |
| 237 |
} |
| 238 |
particle += (particle / (params.trail_size - 1)) + 1; |
| 239 |
} else { |
| 240 |
if (particle >= params.total_particles) { |
| 241 |
return; |
| 242 |
} |
| 243 |
particle *= params.trail_size; |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
if (particle >= params.total_particles * params.trail_size) { |
| 248 |
return; //discard |
| 249 |
} |
| 250 |
|
| 251 |
uint index = particle / params.trail_size; |
| 252 |
uint frame = (particle % params.trail_size); |
| 253 |
|
| 254 |
#define FRAME frame_history.data[frame] |
| 255 |
#define PARTICLE particles.data[particle] |
| 256 |
|
| 257 |
bool apply_forces = true; |
| 258 |
bool apply_velocity = true; |
| 259 |
float local_delta = FRAME.delta; |
| 260 |
|
| 261 |
float mass = 1.0; |
| 262 |
|
| 263 |
bool restart = false; |
| 264 |
|
| 265 |
bool restart_position = false; |
| 266 |
bool restart_rotation_scale = false; |
| 267 |
bool restart_velocity = false; |
| 268 |
bool restart_color = false; |
| 269 |
bool restart_custom = false; |
| 270 |
|
| 271 |
if (params.clear) { |
| 272 |
PARTICLE.color = vec4(1.0); |
| 273 |
PARTICLE.custom = vec4(0.0); |
| 274 |
PARTICLE.velocity = vec3(0.0); |
| 275 |
PARTICLE.flags = 0; |
| 276 |
PARTICLE.xform = mat4( |
| 277 |
vec4(1.0, 0.0, 0.0, 0.0), |
| 278 |
vec4(0.0, 1.0, 0.0, 0.0), |
| 279 |
vec4(0.0, 0.0, 1.0, 0.0), |
| 280 |
vec4(0.0, 0.0, 0.0, 1.0)); |
| 281 |
} |
| 282 |
|
| 283 |
//clear started flag if set |
| 284 |
|
| 285 |
if (params.trail_pass) { |
| 286 |
//trail started |
| 287 |
uint src_idx = index * params.trail_size; |
| 288 |
if (bool(particles.data[src_idx].flags & PARTICLE_FLAG_STARTED)) { |
| 289 |
//save start conditions for trails |
| 290 |
PARTICLE.color = particles.data[src_idx].color; |
| 291 |
PARTICLE.custom = particles.data[src_idx].custom; |
| 292 |
PARTICLE.velocity = particles.data[src_idx].velocity; |
| 293 |
PARTICLE.flags = PARTICLE_FLAG_TRAILED | ((frame_history.data[0].frame & PARTICLE_FRAME_MASK) << PARTICLE_FRAME_SHIFT); //mark it as trailed, save in which frame it will start |
| 294 |
PARTICLE.xform = particles.data[src_idx].xform; |
| 295 |
#ifdef USERDATA1_USED |
| 296 |
PARTICLE.userdata1 = particles.data[src_idx].userdata1; |
| 297 |
#endif |
| 298 |
#ifdef USERDATA2_USED |
| 299 |
PARTICLE.userdata2 = particles.data[src_idx].userdata2; |
| 300 |
#endif |
| 301 |
#ifdef USERDATA3_USED |
| 302 |
PARTICLE.userdata3 = particles.data[src_idx].userdata3; |
| 303 |
#endif |
| 304 |
#ifdef USERDATA4_USED |
| 305 |
PARTICLE.userdata4 = particles.data[src_idx].userdata4; |
| 306 |
#endif |
| 307 |
#ifdef USERDATA5_USED |
| 308 |
PARTICLE.userdata5 = particles.data[src_idx].userdata5; |
| 309 |
#endif |
| 310 |
#ifdef USERDATA6_USED |
| 311 |
PARTICLE.userdata6 = particles.data[src_idx].userdata6; |
| 312 |
#endif |
| 313 |
} |
| 314 |
if (!bool(particles.data[src_idx].flags & PARTICLE_FLAG_ACTIVE)) { |
| 315 |
// Disable the entire trail if the parent is no longer active. |
| 316 |
PARTICLE.flags = 0; |
| 317 |
return; |
| 318 |
} |
| 319 |
if (bool(PARTICLE.flags & PARTICLE_FLAG_TRAILED) && ((PARTICLE.flags >> PARTICLE_FRAME_SHIFT) == (FRAME.frame & PARTICLE_FRAME_MASK))) { //check this is trailed and see if it should start now |
| 320 |
// we just assume that this is the first frame of the particle, the rest is deterministic |
| 321 |
PARTICLE.flags = PARTICLE_FLAG_ACTIVE | (particles.data[src_idx].flags & (PARTICLE_FRAME_MASK << PARTICLE_FRAME_SHIFT)); |
| 322 |
return; //- this appears like it should be correct, but it seems not to be.. wonder why. |
| 323 |
} |
| 324 |
|
| 325 |
} else { |
| 326 |
PARTICLE.flags &= ~PARTICLE_FLAG_STARTED; |
| 327 |
} |
| 328 |
|
| 329 |
bool collided = false; |
| 330 |
vec3 collision_normal = vec3(0.0); |
| 331 |
float collision_depth = 0.0; |
| 332 |
|
| 333 |
vec3 attractor_force = vec3(0.0); |
| 334 |
|
| 335 |
#if !defined(DISABLE_VELOCITY) |
| 336 |
|
| 337 |
if (bool(PARTICLE.flags & PARTICLE_FLAG_ACTIVE)) { |
| 338 |
PARTICLE.xform[3].xyz += PARTICLE.velocity * local_delta; |
| 339 |
} |
| 340 |
#endif |
| 341 |
|
| 342 |
if (!params.trail_pass && params.sub_emitter_mode) { |
| 343 |
if (!bool(PARTICLE.flags & PARTICLE_FLAG_ACTIVE)) { |
| 344 |
int src_index = atomicAdd(src_particles.particle_count, -1) - 1; |
| 345 |
|
| 346 |
if (src_index >= 0) { |
| 347 |
PARTICLE.flags = (PARTICLE_FLAG_ACTIVE | PARTICLE_FLAG_STARTED | (FRAME.cycle << PARTICLE_FRAME_SHIFT)); |
| 348 |
restart = true; |
| 349 |
|
| 350 |
if (bool(src_particles.data[src_index].flags & EMISSION_FLAG_HAS_POSITION)) { |
| 351 |
PARTICLE.xform[3] = src_particles.data[src_index].xform[3]; |
| 352 |
} else { |
| 353 |
PARTICLE.xform[3] = vec4(0, 0, 0, 1); |
| 354 |
restart_position = true; |
| 355 |
} |
| 356 |
if (bool(src_particles.data[src_index].flags & EMISSION_FLAG_HAS_ROTATION_SCALE)) { |
| 357 |
PARTICLE.xform[0] = src_particles.data[src_index].xform[0]; |
| 358 |
PARTICLE.xform[1] = src_particles.data[src_index].xform[1]; |
| 359 |
PARTICLE.xform[2] = src_particles.data[src_index].xform[2]; |
| 360 |
} else { |
| 361 |
PARTICLE.xform[0] = vec4(1, 0, 0, 0); |
| 362 |
PARTICLE.xform[1] = vec4(0, 1, 0, 0); |
| 363 |
PARTICLE.xform[2] = vec4(0, 0, 1, 0); |
| 364 |
restart_rotation_scale = true; |
| 365 |
} |
| 366 |
if (bool(src_particles.data[src_index].flags & EMISSION_FLAG_HAS_VELOCITY)) { |
| 367 |
PARTICLE.velocity = src_particles.data[src_index].velocity; |
| 368 |
} else { |
| 369 |
PARTICLE.velocity = vec3(0); |
| 370 |
restart_velocity = true; |
| 371 |
} |
| 372 |
if (bool(src_particles.data[src_index].flags & EMISSION_FLAG_HAS_COLOR)) { |
| 373 |
PARTICLE.color = src_particles.data[src_index].color; |
| 374 |
} else { |
| 375 |
PARTICLE.color = vec4(1); |
| 376 |
restart_color = true; |
| 377 |
} |
| 378 |
|
| 379 |
if (bool(src_particles.data[src_index].flags & EMISSION_FLAG_HAS_CUSTOM)) { |
| 380 |
PARTICLE.custom = src_particles.data[src_index].custom; |
| 381 |
} else { |
| 382 |
PARTICLE.custom = vec4(0); |
| 383 |
restart_custom = true; |
| 384 |
} |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
} else if (FRAME.emitting) { |
| 389 |
float restart_phase = float(index) / float(params.total_particles); |
| 390 |
|
| 391 |
if (FRAME.randomness > 0.0) { |
| 392 |
uint seed = FRAME.cycle; |
| 393 |
if (restart_phase >= FRAME.system_phase) { |
| 394 |
seed -= uint(1); |
| 395 |
} |
| 396 |
seed *= uint(params.total_particles); |
| 397 |
seed += uint(index); |
| 398 |
float random = float(hash(seed) % uint(65536)) / 65536.0; |
| 399 |
restart_phase += FRAME.randomness * random * 1.0 / float(params.total_particles); |
| 400 |
} |
| 401 |
|
| 402 |
restart_phase *= (1.0 - FRAME.explosiveness); |
| 403 |
|
| 404 |
if (FRAME.system_phase > FRAME.prev_system_phase) { |
| 405 |
// restart_phase >= prev_system_phase is used so particles emit in the first frame they are processed |
| 406 |
|
| 407 |
if (restart_phase >= FRAME.prev_system_phase && restart_phase < FRAME.system_phase) { |
| 408 |
restart = true; |
| 409 |
if (params.use_fractional_delta) { |
| 410 |
local_delta = (FRAME.system_phase - restart_phase) * params.lifetime; |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
} else if (FRAME.delta > 0.0) { |
| 415 |
if (restart_phase >= FRAME.prev_system_phase) { |
| 416 |
restart = true; |
| 417 |
if (params.use_fractional_delta) { |
| 418 |
local_delta = (1.0 - restart_phase + FRAME.system_phase) * params.lifetime; |
| 419 |
} |
| 420 |
|
| 421 |
} else if (restart_phase < FRAME.system_phase) { |
| 422 |
restart = true; |
| 423 |
if (params.use_fractional_delta) { |
| 424 |
local_delta = (FRAME.system_phase - restart_phase) * params.lifetime; |
| 425 |
} |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
if (params.trail_pass) { |
| 430 |
restart = false; |
| 431 |
} |
| 432 |
|
| 433 |
if (restart) { |
| 434 |
PARTICLE.flags = FRAME.emitting ? (PARTICLE_FLAG_ACTIVE | PARTICLE_FLAG_STARTED | (FRAME.cycle << PARTICLE_FRAME_SHIFT)) : 0; |
| 435 |
restart_position = true; |
| 436 |
restart_rotation_scale = true; |
| 437 |
restart_velocity = true; |
| 438 |
restart_color = true; |
| 439 |
restart_custom = true; |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
bool particle_active = bool(PARTICLE.flags & PARTICLE_FLAG_ACTIVE); |
| 444 |
|
| 445 |
uint particle_number = (PARTICLE.flags >> PARTICLE_FRAME_SHIFT) * uint(params.total_particles) + index; |
| 446 |
|
| 447 |
if (restart && particle_active) { |
| 448 |
#CODE : START |
| 449 |
} |
| 450 |
|
| 451 |
if (particle_active) { |
| 452 |
for (uint i = 0; i < FRAME.attractor_count; i++) { |
| 453 |
vec3 dir; |
| 454 |
float amount; |
| 455 |
vec3 rel_vec = PARTICLE.xform[3].xyz - FRAME.attractors[i].transform[3].xyz; |
| 456 |
vec3 local_pos = rel_vec * mat3(FRAME.attractors[i].transform); |
| 457 |
|
| 458 |
switch (FRAME.attractors[i].type) { |
| 459 |
case ATTRACTOR_TYPE_SPHERE: { |
| 460 |
dir = safe_normalize(rel_vec); |
| 461 |
float d = length(local_pos) / FRAME.attractors[i].extents.x; |
| 462 |
if (d > 1.0) { |
| 463 |
continue; |
| 464 |
} |
| 465 |
amount = max(0.0, 1.0 - d); |
| 466 |
} break; |
| 467 |
case ATTRACTOR_TYPE_BOX: { |
| 468 |
dir = safe_normalize(rel_vec); |
| 469 |
|
| 470 |
vec3 abs_pos = abs(local_pos / FRAME.attractors[i].extents); |
| 471 |
float d = max(abs_pos.x, max(abs_pos.y, abs_pos.z)); |
| 472 |
if (d > 1.0) { |
| 473 |
continue; |
| 474 |
} |
| 475 |
amount = max(0.0, 1.0 - d); |
| 476 |
|
| 477 |
} break; |
| 478 |
case ATTRACTOR_TYPE_VECTOR_FIELD: { |
| 479 |
vec3 uvw_pos = (local_pos / FRAME.attractors[i].extents + 1.0) * 0.5; |
| 480 |
if (any(lessThan(uvw_pos, vec3(0.0))) || any(greaterThan(uvw_pos, vec3(1.0)))) { |
| 481 |
continue; |
| 482 |
} |
| 483 |
vec3 s = texture(sampler3D(sdf_vec_textures[FRAME.attractors[i].texture_index], SAMPLER_LINEAR_CLAMP), uvw_pos).xyz * -2.0 + 1.0; |
| 484 |
dir = mat3(FRAME.attractors[i].transform) * safe_normalize(s); //revert direction |
| 485 |
amount = length(s); |
| 486 |
|
| 487 |
} break; |
| 488 |
} |
| 489 |
amount = pow(amount, FRAME.attractors[i].attenuation); |
| 490 |
dir = safe_normalize(mix(dir, FRAME.attractors[i].transform[2].xyz, FRAME.attractors[i].directionality)); |
| 491 |
attractor_force -= mass * amount * dir * FRAME.attractors[i].strength; |
| 492 |
} |
| 493 |
|
| 494 |
float particle_size = FRAME.particle_size; |
| 495 |
|
| 496 |
#ifdef USE_COLLISION_SCALE |
| 497 |
|
| 498 |
particle_size *= dot(vec3(length(PARTICLE.xform[0].xyz), length(PARTICLE.xform[1].xyz), length(PARTICLE.xform[2].xyz)), vec3(0.33333333333)); |
| 499 |
|
| 500 |
#endif |
| 501 |
|
| 502 |
if (FRAME.collider_count == 1 && FRAME.colliders[0].type == COLLIDER_TYPE_2D_SDF) { |
| 503 |
//2D collision |
| 504 |
|
| 505 |
vec2 pos = PARTICLE.xform[3].xy; |
| 506 |
vec4 to_sdf_x = FRAME.colliders[0].transform[0]; |
| 507 |
vec4 to_sdf_y = FRAME.colliders[0].transform[1]; |
| 508 |
vec2 sdf_pos = vec2(dot(vec4(pos, 0, 1), to_sdf_x), dot(vec4(pos, 0, 1), to_sdf_y)); |
| 509 |
|
| 510 |
vec4 sdf_to_screen = vec4(FRAME.colliders[0].extents, FRAME.colliders[0].scale); |
| 511 |
|
| 512 |
vec2 uv_pos = sdf_pos * sdf_to_screen.xy + sdf_to_screen.zw; |
| 513 |
|
| 514 |
if (all(greaterThan(uv_pos, vec2(0.0))) && all(lessThan(uv_pos, vec2(1.0)))) { |
| 515 |
vec2 pos2 = pos + vec2(0, particle_size); |
| 516 |
vec2 sdf_pos2 = vec2(dot(vec4(pos2, 0, 1), to_sdf_x), dot(vec4(pos2, 0, 1), to_sdf_y)); |
| 517 |
float sdf_particle_size = distance(sdf_pos, sdf_pos2); |
| 518 |
|
| 519 |
float d = texture(sampler2D(height_field_texture, SAMPLER_LINEAR_CLAMP), uv_pos).r * SDF_MAX_LENGTH; |
| 520 |
|
| 521 |
d -= sdf_particle_size; |
| 522 |
|
| 523 |
if (d < 0.0) { |
| 524 |
const float EPSILON = 0.001; |
| 525 |
vec2 n = normalize(vec2( |
| 526 |
texture(sampler2D(height_field_texture, SAMPLER_LINEAR_CLAMP), uv_pos + vec2(EPSILON, 0.0)).r - texture(sampler2D(height_field_texture, SAMPLER_LINEAR_CLAMP), uv_pos - vec2(EPSILON, 0.0)).r, |
| 527 |
texture(sampler2D(height_field_texture, SAMPLER_LINEAR_CLAMP), uv_pos + vec2(0.0, EPSILON)).r - texture(sampler2D(height_field_texture, SAMPLER_LINEAR_CLAMP), uv_pos - vec2(0.0, EPSILON)).r)); |
| 528 |
|
| 529 |
collided = true; |
| 530 |
sdf_pos2 = sdf_pos + n * d; |
| 531 |
pos2 = vec2(dot(vec4(sdf_pos2, 0, 1), FRAME.colliders[0].transform[2]), dot(vec4(sdf_pos2, 0, 1), FRAME.colliders[0].transform[3])); |
| 532 |
|
| 533 |
n = pos - pos2; |
| 534 |
|
| 535 |
collision_normal = normalize(vec3(n, 0.0)); |
| 536 |
collision_depth = length(n); |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
} else { |
| 541 |
for (uint i = 0; i < FRAME.collider_count; i++) { |
| 542 |
vec3 normal; |
| 543 |
float depth; |
| 544 |
bool col = false; |
| 545 |
|
| 546 |
vec3 rel_vec = PARTICLE.xform[3].xyz - FRAME.colliders[i].transform[3].xyz; |
| 547 |
vec3 local_pos = rel_vec * mat3(FRAME.colliders[i].transform); |
| 548 |
|
| 549 |
// Allowing for a small epsilon to allow particle just touching colliders to count as collided |
| 550 |
const float EPSILON = 0.001; |
| 551 |
switch (FRAME.colliders[i].type) { |
| 552 |
case COLLIDER_TYPE_SPHERE: { |
| 553 |
float d = length(rel_vec) - (particle_size + FRAME.colliders[i].extents.x); |
| 554 |
|
| 555 |
if (d <= EPSILON) { |
| 556 |
col = true; |
| 557 |
depth = -d; |
| 558 |
normal = normalize(rel_vec); |
| 559 |
} |
| 560 |
|
| 561 |
} break; |
| 562 |
case COLLIDER_TYPE_BOX: { |
| 563 |
vec3 abs_pos = abs(local_pos); |
| 564 |
vec3 sgn_pos = sign(local_pos); |
| 565 |
|
| 566 |
if (any(greaterThan(abs_pos, FRAME.colliders[i].extents))) { |
| 567 |
//point outside box |
| 568 |
|
| 569 |
vec3 closest = min(abs_pos, FRAME.colliders[i].extents); |
| 570 |
vec3 rel = abs_pos - closest; |
| 571 |
depth = length(rel) - particle_size; |
| 572 |
if (depth <= EPSILON) { |
| 573 |
col = true; |
| 574 |
normal = mat3(FRAME.colliders[i].transform) * (normalize(rel) * sgn_pos); |
| 575 |
depth = -depth; |
| 576 |
} |
| 577 |
} else { |
| 578 |
//point inside box |
| 579 |
vec3 axis_len = FRAME.colliders[i].extents - abs_pos; |
| 580 |
// there has to be a faster way to do this? |
| 581 |
if (all(lessThan(axis_len.xx, axis_len.yz))) { |
| 582 |
normal = vec3(1, 0, 0); |
| 583 |
} else if (all(lessThan(axis_len.yy, axis_len.xz))) { |
| 584 |
normal = vec3(0, 1, 0); |
| 585 |
} else { |
| 586 |
normal = vec3(0, 0, 1); |
| 587 |
} |
| 588 |
|
| 589 |
col = true; |
| 590 |
depth = dot(normal * axis_len, vec3(1)) + particle_size; |
| 591 |
normal = mat3(FRAME.colliders[i].transform) * (normal * sgn_pos); |
| 592 |
} |
| 593 |
|
| 594 |
} break; |
| 595 |
case COLLIDER_TYPE_SDF: { |
| 596 |
vec3 apos = abs(local_pos); |
| 597 |
float extra_dist = 0.0; |
| 598 |
if (any(greaterThan(apos, FRAME.colliders[i].extents))) { //outside |
| 599 |
vec3 mpos = min(apos, FRAME.colliders[i].extents); |
| 600 |
extra_dist = distance(mpos, apos); |
| 601 |
} |
| 602 |
|
| 603 |
if (extra_dist > particle_size) { |
| 604 |
continue; |
| 605 |
} |
| 606 |
|
| 607 |
vec3 uvw_pos = (local_pos / FRAME.colliders[i].extents) * 0.5 + 0.5; |
| 608 |
float s = texture(sampler3D(sdf_vec_textures[FRAME.colliders[i].texture_index], SAMPLER_LINEAR_CLAMP), uvw_pos).r; |
| 609 |
s *= FRAME.colliders[i].scale; |
| 610 |
s += extra_dist; |
| 611 |
if (s <= particle_size + EPSILON) { |
| 612 |
col = true; |
| 613 |
depth = particle_size - s; |
| 614 |
|
| 615 |
normal = mat3(FRAME.colliders[i].transform) * |
| 616 |
normalize( |
| 617 |
vec3( |
| 618 |
texture(sampler3D(sdf_vec_textures[FRAME.colliders[i].texture_index], SAMPLER_LINEAR_CLAMP), uvw_pos + vec3(EPSILON, 0.0, 0.0)).r - texture(sampler3D(sdf_vec_textures[FRAME.colliders[i].texture_index], SAMPLER_LINEAR_CLAMP), uvw_pos - vec3(EPSILON, 0.0, 0.0)).r, |
| 619 |
texture(sampler3D(sdf_vec_textures[FRAME.colliders[i].texture_index], SAMPLER_LINEAR_CLAMP), uvw_pos + vec3(0.0, EPSILON, 0.0)).r - texture(sampler3D(sdf_vec_textures[FRAME.colliders[i].texture_index], SAMPLER_LINEAR_CLAMP), uvw_pos - vec3(0.0, EPSILON, 0.0)).r, |
| 620 |
texture(sampler3D(sdf_vec_textures[FRAME.colliders[i].texture_index], SAMPLER_LINEAR_CLAMP), uvw_pos + vec3(0.0, 0.0, EPSILON)).r - texture(sampler3D(sdf_vec_textures[FRAME.colliders[i].texture_index], SAMPLER_LINEAR_CLAMP), uvw_pos - vec3(0.0, 0.0, EPSILON)).r)); |
| 621 |
} |
| 622 |
|
| 623 |
} break; |
| 624 |
case COLLIDER_TYPE_HEIGHT_FIELD: { |
| 625 |
vec3 local_pos_bottom = local_pos; |
| 626 |
local_pos_bottom.y -= particle_size; |
| 627 |
|
| 628 |
if (any(greaterThan(abs(local_pos_bottom), FRAME.colliders[i].extents))) { |
| 629 |
continue; |
| 630 |
} |
| 631 |
const float DELTA = 1.0 / 8192.0; |
| 632 |
|
| 633 |
vec3 uvw_pos = vec3(local_pos_bottom / FRAME.colliders[i].extents) * 0.5 + 0.5; |
| 634 |
|
| 635 |
float y = texture(sampler2D(height_field_texture, SAMPLER_LINEAR_CLAMP), uvw_pos.xz).r; |
| 636 |
|
| 637 |
if (y + EPSILON >= uvw_pos.y) { |
| 638 |
//inside heightfield |
| 639 |
|
| 640 |
vec3 pos1 = (vec3(uvw_pos.x, y, uvw_pos.z) * 2.0 - 1.0) * FRAME.colliders[i].extents; |
| 641 |
vec3 pos2 = (vec3(uvw_pos.x + DELTA, texture(sampler2D(height_field_texture, SAMPLER_LINEAR_CLAMP), uvw_pos.xz + vec2(DELTA, 0)).r, uvw_pos.z) * 2.0 - 1.0) * FRAME.colliders[i].extents; |
| 642 |
vec3 pos3 = (vec3(uvw_pos.x, texture(sampler2D(height_field_texture, SAMPLER_LINEAR_CLAMP), uvw_pos.xz + vec2(0, DELTA)).r, uvw_pos.z + DELTA) * 2.0 - 1.0) * FRAME.colliders[i].extents; |
| 643 |
|
| 644 |
normal = normalize(cross(pos1 - pos2, pos1 - pos3)); |
| 645 |
float local_y = (vec3(local_pos / FRAME.colliders[i].extents) * 0.5 + 0.5).y; |
| 646 |
|
| 647 |
col = true; |
| 648 |
depth = dot(normal, pos1) - dot(normal, local_pos_bottom); |
| 649 |
} |
| 650 |
|
| 651 |
} break; |
| 652 |
} |
| 653 |
|
| 654 |
if (col) { |
| 655 |
if (!collided) { |
| 656 |
collided = true; |
| 657 |
collision_normal = normal; |
| 658 |
collision_depth = depth; |
| 659 |
} else { |
| 660 |
vec3 c = collision_normal * collision_depth; |
| 661 |
c += normal * max(0.0, depth - dot(normal, c)); |
| 662 |
collision_normal = normalize(c); |
| 663 |
collision_depth = length(c); |
| 664 |
} |
| 665 |
} |
| 666 |
} |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
if (particle_active) { |
| 671 |
#CODE : PROCESS |
| 672 |
} |
| 673 |
|
| 674 |
PARTICLE.flags &= ~PARTICLE_FLAG_ACTIVE; |
| 675 |
if (particle_active) { |
| 676 |
PARTICLE.flags |= PARTICLE_FLAG_ACTIVE; |
| 677 |
} |
| 678 |
} |