There seems to be quite a lot about shader programming, some bullshit about openGL, vulcan, or whatever. I ain't got time for that shit, I just wanna make pretty pictures with code. From this video https://www.youtube.com/watch?v=f4s1h2YETNY
and the first few sections of The Book of Shaders it looks like the kind of shader I can run on fragcoord.xyz is such that the code in main() runs in parallel for every pixel on the canvas. So the code runs in parallel, which means that every pixel is blind to what each other is doing, and also every frame is independent, so we don't have access to data computed previously.
But we do have access to uniforms, which is immutable data available to all threads, we can use
wait, that's actually the names for how they are made available in Shadertoy, but they are otherwise the same on fragCoord.
It's convention to normalize the coordinates between [0, 1] because it's cool or something idk. The signature of main in fragCoord is
and we have access to the globals in vec2 gl_fragCoord and out vec4 fragColor. The first one being the coordinates of the current pixel on the canvas, and the second a vector that when set will set the color of the current pixel. It's a vec4 because some random bullshit or whatever, I read the fourth parameter actually doesn't matter.
Anyway, we can normalize the coordinates if we divide gl_fragCoord.xy by u_resolution. The first expression creates a new vector, it's called (let me look it up) swizziling, and it's syntax sugar for vec2(gl_fragCoord.x, gl_fragCoord.y).
Anyway, if the canvas is not a square, we'd end up with a stretched canvas, imagine a 100 by 10 canvas. Where would we want the middle of the bottom right quadrant to be? we can put it where it fucking is, but if we want to draw a circle wouldn't it be better to have it be a circle, rather than a squished circle? in that case we would want to scale the x and y axes by the same factor. What the fuck am I talking about? I don't fucking know, but the gist of it is just that; scale both axes by the same factor so we don't end up with a stretched canvas. It's convention to scale in such a way that the shortest axis is in [0, 1]. In that case the longest will be in [0, > 1]. Some people like to normalize the shortest axis to [-1, 1]. I'll do that as well. There was a quick formula for normalizing the coordinates in one line.
If c is the coordinate of the shortest axis, r is the resolution, then we want
but r is actually the shortest axis. Since gl_fragCoord and u_resolution are global we can do
oh, yeah, I think I have to swizzle them out
Let's see if I can remember how to draw a circle.
In a shader (at least in a 2D one) we use masks for drawing, which is like a stencil, so we give the pixels we want to paint a value > 1, and those we don't a value <= 0. For some reason we use signed distance fields, which is just a function that gives you the signed distance to a shape.
If we have a circle, how do we compute its sdf? If the circle is at (x, y) I guess we can start with
then what? we can get the length of a vector with lenght(vector). Imagine we want an sdf of a circle at the origin
exactly what is in https://iquilezles.org/articles/distfunctions2d/
what if I want the circle to be somewhere else? I want to know the length of a vector, and check if it's longer or shorter than radius. Isn't that just uv - center? it fucking is. I hate being retarded. So we have
To make a circle we can do
but apparently conditionals are evil in shader code, so we shold use step() instead.
for d < r, step(r, d) == 0.0, and for d > r, step(r, d) == 1.0; it gives us the inverse of our stencil, so we have to invert it.
Then we can use it like this
mix() is like the color ramp in blender with the third parameter being the weight.
It works, but the edges look pixelated. What people often do is use smoothstep(), which works like step(), but smoothly interpolates between the edges.
I think it went like this
I don't quite get how it works, but it was something about fwidth() telling you how much change there was between neighboring pixels, which can vary depending on screen size, so we can't just use a constant. We could just do aa = 0.01, and it looks like it works on my screen, but on larger screens the change in pixels would be larger, so the edge would look more blurry. So we smoothly interpolate between radius - aa and radius + aa.
We can also use the fract() function to fracture the space. It actually just returns the fractional part of a number, but that's actually good because that way we get local [0, 1] ranges for every number.
and there's way more, but I think I'm starting to understand shaders.