Toshihiro Shimizu 890ddd
#ifdef GL_ES
Toshihiro Shimizu 890ddd
precision mediump float;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
uniform mat3      worldToOutput;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
uniform sampler2D inputImage[1];
Toshihiro Shimizu 890ddd
uniform mat3      outputToInput[1];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
uniform vec2  center;
Toshihiro Shimizu 890ddd
uniform float radius;
Toshihiro Shimizu 890ddd
uniform float blur;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
float det(mat3 m) { return m[0][0] * m[1][1] - m[0][1] * m[1][0]; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
mat3 worldToInput = outputToInput[0] * worldToOutput;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
vec2 center_s     = (worldToOutput * vec3(center, 1.0)).xy;
Toshihiro Shimizu 890ddd
float scale_s     = sqrt(abs(det(worldToOutput)));
Toshihiro Shimizu 890ddd
float rad_s       = scale_s * max(radius, 0.0);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define STEPS_PER_PIXEL   4.0
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void main( void )
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
  // Build lengths on output metrics
Toshihiro Shimizu 890ddd
  vec2  v           = gl_FragCoord.xy - center_s;
Toshihiro Shimizu 890ddd
  float vLength     = length(v);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  float dist_s      = max(vLength - rad_s, 0.0);
Toshihiro Shimizu 890ddd
  float b_s         = blur * dist_s;
Toshihiro Shimizu 890ddd
  
Toshihiro Shimizu 890ddd
  // Putting a maximum samples count - to prevent freezes; besides, blurring too many
Toshihiro Shimizu 890ddd
  // pixels is typically useless...
Toshihiro Shimizu 890ddd
  int samplesCount  = int(clamp(ceil(b_s * STEPS_PER_PIXEL), 1.0, 2000.0));
Toshihiro Shimizu 890ddd
  float step_s      = b_s / float(samplesCount);
Toshihiro Shimizu 890ddd
  
Toshihiro Shimizu 890ddd
  
Toshihiro Shimizu 890ddd
  // Perform filtering
Toshihiro Shimizu 890ddd
  vec2 texPos       = (outputToInput[0] * vec3(gl_FragCoord.xy, 1.0)).xy;
Toshihiro Shimizu 890ddd
  vec4 pix          = texture2D(inputImage[0], texPos);
Toshihiro Shimizu 890ddd
  
Toshihiro Shimizu 890ddd
  vec2 vStep = v * (step_s / max(vLength, 0.01));
Toshihiro Shimizu 890ddd
  vStep      = (outputToInput[0] * vec3(vStep, 0.0)).xy;
Toshihiro Shimizu 890ddd
  
Toshihiro Shimizu 890ddd
  vec2 tPos0 = texPos + vStep;
Toshihiro Shimizu 890ddd
  vec2 tPos1 = texPos - vStep;
Toshihiro Shimizu 890ddd
  
Toshihiro Shimizu 890ddd
  for(int s = 1; s < samplesCount; ++s)
Toshihiro Shimizu 890ddd
  {
Toshihiro Shimizu 890ddd
    pix += texture2D(inputImage[0], tPos0);
Toshihiro Shimizu 890ddd
    pix += texture2D(inputImage[0], tPos1);
Toshihiro Shimizu 890ddd
  
Toshihiro Shimizu 890ddd
    tPos0 += vStep, tPos1 -= vStep;
Toshihiro Shimizu 890ddd
  }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  gl_FragColor = pix / float(2 * samplesCount - 1);
Toshihiro Shimizu 890ddd
}