1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
#version 120
/**
* @project Spring RTS
* @file bumpWaterCoastBlurFS.glsl
* @brief Input is a 0/1 bitmap and 1 indicates land.
* Now this shader blurs this map, so you get something like
* a distance to land map (->coastmap).
* @author jK
*
* Copyright (C) 2008,2009. Licensed under the terms of the
* GNU GPL, v2 or later.
*/
#define res 15.0
#define renderToAtlas (gl_TexCoord[2].x > 0.5)
#define radius gl_TexCoord[2].y
uniform sampler2D tex0; //! final (fullsize) texture
uniform sampler2D tex1; //! atlas with to be updated rects
const float kernel = 1.0 / 10.0;
vec2 texelScissor = vec2(dFdx(gl_TexCoord[0].p), dFdy(gl_TexCoord[0].q)); // heightmap pos
vec2 texel0 = vec2(dFdx(gl_TexCoord[0].s), dFdy(gl_TexCoord[0].t)); // 0..1
vec4 tex2D(vec2 offset) {
if (renderToAtlas) {
return texture2D(tex0, gl_TexCoord[0].st + offset * texel0);
} else {
vec2 scissor = gl_TexCoord[0].pq + (offset * texelScissor);
bool outOfAtlasBound = any(greaterThan(scissor,vec2(1.0))) || any(lessThan(scissor,vec2(0.0)));
if (outOfAtlasBound) {
return texture2D(tex1, gl_TexCoord[0].st);
} else {
return texture2D(tex1, gl_TexCoord[0].st + offset * texel0);
}
}
}
vec2 getDistRect(float d, vec2 offset) {
vec2 dist;
float minDist = res - d * res;
dist.x = floor(minDist);
float iDist = dist.x * dist.x;
minDist *= minDist;
dist.y = sqrt(minDist - iDist);
dist += offset;
return dist;
}
float sqlength(vec2 v) {
return dot(v, v);
}
void LoopIter(in out float maxDist, in out vec3 minDist, float i) {
// 0____1____2
// | |
// | |
// 3 x 4
// | |
// | |
// 5____6____7
vec4 v1, v2;
v1.x = tex2D(vec2(-i, radius)).g;
v1.y = tex2D(vec2( i, radius)).g;
v1.z = tex2D(vec2(-i, -radius)).g;
v1.w = tex2D(vec2( i, -radius)).g;
v2.x = tex2D(vec2( radius, i)).g;
v2.y = tex2D(vec2( radius, -i)).g;
v2.z = tex2D(vec2(-radius, i)).g;
v2.w = tex2D(vec2(-radius, -i)).g;
v1 = max(v1, v2 );
v1.xy = max(v1.xy, v1.zw);
v1.x = max(v1.x, v1.y );
v1.x = max(v1.x, maxDist );
vec2 dist = getDistRect(v1.x, vec2(radius, i));
//minDist.z = min(minDist.z, sqlength(dist));
if (sqlength(dist) < minDist.z) {
maxDist = v1.x;
minDist = vec3(dist, sqlength(dist));
}
}
void main() {
if (radius < 0.5) {
//! initialize
gl_FragColor = texture2D(tex1, gl_TexCoord[0].st);
return;
}
if (radius > 9.5) {
//! blur the texture in the final stage
vec2
groundSurrounding = tex2D(vec2( 1.0, 1.0)).rb;
groundSurrounding += tex2D(vec2(-1.0, 1.0)).rb;
groundSurrounding += tex2D(vec2(-1.0,-1.0)).rb;
groundSurrounding += tex2D(vec2( 1.0,-1.0)).rb;
gl_FragColor = texture2D(tex1, gl_TexCoord[0].st);
if (groundSurrounding.x + gl_FragColor.r == 5.0) {
gl_FragColor.r = 1.0;
} else {
gl_FragColor.r = 0.93 - (groundSurrounding.y + gl_FragColor.b) / 5.0;
}
return;
} else if (radius > 8.5) {
//! blur the texture in the final stage
vec2
blur = texture2D(tex0, gl_TexCoord[0].st + vec2( 1.0, 1.0) * texel0).rg;
blur += texture2D(tex0, gl_TexCoord[0].st + vec2(-1.0, 1.0) * texel0).rg;
blur += texture2D(tex0, gl_TexCoord[0].st + vec2(-1.0,-1.0) * texel0).rg;
blur += texture2D(tex0, gl_TexCoord[0].st + vec2( 1.0,-1.0) * texel0).rg;
gl_FragColor = texture2D(tex0, gl_TexCoord[0].st);
gl_FragColor.r = step(5.0, blur.x + gl_FragColor.r);
gl_FragColor.g = mix(gl_FragColor.g, blur.y * 0.25, 0.4);
return;
}
float maxValue = 0.0;
vec3 minDist = vec3(1e9);
// driver fails at unrolling when count is not known
// at compile-time, so we do it manually (radius is
// in [0.5, 8.5], so we need at most 8 iterations)
float iter = 0.0;
if (iter <= radius) { LoopIter(maxValue, minDist, iter); iter += 1.0; }
if (iter <= radius) { LoopIter(maxValue, minDist, iter); iter += 1.0; }
if (iter <= radius) { LoopIter(maxValue, minDist, iter); iter += 1.0; }
if (iter <= radius) { LoopIter(maxValue, minDist, iter); iter += 1.0; }
if (iter <= radius) { LoopIter(maxValue, minDist, iter); iter += 1.0; }
if (iter <= radius) { LoopIter(maxValue, minDist, iter); iter += 1.0; }
if (iter <= radius) { LoopIter(maxValue, minDist, iter); iter += 1.0; }
if (iter <= radius) { LoopIter(maxValue, minDist, iter); iter += 1.0; }
//! PROCESS maxValue
//if (maxValue == 0.0)
// discard;
float fDist = 1.0 - (min(res, sqrt(minDist.z)) / res);
if (renderToAtlas) {
gl_FragColor = texture2D(tex0, gl_TexCoord[0].st);
} else {
gl_FragColor = texture2D(tex1, gl_TexCoord[0].st);
}
gl_FragColor.g = max(gl_FragColor.g, fDist * fDist * fDist);
}
|