particles.glsl 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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. }
  237. if (!bool(particles.data[src_idx].flags & PARTICLE_FLAG_ACTIVE)) {
  238. // Disable the entire trail if the parent is no longer active.
  239. PARTICLE.flags = 0;
  240. return;
  241. }
  242. 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
  243. // we just assume that this is the first frame of the particle, the rest is deterministic
  244. PARTICLE.flags = PARTICLE_FLAG_ACTIVE | (particles.data[src_idx].flags & (PARTICLE_FRAME_MASK << PARTICLE_FRAME_SHIFT));
  245. return; //- this appears like it should be correct, but it seems not to be.. wonder why.
  246. }
  247. } else {
  248. PARTICLE.flags &= ~PARTICLE_FLAG_STARTED;
  249. }
  250. bool collided = false;
  251. vec3 collision_normal = vec3(0.0);
  252. float collision_depth = 0.0;
  253. vec3 attractor_force = vec3(0.0);
  254. #if !defined(DISABLE_VELOCITY)
  255. if (bool(PARTICLE.flags & PARTICLE_FLAG_ACTIVE)) {
  256. PARTICLE.xform[3].xyz += PARTICLE.velocity * local_delta;
  257. }
  258. #endif
  259. if (!params.trail_pass && params.sub_emitter_mode) {
  260. if (!bool(PARTICLE.flags & PARTICLE_FLAG_ACTIVE)) {
  261. int src_index = atomicAdd(src_particles.particle_count, -1) - 1;
  262. if (src_index >= 0) {
  263. PARTICLE.flags = (PARTICLE_FLAG_ACTIVE | PARTICLE_FLAG_STARTED | (FRAME.cycle << PARTICLE_FRAME_SHIFT));
  264. restart = true;
  265. if (bool(src_particles.data[src_index].flags & EMISSION_FLAG_HAS_POSITION)) {
  266. PARTICLE.xform[3] = src_particles.data[src_index].xform[3];
  267. } else {
  268. PARTICLE.xform[3] = vec4(0, 0, 0, 1);
  269. restart_position = true;
  270. }
  271. if (bool(src_particles.data[src_index].flags & EMISSION_FLAG_HAS_ROTATION_SCALE)) {
  272. PARTICLE.xform[0] = src_particles.data[src_index].xform[0];
  273. PARTICLE.xform[1] = src_particles.data[src_index].xform[1];
  274. PARTICLE.xform[2] = src_particles.data[src_index].xform[2];
  275. } else {
  276. PARTICLE.xform[0] = vec4(1, 0, 0, 0);
  277. PARTICLE.xform[1] = vec4(0, 1, 0, 0);
  278. PARTICLE.xform[2] = vec4(0, 0, 1, 0);
  279. restart_rotation_scale = true;
  280. }
  281. if (bool(src_particles.data[src_index].flags & EMISSION_FLAG_HAS_VELOCITY)) {
  282. PARTICLE.velocity = src_particles.data[src_index].velocity;
  283. } else {
  284. PARTICLE.velocity = vec3(0);
  285. restart_velocity = true;
  286. }
  287. if (bool(src_particles.data[src_index].flags & EMISSION_FLAG_HAS_COLOR)) {
  288. PARTICLE.color = src_particles.data[src_index].color;
  289. } else {
  290. PARTICLE.color = vec4(1);
  291. restart_color = true;
  292. }
  293. if (bool(src_particles.data[src_index].flags & EMISSION_FLAG_HAS_CUSTOM)) {
  294. PARTICLE.custom = src_particles.data[src_index].custom;
  295. } else {
  296. PARTICLE.custom = vec4(0);
  297. restart_custom = true;
  298. }
  299. }
  300. }
  301. } else if (FRAME.emitting) {
  302. float restart_phase = float(index) / float(params.total_particles);
  303. if (FRAME.randomness > 0.0) {
  304. uint seed = FRAME.cycle;
  305. if (restart_phase >= FRAME.system_phase) {
  306. seed -= uint(1);
  307. }
  308. seed *= uint(params.total_particles);
  309. seed += uint(index);
  310. float random = float(hash(seed) % uint(65536)) / 65536.0;
  311. restart_phase += FRAME.randomness * random * 1.0 / float(params.total_particles);
  312. }
  313. restart_phase *= (1.0 - FRAME.explosiveness);
  314. if (FRAME.system_phase > FRAME.prev_system_phase) {
  315. // restart_phase >= prev_system_phase is used so particles emit in the first frame they are processed
  316. if (restart_phase >= FRAME.prev_system_phase && restart_phase < FRAME.system_phase) {
  317. restart = true;
  318. if (params.use_fractional_delta) {
  319. local_delta = (FRAME.system_phase - restart_phase) * params.lifetime;
  320. }
  321. }
  322. } else if (FRAME.delta > 0.0) {
  323. if (restart_phase >= FRAME.prev_system_phase) {
  324. restart = true;
  325. if (params.use_fractional_delta) {
  326. local_delta = (1.0 - restart_phase + FRAME.system_phase) * params.lifetime;
  327. }
  328. } else if (restart_phase < FRAME.system_phase) {
  329. restart = true;
  330. if (params.use_fractional_delta) {
  331. local_delta = (FRAME.system_phase - restart_phase) * params.lifetime;
  332. }
  333. }
  334. }
  335. if (params.trail_pass) {
  336. restart = false;
  337. }
  338. if (restart) {
  339. PARTICLE.flags = FRAME.emitting ? (PARTICLE_FLAG_ACTIVE | PARTICLE_FLAG_STARTED | (FRAME.cycle << PARTICLE_FRAME_SHIFT)) : 0;
  340. restart_position = true;
  341. restart_rotation_scale = true;
  342. restart_velocity = true;
  343. restart_color = true;
  344. restart_custom = true;
  345. }
  346. }
  347. bool particle_active = bool(PARTICLE.flags & PARTICLE_FLAG_ACTIVE);
  348. uint particle_number = (PARTICLE.flags >> PARTICLE_FRAME_SHIFT) * uint(params.total_particles) + index;
  349. if (restart && particle_active) {
  350. #CODE : START
  351. }
  352. if (particle_active) {
  353. for (uint i = 0; i < FRAME.attractor_count; i++) {
  354. vec3 dir;
  355. float amount;
  356. vec3 rel_vec = PARTICLE.xform[3].xyz - FRAME.attractors[i].transform[3].xyz;
  357. vec3 local_pos = rel_vec * mat3(FRAME.attractors[i].transform);
  358. switch (FRAME.attractors[i].type) {
  359. case ATTRACTOR_TYPE_SPHERE: {
  360. dir = safe_normalize(rel_vec);
  361. float d = length(local_pos) / FRAME.attractors[i].extents.x;
  362. if (d > 1.0) {
  363. continue;
  364. }
  365. amount = max(0.0, 1.0 - d);
  366. } break;
  367. case ATTRACTOR_TYPE_BOX: {
  368. dir = safe_normalize(rel_vec);
  369. vec3 abs_pos = abs(local_pos / FRAME.attractors[i].extents);
  370. float d = max(abs_pos.x, max(abs_pos.y, abs_pos.z));
  371. if (d > 1.0) {
  372. continue;
  373. }
  374. amount = max(0.0, 1.0 - d);
  375. } break;
  376. case ATTRACTOR_TYPE_VECTOR_FIELD: {
  377. vec3 uvw_pos = (local_pos / FRAME.attractors[i].extents + 1.0) * 0.5;
  378. if (any(lessThan(uvw_pos, vec3(0.0))) || any(greaterThan(uvw_pos, vec3(1.0)))) {
  379. continue;
  380. }
  381. vec3 s = texture(sampler3D(sdf_vec_textures[FRAME.attractors[i].texture_index], SAMPLER_LINEAR_CLAMP), uvw_pos).xyz * -2.0 + 1.0;
  382. dir = mat3(FRAME.attractors[i].transform) * safe_normalize(s); //revert direction
  383. amount = length(s);
  384. } break;
  385. }
  386. amount = pow(amount, FRAME.attractors[i].attenuation);
  387. dir = safe_normalize(mix(dir, FRAME.attractors[i].transform[2].xyz, FRAME.attractors[i].directionality));
  388. attractor_force -= amount * dir * FRAME.attractors[i].strength;
  389. }
  390. float particle_size = FRAME.particle_size;
  391. #ifdef USE_COLLISION_SCALE
  392. particle_size *= dot(vec3(length(PARTICLE.xform[0].xyz), length(PARTICLE.xform[1].xyz), length(PARTICLE.xform[2].xyz)), vec3(0.33333333333));
  393. #endif
  394. if (FRAME.collider_count == 1 && FRAME.colliders[0].type == COLLIDER_TYPE_2D_SDF) {
  395. //2D collision
  396. vec2 pos = PARTICLE.xform[3].xy;
  397. vec4 to_sdf_x = FRAME.colliders[0].transform[0];
  398. vec4 to_sdf_y = FRAME.colliders[0].transform[1];
  399. vec2 sdf_pos = vec2(dot(vec4(pos, 0, 1), to_sdf_x), dot(vec4(pos, 0, 1), to_sdf_y));
  400. vec4 sdf_to_screen = vec4(FRAME.colliders[0].extents, FRAME.colliders[0].scale);
  401. vec2 uv_pos = sdf_pos * sdf_to_screen.xy + sdf_to_screen.zw;
  402. if (all(greaterThan(uv_pos, vec2(0.0))) && all(lessThan(uv_pos, vec2(1.0)))) {
  403. vec2 pos2 = pos + vec2(0, particle_size);
  404. vec2 sdf_pos2 = vec2(dot(vec4(pos2, 0, 1), to_sdf_x), dot(vec4(pos2, 0, 1), to_sdf_y));
  405. float sdf_particle_size = distance(sdf_pos, sdf_pos2);
  406. float d = texture(sampler2D(height_field_texture, SAMPLER_LINEAR_CLAMP), uv_pos).r * SDF_MAX_LENGTH;
  407. d -= sdf_particle_size;
  408. if (d < 0.0) {
  409. const float EPSILON = 0.001;
  410. vec2 n = normalize(vec2(
  411. 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,
  412. 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));
  413. collided = true;
  414. sdf_pos2 = sdf_pos + n * d;
  415. pos2 = vec2(dot(vec4(sdf_pos2, 0, 1), FRAME.colliders[0].transform[2]), dot(vec4(sdf_pos2, 0, 1), FRAME.colliders[0].transform[3]));
  416. n = pos - pos2;
  417. collision_normal = normalize(vec3(n, 0.0));
  418. collision_depth = length(n);
  419. }
  420. }
  421. } else {
  422. for (uint i = 0; i < FRAME.collider_count; i++) {
  423. vec3 normal;
  424. float depth;
  425. bool col = false;
  426. vec3 rel_vec = PARTICLE.xform[3].xyz - FRAME.colliders[i].transform[3].xyz;
  427. vec3 local_pos = rel_vec * mat3(FRAME.colliders[i].transform);
  428. switch (FRAME.colliders[i].type) {
  429. case COLLIDER_TYPE_SPHERE: {
  430. float d = length(rel_vec) - (particle_size + FRAME.colliders[i].extents.x);
  431. if (d < 0.0) {
  432. col = true;
  433. depth = -d;
  434. normal = normalize(rel_vec);
  435. }
  436. } break;
  437. case COLLIDER_TYPE_BOX: {
  438. vec3 abs_pos = abs(local_pos);
  439. vec3 sgn_pos = sign(local_pos);
  440. if (any(greaterThan(abs_pos, FRAME.colliders[i].extents))) {
  441. //point outside box
  442. vec3 closest = min(abs_pos, FRAME.colliders[i].extents);
  443. vec3 rel = abs_pos - closest;
  444. depth = length(rel) - particle_size;
  445. if (depth < 0.0) {
  446. col = true;
  447. normal = mat3(FRAME.colliders[i].transform) * (normalize(rel) * sgn_pos);
  448. depth = -depth;
  449. }
  450. } else {
  451. //point inside box
  452. vec3 axis_len = FRAME.colliders[i].extents - abs_pos;
  453. // there has to be a faster way to do this?
  454. if (all(lessThan(axis_len.xx, axis_len.yz))) {
  455. normal = vec3(1, 0, 0);
  456. } else if (all(lessThan(axis_len.yy, axis_len.xz))) {
  457. normal = vec3(0, 1, 0);
  458. } else {
  459. normal = vec3(0, 0, 1);
  460. }
  461. col = true;
  462. depth = dot(normal * axis_len, vec3(1)) + particle_size;
  463. normal = mat3(FRAME.colliders[i].transform) * (normal * sgn_pos);
  464. }
  465. } break;
  466. case COLLIDER_TYPE_SDF: {
  467. vec3 apos = abs(local_pos);
  468. float extra_dist = 0.0;
  469. if (any(greaterThan(apos, FRAME.colliders[i].extents))) { //outside
  470. vec3 mpos = min(apos, FRAME.colliders[i].extents);
  471. extra_dist = distance(mpos, apos);
  472. }
  473. if (extra_dist > particle_size) {
  474. continue;
  475. }
  476. vec3 uvw_pos = (local_pos / FRAME.colliders[i].extents) * 0.5 + 0.5;
  477. float s = texture(sampler3D(sdf_vec_textures[FRAME.colliders[i].texture_index], SAMPLER_LINEAR_CLAMP), uvw_pos).r;
  478. s *= FRAME.colliders[i].scale;
  479. s += extra_dist;
  480. if (s < particle_size) {
  481. col = true;
  482. depth = particle_size - s;
  483. const float EPSILON = 0.001;
  484. normal = mat3(FRAME.colliders[i].transform) *
  485. normalize(
  486. vec3(
  487. 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,
  488. 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,
  489. 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));
  490. }
  491. } break;
  492. case COLLIDER_TYPE_HEIGHT_FIELD: {
  493. vec3 local_pos_bottom = local_pos;
  494. local_pos_bottom.y -= particle_size;
  495. if (any(greaterThan(abs(local_pos_bottom), FRAME.colliders[i].extents))) {
  496. continue;
  497. }
  498. const float DELTA = 1.0 / 8192.0;
  499. vec3 uvw_pos = vec3(local_pos_bottom / FRAME.colliders[i].extents) * 0.5 + 0.5;
  500. float y = 1.0 - texture(sampler2D(height_field_texture, SAMPLER_LINEAR_CLAMP), uvw_pos.xz).r;
  501. if (y > uvw_pos.y) {
  502. //inside heightfield
  503. vec3 pos1 = (vec3(uvw_pos.x, y, uvw_pos.z) * 2.0 - 1.0) * FRAME.colliders[i].extents;
  504. vec3 pos2 = (vec3(uvw_pos.x + DELTA, 1.0 - 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;
  505. vec3 pos3 = (vec3(uvw_pos.x, 1.0 - 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;
  506. normal = normalize(cross(pos1 - pos2, pos1 - pos3));
  507. float local_y = (vec3(local_pos / FRAME.colliders[i].extents) * 0.5 + 0.5).y;
  508. col = true;
  509. depth = dot(normal, pos1) - dot(normal, local_pos_bottom);
  510. }
  511. } break;
  512. }
  513. if (col) {
  514. if (!collided) {
  515. collided = true;
  516. collision_normal = normal;
  517. collision_depth = depth;
  518. } else {
  519. vec3 c = collision_normal * collision_depth;
  520. c += normal * max(0.0, depth - dot(normal, c));
  521. collision_normal = normalize(c);
  522. collision_depth = length(c);
  523. }
  524. }
  525. }
  526. }
  527. }
  528. if (particle_active) {
  529. #CODE : PROCESS
  530. }
  531. PARTICLE.flags &= ~PARTICLE_FLAG_ACTIVE;
  532. if (particle_active) {
  533. PARTICLE.flags |= PARTICLE_FLAG_ACTIVE;
  534. }
  535. }