Minecraft shaders there’s a lot of them but there’s one key feature that separates most of them from the vanilla Minecraft lighting shadows in most of the fancy shaders the blocks will cast Shadows based on the position of the sun while in normal Minecraft you would just get Shadows when blocks are directly above other blocks newer versions of the game will blur the Shadows to make them more realistic but if you look at the Alpha version of the game it’s very very obvious what’s going on here the interesting thing is that making realistic Shadows is easier than you’d think even those of you without programming experience can easily understand the concept and those of you with programming experience may end up wanting to try to code it yourself since it’s quite fun long story short I’m doing that thing you’re not supposed to do as a game developer where you make an online multiplayer open world action RPG Sandbox game with a custom game engine made from scratch in python as a solo developer will I finish it who knows but I already have a working networking framework and my rendering engine is what led to this video to start with I set up the world geometry in a similar way to Minecraft the world is split into chunks and I combine all the blocks into a single chunk to remove hidden faces and create a Vertex array in modern GL which the GPU sees as a single object think of it like a single model that you’re putting into the game the world generation will place chunks one at a time and use 2D pear and noise to look up the height level for each stack of blocks afterwards we just put some trees down on top and add a blue plane for the water which creates a quite decent looking scene now for the focus of the video Shadows the core idea for the lighting technique I’ll use which is called Shadow mapping is to render the world from the perspective of the light source the geometry that the light can see must be in light and everything else must be in Shadow the idea is fundamentally simple but the implementation takes a little bit more thought since I’m using python in modern GL I have to do all the top level rendering calls in Python while the GPU does most of the work through opengl the way I tell the GPU what to do is using the opengl shading language also known as glsl so let’s start with talking about what a Shader actually is fundamentally they are programs that you run on your GPU to write them when using openg G like I mentioned you use glsl which kind of looks like C with a bunch of keywords GPU programs will typically take in simple inputs and create simple outputs but there will be thousands or even millions of instances running simultaneously there are two main types of shaders that most projects will use first there are vertex shaders which in our case take in individual points the vertices of geometry in 3D space and allow you to Output a new Point these are used to move objects around place things from the perspective of an arbitrary camera Etc the second type of Shader which this video will also focus on is the fragment Shader for each triangle you created through the geometry that just passed through the vertex Shader all the fragments in our case pixels that the triangle would contain are run through the fragment Shader to determine an output color that is you get information about a pixel you want to render and use whatever algorithm you’d like to select a color the geometry information that you start with is the interpolation between points on the triangle being rendered as a result you can know where exactly in the world the geometry that your pixel represents is when you run the vertex and fragment shaders to render a scene the output gets saved as a two-dimensional image to something called a frame buffer it’s easiest in this case to just think of frame buffers as images anyways when this rendering happens openg GL will keep track of which geometry is in front by saving the depth of each Pixel It renders if you have one triangle in front of another the triangle in the back will not be visible since the depths are tracked during rendering the side effect of this is that we can access this depth image created by the depth of each pixel this is called the depth buffer and it’s the key to generating shadows as I mentioned you can use vertex shaders to do things like render a scene from the perspective of a camera placed somewhere in the world this camera placement is mathematically represented as a matrix if you don’t know linear algebra just think of it as a bunch of numbers that can move rotate and scale or points in 3D space they can do a lot more as well but the transforms are the important part here so now we can use our vertex Shader to render the scene from the perspective of our light source and get an image where each pixel has an Associated depth if you render the result it actually looks super cool I’m kind of tempted to make a game around this gray stale visual style but I’m already juggling three projects at once I don’t need another one the most important step is next we must render the scene from The Player’s perspective and figure out whether the pixel the player sees is the same one that the light sees the trick here is to have our main vertex Shader generate two points instead of one first it needs to generate the points needed to render the geometry from the perspective of the player but for the second we make the vertex Shader apply the lights Matrix to get the point’s position from the perspective of the light this means that once we get to the fragment Shader we now have the position and therefore the depth of each pixel from the perspective of the light that we rendered earlier on top of the information to render the pixel from the perspective of the player all that’s left is to check whether the light depth of the pixel is greater than what we had saved in our rendering from the lights perspective remember that the texture we got at the end only contains the depth for each pixel closest to the camera so any pixel that the player sees with a calculated greater light depth from the perspective of the light source must be in Shadow with that comparison we can just darken the pixel if we calculate it to be in Shadow and we have functioning Shadows although you can see there’s a lot of artifacts here the fix for this is simple since these are just coming from imprecision we just offset the shadow depth comparison with a bias value to account for the imprecision and poof it’s clean now the really cool thing here is that the light transform is arbitrary and can be dynamically changed to move the white source and consequently the Shadows as a result we get some beautiful lighting adding more postprocessing could make things look even nicer similar to many of the real Minecraft Shader mods but that could be a whole extra video all that said our shadows are a bit Jagged you can see the pixels from the lights perspective along the edges this is fortunately very easy to fix since you can just average out the shadow strength from sampling multiple points around the pixel in question now the Shadows are nice and smooth this technique is called percentage closer filtering or PCF for short there are some super interesting tricks used in computer Graphics but Shadow mapping has to be one of my favorites the fundamentals needed to approach some of these Concepts can be tricky to grasp but this video sponsor brilliant is a great way to learn those fundamentals brilliant is where you can learn by doing with thousands of interactive lessons in math data analysis programming and AI it’s designed to be uniquely effective by building from the ground up and allowing you to play with the concepts that you’re learning one of the found ational Concepts and computer Graphics is the humble Vector it can be used to represent points in space movement Direction and a load of other things brilliant has a great course on vectors where you can Master the basic Vector operations work on your multi-dimensional problem solving and even learn about their applications and games as you’ve seen in this video to try everything brilliant has to offer for free for 30 days visit brilliant.org thee fluffy potato or use the link in the description you’ll also get 20% off an annual subscription while this video is about Shadow mapping I have a lot more on this project coming I’ll be polishing up my code to essentially create my own engine then I’ll use the engine to start working on yet another game and doing the just add multiplayer part I have some super cool stuff in mind but yanuk and pix carts are my main priorities for now anyways thanks for watching [Music] Video Information
This video, titled ‘Minecraft Shaders, but it’s actually just Python’, was uploaded by DaFluffyPotato on 2024-05-09 00:51:27. It has garnered 22185 views and 1428 likes. The duration of the video is 00:08:26 or 506 seconds.
Check out https://brilliant.org/DaFluffyPotato/ to start your free 30 day trial of Brilliant and receive 20% off a premium subscription.
The most important feature in Minecraft shaders are the dynamic shadows. While I was working on a game engine for a future project of mine, I decided to take a quick stop to create Minecraft along with some fancy shadowmapping shaders using Python, Pygame, and ModernGL.
Wishlist Yawnoc: https://store.steampowered.com/app/2824730/Yawnoc
Learning resources for shadowmapping: https://learnopengl.com/Advanced-Lighting/Shadows/Shadow-Mapping
My Discord: https://discord.gg/fluffland-554137097229959188
Support Me (and get source code for the project in this video): https://www.patreon.com/dafluffypotato https://www.youtube.com/@DaFluffyPotato/join
My Website (links to games): https://dafluffypotato.com
Special thanks to my Potato Tier Patrons: Agent Effe Damion Eivind Teig f0ursqu4r3 Levi Jeske Rethanon Sam Cork Szabolcs Dombi tbf135
Timestamps: 0:00 – shadows are key 0:50 – why 1:08 – world generation 1:42 – a simple concept 2:19 – what’s a shader? 3:52 – depth buffers 4:12 – implementation 6:05 – polishing the shadows 7:03 – final thoughts
#gamedev #pygame #python