Skip to content

Instantly share code, notes, and snippets.

@ufna
Last active October 13, 2019 10:23
Show Gist options
  • Save ufna/ac57328e285659569547b505567bf027 to your computer and use it in GitHub Desktop.
Save ufna/ac57328e285659569547b505567bf027 to your computer and use it in GitHub Desktop.
Gaussian blur using indirect texture access (by UVs for mobile)
const float PI = 3.1415926535897932384626433832795;
const float DoublePI = 2.0 * PI;
const float CircleSize = 1.0;
const float SmoothPadding = 0.33;
const float Distance = 0.04;
const float DistanceSteps = 8.0;
const float RadialSteps = 8.0;
const float RadialOffset = 0.5;
const float KernelPower = 1.0;
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = fragCoord/iResolution.xy;
// Clear color
vec3 ClearColor = texture(iChannel0, uv).rgb;
// Blur using UV access
float StepSize = Distance / DistanceSteps;
vec3 BlurColor = vec3(0);
vec2 SubUV = uv;
float CurDistance = 0.0;
vec2 CurOffset = vec2(0);
float SubOffset = 0.0;
float KernelDistance = 0.0;
float AccumulatedDistance = 0.0;
for(int i = 0; i < int(DistanceSteps); i++)
{
CurDistance += StepSize;
for (int j = 0; j < int(RadialSteps); j++)
{
SubOffset += 1.0;
CurOffset.x = cos(DoublePI * (SubOffset / RadialSteps));
CurOffset.y = sin(DoublePI * (SubOffset / RadialSteps));
SubUV.x = uv.x + CurOffset.x * CurDistance;
SubUV.y = uv.y + CurOffset.y * CurDistance;
KernelDistance = pow(CurDistance, KernelPower);
BlurColor += texture(iChannel0,SubUV).rgb * KernelDistance;
AccumulatedDistance += KernelDistance;
}
SubOffset += RadialOffset;
}
BlurColor /= AccumulatedDistance;
// Calculate clear vision circle
float AspectRatio = iResolution.x / iResolution.y;
vec2 VisionCenter = iMouse.xy/iResolution.xy;
vec2 v = uv - VisionCenter;
v.x = v.x * AspectRatio;
float CirleRadius = CircleSize / 2.0;
float SmoothRadius = CirleRadius * SmoothPadding;
float CircleMask = smoothstep(CirleRadius, CirleRadius - SmoothRadius, length(v));
BlurColor.rgb = mix(BlurColor.rgb, ClearColor.rgb, CircleMask);
fragColor = vec4(BlurColor, 1.0);
}
@ufna
Copy link
Author

ufna commented Oct 12, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment