Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. // ## FRAGMENT SHADER ##
  2. // Author: Jackson Luff
  3. // Name: Interact4V.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.    
  45.     // Gemerate unique seed
  46.     uint seed = uint(dist);
  47.     seed = seed * uint(sin( vCoords.x * elapsedTime));
  48.     seed = seed * uint(sin( vCoords.y * elapsedTime));                     
  49.  
  50.     // Generate random values -1 to 1
  51.     float r = ra(seed++);
  52.     float g = ra(seed++);
  53.     float b = ra(seed++);
  54.    
  55.     vec3 outRGB = vec3(0);
  56.    
  57.     //
  58.     outRGB.r = outRGB.r + vCoords.x + sin(r * elapsedTime);
  59.     outRGB.g = outRGB.g + vCoords.y + cos(g * elapsedTime);
  60.     outRGB.b = outRGB.b + tan(b * elapsedTime);
  61.    
  62.     //
  63.     outRGB = outRGB + g * sin(vPosition.x);
  64.     outRGB = outRGB + b * cos(vPosition.z);
  65.    
  66.     // Output
  67.     gl_FragColor = vec4(outRGB, 1);
  68. }