particles.glsl 20 KB

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