particles.glsl 20 KB

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