Toshihiro Shimizu 890ddd
#ifdef GL_ES
Toshihiro Shimizu 890ddd
precision mediump float;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
uniform vec4 infiniteRect;
Toshihiro Shimizu 890ddd
uniform vec4 inputBBox[1];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
varying vec4 outputBBox;
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
const float pi          = 3.141592653;
Toshihiro Shimizu 890ddd
const float pi_twice    = 2.0 * pi;
Toshihiro Shimizu 890ddd
const float pi_half     = pi / 2.0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void addPoint(inout vec4 rect, vec2 p) {
Toshihiro Shimizu 890ddd
  rect.xy = min(rect.xy, p.xy);
Toshihiro Shimizu 890ddd
  rect.zw = max(rect.zw, p.xy);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void addBlurredPointBox(inout vec4 rect, vec2 p)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
  // Remember the *definition* of angle:    angle = arc length / radius
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  // Build p's blurred angular range
Toshihiro Shimizu 890ddd
  float distance    = length(p - center);
Toshihiro Shimizu 890ddd
  float angle       = atan(p.y - center.y, p.x - center.x);
Toshihiro Shimizu 890ddd
  
Toshihiro Shimizu 890ddd
  // If radius > 0, we assume that the blurred length is proportional to (distance - radius);
Toshihiro Shimizu 890ddd
  float dist_       = max(distance - radius, 0.0);
Toshihiro Shimizu 890ddd
  float blurLen     = radians(max(blur, 0.0)) * dist_;
Toshihiro Shimizu 890ddd
  
Toshihiro Shimizu 890ddd
  // The actual blurring angle is then found as (blurLen_ / distance)
Toshihiro Shimizu 890ddd
  float blur_       = blurLen / max(distance, 0.01);            // Jump the singularity
Toshihiro Shimizu 890ddd
  
Toshihiro Shimizu 890ddd
  vec2 angleRange   = vec2(angle - blur_, angle + blur_);       // Couldn't make it an array with
Toshihiro Shimizu 890ddd
                                                                // explicit initialization... GLSL complained -.-
Toshihiro Shimizu 890ddd
  // Include the points at angleRange's extremes
Toshihiro Shimizu 890ddd
  addPoint(rect, center + distance * vec2(cos(angleRange.x), sin(angleRange.x)));
Toshihiro Shimizu 890ddd
  addPoint(rect, center + distance * vec2(cos(angleRange.y), sin(angleRange.y)));
Toshihiro Shimizu 890ddd
  
Toshihiro Shimizu 890ddd
  // At pi/2 multiples we get a box extreme. Include them if present.
Toshihiro Shimizu 890ddd
  float blur_twice = 2.0 * blur_;
Toshihiro Shimizu 890ddd
  
Toshihiro Shimizu 890ddd
  if(mod( - angleRange.x, pi_twice) < blur_twice)
Toshihiro Shimizu 890ddd
    addPoint(rect, center + vec2(distance, 0.0));
Toshihiro Shimizu 890ddd
  if(mod(pi_half - angleRange.x, pi_twice) < blur_twice)
Toshihiro Shimizu 890ddd
    addPoint(rect, center + vec2(0.0, distance));
Toshihiro Shimizu 890ddd
  if(mod(pi - angleRange.x, pi_twice) < blur_twice)
Toshihiro Shimizu 890ddd
    addPoint(rect, center + vec2(-distance, 0.0));
Toshihiro Shimizu 890ddd
  if(mod(-pi_half - angleRange.x, pi_twice) < blur_twice)
Toshihiro Shimizu 890ddd
    addPoint(rect, center + vec2(0.0, -distance));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void main( void )
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
  outputBBox = inputBBox[0];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  if(outputBBox != infiniteRect)
Toshihiro Shimizu 890ddd
  {
Toshihiro Shimizu 890ddd
    // Add the bounding box of each blurred corner
Toshihiro Shimizu 890ddd
    addBlurredPointBox(outputBBox, inputBBox[0].xy);
Toshihiro Shimizu 890ddd
    addBlurredPointBox(outputBBox, inputBBox[0].xw);
Toshihiro Shimizu 890ddd
    addBlurredPointBox(outputBBox, inputBBox[0].zy);
Toshihiro Shimizu 890ddd
    addBlurredPointBox(outputBBox, inputBBox[0].zw);
Toshihiro Shimizu 890ddd
  }
Toshihiro Shimizu 890ddd
  
Toshihiro Shimizu 890ddd
  gl_Position = vec4(0.0);                            // Does not link without
Toshihiro Shimizu 890ddd
}