Skip to content

Instantly share code, notes, and snippets.

@JBlackCat
Last active March 30, 2017 18:07
Show Gist options
  • Save JBlackCat/2f9e4604e9ccd9ee7853ef91fda19c30 to your computer and use it in GitHub Desktop.
Save JBlackCat/2f9e4604e9ccd9ee7853ef91fda19c30 to your computer and use it in GitHub Desktop.
Fragment Shader start template from https://www.shadertoy.com/view/Md23DV
//uniform vec2 resolution;
#define PI 3.14159265359
//Anti-aliased coordinate system grid
float coordinateGrid(vec2 coords) {
//These colors are unused and irrelevant in current implementation.
vec3 axesColor = vec3(0.0, 0.0, 1.0);
float axesThickness = 0.015;
vec3 gridColor = vec3(0.5);
float gridThickness = 0.008;
float retrn = 0.0; //
//Draw grid lines
const float tickWidth = 0.1;
for(float i=-2.0; i<2.0; i+=tickWidth) {
// "i" is the line coordinate.
retrn += 1.0 - smoothstep(0.0, gridThickness, abs(coords.x - i));
retrn += 1.0 - smoothstep(0.0, gridThickness, abs(coords.y - i));
}
//Draw the axes
retrn += 1.0 - smoothstep(0.001, axesThickness, abs(coords.x));
retrn += 1.0 - smoothstep(0.001, axesThickness, abs(coords.y));
return retrn;
}
//returns 1.0 if inside circle
float circle(vec2 coords, vec2 center, float radius) {
float antiAliasTolerance = 0.005;
return 1.0 - smoothstep(radius - antiAliasTolerance, radius + antiAliasTolerance, length(coords - center));
}
void main() {
vec2 defaultCoords = vec2(gl_FragCoord.xy/resolution.xy);
vec2 viewportCenterCoords = 2.0 * vec2(gl_FragCoord.xy - resolution.xy/2.0) / resolution.y;
vec3 colorOut;
// --------------------------------------
// ------------- Start ------------------
// Fancy Code Here
// ------------- End ------------------
// --------------------------------------
vec3 gridColor = vec3(0.0, 0.634, 0.876);
colorOut = mix(colorOut, gridColor, coordinateGrid(viewportCenterCoords));
vec3 pixel = colorOut;
gl_FragColor = vec4(pixel, 1.0);
}
@JBlackCat
Copy link
Author

TODO: clean up and needs some polishing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment