Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. // ## VERTEX SHADER ##
  2. // Author: Jackson Luff
  3. // Name: ParticleUpdate.vert
  4. // Type: Vertex Shader
  5. // Description:
  6. // * This shader is utilized as the
  7. // * standard update loop for the
  8. // * particles. Only difference is
  9. // * that this update loop is on
  10. // * the GPU and not CPU.
  11.  
  12. #version 410
  13.  
  14. in layout(location=0) vec3 Position;
  15. in layout(location=1) vec3 Velocity;
  16. in layout(location=2) float Lifetime;
  17. in layout(location=3) float Lifespan;
  18.  
  19. out vec3 vPosition;
  20. out vec3 vVelocity;
  21. out float vLifetime;
  22. out float vLifespan;
  23.  
  24. uniform mat4 World;
  25. uniform vec3 emitterPosition;
  26. uniform float time;
  27. uniform float deltaTime;
  28. uniform float lifeMin;
  29. uniform float lifeMax;
  30. uniform float veloMax;
  31.  
  32. const float INVERSE_MAX_UINT = 1.0f / 4294967295.0f;
  33.  
  34. float rand(uint seed, float range)
  35. {
  36.     uint i = (seed ^ 12345391u) * 2654435769u;
  37.     i ^= (i << 6u) ^ (i >> 26u);
  38.     i *= 2654435769u;
  39.     i += (i << 5u) ^ (i >> 12u);
  40.     return float(range * i) * INVERSE_MAX_UINT;
  41. }
  42.  
  43. vec3 newVel = vec3(0);
  44.  
  45. void main()
  46. {
  47.     //Get Previous Frame Velocity
  48.     newVel = Velocity;
  49.    
  50.     uint seed = uint(time * 1000.0) + uint(gl_VertexID);
  51.     float dt = deltaTime;
  52.  
  53.     // Warp velocity using trigonometry
  54.     float a = Lifetime, b = Lifespan, c = 0.5, d = 0.5;
  55.     newVel.x = (a-b)*sin(time) + b * sin(time * ((a/b)-1));
  56.     newVel.y = (a-b)*sin(time) / b * cos(time * ((a/b)-1));
  57.     newVel.z = (a-b)*cos(time) - b * cos(time * ((a/b)-1));
  58.    
  59.     // Update base data with new information
  60.     vPosition = Position + (Velocity * dt);
  61.     vVelocity = newVel;
  62.     vLifetime = Lifetime + dt;
  63.     vLifespan = Lifespan;
  64.    
  65.     // if life time is up, reset
  66.     if(vLifetime > vLifespan)
  67.     {          
  68.         //Initialise velocity
  69.         vVelocity.x = rand(seed++, 2) / veloMax + sin(time);
  70.         vVelocity.y = rand(seed++, 2) / veloMax + cos(time);
  71.         vVelocity.z = rand(seed++, 2) / veloMax + tan(time);
  72.         vVelocity = normalize(vVelocity);
  73.         vPosition = emitterPosition + vec3(sin(time)*0.5, -sin(time)*0.5, cos(time)*0.5);
  74.         vLifetime = 0;
  75.         vLifespan = rand(seed++, lifeMax - lifeMin) + lifeMin;
  76.     }
  77. }