particles.glsl 19 KB

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