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. layout(set = 3, binding = 0, std140) uniform MaterialUniforms{
  137. #MATERIAL_UNIFORMS
  138. } material;
  139. #endif
  140. layout(push_constant, std430) uniform Params {
  141. float lifetime;
  142. bool clear;
  143. uint total_particles;
  144. uint trail_size;
  145. bool use_fractional_delta;
  146. bool sub_emitter_mode;
  147. bool can_emit;
  148. bool trail_pass;
  149. }
  150. params;
  151. uint hash(uint x) {
  152. x = ((x >> uint(16)) ^ x) * uint(0x45d9f3b);
  153. x = ((x >> uint(16)) ^ x) * uint(0x45d9f3b);
  154. x = (x >> uint(16)) ^ x;
  155. return x;
  156. }
  157. bool emit_subparticle(mat4 p_xform, vec3 p_velocity, vec4 p_color, vec4 p_custom, uint p_flags) {
  158. if (!params.can_emit) {
  159. return false;
  160. }
  161. bool valid = false;
  162. int dst_index = atomicAdd(dst_particles.particle_count, 1);
  163. if (dst_index >= dst_particles.particle_max) {
  164. atomicAdd(dst_particles.particle_count, -1);
  165. return false;
  166. }
  167. dst_particles.data[dst_index].xform = p_xform;
  168. dst_particles.data[dst_index].velocity = p_velocity;
  169. dst_particles.data[dst_index].color = p_color;
  170. dst_particles.data[dst_index].custom = p_custom;
  171. dst_particles.data[dst_index].flags = p_flags;
  172. return true;
  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. #GLOBALS
  182. void main() {
  183. uint particle = gl_GlobalInvocationID.x;
  184. if (params.trail_size > 1) {
  185. if (params.trail_pass) {
  186. if (particle >= params.total_particles * (params.trail_size - 1)) {
  187. return;
  188. }
  189. particle += (particle / (params.trail_size - 1)) + 1;
  190. } else {
  191. if (particle >= params.total_particles) {
  192. return;
  193. }
  194. particle *= params.trail_size;
  195. }
  196. }
  197. if (particle >= params.total_particles * params.trail_size) {
  198. return; //discard
  199. }
  200. uint index = particle / params.trail_size;
  201. uint frame = (particle % params.trail_size);
  202. #define FRAME frame_history.data[frame]
  203. #define PARTICLE particles.data[particle]
  204. bool apply_forces = true;
  205. bool apply_velocity = true;
  206. float local_delta = FRAME.delta;
  207. float mass = 1.0;
  208. bool restart = false;
  209. bool restart_position = false;
  210. bool restart_rotation_scale = false;
  211. bool restart_velocity = false;
  212. bool restart_color = false;
  213. bool restart_custom = false;
  214. if (params.clear) {
  215. PARTICLE.color = vec4(1.0);
  216. PARTICLE.custom = vec4(0.0);
  217. PARTICLE.velocity = vec3(0.0);
  218. PARTICLE.flags = 0;
  219. PARTICLE.xform = mat4(
  220. vec4(1.0, 0.0, 0.0, 0.0),
  221. vec4(0.0, 1.0, 0.0, 0.0),
  222. vec4(0.0, 0.0, 1.0, 0.0),
  223. vec4(0.0, 0.0, 0.0, 1.0));
  224. }
  225. //clear started flag if set
  226. if (params.trail_pass) {
  227. //trail started
  228. uint src_idx = index * params.trail_size;
  229. if (bool(particles.data[src_idx].flags & PARTICLE_FLAG_STARTED)) {
  230. //save start conditions for trails
  231. PARTICLE.color = particles.data[src_idx].color;
  232. PARTICLE.custom = particles.data[src_idx].custom;
  233. PARTICLE.velocity = particles.data[src_idx].velocity;
  234. 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
  235. PARTICLE.xform = particles.data[src_idx].xform;
  236. #ifdef USERDATA1_USED
  237. PARTICLE.userdata1 = particles.data[src_idx].userdata1;
  238. #endif
  239. #ifdef USERDATA2_USED
  240. PARTICLE.userdata2 = particles.data[src_idx].userdata2;
  241. #endif
  242. #ifdef USERDATA3_USED
  243. PARTICLE.userdata3 = particles.data[src_idx].userdata3;
  244. #endif
  245. #ifdef USERDATA4_USED
  246. PARTICLE.userdata4 = particles.data[src_idx].userdata4;
  247. #endif
  248. #ifdef USERDATA5_USED
  249. PARTICLE.userdata5 = particles.data[src_idx].userdata5;
  250. #endif
  251. #ifdef USERDATA6_USED
  252. PARTICLE.userdata6 = particles.data[src_idx].userdata6;
  253. #endif
  254. }
  255. if (!bool(particles.data[src_idx].flags & PARTICLE_FLAG_ACTIVE)) {
  256. // Disable the entire trail if the parent is no longer active.
  257. PARTICLE.flags = 0;
  258. return;
  259. }
  260. 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
  261. // we just assume that this is the first frame of the particle, the rest is deterministic
  262. PARTICLE.flags = PARTICLE_FLAG_ACTIVE | (particles.data[src_idx].flags & (PARTICLE_FRAME_MASK << PARTICLE_FRAME_SHIFT));
  263. return; //- this appears like it should be correct, but it seems not to be.. wonder why.
  264. }
  265. } else {
  266. PARTICLE.flags &= ~PARTICLE_FLAG_STARTED;
  267. }
  268. bool collided = false;
  269. vec3 collision_normal = vec3(0.0);
  270. float collision_depth = 0.0;
  271. vec3 attractor_force = vec3(0.0);
  272. #if !defined(DISABLE_VELOCITY)
  273. if (bool(PARTICLE.flags & PARTICLE_FLAG_ACTIVE)) {
  274. PARTICLE.xform[3].xyz += PARTICLE.velocity * local_delta;
  275. }
  276. #endif
  277. if (!params.trail_pass && params.sub_emitter_mode) {
  278. if (!bool(PARTICLE.flags & PARTICLE_FLAG_ACTIVE)) {
  279. int src_index = atomicAdd(src_particles.particle_count, -1) - 1;
  280. if (src_index >= 0) {
  281. PARTICLE.flags = (PARTICLE_FLAG_ACTIVE | PARTICLE_FLAG_STARTED | (FRAME.cycle << PARTICLE_FRAME_SHIFT));
  282. restart = true;
  283. if (bool(src_particles.data[src_index].flags & EMISSION_FLAG_HAS_POSITION)) {
  284. PARTICLE.xform[3] = src_particles.data[src_index].xform[3];
  285. } else {
  286. PARTICLE.xform[3] = vec4(0, 0, 0, 1);
  287. restart_position = true;
  288. }
  289. if (bool(src_particles.data[src_index].flags & EMISSION_FLAG_HAS_ROTATION_SCALE)) {
  290. PARTICLE.xform[0] = src_particles.data[src_index].xform[0];
  291. PARTICLE.xform[1] = src_particles.data[src_index].xform[1];
  292. PARTICLE.xform[2] = src_particles.data[src_index].xform[2];
  293. } else {
  294. PARTICLE.xform[0] = vec4(1, 0, 0, 0);
  295. PARTICLE.xform[1] = vec4(0, 1, 0, 0);
  296. PARTICLE.xform[2] = vec4(0, 0, 1, 0);
  297. restart_rotation_scale = true;
  298. }
  299. if (bool(src_particles.data[src_index].flags & EMISSION_FLAG_HAS_VELOCITY)) {
  300. PARTICLE.velocity = src_particles.data[src_index].velocity;
  301. } else {
  302. PARTICLE.velocity = vec3(0);
  303. restart_velocity = true;
  304. }
  305. if (bool(src_particles.data[src_index].flags & EMISSION_FLAG_HAS_COLOR)) {
  306. PARTICLE.color = src_particles.data[src_index].color;
  307. } else {
  308. PARTICLE.color = vec4(1);
  309. restart_color = true;
  310. }
  311. if (bool(src_particles.data[src_index].flags & EMISSION_FLAG_HAS_CUSTOM)) {
  312. PARTICLE.custom = src_particles.data[src_index].custom;
  313. } else {
  314. PARTICLE.custom = vec4(0);
  315. restart_custom = true;
  316. }
  317. }
  318. }
  319. } else if (FRAME.emitting) {
  320. float restart_phase = float(index) / float(params.total_particles);
  321. if (FRAME.randomness > 0.0) {
  322. uint seed = FRAME.cycle;
  323. if (restart_phase >= FRAME.system_phase) {
  324. seed -= uint(1);
  325. }
  326. seed *= uint(params.total_particles);
  327. seed += uint(index);
  328. float random = float(hash(seed) % uint(65536)) / 65536.0;
  329. restart_phase += FRAME.randomness * random * 1.0 / float(params.total_particles);
  330. }
  331. restart_phase *= (1.0 - FRAME.explosiveness);
  332. if (FRAME.system_phase > FRAME.prev_system_phase) {
  333. // restart_phase >= prev_system_phase is used so particles emit in the first frame they are processed
  334. if (restart_phase >= FRAME.prev_system_phase && restart_phase < FRAME.system_phase) {
  335. restart = true;
  336. if (params.use_fractional_delta) {
  337. local_delta = (FRAME.system_phase - restart_phase) * params.lifetime;
  338. }
  339. }
  340. } else if (FRAME.delta > 0.0) {
  341. if (restart_phase >= FRAME.prev_system_phase) {
  342. restart = true;
  343. if (params.use_fractional_delta) {
  344. local_delta = (1.0 - restart_phase + FRAME.system_phase) * params.lifetime;
  345. }
  346. } else if (restart_phase < FRAME.system_phase) {
  347. restart = true;
  348. if (params.use_fractional_delta) {
  349. local_delta = (FRAME.system_phase - restart_phase) * params.lifetime;
  350. }
  351. }
  352. }
  353. if (params.trail_pass) {
  354. restart = false;
  355. }
  356. if (restart) {
  357. PARTICLE.flags = FRAME.emitting ? (PARTICLE_FLAG_ACTIVE | PARTICLE_FLAG_STARTED | (FRAME.cycle << PARTICLE_FRAME_SHIFT)) : 0;
  358. restart_position = true;
  359. restart_rotation_scale = true;
  360. restart_velocity = true;
  361. restart_color = true;
  362. restart_custom = true;
  363. }
  364. }
  365. bool particle_active = bool(PARTICLE.flags & PARTICLE_FLAG_ACTIVE);
  366. uint particle_number = (PARTICLE.flags >> PARTICLE_FRAME_SHIFT) * uint(params.total_particles) + index;
  367. if (restart && particle_active) {
  368. #CODE : START
  369. }
  370. if (particle_active) {
  371. for (uint i = 0; i < FRAME.attractor_count; i++) {
  372. vec3 dir;
  373. float amount;
  374. vec3 rel_vec = PARTICLE.xform[3].xyz - FRAME.attractors[i].transform[3].xyz;
  375. vec3 local_pos = rel_vec * mat3(FRAME.attractors[i].transform);
  376. switch (FRAME.attractors[i].type) {
  377. case ATTRACTOR_TYPE_SPHERE: {
  378. dir = safe_normalize(rel_vec);
  379. float d = length(local_pos) / FRAME.attractors[i].extents.x;
  380. if (d > 1.0) {
  381. continue;
  382. }
  383. amount = max(0.0, 1.0 - d);
  384. } break;
  385. case ATTRACTOR_TYPE_BOX: {
  386. dir = safe_normalize(rel_vec);
  387. vec3 abs_pos = abs(local_pos / FRAME.attractors[i].extents);
  388. float d = max(abs_pos.x, max(abs_pos.y, abs_pos.z));
  389. if (d > 1.0) {
  390. continue;
  391. }
  392. amount = max(0.0, 1.0 - d);
  393. } break;
  394. case ATTRACTOR_TYPE_VECTOR_FIELD: {
  395. vec3 uvw_pos = (local_pos / FRAME.attractors[i].extents + 1.0) * 0.5;
  396. if (any(lessThan(uvw_pos, vec3(0.0))) || any(greaterThan(uvw_pos, vec3(1.0)))) {
  397. continue;
  398. }
  399. vec3 s = texture(sampler3D(sdf_vec_textures[FRAME.attractors[i].texture_index], SAMPLER_LINEAR_CLAMP), uvw_pos).xyz * -2.0 + 1.0;
  400. dir = mat3(FRAME.attractors[i].transform) * safe_normalize(s); //revert direction
  401. amount = length(s);
  402. } break;
  403. }
  404. amount = pow(amount, FRAME.attractors[i].attenuation);
  405. dir = safe_normalize(mix(dir, FRAME.attractors[i].transform[2].xyz, FRAME.attractors[i].directionality));
  406. attractor_force -= amount * dir * FRAME.attractors[i].strength;
  407. }
  408. float particle_size = FRAME.particle_size;
  409. #ifdef USE_COLLISION_SCALE
  410. particle_size *= dot(vec3(length(PARTICLE.xform[0].xyz), length(PARTICLE.xform[1].xyz), length(PARTICLE.xform[2].xyz)), vec3(0.33333333333));
  411. #endif
  412. if (FRAME.collider_count == 1 && FRAME.colliders[0].type == COLLIDER_TYPE_2D_SDF) {
  413. //2D collision
  414. vec2 pos = PARTICLE.xform[3].xy;
  415. vec4 to_sdf_x = FRAME.colliders[0].transform[0];
  416. vec4 to_sdf_y = FRAME.colliders[0].transform[1];
  417. vec2 sdf_pos = vec2(dot(vec4(pos, 0, 1), to_sdf_x), dot(vec4(pos, 0, 1), to_sdf_y));
  418. vec4 sdf_to_screen = vec4(FRAME.colliders[0].extents, FRAME.colliders[0].scale);
  419. vec2 uv_pos = sdf_pos * sdf_to_screen.xy + sdf_to_screen.zw;
  420. if (all(greaterThan(uv_pos, vec2(0.0))) && all(lessThan(uv_pos, vec2(1.0)))) {
  421. vec2 pos2 = pos + vec2(0, particle_size);
  422. vec2 sdf_pos2 = vec2(dot(vec4(pos2, 0, 1), to_sdf_x), dot(vec4(pos2, 0, 1), to_sdf_y));
  423. float sdf_particle_size = distance(sdf_pos, sdf_pos2);
  424. float d = texture(sampler2D(height_field_texture, SAMPLER_LINEAR_CLAMP), uv_pos).r * SDF_MAX_LENGTH;
  425. d -= sdf_particle_size;
  426. if (d < 0.0) {
  427. const float EPSILON = 0.001;
  428. vec2 n = normalize(vec2(
  429. 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,
  430. 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));
  431. collided = true;
  432. sdf_pos2 = sdf_pos + n * d;
  433. pos2 = vec2(dot(vec4(sdf_pos2, 0, 1), FRAME.colliders[0].transform[2]), dot(vec4(sdf_pos2, 0, 1), FRAME.colliders[0].transform[3]));
  434. n = pos - pos2;
  435. collision_normal = normalize(vec3(n, 0.0));
  436. collision_depth = length(n);
  437. }
  438. }
  439. } else {
  440. for (uint i = 0; i < FRAME.collider_count; i++) {
  441. vec3 normal;
  442. float depth;
  443. bool col = false;
  444. vec3 rel_vec = PARTICLE.xform[3].xyz - FRAME.colliders[i].transform[3].xyz;
  445. vec3 local_pos = rel_vec * mat3(FRAME.colliders[i].transform);
  446. // Allowing for a small epsilon to allow particle just touching colliders to count as collided
  447. const float EPSILON = 0.001;
  448. switch (FRAME.colliders[i].type) {
  449. case COLLIDER_TYPE_SPHERE: {
  450. float d = length(rel_vec) - (particle_size + FRAME.colliders[i].extents.x);
  451. if (d <= EPSILON) {
  452. col = true;
  453. depth = -d;
  454. normal = normalize(rel_vec);
  455. }
  456. } break;
  457. case COLLIDER_TYPE_BOX: {
  458. vec3 abs_pos = abs(local_pos);
  459. vec3 sgn_pos = sign(local_pos);
  460. if (any(greaterThan(abs_pos, FRAME.colliders[i].extents))) {
  461. //point outside box
  462. vec3 closest = min(abs_pos, FRAME.colliders[i].extents);
  463. vec3 rel = abs_pos - closest;
  464. depth = length(rel) - particle_size;
  465. if (depth <= EPSILON) {
  466. col = true;
  467. normal = mat3(FRAME.colliders[i].transform) * (normalize(rel) * sgn_pos);
  468. depth = -depth;
  469. }
  470. } else {
  471. //point inside box
  472. vec3 axis_len = FRAME.colliders[i].extents - abs_pos;
  473. // there has to be a faster way to do this?
  474. if (all(lessThan(axis_len.xx, axis_len.yz))) {
  475. normal = vec3(1, 0, 0);
  476. } else if (all(lessThan(axis_len.yy, axis_len.xz))) {
  477. normal = vec3(0, 1, 0);
  478. } else {
  479. normal = vec3(0, 0, 1);
  480. }
  481. col = true;
  482. depth = dot(normal * axis_len, vec3(1)) + particle_size;
  483. normal = mat3(FRAME.colliders[i].transform) * (normal * sgn_pos);
  484. }
  485. } break;
  486. case COLLIDER_TYPE_SDF: {
  487. vec3 apos = abs(local_pos);
  488. float extra_dist = 0.0;
  489. if (any(greaterThan(apos, FRAME.colliders[i].extents))) { //outside
  490. vec3 mpos = min(apos, FRAME.colliders[i].extents);
  491. extra_dist = distance(mpos, apos);
  492. }
  493. if (extra_dist > particle_size) {
  494. continue;
  495. }
  496. vec3 uvw_pos = (local_pos / FRAME.colliders[i].extents) * 0.5 + 0.5;
  497. float s = texture(sampler3D(sdf_vec_textures[FRAME.colliders[i].texture_index], SAMPLER_LINEAR_CLAMP), uvw_pos).r;
  498. s *= FRAME.colliders[i].scale;
  499. s += extra_dist;
  500. if (s <= particle_size + EPSILON) {
  501. col = true;
  502. depth = particle_size - s;
  503. normal = mat3(FRAME.colliders[i].transform) *
  504. normalize(
  505. vec3(
  506. 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,
  507. 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,
  508. 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));
  509. }
  510. } break;
  511. case COLLIDER_TYPE_HEIGHT_FIELD: {
  512. vec3 local_pos_bottom = local_pos;
  513. local_pos_bottom.y -= particle_size;
  514. if (any(greaterThan(abs(local_pos_bottom), FRAME.colliders[i].extents))) {
  515. continue;
  516. }
  517. const float DELTA = 1.0 / 8192.0;
  518. vec3 uvw_pos = vec3(local_pos_bottom / FRAME.colliders[i].extents) * 0.5 + 0.5;
  519. float y = texture(sampler2D(height_field_texture, SAMPLER_LINEAR_CLAMP), uvw_pos.xz).r;
  520. if (y + EPSILON >= uvw_pos.y) {
  521. //inside heightfield
  522. vec3 pos1 = (vec3(uvw_pos.x, y, uvw_pos.z) * 2.0 - 1.0) * FRAME.colliders[i].extents;
  523. 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;
  524. 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;
  525. normal = normalize(cross(pos1 - pos2, pos1 - pos3));
  526. float local_y = (vec3(local_pos / FRAME.colliders[i].extents) * 0.5 + 0.5).y;
  527. col = true;
  528. depth = dot(normal, pos1) - dot(normal, local_pos_bottom);
  529. }
  530. } break;
  531. }
  532. if (col) {
  533. if (!collided) {
  534. collided = true;
  535. collision_normal = normal;
  536. collision_depth = depth;
  537. } else {
  538. vec3 c = collision_normal * collision_depth;
  539. c += normal * max(0.0, depth - dot(normal, c));
  540. collision_normal = normalize(c);
  541. collision_depth = length(c);
  542. }
  543. }
  544. }
  545. }
  546. }
  547. if (particle_active) {
  548. #CODE : PROCESS
  549. }
  550. PARTICLE.flags &= ~PARTICLE_FLAG_ACTIVE;
  551. if (particle_active) {
  552. PARTICLE.flags |= PARTICLE_FLAG_ACTIVE;
  553. }
  554. }