diff --git a/shaders/gles2/stencil-aaa.fs.glsl b/shaders/gles2/stencil-aaa.fs.glsl index f0097163..e79e7162 100644 --- a/shaders/gles2/stencil-aaa.fs.glsl +++ b/shaders/gles2/stencil-aaa.fs.glsl @@ -21,20 +21,26 @@ void main() { vec2 from = vFrom, ctrl = vCtrl, to = vTo; // Determine winding, and sort into a consistent order so we only need to find one root below. - bool winding = from.x < to.x; - vec2 left = winding ? from : to, right = winding ? to : from; - vec2 v0 = ctrl - left, v1 = right - ctrl; + vec2 v0 = ctrl - from, v1 = to - ctrl; // Shoot a vertical ray toward the curve. vec2 window = clamp(vec2(from.x, to.x), -0.5, 0.5); - float offset = mix(window.x, window.y, 0.5) - left.x; - float t = offset / (v0.x + sqrt(v1.x * offset - v0.x * (offset - v0.x))); + //float offset = mix(window.x, window.y, 0.5) - left.x; + //float t = offset / (v0.x + sqrt(v1.x * offset - v0.x * (offset - v0.x))); + float t = 0.5; + float x = mix(mix(from.x, ctrl.x, t), mix(ctrl.x, to.x, t), t); + float dX = 2.0 * mix(v0.x, v1.x, t); + t -= x / dX; + x = mix(mix(from.x, ctrl.x, t), mix(ctrl.x, to.x, t), t); + dX = 2.0 * mix(v0.x, v1.x, t); + t -= x / dX; // Compute position and derivative to form a line approximation. - float y = mix(mix(left.y, ctrl.y, t), mix(ctrl.y, right.y, t), t); - float d = mix(v0.y, v1.y, t) / mix(v0.x, v1.x, t); + float y = mix(mix(from.y, ctrl.y, t), mix(ctrl.y, to.y, t), t); + //float dYDX = mix(v0.y, v1.y, t) / mix(v0.x, v1.x, t); + float dYDX = dFdx(y); // Look up area under that line, and scale horizontally to the window size. - float dX = window.x - window.y; - gl_FragColor = vec4(texture2D(uAreaLUT, vec2(y + 8.0, abs(d * dX)) / 16.0).r * dX); + dX = (gl_FrontFacing ? 1.0 : -1.0) * (window.x - window.y); + gl_FragColor = vec4(texture2D(uAreaLUT, vec2(y + 8.0, abs(dYDX * dX)) / 16.0).r * dX); }