Init
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
#version 330
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
uniform float time;
|
||||
uniform vec2 resolution;
|
||||
uniform vec3 baseColor;
|
||||
|
||||
|
||||
|
||||
//A simple pseudo-random function
|
||||
float random(vec2 st){
|
||||
return fract(sin(dot(st.xy, vec2(12.9898, 78.233))) * 43758.5853123);
|
||||
}
|
||||
|
||||
void main(){
|
||||
vec2 uv = gl_FragCoord.xy / resolution.xy;
|
||||
|
||||
//Create some noise
|
||||
float noise = random(uv + time * 0.5);
|
||||
|
||||
//Adjust these values to control the intensity and color of the grain
|
||||
float grainIntensity = 0.01;
|
||||
|
||||
//Mix the base color with the noise
|
||||
vec3 color = baseColor + vec3(noise * grainIntensity);
|
||||
|
||||
FragColor = vec4(color, 1.0);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
#version 330
|
||||
|
||||
layout(location = 0) in vec2 v_position;
|
||||
|
||||
void main(void){
|
||||
gl_Position = vec4(v_position, 0.0f, 1.0f);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#version 330
|
||||
|
||||
in vec2 f_position;
|
||||
in vec4 f_color;
|
||||
in float f_thickness;
|
||||
|
||||
|
||||
out vec4 fragColor;
|
||||
|
||||
void main(){
|
||||
float radius = 1.0;
|
||||
|
||||
//distance to circle
|
||||
vec2 w = f_position;
|
||||
float dw = length(w);
|
||||
float d = abs(dw - radius);
|
||||
|
||||
fragColor = vec4(f_color.rgb, smoothstep(f_thickness, 0.0, d));
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#version 330
|
||||
|
||||
uniform mat4 projectionMatrix;
|
||||
uniform float pixelScale;
|
||||
|
||||
layout(location = 0) in vec2 v_localPosition;
|
||||
layout(location = 1) in vec2 v_instancePosition;
|
||||
layout(location = 2) in float v_instanceRadius;
|
||||
layout(location = 3) in vec4 v_instanceColor;
|
||||
|
||||
|
||||
out vec2 f_position;
|
||||
out vec4 f_color;
|
||||
out float f_thickness;
|
||||
|
||||
|
||||
void main(){
|
||||
f_position = v_localPosition;
|
||||
f_color = v_instanceColor;
|
||||
float radius = v_instanceRadius;
|
||||
|
||||
//resolution.y = pixelScale * radius
|
||||
|
||||
f_thickness = 3.0f / (pixelScale * radius);
|
||||
|
||||
vec2 p = vec2(radius * v_localPosition.x, radius * v_localPosition.y) + v_instancePosition;
|
||||
|
||||
gl_Position = projectionMatrix * vec4(p, 0.0f, 1.0f);
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
// SPDX-FileCopyrightText: 2024 Erin Catto
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#version 330
|
||||
|
||||
in vec2 f_position;
|
||||
in vec4 f_color;
|
||||
in float f_length;
|
||||
in float f_thickness;
|
||||
|
||||
out vec4 color;
|
||||
|
||||
// Thanks to baz and kolyan3040 for help on this shader
|
||||
// todo this can be optimized a bit, keeping some terms for clarity
|
||||
|
||||
// https://en.wikipedia.org/wiki/Alpha_compositing
|
||||
vec4 blend_colors(vec4 front,vec4 back)
|
||||
{
|
||||
vec3 cSrc = front.rgb;
|
||||
float alphaSrc = front.a;
|
||||
vec3 cDst = back.rgb;
|
||||
float alphaDst = back.a;
|
||||
|
||||
vec3 cOut = cSrc * alphaSrc + cDst * alphaDst * (1.0 - alphaSrc);
|
||||
float alphaOut = alphaSrc + alphaDst * (1.0 - alphaSrc);
|
||||
|
||||
// remove alpha from rgb
|
||||
cOut = cOut / alphaOut;
|
||||
|
||||
return vec4(cOut, alphaOut);
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
// radius in unit quad
|
||||
float radius = 0.5 * (2.0 - f_length);
|
||||
|
||||
vec4 borderColor = f_color;
|
||||
vec4 fillColor = 0.6f * borderColor;
|
||||
|
||||
vec2 v1 = vec2(-0.5 * f_length, 0);
|
||||
vec2 v2 = vec2(0.5 * f_length, 0);
|
||||
|
||||
// distance to line segment
|
||||
vec2 e = v2 - v1;
|
||||
vec2 w = f_position - v1;
|
||||
float we = dot(w, e);
|
||||
vec2 b = w - e * clamp(we / dot(e, e), 0.0, 1.0);
|
||||
float dw = length(b);
|
||||
|
||||
// SDF union of capsule and line segment
|
||||
float d = min(dw, abs(dw - radius));
|
||||
|
||||
// roll the fill alpha down at the border
|
||||
vec4 back = vec4(fillColor.rgb, fillColor.a * smoothstep(radius + f_thickness, radius, dw));
|
||||
|
||||
// roll the border alpha down from 1 to 0 across the border thickness
|
||||
vec4 front = vec4(borderColor.rgb, smoothstep(f_thickness, 0.0f, d));
|
||||
|
||||
color = blend_colors(front, back);
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
// SPDX-FileCopyrightText: 2024 Erin Catto
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#version 330
|
||||
|
||||
uniform mat4 projectionMatrix;
|
||||
uniform float pixelScale;
|
||||
|
||||
layout(location=0) in vec2 v_localPosition;
|
||||
layout(location=1) in vec4 v_instanceTransform;
|
||||
layout(location=2) in float v_instanceRadius;
|
||||
layout(location=3) in float v_instanceLength;
|
||||
layout(location=4) in vec4 v_instanceColor;
|
||||
|
||||
out vec2 f_position;
|
||||
out vec4 f_color;
|
||||
out float f_length;
|
||||
out float f_thickness;
|
||||
|
||||
void main()
|
||||
{
|
||||
f_position = v_localPosition;
|
||||
f_color = v_instanceColor;
|
||||
|
||||
float radius = v_instanceRadius;
|
||||
float length = v_instanceLength;
|
||||
|
||||
// scale quad large enough to hold capsule
|
||||
float scale = radius + 0.5 * length;
|
||||
|
||||
// quad range of [-1, 1] implies normalize radius and length
|
||||
f_length = length / scale;
|
||||
|
||||
// resolution.y = pixelScale * scale
|
||||
f_thickness = 3.0f / (pixelScale * scale);
|
||||
|
||||
float x = v_instanceTransform.x;
|
||||
float y = v_instanceTransform.y;
|
||||
float c = v_instanceTransform.z;
|
||||
float s = v_instanceTransform.w;
|
||||
vec2 p = vec2(scale * v_localPosition.x, scale * v_localPosition.y);
|
||||
p = vec2((c * p.x - s * p.y) + x, (s * p.x + c * p.y) + y);
|
||||
gl_Position = projectionMatrix * vec4(p, 0.0, 1.0);
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
// SPDX-FileCopyrightText: 2024 Erin Catto
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#version 330
|
||||
|
||||
in vec2 f_position;
|
||||
in vec4 f_color;
|
||||
in float f_thickness;
|
||||
|
||||
out vec4 fragColor;
|
||||
|
||||
// https://en.wikipedia.org/wiki/Alpha_compositing
|
||||
vec4 blend_colors(vec4 front, vec4 back)
|
||||
{
|
||||
vec3 cSrc = front.rgb;
|
||||
float alphaSrc = front.a;
|
||||
vec3 cDst = back.rgb;
|
||||
float alphaDst = back.a;
|
||||
|
||||
vec3 cOut = cSrc * alphaSrc + cDst * alphaDst * (1.0 - alphaSrc);
|
||||
float alphaOut = alphaSrc + alphaDst * (1.0 - alphaSrc);
|
||||
cOut = cOut / alphaOut;
|
||||
|
||||
return vec4(cOut, alphaOut);
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
// radius in unit quad
|
||||
float radius = 1.0;
|
||||
|
||||
// distance to axis line segment
|
||||
vec2 e = vec2(radius, 0);
|
||||
vec2 w = f_position;
|
||||
float we = dot(w, e);
|
||||
vec2 b = w - e * clamp(we / dot(e, e), 0.0, 1.0);
|
||||
float da = length(b);
|
||||
|
||||
// distance to circle
|
||||
float dw = length(w);
|
||||
float dc = abs(dw - radius);
|
||||
|
||||
// union of circle and axis
|
||||
float d = min(da, dc);
|
||||
|
||||
vec4 borderColor = f_color;
|
||||
vec4 fillColor = 0.6f * borderColor;
|
||||
|
||||
// roll the fill alpha down at the border
|
||||
vec4 back = vec4(fillColor.rgb, fillColor.a * smoothstep(radius + f_thickness, radius, dw));
|
||||
|
||||
// roll the border alpha down from 1 to 0 across the border thickness
|
||||
vec4 front = vec4(borderColor.rgb, smoothstep(f_thickness, 0.0f, d));
|
||||
|
||||
fragColor = blend_colors(front, back);
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
// SPDX-FileCopyrightText: 2024 Erin Catto
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#version 330
|
||||
|
||||
uniform mat4 projectionMatrix;
|
||||
uniform float pixelScale;
|
||||
|
||||
layout(location = 0) in vec2 v_localPosition;
|
||||
layout(location = 1) in vec4 v_instanceTransform;
|
||||
layout(location = 2) in float v_instanceRadius;
|
||||
layout(location = 3) in vec4 v_instanceColor;
|
||||
|
||||
out vec2 f_position;
|
||||
out vec4 f_color;
|
||||
out float f_thickness;
|
||||
|
||||
void main()
|
||||
{
|
||||
f_position = v_localPosition;
|
||||
f_color = v_instanceColor;
|
||||
float radius = v_instanceRadius;
|
||||
|
||||
// resolution.y = pixelScale * radius
|
||||
f_thickness = 3.0f / (pixelScale * radius);
|
||||
|
||||
float x = v_instanceTransform.x;
|
||||
float y = v_instanceTransform.y;
|
||||
float c = v_instanceTransform.z;
|
||||
float s = v_instanceTransform.w;
|
||||
vec2 p = vec2(radius * v_localPosition.x, radius * v_localPosition.y);
|
||||
p = vec2((c * p.x - s * p.y) + x, (s * p.x + c * p.y) + y);
|
||||
gl_Position = projectionMatrix * vec4(p, 0.0f, 1.0f);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
// SPDX-FileCopyrightText: 2024 Erin Catto
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#version 330
|
||||
|
||||
in vec2 f_position;
|
||||
in vec2 f_points[8];
|
||||
flat in int f_count;
|
||||
in float f_radius;
|
||||
in vec4 f_color;
|
||||
in float f_thickness;
|
||||
|
||||
out vec4 fragColor;
|
||||
|
||||
// https://en.wikipedia.org/wiki/Alpha_compositing
|
||||
vec4 blend_colors(vec4 front, vec4 back)
|
||||
{
|
||||
vec3 cSrc = front.rgb;
|
||||
float alphaSrc = front.a;
|
||||
vec3 cDst = back.rgb;
|
||||
float alphaDst = back.a;
|
||||
|
||||
vec3 cOut = cSrc * alphaSrc + cDst * alphaDst * (1.0 - alphaSrc);
|
||||
float alphaOut = alphaSrc + alphaDst * (1.0 - alphaSrc);
|
||||
|
||||
// remove alpha from rgb
|
||||
cOut = cOut / alphaOut;
|
||||
|
||||
return vec4(cOut, alphaOut);
|
||||
}
|
||||
|
||||
float cross2d(in vec2 v1, in vec2 v2)
|
||||
{
|
||||
return v1.x * v2.y - v1.y * v2.x;
|
||||
}
|
||||
|
||||
// Signed distance function for convex polygon
|
||||
float sdConvexPolygon(in vec2 p, in vec2[8] v, in int count)
|
||||
{
|
||||
// Initial squared distance
|
||||
float d = dot(p - v[0], p - v[0]);
|
||||
|
||||
// Consider query point inside to start
|
||||
float side = -1.0;
|
||||
int j = count - 1;
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
// Distance to a polygon edge
|
||||
vec2 e = v[i] - v[j];
|
||||
vec2 w = p - v[j];
|
||||
float we = dot(w, e);
|
||||
vec2 b = w - e * clamp(we / dot(e, e), 0.0, 1.0);
|
||||
float bb = dot(b, b);
|
||||
|
||||
// Track smallest distance
|
||||
if (bb < d)
|
||||
{
|
||||
d = bb;
|
||||
}
|
||||
|
||||
// If the query point is outside any edge then it is outside the entire polygon.
|
||||
// This depends on the CCW winding order of points.
|
||||
float s = cross2d(w, e);
|
||||
if (s >= 0.0)
|
||||
{
|
||||
side = 1.0;
|
||||
}
|
||||
|
||||
j = i;
|
||||
}
|
||||
|
||||
return side * sqrt(d);
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
vec4 borderColor = f_color;
|
||||
vec4 fillColor = 0.6f * borderColor;
|
||||
|
||||
float dw = sdConvexPolygon(f_position, f_points, f_count);
|
||||
float d = abs(dw - f_radius);
|
||||
|
||||
// roll the fill alpha down at the border
|
||||
vec4 back = vec4(fillColor.rgb, fillColor.a * smoothstep(f_radius + f_thickness, f_radius, dw));
|
||||
|
||||
// roll the border alpha down from 1 to 0 across the border thickness
|
||||
vec4 front = vec4(borderColor.rgb, smoothstep(f_thickness, 0.0f, d));
|
||||
|
||||
fragColor = blend_colors(front, back);
|
||||
|
||||
// todo debugging
|
||||
// float resy = 3.0f / f_thickness;
|
||||
|
||||
// if (resy < 539.9f)
|
||||
// {
|
||||
// fragColor = vec4(1, 0, 0, 1);
|
||||
// }
|
||||
// else if (resy > 540.1f)
|
||||
// {
|
||||
// fragColor = vec4(0, 1, 0, 1);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// fragColor = vec4(0, 0, 1, 1);
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
// SPDX-FileCopyrightText: 2024 Erin Catto
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#version 330
|
||||
|
||||
uniform mat4 projectionMatrix;
|
||||
uniform float pixelScale;
|
||||
|
||||
layout(location = 0) in vec2 v_localPosition;
|
||||
layout(location = 1) in vec4 v_instanceTransform;
|
||||
layout(location = 2) in vec4 v_instancePoints12;
|
||||
layout(location = 3) in vec4 v_instancePoints34;
|
||||
layout(location = 4) in vec4 v_instancePoints56;
|
||||
layout(location = 5) in vec4 v_instancePoints78;
|
||||
layout(location = 6) in int v_instanceCount;
|
||||
layout(location = 7) in float v_instanceRadius;
|
||||
layout(location = 8) in vec4 v_instanceColor;
|
||||
|
||||
out vec2 f_position;
|
||||
out vec4 f_color;
|
||||
out vec2 f_points[8];
|
||||
flat out int f_count;
|
||||
out float f_radius;
|
||||
out float f_thickness;
|
||||
|
||||
void main()
|
||||
{
|
||||
f_position = v_localPosition;
|
||||
f_color = v_instanceColor;
|
||||
|
||||
f_radius = v_instanceRadius;
|
||||
f_count = v_instanceCount;
|
||||
|
||||
f_points[0] = v_instancePoints12.xy;
|
||||
f_points[1] = v_instancePoints12.zw;
|
||||
f_points[2] = v_instancePoints34.xy;
|
||||
f_points[3] = v_instancePoints34.zw;
|
||||
f_points[4] = v_instancePoints56.xy;
|
||||
f_points[5] = v_instancePoints56.zw;
|
||||
f_points[6] = v_instancePoints78.xy;
|
||||
f_points[7] = v_instancePoints78.zw;
|
||||
|
||||
// Compute polygon AABB
|
||||
vec2 lower = f_points[0];
|
||||
vec2 upper = f_points[0];
|
||||
for (int i = 1; i < v_instanceCount; ++i)
|
||||
{
|
||||
lower = min(lower, f_points[i]);
|
||||
upper = max(upper, f_points[i]);
|
||||
}
|
||||
|
||||
vec2 center = 0.5 * (lower + upper);
|
||||
vec2 width = upper - lower;
|
||||
float maxWidth = max(width.x, width.y);
|
||||
|
||||
float scale = f_radius + 0.5 * maxWidth;
|
||||
float invScale = 1.0 / scale;
|
||||
|
||||
// Shift and scale polygon points so they fit in 2x2 quad
|
||||
for (int i = 0; i < f_count; ++i)
|
||||
{
|
||||
f_points[i] = invScale * (f_points[i] - center);
|
||||
}
|
||||
|
||||
// Scale radius as well
|
||||
f_radius = invScale * f_radius;
|
||||
|
||||
// resolution.y = pixelScale * scale
|
||||
f_thickness = 3.0f / (pixelScale * scale);
|
||||
|
||||
// scale up and transform quad to fit polygon
|
||||
float x = v_instanceTransform.x;
|
||||
float y = v_instanceTransform.y;
|
||||
float c = v_instanceTransform.z;
|
||||
float s = v_instanceTransform.w;
|
||||
vec2 p = vec2(scale * v_localPosition.x, scale * v_localPosition.y) + center;
|
||||
p = vec2((c * p.x - s * p.y) + x, (s * p.x + c * p.y) + y);
|
||||
gl_Position = projectionMatrix * vec4(p, 0.0f, 1.0f);
|
||||
}
|
||||
Reference in New Issue
Block a user