diff --git a/src/shaders/linechart.frag b/src/shaders/linechart.frag index 3420216..2340f24 100644 --- a/src/shaders/linechart.frag +++ b/src/shaders/linechart.frag @@ -1,90 +1,90 @@ /* * This file is part of KQuickCharts * SPDX-FileCopyrightText: 2019 Arjen Hiemstra * * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL */ // This requires "sdf.frag" which is included through SDFShader. uniform lowp float opacity; // inherited opacity of this item uniform lowp vec4 lineColor; uniform lowp vec4 fillColor; uniform lowp float lineWidth; uniform lowp vec2 bounds; uniform lowp vec2 points[SDF_POLYGON_MAX_POINT_COUNT]; uniform int pointCount; #ifdef LEGACY_STAGE_INOUT varying lowp vec2 uv; #else in lowp vec2 uv; out lowp vec4 out_color; #endif #ifdef API_ES2 // ES2 does not support array function arguments. So instead we need to // reference the uniform array directly. So this copies the implementation of // sdf_polygon from sdf.glsl, changing it to refer to the points array directly. lowp float sdf_polygon(in lowp vec2 point, in lowp int count) { lowp float d = dot(point - points[0], point - points[0]); lowp float s = 1.0; for (int i = 0, j = count - 1; i < count && i < SDF_POLYGON_MAX_POINT_COUNT; j = i, i++) { lowp vec2 e = points[j] - points[i]; lowp vec2 w = point - points[i]; lowp float h = clamp( dot(w, e) / dot(e, e), 0.0, 1.0 ); lowp vec2 b = w - e * h; d = min(d, dot(b, b)); bvec3 c = bvec3(point.y >= points[i].y, point.y < points[j].y, e.x * w.y > e.y * w.x); if(all(c) || all(not(c))) s *= -1.0; } return s * sqrt(d); } #endif void main() { lowp vec2 point = uv; lowp vec4 color = vec4(0.0, 0.0, 0.0, 0.0); // bounds.y contains the line segment's maximum value. If we are a bit above // that, we will never render anything, so just discard the pixel. if (point.y > bounds.y + 0.01) { discard; } // bounds.x contains the line segment's minimum value. If we are a bit below // that, we know we will always be inside the polygon described by points. // So just return a pixel with fillColor. if (point.y < bounds.x - 0.01) { #ifdef LEGACY_STAGE_INOUT gl_FragColor = fillColor * opacity; #else out_color = fillColor * opacity; #endif return; } #ifdef API_ES2 lowp float polygon = sdf_polygon(point, pointCount); #else lowp float polygon = sdf_polygon(point, points, pointCount); #endif - color = sdf_render(polygon, color, fillColor, 0.001); + color = sdf_render(polygon, color, fillColor); if (lineWidth > 0.0) { - color = sdf_render(sdf_annular(sdf_outline(polygon), lineWidth), color, lineColor, 0.0002); + color = sdf_render(sdf_annular(sdf_outline(polygon), lineWidth), color, lineColor); } #ifdef LEGACY_STAGE_INOUT gl_FragColor = color * opacity; #else out_color = color * opacity; #endif } diff --git a/src/shaders/piechart.frag b/src/shaders/piechart.frag index ac28193..d31da73 100644 --- a/src/shaders/piechart.frag +++ b/src/shaders/piechart.frag @@ -1,78 +1,78 @@ /* * This file is part of KQuickCharts * SPDX-FileCopyrightText: 2019 Arjen Hiemstra * * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL */ // This requires "sdf.frag" which is included through SDFShader. // The maximum number of segments we can support for a single pie. // This is based on OpenGL's MAX_FRAGMENT_UNIFORM_COMPONENTS. // MAX_FRAGMENT_UNIFORM_COMPONENTS is required to be at least 1024. // Assuming a segment of size 1, each segment needs // 2 (size of a vec2) * 2 (number of points) + 4 (size of vec4) + 1 (segment size) // components. We also need to leave some room for the other uniforms. #define MAX_SEGMENTS 100 uniform lowp float opacity; uniform lowp float innerRadius; uniform lowp float outerRadius; uniform lowp vec4 backgroundColor; uniform bool smoothEnds; uniform lowp vec2 triangles[MAX_SEGMENTS * 2]; uniform lowp vec4 colors[MAX_SEGMENTS]; uniform int segments[MAX_SEGMENTS]; uniform int segmentCount; #ifdef LEGACY_STAGE_INOUT varying lowp vec2 uv; #else in lowp vec2 uv; out lowp vec4 out_color; #endif const lowp vec2 origin = vec2(0.0, 0.0); const lowp float lineSmooth = 0.001; void main() { lowp vec2 point = uv * (1.0 + lineSmooth * 2.0); lowp float thickness = (outerRadius - innerRadius) / 2.0; lowp float donut = sdf_annular(sdf_circle(point, innerRadius + thickness), thickness); lowp vec4 color = vec4(0.0); lowp float totalSegments = sdf_null; int index = 0; for (int i = 0; i < segmentCount && i < MAX_SEGMENTS; ++i) { lowp float segment = sdf_null; for(int j = 0; j < segments[i] && j < MAX_SEGMENTS; j++) { segment = sdf_union(segment, sdf_round(sdf_triangle(point, origin, triangles[index++], triangles[index++]), lineSmooth)); } totalSegments = sdf_union(totalSegments, segment); segment = smoothEnds ? sdf_intersect_smooth(donut, segment, thickness) : sdf_intersect(donut, segment); - color = sdf_render(segment, color, colors[i], lineSmooth); + color = sdf_render(segment, color, colors[i]); } // Finally, render an end segment with the background color. if (smoothEnds) { - lowp vec4 background = sdf_render(donut, vec4(0.0), backgroundColor, lineSmooth); + lowp vec4 background = sdf_render(donut, vec4(0.0), backgroundColor); color = mix(background, color, color.a); } else { lowp float segment = sdf_subtract(sdf_round(donut, lineSmooth), totalSegments); - color = sdf_render(segment, color, backgroundColor, lineSmooth); + color = sdf_render(segment, color, backgroundColor); } #ifdef LEGACY_STAGE_INOUT gl_FragColor = color * opacity; #else out_color = color * opacity; #endif }