particles.glsl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /* clang-format off */
  2. #[modes]
  3. mode_default =
  4. #[specializations]
  5. MODE_3D = false
  6. USERDATA1_USED = false
  7. USERDATA2_USED = false
  8. USERDATA3_USED = false
  9. USERDATA4_USED = false
  10. USERDATA5_USED = false
  11. USERDATA6_USED = false
  12. #[vertex]
  13. #define SDF_MAX_LENGTH 16384.0
  14. layout(std140) uniform GlobalShaderUniformData { //ubo:1
  15. vec4 global_shader_uniforms[MAX_GLOBAL_SHADER_UNIFORMS];
  16. };
  17. // This needs to be outside clang-format so the ubo comment is in the right place
  18. #ifdef MATERIAL_UNIFORMS_USED
  19. layout(std140) uniform MaterialUniforms{ //ubo:2
  20. #MATERIAL_UNIFORMS
  21. };
  22. #endif
  23. /* clang-format on */
  24. #define MAX_ATTRACTORS 32
  25. #define ATTRACTOR_TYPE_SPHERE uint(0)
  26. #define ATTRACTOR_TYPE_BOX uint(1)
  27. #define ATTRACTOR_TYPE_VECTOR_FIELD uint(2)
  28. struct Attractor {
  29. mat4 transform;
  30. vec4 extents; // Extents or radius. w-channel is padding.
  31. uint type;
  32. float strength;
  33. float attenuation;
  34. float directionality;
  35. };
  36. #define MAX_COLLIDERS 32
  37. #define COLLIDER_TYPE_SPHERE uint(0)
  38. #define COLLIDER_TYPE_BOX uint(1)
  39. #define COLLIDER_TYPE_SDF uint(2)
  40. #define COLLIDER_TYPE_HEIGHT_FIELD uint(3)
  41. #define COLLIDER_TYPE_2D_SDF uint(4)
  42. struct Collider {
  43. mat4 transform;
  44. vec4 extents; // Extents or radius. w-channel is padding.
  45. uint type;
  46. float scale;
  47. float pad0;
  48. float pad1;
  49. };
  50. layout(std140) uniform FrameData { //ubo:0
  51. bool emitting;
  52. uint cycle;
  53. float system_phase;
  54. float prev_system_phase;
  55. float explosiveness;
  56. float randomness;
  57. float time;
  58. float delta;
  59. float particle_size;
  60. float pad0;
  61. float pad1;
  62. float pad2;
  63. uint random_seed;
  64. uint attractor_count;
  65. uint collider_count;
  66. uint frame;
  67. mat4 emission_transform;
  68. Attractor attractors[MAX_ATTRACTORS];
  69. Collider colliders[MAX_COLLIDERS];
  70. };
  71. #define PARTICLE_FLAG_ACTIVE uint(1)
  72. #define PARTICLE_FLAG_STARTED uint(2)
  73. #define PARTICLE_FLAG_TRAILED uint(4)
  74. #define PARTICLE_FRAME_MASK uint(0xFFFF)
  75. #define PARTICLE_FRAME_SHIFT uint(16)
  76. // ParticleData
  77. layout(location = 0) in highp vec4 color;
  78. layout(location = 1) in highp vec4 velocity_flags;
  79. layout(location = 2) in highp vec4 custom;
  80. layout(location = 3) in highp vec4 xform_1;
  81. layout(location = 4) in highp vec4 xform_2;
  82. #ifdef MODE_3D
  83. layout(location = 5) in highp vec4 xform_3;
  84. #endif
  85. #ifdef USERDATA1_USED
  86. layout(location = 6) in highp vec4 userdata1;
  87. #endif
  88. #ifdef USERDATA2_USED
  89. layout(location = 7) in highp vec4 userdata2;
  90. #endif
  91. #ifdef USERDATA3_USED
  92. layout(location = 8) in highp vec4 userdata3;
  93. #endif
  94. #ifdef USERDATA4_USED
  95. layout(location = 9) in highp vec4 userdata4;
  96. #endif
  97. #ifdef USERDATA5_USED
  98. layout(location = 10) in highp vec4 userdata5;
  99. #endif
  100. #ifdef USERDATA6_USED
  101. layout(location = 11) in highp vec4 userdata6;
  102. #endif
  103. out highp vec4 out_color; //tfb:
  104. out highp vec4 out_velocity_flags; //tfb:
  105. out highp vec4 out_custom; //tfb:
  106. out highp vec4 out_xform_1; //tfb:
  107. out highp vec4 out_xform_2; //tfb:
  108. #ifdef MODE_3D
  109. out highp vec4 out_xform_3; //tfb:MODE_3D
  110. #endif
  111. #ifdef USERDATA1_USED
  112. out highp vec4 out_userdata1; //tfb:USERDATA1_USED
  113. #endif
  114. #ifdef USERDATA2_USED
  115. out highp vec4 out_userdata2; //tfb:USERDATA2_USED
  116. #endif
  117. #ifdef USERDATA3_USED
  118. out highp vec4 out_userdata3; //tfb:USERDATA3_USED
  119. #endif
  120. #ifdef USERDATA4_USED
  121. out highp vec4 out_userdata4; //tfb:USERDATA4_USED
  122. #endif
  123. #ifdef USERDATA5_USED
  124. out highp vec4 out_userdata5; //tfb:USERDATA5_USED
  125. #endif
  126. #ifdef USERDATA6_USED
  127. out highp vec4 out_userdata6; //tfb:USERDATA6_USED
  128. #endif
  129. uniform sampler2D height_field_texture; //texunit:0
  130. uniform float lifetime;
  131. uniform bool clear;
  132. uniform uint total_particles;
  133. uniform bool use_fractional_delta;
  134. uint hash(uint x) {
  135. x = ((x >> uint(16)) ^ x) * uint(0x45d9f3b);
  136. x = ((x >> uint(16)) ^ x) * uint(0x45d9f3b);
  137. x = (x >> uint(16)) ^ x;
  138. return x;
  139. }
  140. vec3 safe_normalize(vec3 direction) {
  141. const float EPSILON = 0.001;
  142. if (length(direction) < EPSILON) {
  143. return vec3(0.0);
  144. }
  145. return normalize(direction);
  146. }
  147. // Needed whenever 2D sdf texture is read from as it is packed in RGBA8.
  148. float vec4_to_float(vec4 p_vec) {
  149. return dot(p_vec, vec4(1.0 / (255.0 * 255.0 * 255.0), 1.0 / (255.0 * 255.0), 1.0 / 255.0, 1.0)) * 2.0 - 1.0;
  150. }
  151. #GLOBALS
  152. void main() {
  153. bool apply_forces = true;
  154. bool apply_velocity = true;
  155. float local_delta = delta;
  156. float mass = 1.0;
  157. bool restart = false;
  158. bool restart_position = false;
  159. bool restart_rotation_scale = false;
  160. bool restart_velocity = false;
  161. bool restart_color = false;
  162. bool restart_custom = false;
  163. mat4 xform = mat4(1.0);
  164. uint flags = 0u;
  165. if (clear) {
  166. out_color = vec4(1.0);
  167. out_custom = vec4(0.0);
  168. out_velocity_flags = vec4(0.0);
  169. } else {
  170. out_color = color;
  171. out_velocity_flags = velocity_flags;
  172. out_custom = custom;
  173. xform[0] = xform_1;
  174. xform[1] = xform_2;
  175. #ifdef MODE_3D
  176. xform[2] = xform_3;
  177. #endif
  178. xform = transpose(xform);
  179. flags = floatBitsToUint(velocity_flags.w);
  180. }
  181. //clear started flag if set
  182. flags &= ~PARTICLE_FLAG_STARTED;
  183. bool collided = false;
  184. vec3 collision_normal = vec3(0.0);
  185. float collision_depth = 0.0;
  186. vec3 attractor_force = vec3(0.0);
  187. #if !defined(DISABLE_VELOCITY)
  188. if (bool(flags & PARTICLE_FLAG_ACTIVE)) {
  189. xform[3].xyz += out_velocity_flags.xyz * local_delta;
  190. }
  191. #endif
  192. uint index = uint(gl_VertexID);
  193. if (emitting) {
  194. float restart_phase = float(index) / float(total_particles);
  195. if (randomness > 0.0) {
  196. uint seed = cycle;
  197. if (restart_phase >= system_phase) {
  198. seed -= uint(1);
  199. }
  200. seed *= uint(total_particles);
  201. seed += index;
  202. float random = float(hash(seed) % uint(65536)) / 65536.0;
  203. restart_phase += randomness * random * 1.0 / float(total_particles);
  204. }
  205. restart_phase *= (1.0 - explosiveness);
  206. if (system_phase > prev_system_phase) {
  207. // restart_phase >= prev_system_phase is used so particles emit in the first frame they are processed
  208. if (restart_phase >= prev_system_phase && restart_phase < system_phase) {
  209. restart = true;
  210. if (use_fractional_delta) {
  211. local_delta = (system_phase - restart_phase) * lifetime;
  212. }
  213. }
  214. } else if (delta > 0.0) {
  215. if (restart_phase >= prev_system_phase) {
  216. restart = true;
  217. if (use_fractional_delta) {
  218. local_delta = (1.0 - restart_phase + system_phase) * lifetime;
  219. }
  220. } else if (restart_phase < system_phase) {
  221. restart = true;
  222. if (use_fractional_delta) {
  223. local_delta = (system_phase - restart_phase) * lifetime;
  224. }
  225. }
  226. }
  227. if (restart) {
  228. flags = emitting ? (PARTICLE_FLAG_ACTIVE | PARTICLE_FLAG_STARTED | (cycle << PARTICLE_FRAME_SHIFT)) : 0u;
  229. restart_position = true;
  230. restart_rotation_scale = true;
  231. restart_velocity = true;
  232. restart_color = true;
  233. restart_custom = true;
  234. }
  235. }
  236. bool particle_active = bool(flags & PARTICLE_FLAG_ACTIVE);
  237. uint particle_number = (flags >> PARTICLE_FRAME_SHIFT) * uint(total_particles) + index;
  238. if (restart && particle_active) {
  239. #CODE : START
  240. }
  241. if (particle_active) {
  242. for (uint i = 0u; i < attractor_count; i++) {
  243. vec3 dir;
  244. float amount;
  245. vec3 rel_vec = xform[3].xyz - attractors[i].transform[3].xyz;
  246. vec3 local_pos = rel_vec * mat3(attractors[i].transform);
  247. if (attractors[i].type == ATTRACTOR_TYPE_SPHERE) {
  248. dir = safe_normalize(rel_vec);
  249. float d = length(local_pos) / attractors[i].extents.x;
  250. if (d > 1.0) {
  251. continue;
  252. }
  253. amount = max(0.0, 1.0 - d);
  254. } else if (attractors[i].type == ATTRACTOR_TYPE_BOX) {
  255. dir = safe_normalize(rel_vec);
  256. vec3 abs_pos = abs(local_pos / attractors[i].extents.xyz);
  257. float d = max(abs_pos.x, max(abs_pos.y, abs_pos.z));
  258. if (d > 1.0) {
  259. continue;
  260. }
  261. amount = max(0.0, 1.0 - d);
  262. } else if (attractors[i].type == ATTRACTOR_TYPE_VECTOR_FIELD) {
  263. }
  264. amount = pow(amount, attractors[i].attenuation);
  265. dir = safe_normalize(mix(dir, attractors[i].transform[2].xyz, attractors[i].directionality));
  266. attractor_force -= amount * dir * attractors[i].strength;
  267. }
  268. float particle_size = particle_size;
  269. #ifdef USE_COLLISION_SCALE
  270. particle_size *= dot(vec3(length(xform[0].xyz), length(xform[1].xyz), length(xform[2].xyz)), vec3(0.33333333333));
  271. #endif
  272. if (collider_count == 1u && colliders[0].type == COLLIDER_TYPE_2D_SDF) {
  273. //2D collision
  274. vec2 pos = xform[3].xy;
  275. vec4 to_sdf_x = colliders[0].transform[0];
  276. vec4 to_sdf_y = colliders[0].transform[1];
  277. vec2 sdf_pos = vec2(dot(vec4(pos, 0, 1), to_sdf_x), dot(vec4(pos, 0, 1), to_sdf_y));
  278. vec4 sdf_to_screen = vec4(colliders[0].extents.xyz, colliders[0].scale);
  279. vec2 uv_pos = sdf_pos * sdf_to_screen.xy + sdf_to_screen.zw;
  280. if (all(greaterThan(uv_pos, vec2(0.0))) && all(lessThan(uv_pos, vec2(1.0)))) {
  281. vec2 pos2 = pos + vec2(0, particle_size);
  282. vec2 sdf_pos2 = vec2(dot(vec4(pos2, 0, 1), to_sdf_x), dot(vec4(pos2, 0, 1), to_sdf_y));
  283. float sdf_particle_size = distance(sdf_pos, sdf_pos2);
  284. float d = vec4_to_float(texture(height_field_texture, uv_pos)) * SDF_MAX_LENGTH;
  285. d -= sdf_particle_size;
  286. if (d < 0.0) {
  287. const float EPSILON = 0.001;
  288. vec2 n = normalize(vec2(
  289. vec4_to_float(texture(height_field_texture, uv_pos + vec2(EPSILON, 0.0))) - vec4_to_float(texture(height_field_texture, uv_pos - vec2(EPSILON, 0.0))),
  290. vec4_to_float(texture(height_field_texture, uv_pos + vec2(0.0, EPSILON))) - vec4_to_float(texture(height_field_texture, uv_pos - vec2(0.0, EPSILON)))));
  291. collided = true;
  292. sdf_pos2 = sdf_pos + n * d;
  293. pos2 = vec2(dot(vec4(sdf_pos2, 0, 1), colliders[0].transform[2]), dot(vec4(sdf_pos2, 0, 1), colliders[0].transform[3]));
  294. n = pos - pos2;
  295. collision_normal = normalize(vec3(n, 0.0));
  296. collision_depth = length(n);
  297. }
  298. }
  299. } else {
  300. for (uint i = 0u; i < collider_count; i++) {
  301. vec3 normal;
  302. float depth;
  303. bool col = false;
  304. vec3 rel_vec = xform[3].xyz - colliders[i].transform[3].xyz;
  305. vec3 local_pos = rel_vec * mat3(colliders[i].transform);
  306. if (colliders[i].type == COLLIDER_TYPE_SPHERE) {
  307. float d = length(rel_vec) - (particle_size + colliders[i].extents.x);
  308. if (d < 0.0) {
  309. col = true;
  310. depth = -d;
  311. normal = normalize(rel_vec);
  312. }
  313. } else if (colliders[i].type == COLLIDER_TYPE_BOX) {
  314. vec3 abs_pos = abs(local_pos);
  315. vec3 sgn_pos = sign(local_pos);
  316. if (any(greaterThan(abs_pos, colliders[i].extents.xyz))) {
  317. //point outside box
  318. vec3 closest = min(abs_pos, colliders[i].extents.xyz);
  319. vec3 rel = abs_pos - closest;
  320. depth = length(rel) - particle_size;
  321. if (depth < 0.0) {
  322. col = true;
  323. normal = mat3(colliders[i].transform) * (normalize(rel) * sgn_pos);
  324. depth = -depth;
  325. }
  326. } else {
  327. //point inside box
  328. vec3 axis_len = colliders[i].extents.xyz - abs_pos;
  329. // there has to be a faster way to do this?
  330. if (all(lessThan(axis_len.xx, axis_len.yz))) {
  331. normal = vec3(1, 0, 0);
  332. } else if (all(lessThan(axis_len.yy, axis_len.xz))) {
  333. normal = vec3(0, 1, 0);
  334. } else {
  335. normal = vec3(0, 0, 1);
  336. }
  337. col = true;
  338. depth = dot(normal * axis_len, vec3(1)) + particle_size;
  339. normal = mat3(colliders[i].transform) * (normal * sgn_pos);
  340. }
  341. } else if (colliders[i].type == COLLIDER_TYPE_SDF) {
  342. } else if (colliders[i].type == COLLIDER_TYPE_HEIGHT_FIELD) {
  343. vec3 local_pos_bottom = local_pos;
  344. local_pos_bottom.y -= particle_size;
  345. if (any(greaterThan(abs(local_pos_bottom), colliders[i].extents.xyz))) {
  346. continue;
  347. }
  348. const float DELTA = 1.0 / 8192.0;
  349. vec3 uvw_pos = vec3(local_pos_bottom / colliders[i].extents.xyz) * 0.5 + 0.5;
  350. float y = 1.0 - texture(height_field_texture, uvw_pos.xz).r;
  351. if (y > uvw_pos.y) {
  352. //inside heightfield
  353. vec3 pos1 = (vec3(uvw_pos.x, y, uvw_pos.z) * 2.0 - 1.0) * colliders[i].extents.xyz;
  354. vec3 pos2 = (vec3(uvw_pos.x + DELTA, 1.0 - texture(height_field_texture, uvw_pos.xz + vec2(DELTA, 0)).r, uvw_pos.z) * 2.0 - 1.0) * colliders[i].extents.xyz;
  355. vec3 pos3 = (vec3(uvw_pos.x, 1.0 - texture(height_field_texture, uvw_pos.xz + vec2(0, DELTA)).r, uvw_pos.z + DELTA) * 2.0 - 1.0) * colliders[i].extents.xyz;
  356. normal = normalize(cross(pos1 - pos2, pos1 - pos3));
  357. float local_y = (vec3(local_pos / colliders[i].extents.xyz) * 0.5 + 0.5).y;
  358. col = true;
  359. depth = dot(normal, pos1) - dot(normal, local_pos_bottom);
  360. }
  361. }
  362. if (col) {
  363. if (!collided) {
  364. collided = true;
  365. collision_normal = normal;
  366. collision_depth = depth;
  367. } else {
  368. vec3 c = collision_normal * collision_depth;
  369. c += normal * max(0.0, depth - dot(normal, c));
  370. collision_normal = normalize(c);
  371. collision_depth = length(c);
  372. }
  373. }
  374. }
  375. }
  376. }
  377. if (particle_active) {
  378. #CODE : PROCESS
  379. }
  380. flags &= ~PARTICLE_FLAG_ACTIVE;
  381. if (particle_active) {
  382. flags |= PARTICLE_FLAG_ACTIVE;
  383. }
  384. xform = transpose(xform);
  385. out_xform_1 = xform[0];
  386. out_xform_2 = xform[1];
  387. #ifdef MODE_3D
  388. out_xform_3 = xform[2];
  389. #endif
  390. out_velocity_flags.w = uintBitsToFloat(flags);
  391. }
  392. /* clang-format off */
  393. #[fragment]
  394. void main() {
  395. }
  396. /* clang-format on */