// ## FRAGMENT SHADER ##
// Author: Jackson Luff
// Name: Interact3V.frag
// Type: Fragment Shader
// Description:
// * In this frag shader I
// * wrote up some pretty
// * cool shader work without
// * any textures. Pretty nifty
#version 430
in vec4 vPosition;
in vec4 vNormal;
in vec2 vCoords;
uniform float elapsedTime;
const float INVERSE_MAX_UINT = 1.0 / 4294967295.0;
// The GPU version of C++'s rand()
float rand(uint seed, float range)
{
uint i = (seed ^ 12345391u) * 2654435769u;
i ^= (i << 6u) ^ (i >> 26u);
i *= 2654435769u;
i += (i << 5u) ^ (i >> 12u);
return float(range * i) * INVERSE_MAX_UINT;
}
// Helper function for rand()
float ra(uint seed)
{
return rand(seed++, 2) - 1;
}
void main()
{
// pass in mousePos as a uniform from on CPU.
// use line–plane intersection to determine
// screen-space mousePos in world space
// ref: (https://en.wikipedia.org/wiki/Line%E2%80%93plane_intersection)
float dist = length(mousePos - vPosition.xyz);
uint seed = uint(elapsedTime) + uint(dist);
// generate random values -1 to 1
float r = ra(seed++);
float g = ra(seed++);
float b = ra(seed++);
vec3 outRGB = vec3(0);
// Warps perspective of colours
outRGB.r = outRGB.r + vCoords.x + sin(r * elapsedTime);
outRGB.g = outRGB.g + vCoords.y + cos(g * elapsedTime);
outRGB.b = outRGB.b + tan(b * elapsedTime);
// Generates new colours surrounding
outRGB = outRGB + (ra(seed++) + elapsedTime/seed)* tan(dist);
outRGB = outRGB + (ra(seed++) + elapsedTime/seed)* sin(vPosition.x);
outRGB = outRGB + (ra(seed++) + elapsedTime/seed)* cos(vPosition.z);
// Output
gl_FragColor = vec4(outRGB, 1);
}