GBufferGpuParticles.ankiprog 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma anki mutator ANKI_PASS 0 2 3
  6. #pragma anki mutator ANKI_VELOCITY 0 1
  7. #include <AnKi/Shaders/Include/ParticleTypes.h>
  8. struct PerDraw
  9. {
  10. Mat4 m_ankiMvp;
  11. Vec3 m_diffColor;
  12. F32 m_roughness;
  13. Vec3 m_specColor;
  14. F32 m_metallic;
  15. Vec3 m_initialEmission;
  16. Vec3 m_finalEmission;
  17. };
  18. #pragma anki reflect b_ankiPerDraw
  19. layout(set = 0, binding = 0, std140, row_major) uniform b_ankiPerDraw
  20. {
  21. PerDraw u_ankiPerDraw;
  22. };
  23. layout(set = 0, binding = 1) buffer b_particles
  24. {
  25. GpuParticle u_particles[];
  26. };
  27. layout(set = 0, binding = 2) uniform b_extra
  28. {
  29. Vec3 u_minusCameraZ;
  30. };
  31. #pragma anki start vert
  32. #include <AnKi/Shaders/Common.glsl>
  33. layout(location = 0) out Vec2 out_velocity;
  34. layout(location = 1) flat out F32 out_lifeFactor;
  35. void main()
  36. {
  37. const GpuParticle part = u_particles[gl_VertexID / 2];
  38. const Vec4 crntClipPos = u_ankiPerDraw.m_ankiMvp * Vec4(part.m_newWorldPosition, 1.0);
  39. const Vec4 prevClipPos = u_ankiPerDraw.m_ankiMvp * Vec4(part.m_oldWorldPosition, 1.0);
  40. gl_Position = ((gl_VertexID & 1) == 0) ? crntClipPos : prevClipPos;
  41. const Vec2 crntNdc = crntClipPos.xy / crntClipPos.w;
  42. const Vec2 prevNdc = prevClipPos.xy / prevClipPos.w;
  43. // It's NDC_TO_UV(prevNdc) - NDC_TO_UV(crntNdc) or:
  44. out_velocity = ((gl_VertexID & 1) == 0) ? Vec2(0.0) : (prevNdc - crntNdc) * 0.5;
  45. out_lifeFactor = saturate(1.0 - (part.m_life / part.m_startingLife));
  46. }
  47. #pragma anki end
  48. #pragma anki start frag
  49. #define MAX_EMISSION 1024.0
  50. #include <AnKi/Shaders/PackFunctions.glsl>
  51. layout(location = 0) out Vec4 out_gbuffer0;
  52. layout(location = 1) out Vec4 out_gbuffer1;
  53. layout(location = 2) out Vec4 out_gbuffer2;
  54. layout(location = 3) out Vec2 out_gbuffer3;
  55. layout(location = 0) in Vec2 in_velocity;
  56. layout(location = 1) flat in F32 in_lifeFactor;
  57. void main()
  58. {
  59. GbufferInfo g;
  60. g.m_diffuse = u_ankiPerDraw.m_diffColor;
  61. g.m_normal = u_minusCameraZ;
  62. g.m_specular = u_ankiPerDraw.m_specColor;
  63. g.m_roughness = u_ankiPerDraw.m_roughness;
  64. g.m_subsurface = 0.0;
  65. const Vec3 emission = mix(u_ankiPerDraw.m_initialEmission, u_ankiPerDraw.m_finalEmission, in_lifeFactor);
  66. g.m_emission = (emission.r + emission.g + emission.b) / 3.0;
  67. g.m_metallic = u_ankiPerDraw.m_metallic;
  68. g.m_velocity = in_velocity;
  69. writeGBuffer(g, out_gbuffer0, out_gbuffer1, out_gbuffer2, out_gbuffer3);
  70. }
  71. #pragma anki end