Toshihiro Shimizu 890ddd
#ifdef GL_ES
Toshihiro Shimizu 890ddd
precision mediump float;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
uniform mat3 worldToOutput;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
uniform vec4 outputRect;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
varying vec4 inputRect[1];
Toshihiro Shimizu 890ddd
varying mat3 worldToInput[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
float scale     = sqrt(abs(det(worldToOutput)));
Toshihiro Shimizu 890ddd
  
Toshihiro Shimizu 890ddd
vec2 center_    = (worldToOutput * vec3(center, 1.0)).xy;
Toshihiro Shimizu 890ddd
float rad_      = scale * max(radius, 0.0);
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 rad_ > 0, we assume that the blurred length is proportional to (distance - rad_);
Toshihiro Shimizu 890ddd
  float dist_       = max(distance - rad_, 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
  worldToInput[0] = worldToOutput;                    // Let the input and output references be the same
Toshihiro Shimizu 890ddd
  inputRect[0]    = outputRect;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  // Add the bounding box of each blurred corner
Toshihiro Shimizu 890ddd
  addBlurredPointBox(inputRect[0], outputRect.xy);
Toshihiro Shimizu 890ddd
  addBlurredPointBox(inputRect[0], outputRect.xw);
Toshihiro Shimizu 890ddd
  addBlurredPointBox(inputRect[0], outputRect.zy);
Toshihiro Shimizu 890ddd
  addBlurredPointBox(inputRect[0], outputRect.zw);
Toshihiro Shimizu 890ddd
  
Toshihiro Shimizu 890ddd
  gl_Position = vec4(0.0);                            // Does not link without
Toshihiro Shimizu 890ddd
}