Skip to content

Commit

Permalink
Simplified anti aliasing implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jobtalle committed May 30, 2023
1 parent 7df0fe3 commit 474c0ad
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 10 deletions.
11 changes: 2 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,9 @@ vec4 color = vec4(pixel.rgb, step(0.5, pixel.a));
This example sets alpha to either `0` or `1`. Edges can also be anti aliased using the following GLSL snippet:

``` glsl
const float epsilon = 0.000001;
vec4 pixel = texture(source, uv);
float distance = fwidth(pixel.a) * .5;
float lower = .5 - distance;
float upper = .5 + distance;
float alpha = clamp((pixel.a - lower) / (upper - lower), 0., 1.));
vec4 color = vec4(pixel.rgb, alpha);
vec4 color = vec4(pixel.rgb, clamp(0.5 - pixel.a * 2.0 / fwidth(pixel.a), 0., 1.)));
```

This example interpolates alpha from `0` to `1`, creating smooth anti aliased edges.
This example interpolates alpha from `0` to `1` using standard derivatives, creating smooth anti aliased edges.
2 changes: 1 addition & 1 deletion js/gl/shaderPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class ShaderPreview extends Shader {
mix(
vec3(0.),
pixel.rgb,
clamp((pixel.a - lower) / (upper - lower), 0., 1.)),
clamp(.5 - pixel.a * 2. / fwidth(pixel.a), 0., 1.)),
1. - clamp((length(delta) - radius) * radiusPixels, 0., 1.));
}
`;
Expand Down

0 comments on commit 474c0ad

Please sign in to comment.