Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. // ## FRAGMENT SHADER ##
  2. // Author: Jackson Luff
  3. // Name: Interact3V.frag
  4. // Type: Fragment Shader
  5. // Description:
  6. // * In this frag shader I
  7. // * wrote up some pretty
  8. // * cool shader work without
  9. // * any textures. Pretty nifty
  10.  
  11. #version 430
  12.  
  13. in vec4 vPosition;
  14. in vec4 vNormal;
  15. in vec2 vCoords;
  16.  
  17. uniform float elapsedTime;
  18.  
  19. const float INVERSE_MAX_UINT = 1.0 / 4294967295.0;
  20.  
  21. // The GPU version of C++'s rand()
  22. float rand(uint seed, float range)
  23. {
  24.     uint i = (seed ^ 12345391u) * 2654435769u;
  25.     i ^= (i << 6u) ^ (i >> 26u);
  26.     i *= 2654435769u;
  27.     i += (i << 5u) ^ (i >> 12u);
  28.     return float(range * i) * INVERSE_MAX_UINT;
  29. }
  30.  
  31. // Helper function for rand()
  32. float ra(uint seed)
  33. {
  34.     return rand(seed++, 2) - 1;
  35. }
  36.  
  37. void main()
  38. {
  39.     // pass in mousePos as a uniform from on CPU.
  40.     // use line–plane intersection to determine
  41.     // screen-space mousePos in world space
  42.     // ref: (https://en.wikipedia.org/wiki/Line%E2%80%93plane_intersection)
  43.     float dist = length(mousePos - vPosition.xyz);
  44.     uint seed = uint(elapsedTime) + uint(dist);
  45.    
  46.     // generate random values -1 to 1
  47.     float r = ra(seed++);
  48.     float g = ra(seed++);
  49.     float b = ra(seed++);
  50.    
  51.     vec3 outRGB = vec3(0);
  52.    
  53.     // Warps perspective of colours
  54.     outRGB.r = outRGB.r + vCoords.x + sin(r * elapsedTime);
  55.     outRGB.g = outRGB.g + vCoords.y + cos(g * elapsedTime);
  56.     outRGB.b = outRGB.b + tan(b * elapsedTime);
  57.    
  58.     // Generates new colours surrounding
  59.     outRGB = outRGB + (ra(seed++) + elapsedTime/seed)* tan(dist);
  60.     outRGB = outRGB + (ra(seed++) + elapsedTime/seed)* sin(vPosition.x);
  61.     outRGB = outRGB + (ra(seed++) + elapsedTime/seed)* cos(vPosition.z);
  62.    
  63.     // Output
  64.     gl_FragColor = vec4(outRGB, 1);
  65. }