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 vec3 emitterPosition;
  25. uniform float time;
  26. uniform float deltaTime;
  27. uniform float lifeMin;
  28. uniform float lifeMax;
  29.  
  30. const float INVERSE_MAX_UINT = 1.0f / 4294967295.0f;
  31.  
  32. float rand(uint seed, float range)
  33. {
  34.     uint i = (seed ^ 12345391u) * 2654435769u;
  35.     i ^= (i << 6u) ^ (i >> 26u);
  36.     i *= 2654435769u;
  37.     i += (i << 5u) ^ (i >> 12u);
  38.     return float(range * i) * INVERSE_MAX_UINT;
  39. }
  40.  
  41.  
  42. vec3 newVel = vec3(0);
  43.  
  44. void main()
  45. {
  46.     // Get previous velocity
  47.     newVel = Velocity;
  48.    
  49.     // Generate seed based on time and vertex ID
  50.     uint seed = uint(time * 1000.0) + uint(gl_VertexID);
  51.    
  52.     // Apply trigonometrical positional tracking
  53.     vec3 Target = vec3(sin(time), cos(time), tan(time));
  54.     vec3 dir = normalize(Target - Position);
  55.     newVel += dir;
  56.    
  57.     // Update base information
  58.     vPosition = Position + (Velocity * deltaTime);
  59.     vVelocity = newVel;
  60.     vLifetime = Lifetime + deltaTime;
  61.     vLifespan = Lifespan;
  62.    
  63.     // If it's life time is up, reset.
  64.     if(vLifetime > vLifespan)
  65.     {          
  66.         //Initialise velocity
  67.         vVelocity.x = rand(seed++, 2) - 1;
  68.         vVelocity.y = rand(seed++, 2) - 1;
  69.         vVelocity.z = rand(seed++, 2) - 1;
  70.         vVelocity = normalize(vVelocity);
  71.         vPosition = emitterPosition + vec3(sin(time)*0.5, tan(time)*0.5, cos(time)*0.5);
  72.         vLifetime = 0;
  73.         vLifespan = rand(seed++, lifeMax - lifeMin) + lifeMin;
  74.     }
  75. }