Since we are working with a GPU fragment shader, we are running code on every pixel on screen at once while knowing some information about the pixel we are working on. If you notice the first line of the main()
function, we are already cleaning up the pixel position from the starter code
vec2 uv = (gl_FragCoord.xy / u_resolution.xy) * 2.0 - 1.0;
This gives us the variable uv
which stores coordinates for the pixel as a value int he range where represents the centre of the screen and you moving up and right gives positive values. Since we have access to where the centre of the screen is located in pixel-space, lets see how we colour pixels within specific ranges in pixel-space.
Recall, how do we find the distance between two points in 2D space? Your middle school math teacher will be pleased because it's time for the Pythagorean Theorem!
Rearranging this we can implement this in GLSL with a function, since we might need to use this formula again later.
float distance(vec2 p1, vec2 p2) {
return sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2));
}
Since we now can find the distance between two points, and we know where the centre of the screen is, we can check the distance of a given pixel to the centre of the screen, and if it's within some radius we like, it can be part of our planet.
vec3 col = bg_colour;
if (distance(uv, vec2(0.0)) < 0.7) {
col = vec3(1);
}
I chose 0.7 as the distance cutoff, since that will leave a nice gap between the planet and the border of the screen, if it's too high it will get clipped at the exceeding bounds. Try it yourself.
Anyhow, you should get something that looks like...
Our planet shader works, but it is looking a little bit empty, and lifeless, and only one colour. Hell not even one colour, white and black are shades so it doesn't even get colour. It's a binary planet, a yes no planet, yes theres planet or no there's no planet. We don't want this boring yes no planet, we live on a blue marble for christ's sake, where's our blue fucking marble?
Lets see how do start forming some shapes on our planet.