OpenGL: shaders for smooth round points

Solid color with an optional outline.

size (diameter) and outlineWidth are in pixels.
This commit is contained in:
2016-10-08 03:10:59 -04:00
parent b071ac315c
commit 25e4dc45e5
7 changed files with 149 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
uniform float size;
uniform float outlineWidth;
#if __VERSION__ == 120
attribute vec2 pos;
varying vec4 radii;
#else
in vec2 pos;
out vec4 radii;
#endif
void main() {
gl_Position = gl_ModelViewProjectionMatrix * vec4(pos, 0.0, 1.0);
gl_PointSize = size;
// calculate concentric radii in pixels
float radius = 0.5 * size;
// start at the outside and progress toward the center
radii[0] = radius;
radii[1] = radius - 1.0;
radii[2] = radius - outlineWidth;
radii[3] = radius - outlineWidth - 1.0;
// convert to PointCoord units
radii /= size;
}

View File

@@ -0,0 +1,25 @@
uniform float size;
#if __VERSION__ == 120
attribute vec2 pos;
varying vec2 radii;
#else
in vec2 pos;
out vec2 radii;
#endif
void main() {
gl_Position = gl_ModelViewProjectionMatrix * vec4(pos, 0.0, 1.0);
gl_PointSize = size;
// calculate concentric radii in pixels
float radius = 0.5 * size;
// start at the outside and progress toward the center
radii[0] = radius;
radii[1] = radius - 1.0;
// convert to PointCoord units
radii /= size;
}

View File

@@ -0,0 +1,34 @@
uniform vec4 color;
uniform vec4 outlineColor;
#if __VERSION__ == 120
varying vec4 radii;
#define fragColor gl_FragColor
#else
in vec4 radii;
out vec4 fragColor;
#endif
void main() {
float dist = length(gl_PointCoord - vec2(0.5));
// transparent outside of point
// --- 0 ---
// smooth transition
// --- 1 ---
// pure outline color
// --- 2 ---
// smooth transition
// --- 3 ---
// pure point color
// ...
// dist = 0 at center of point
float midStroke = 0.5 * (radii[1] + radii[2]);
if (dist > midStroke)
fragColor = mix(outlineColor, vec4(0.0), smoothstep(radii[1], radii[0], dist));
else
fragColor = mix(color, outlineColor, smoothstep(radii[3], radii[2], dist));
}

View File

@@ -0,0 +1,24 @@
uniform vec4 color;
#if __VERSION__ == 120
varying vec2 radii;
#define fragColor gl_FragColor
#else
in vec2 radii;
out vec4 fragColor;
#endif
void main() {
float dist = length(gl_PointCoord - vec2(0.5));
// transparent outside of point
// --- 0 ---
// smooth transition
// --- 1 ---
// pure point color
// ...
// dist = 0 at center of point
fragColor = mix(color, vec4(0.0), smoothstep(radii[1], radii[0], dist));
}