Skip to content

Instantly share code, notes, and snippets.

@fire
Forked from ruby0x1/tilt.shift.glsl
Created January 26, 2021 20:30
Show Gist options
  • Select an option

  • Save fire/3fdfe2cb24c31c620dbb2bd1de6cf543 to your computer and use it in GitHub Desktop.

Select an option

Save fire/3fdfe2cb24c31c620dbb2bd1de6cf543 to your computer and use it in GitHub Desktop.
Tilt shift shader, modified from something @grapefrukt gave me
// Modified version of a tilt shift shader from Martin Jonasson (http://grapefrukt.com/)
// Read http://notes.underscorediscovery.com/ for context on shaders and this file
// License : MIT
uniform sampler2D tex0;
varying vec2 tcoord;
varying vec4 color;
//I am hardcoding the constants like a jerk,
// Take note that blurring in a single pass (the two for loops below) is more expensive than separating
// the x and the y blur into different passes. This was used where bleeding edge performance
// was not crucial and is to illustrate a point.
// Some interesting discussion : http://theinstructionlimit.com/gaussian-blur-revisited-part-two
const float bluramount = 1.0;
const float center = 1.1;
const float stepSize = 0.004;
const float steps = 3.0;
const float minOffs = (float(steps-1.0)) / -2.0;
const float maxOffs = (float(steps-1.0)) / +2.0;
void main() {
float amount;
vec4 blurred;
//Work out how much to blur based on the mid point
amount = pow((tcoord.y * center) * 2.0 - 1.0, 2.0) * bluramount;
//This is the accumulation of color from the surrounding pixels in the texture
blurred = vec4(0.0, 0.0, 0.0, 1.0);
//From minimum offset to maximum offset
for (float offsX = minOffs; offsX <= maxOffs; ++offsX) {
for (float offsY = minOffs; offsY <= maxOffs; ++offsY) {
//copy the coord so we can mess with it
vec2 temp_tcoord = tcoord.xy;
//work out which uv we want to sample now
temp_tcoord.x += offsX * amount * stepSize;
temp_tcoord.y += offsY * amount * stepSize;
//accumulate the sample
blurred += texture2D(tex0, temp_tcoord);
} //for y
} //for x
//because we are doing an average, we divide by the amount (x AND y, hence steps * steps)
blurred /= float(steps * steps);
//return the final blurred color
gl_FragColor = blurred;
} //main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment