Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Vertex
- varying vec2 vertTexCoord;
- attribute vec3 in_Position;
- attribute vec2 in_TextureCoord;
- void main() {
- vertTexCoord = in_TextureCoord;
- gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * vec4(in_Position, 1.0);
- }
- // Fragment
- uniform sampler2D depthMap;
- uniform vec2 screenSize;
- uniform float blurSize;
- uniform float depth;
- uniform float range;
- uniform float fadeSize;
- uniform float zfar;
- varying vec2 vertTexCoord;
- const float pi = 3.14159265;
- const float tau = pi * 2.0;
- const int samples = 15;
- const int ringDetail = 4;
- const int totalSamples = samples * ringDetail;
- float screenSampleSize = screenSize.y * blurSize;
- vec2 texelSize = 1.0 / screenSize;
- float colorToFloat(vec3 c) { return c.r + c.g / 255.0 + c.b / (255.0 * 255.0); }
- float getDepth(vec2 coord) { return colorToFloat(texture2D(depthMap, coord).rgb) * zfar; }
- float getBlur(float d) { return clamp((abs(d - depth) - range) / fadeSize, 0.0, 1.0); }
- float getFrontBlur(float d) { return clamp(((depth - range) - d) / fadeSize, 0.0, 1.0); }
- float getBackBlur(float d) { return clamp((d - (depth + range)) / fadeSize, 0.0, 1.0); }
- void main() {
- float myDepth = getDepth(vertTexCoord), myFrontBlur = getFrontBlur(myDepth), myBackBlur = getBackBlur(myDepth);
- float blur = 0.0, maxdis = screenSampleSize * screenSampleSize + screenSampleSize * screenSampleSize, colorDiv = 0.0;
- vec4 colorAdd = vec4(0.0);
- gl_FragColor = texture2D(gm_BaseTexture, vertTexCoord);
- for (int i = 0; i < totalSamples; i++) {
- float angle = (float(i / ringDetail) / float(samples)) * tau;
- float dis = 1.0 - mod(float(i),float(ringDetail)) / float(ringDetail);
- vec2 offset = vec2(cos(angle), sin(angle)) * dis * screenSampleSize;
- vec2 tex = vertTexCoord + texelSize * offset;
- float sampleDepth = getDepth(tex);
- float sampleBlur = getBlur(sampleDepth);
- float mul = dis * (1.0 - (1.0 - sampleBlur) * myBackBlur);
- if (mul > 0.0) {
- colorAdd += texture2D(gm_BaseTexture, tex) * mul;
- colorDiv += mul;
- }
- blur += getFrontBlur(sampleDepth);
- }
- blur /= float(totalSamples);
- blur += myFrontBlur + myBackBlur;
- colorAdd *= blur;
- colorDiv *= blur;
- gl_FragColor += colorAdd;
- gl_FragColor /= colorDiv + 1.0;
- }
Advertisement
Add Comment
Please, Sign In to add comment