/* * Fragment shader for a "film grain" function. Applies a random amount of * noise to every pixel; intended to be used on a quad drawn over the * screen (but could be used, say, for the surface of a TV in a scene). * Very simple and stupid; uses a popular pseudorandom number generator. * Much room for improvement. * * Alexandri Zavodny, Fall 2010 * for CSE 40166/60166 Computer Graphics, University of Notre Dame */ uniform float time; varying vec4 vertexInImageSpace; float rand(vec2 co) { return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); } void main() { //just get a random number, using the vertex's screen-space position as a seed along with the time. float r = rand(vec2(vertexInImageSpace.x/2.0 + sin(time), vertexInImageSpace.y/2.0 + cos(time))); //play with the noise a bit; clamp it based on its value and scale the alpha. if(r < 0.75) r = 0.0; vec4 finalColor = vec4(r,r,r,r*0.3); gl_FragColor = clamp(finalColor, 0.0, 1.0); }