Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // More realistic depth-of-field by Azraeil.
- // This is a modification of Daxnitro's depth-of-field shader.
- uniform sampler2D sampler0;
- uniform sampler2D sampler1;
- uniform sampler2D sampler2;
- uniform float aspectRatio;
- uniform float near;
- uniform float far;
- // HYPERFOCAL = (Focal Distance ^ 2)/(Circle of Confusion * F Stop) + Focal Distance
- const float HYPERFOCAL = 3.132;
- const float PICONSTANT = 3.14159;
- float getDepth(vec2 coord);
- float getCursorDepth(vec2 coord);
- vec4 getBlurredColor();
- vec4 getSample(vec2 coord, vec2 aspectCorrection);
- vec4 getSampleWithBoundsCheck(vec2 offset);
- float samples = 0.0;
- vec2 space;
- void main() {
- vec4 baseColor = texture2D(sampler0, gl_TexCoord[0].st);
- float depth = getDepth(gl_TexCoord[0].st);
- float cursorDepth = getCursorDepth(vec2(0.5, 0.5));
- // foreground blur = 1/2 background blur. Blur should follow exponential pattern until cursor = hyperfocal -- Cursor before hyperfocal
- // Blur should go from 0 to 1/2 hyperfocal then clear to infinity -- Cursor @ hyperfocal.
- // hyperfocal to inifity is clear though dof extends from 1/2 hyper to hyper -- Cursor beyond hyperfocal
- /* Use for testing DoF
- float brightness = 0.0;
- if (cursorDepth > HYPERFOCAL) {
- if (depth < HYPERFOCAL) {
- if (depth < 0.5 * HYPERFOCAL) {
- brightness = 0.0;
- } else {
- brightness = 1.0 - (sin(2.0 * (depth - 0.25 * HYPERFOCAL) / HYPERFOCAL * PICONSTANT) / 2.0 + 0.5);
- }
- } else {
- brightness = 1.0;
- }
- } else {
- if (depth - cursorDepth > (cursorDepth * HYPERFOCAL) / (HYPERFOCAL - cursorDepth)) {
- brightness = 0.0;
- } else if (depth > cursorDepth) {
- brightness = 1.0 - (sin(PICONSTANT * ((depth - cursorDepth) - 0.5 * ((cursorDepth * HYPERFOCAL) / (HYPERFOCAL - cursorDepth))) / ((cursorDepth * HYPERFOCAL) / (HYPERFOCAL - cursorDepth))) / 2.0 + 0.5);
- } else if (depth <= 0.5 * cursorDepth) {
- brightness = 0.0;
- } else {
- brightness = 1.0 - (sin(2.0 * (depth - 0.25 * cursorDepth) / cursorDepth * PICONSTANT) / 2.0 + 0.5);
- }
- }
- gl_FragColor = vec4(brightness, brightness, brightness, 1.0);
- */
- // need to optimize in hopes to avoid more branching.
- if (cursorDepth > HYPERFOCAL) {
- /*if (depth < HYPERFOCAL) {
- if (depth < 0.5 * HYPERFOCAL) {
- gl_FragColor = getBlurredColor();
- } else {
- gl_FragColor = mix(baseColor, getBlurredColor(), clamp(sin(2.0 * (depth - 0.25 * HYPERFOCAL) / HYPERFOCAL * PICONSTANT) / 2.0 + 0.5, 0.0, 1.0));
- }
- } else {
- gl_FragColor = baseColor;
- }*/
- gl_FragColor = getBlurredColor();
- } else {
- if ((depth - cursorDepth) > (cursorDepth * HYPERFOCAL) / (HYPERFOCAL - cursorDepth)) {
- gl_FragColor = getBlurredColor();
- } else if (depth > cursorDepth) {
- gl_FragColor = mix(baseColor, getBlurredColor(), clamp(sin(PICONSTANT * ((depth - cursorDepth) - 0.5 * ((cursorDepth * HYPERFOCAL) / (HYPERFOCAL - cursorDepth))) / ((cursorDepth * HYPERFOCAL) / (HYPERFOCAL - cursorDepth))) / 2.0 + 0.5, 0.0, 1.0));
- } else if (depth <= 0.5 * cursorDepth) {
- gl_FragColor = getBlurredColor();
- } else {
- gl_FragColor = mix(baseColor, getBlurredColor(), clamp(sin(2.0 * (depth - 0.25 * cursorDepth) / cursorDepth * PICONSTANT) / 2.0 + 0.5, 0.0, 1.0));
- }
- }
- }
- float getDepth(vec2 coord) {
- float depth = texture2D(sampler1, coord).x;
- float depth2 = texture2D(sampler2, coord).x;
- if (depth2 < 1.0) {
- depth = depth2;
- }
- depth = 2.0 * near * far / (far + near - (2.0 * depth - 1.0) * (far - near));
- return depth;
- }
- float getCursorDepth(vec2 coord) {
- return 2.0 * near * far / (far + near - (2.0 * texture2D(sampler1, coord).x - 1.0) * (far - near));
- }
- vec4 getBlurredColor() {
- vec4 blurredColor = vec4(0.0);
- float depth = getDepth(gl_TexCoord[0].xy);
- vec2 aspectCorrection = vec2(1.0, aspectRatio) * 0.005;
- vec2 ac0_4 = 0.4 * aspectCorrection; // 0.4
- //vec2 ac0_4x0_4 = 0.4 * ac0_4; // 0.16
- //vec2 ac0_4x0_7 = 0.7 * ac0_4; // 0.28
- vec2 ac0_29 = 0.29 * aspectCorrection; // 0.29
- //vec2 ac0_29x0_7 = 0.7 * ac0_29; // 0.203
- //vec2 ac0_29x0_4 = 0.4 * ac0_29; // 0.116
- vec2 ac0_15 = 0.15 * aspectCorrection; // 0.15
- vec2 ac0_37 = 0.37 * aspectCorrection; // 0.37
- //vec2 ac0_15x0_9 = 0.9 * ac0_15; // 0.135
- //vec2 ac0_37x0_9 = 0.37 * ac0_37; // 0.1369
- vec2 lowSpace = gl_TexCoord[0].st;
- vec2 highSpace = 1.0 - lowSpace;
- space = vec2(min(lowSpace.s, highSpace.s), min(lowSpace.t, highSpace.t));
- if (space.s >= ac0_4.s && space.t >= ac0_4.t) {
- blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(0.0, ac0_4.t));
- blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_4.s, 0.0));
- blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(0.0, -ac0_4.t));
- blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_4.s, 0.0));
- //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_4x0_7.s, 0.0));
- //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(0.0, -ac0_4x0_7.t));
- //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_4x0_7.s, 0.0));
- //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(0.0, ac0_4x0_7.t));
- //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_4x0_4.s, 0.0));
- //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(0.0, -ac0_4x0_4.t));
- //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_4x0_4.s, 0.0));
- //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(0.0, ac0_4x0_4.t));
- blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_29.s, -ac0_29.t));
- blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_29.s, ac0_29.t));
- blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_29.s, ac0_29.t));
- blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_29.s, -ac0_29.t));
- //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_29x0_7.s, ac0_29x0_7.t));
- //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_29x0_7.s, -ac0_29x0_7.t));
- //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_29x0_7.s, ac0_29x0_7.t));
- //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_29x0_7.s, -ac0_29x0_7.t));
- //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_29x0_4.s, ac0_29x0_4.t));
- //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_29x0_4.s, -ac0_29x0_4.t));
- //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_29x0_4.s, ac0_29x0_4.t));
- //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_29x0_4.s, -ac0_29x0_4.t));
- blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_15.s, ac0_37.t));
- blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_37.s, ac0_15.t));
- blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_37.s, -ac0_15.t));
- blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_15.s, -ac0_37.t));
- blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_15.s, ac0_37.t));
- blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_37.s, ac0_15.t));
- blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_37.s, -ac0_15.t));
- blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_15.s, -ac0_37.t));
- //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_15x0_9.s, ac0_37x0_9.t));
- //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_37x0_9.s, ac0_15x0_9.t));
- //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_37x0_9.s, -ac0_15x0_9.t));
- //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_15x0_9.s, -ac0_37x0_9.t));
- //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_15x0_9.s, ac0_37x0_9.t));
- //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_37x0_9.s, ac0_15x0_9.t));
- //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_37x0_9.s, -ac0_15x0_9.t));
- //blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_15x0_9.s, -ac0_37x0_9.t));
- //blurredColor /= 41.0;
- blurredColor /= 16.0;
- } else {
- blurredColor += getSampleWithBoundsCheck(vec2(0.0, ac0_4.t));
- blurredColor += getSampleWithBoundsCheck(vec2(ac0_4.s, 0.0));
- blurredColor += getSampleWithBoundsCheck(vec2(0.0, -ac0_4.t));
- blurredColor += getSampleWithBoundsCheck(vec2(-ac0_4.s, 0.0));
- //blurredColor += getSampleWithBoundsCheck(vec2(ac0_4x0_7.s, 0.0));
- //blurredColor += getSampleWithBoundsCheck(vec2(0.0, -ac0_4x0_7.t));
- //blurredColor += getSampleWithBoundsCheck(vec2(-ac0_4x0_7.s, 0.0));
- //blurredColor += getSampleWithBoundsCheck(vec2(0.0, ac0_4x0_7.t));
- //blurredColor += getSampleWithBoundsCheck(vec2(ac0_4x0_4.s, 0.0));
- //blurredColor += getSampleWithBoundsCheck(vec2(0.0, -ac0_4x0_4.t));
- //blurredColor += getSampleWithBoundsCheck(vec2(-ac0_4x0_4.s, 0.0));
- //blurredColor += getSampleWithBoundsCheck(vec2(0.0, ac0_4x0_4.t));
- blurredColor += getSampleWithBoundsCheck(vec2(ac0_29.s, -ac0_29.t));
- blurredColor += getSampleWithBoundsCheck(vec2(ac0_29.s, ac0_29.t));
- blurredColor += getSampleWithBoundsCheck(vec2(-ac0_29.s, ac0_29.t));
- blurredColor += getSampleWithBoundsCheck(vec2(-ac0_29.s, -ac0_29.t));
- //blurredColor += getSampleWithBoundsCheck(vec2(ac0_29x0_7.s, ac0_29x0_7.t));
- //blurredColor += getSampleWithBoundsCheck(vec2(ac0_29x0_7.s, -ac0_29x0_7.t));
- //blurredColor += getSampleWithBoundsCheck(vec2(-ac0_29x0_7.s, ac0_29x0_7.t));
- //blurredColor += getSampleWithBoundsCheck(vec2(-ac0_29x0_7.s, -ac0_29x0_7.t));
- //blurredColor += getSampleWithBoundsCheck(vec2(ac0_29x0_4.s, ac0_29x0_4.t));
- //blurredColor += getSampleWithBoundsCheck(vec2(ac0_29x0_4.s, -ac0_29x0_4.t));
- //blurredColor += getSampleWithBoundsCheck(vec2(-ac0_29x0_4.s, ac0_29x0_4.t));
- //blurredColor += getSampleWithBoundsCheck(vec2(-ac0_29x0_4.s, -ac0_29x0_4.t));
- blurredColor += getSampleWithBoundsCheck(vec2(ac0_15.s, ac0_37.t));
- blurredColor += getSampleWithBoundsCheck(vec2(-ac0_37.s, ac0_15.t));
- blurredColor += getSampleWithBoundsCheck(vec2(ac0_37.s, -ac0_15.t));
- blurredColor += getSampleWithBoundsCheck(vec2(-ac0_15.s, -ac0_37.t));
- blurredColor += getSampleWithBoundsCheck(vec2(-ac0_15.s, ac0_37.t));
- blurredColor += getSampleWithBoundsCheck(vec2(ac0_37.s, ac0_15.t));
- blurredColor += getSampleWithBoundsCheck(vec2(-ac0_37.s, -ac0_15.t));
- blurredColor += getSampleWithBoundsCheck(vec2(ac0_15.s, -ac0_37.t));
- //blurredColor += getSampleWithBoundsCheck(vec2(ac0_15x0_9.s, ac0_37x0_9.t));
- //blurredColor += getSampleWithBoundsCheck(vec2(-ac0_37x0_9.s, ac0_15x0_9.t));
- //blurredColor += getSampleWithBoundsCheck(vec2(ac0_37x0_9.s, -ac0_15x0_9.t));
- //blurredColor += getSampleWithBoundsCheck(vec2(-ac0_15x0_9.s, -ac0_37x0_9.t));
- //blurredColor += getSampleWithBoundsCheck(vec2(-ac0_15x0_9.s, ac0_37x0_9.t));
- //blurredColor += getSampleWithBoundsCheck(vec2(ac0_37x0_9.s, ac0_15x0_9.t));
- //blurredColor += getSampleWithBoundsCheck(vec2(-ac0_37x0_9.s, -ac0_15x0_9.t));
- //blurredColor += getSampleWithBoundsCheck(vec2(ac0_15x0_9.s, -ac0_37x0_9.t));
- blurredColor /= samples;
- }
- return blurredColor;
- }
- vec4 getSampleWithBoundsCheck(vec2 offset) {
- vec2 coord = gl_TexCoord[0].st + offset;
- if (coord.s <= 1.0 && coord.s >= 0.0 && coord.t <= 1.0 && coord.t >= 0.0) {
- samples += 1.0;
- return texture2D(sampler0, coord);
- } else {
- return vec4(0.0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement