This tutorial series will show you how to make infinite procedurally generated voxal Terrain in gdau using C along with a player character that can break and place blocks if you’re feeling impatient the source code for the project is available on my patreon link in the description in this video we’re going to Set up a system for collecting blocks into a chunk which can then be rendered all right so I started with a fresh GTO 4.2 mono project so that’s the version that supports C the only things I’ve done is I’ve added in this block textures folder where I’ve put a few Textures in that I’m going to use for my blocks I’ve also gone into project project settings naming and in here I’ve changed the scene name casing to Pascal case that’s because in C that’s the convention for class names and I want those class names to match the scenes That they’re on so that’s the only reason for that with that out of the way we can start start by creating our first scene we’re going to pick other node because we’re going to want this to be a static body 3D so this is a 3D physics body and this Is going to represent our chunk so if you’re familiar with how something like Minecraft Works instead of making every single individual block it sort of batches them together in chunks of blocks so when it’s loading new terrain it loads an entire chunk instead of you know loading every single individual Block and we’re going to use this to batch together our visuals for the blocks as well as the Collision for the blocks so to implement collisions to implement collisions excuse me we’re going to add a collision shape 2D to our chunk and you can see we have A warning on this we’re going to ignore this for now basically it’s complaining that we haven’t set a shape over here but we’re going to do that in our script as we load the terrain similarly we’re going to add a mesh instance 3D and we’re also going to Leave its mesh empty because that’s something that we’re also going to set in code so I’m go ahead and save this scene going to save it as chunk and now we’re going to add a script to our chunk uh by default the language is going to say GD script just make sure You change that to C so now we create our script open that up and so the first thing we’re going to do is we’re going to add the tool attribute to the top here what this does is it makes everything in this script run while we’re in the editor so we Don’t have to open up and start out our project and play our game to see our changes it’ll just happen in the editor be very useful for testing next we’re going to define a few exports in here this allows us to set values in the editor and we’re just going to get some References to our Collision shape 3D that we just made and also to our mesh instance 3D now we’re going to define a few uh useful sort of constants that we’re going to use for all our chunks these are going to be static instead of actually using the constant Keyword that’s just because uh since it’s a vector 3i struct you can’t use constant so don’t worry about that we’re going to call this dimensions and this is going to define the size size of each chunk and we’re going to set that to 16 by 64 by 16 so that’s 16 Blocks wide you know 64 blocks tall 16 blocks deep next I’m going to copy paste in some values so this vertices array contains the position of each corner of each block essentially so you know you can imagine a cube there are eight corners of a cube and these are all of those Corners I’m just going to fix the formatting and now the order that these are written in is going to matter because what I’m going to copy in next these arrays Define each face of of the cube so there are four corners making up each face each face is a square and These values correspond to the index in this vertices array of You Know Each corner that makes up that face so for example for the top face we can see that it’s referencing vertices number 2 3 7 and six so if we look at this array Here uh index starts at zero so 0 1 2 so it’s these two corners and these two corners those make up the top face now the order of these values is also important uh just because when you’re defining you know a face in a 3D shape depending on if you go clockwise or Counterclockwise around the square that’s going to change uh essentially the direction that it’s facing in so if you get them in the wrong order the cube might be inside out so just make sure you get all these values uh consistent with each other next up we’re going to make a surface Tool and if you Mouse over the definition of surface tool you see that it’s used to construct a mesh by specifying vertex attributes individually and it can be used to construct a mesh from a script so that’s exactly what we’re going to want to do here so that’s what that’s For now we want to define a array of all the blocks in our uh in our chunk you can see that the block type of course doesn’t exist we haven’t defined it yet and these two commas here just show that it is a three-dimensional array so let’s just finish up defining this First so essentially we’re saying we want you know an array of every single block that is in this chunk so that’s why we’re doing dimensions. X Y and Z so now let’s close out of uh our code editor for now we’ll go here back into Good uh we can save our chunk scene over we here go down here to the uh file explorer window thing and do new script so we’re going to write a c script uh for inherits we want it to inherit from resource so this is the base class for Serializable objects this is going to make it so we can have an object that that will be editable in the inspector over here on the on the right side and this is going to represent our block so we can create that if we head back over to our code Editor see that we now have our block class we’re also going to want to add the tool attribute to this one as well and we’re also going to add the global class attribute now what this does is essentially you can see exposes the target class to the Gdo engine this is going to make it so we can actually use it in the inspector uh now for now there’s not really much that we want to do with this so I’m going to do the bare minimum and write a parameterless Constructor so it’s just an empty Constructor uh because otherwise when You use the the global class attribute and do tries to use the class it freaks out apparently if it does not have a Constructor to use so that’s what this is for out of the way we can go back to our chunk script and we can see the blocks array is no longer Complaining next up we’re going to want to Define two really important methods first one is called generate now this is what’s going to fill up this blocks array initially with the generated train after that we want public void update and what this is going to do is It’s going to look at the blocks in this array and it’s going to generate a mesh and collision mesh for our chunk so for both these methods I’m going to start them out with uh a for Loop I’m going to copy paste this into both and this is essentially looping Through every single block position in the chunk and then in this method you know it’s going to set that block and in this method it’s going to make the mesh for that block just for starting off as a bit of a placeholder we’re just going to make a Generic block here with no actual info for it but that’s fine it’s just going to be a placeholder and in our for Loop here we’re going to set blocks XY Z to that basic nothing block so we’re going to come back to this later and actually have this set Them you know to dirt or stone or air but for now uh it’s just a placeholder now here in the update method we are going to want to Define some more methods uh to help out with that the first one it’s going to be private void create block Mesh and it’s going to take in the position of the block that it’s going to create so this way we Loop through every single block and then we can call create block mesh at that position now when we create a block what we’re going to do is individually create Each face that makes up that block so we’re going to make another method called create face mesh this is going to take in an integer array that we’re going to call face and this is going to correspond to one of these six arrays so if we want to create the top Face we’re going to pass in the top array and it’s going to use these indices to look up which vertex it’s going to make for each corner and uh much like the create block mesh method it’s also going to take in the position of the block that it’s Creating now we just need one last method which is going to be called private bu check transparent which just going to take in a block position and the point of this is you know imagine you have a block that’s deep underground it’s completely surrounded by other blocks there’s no point in creating its Mesh since it’s completely hidden so this method here we’re going to use it to check the surrounding blocks and if there is a transparent block so say like an AIR block uh that’s you know on top of the top face then we would create the top face But if there’s an opaque block there then we do not create that face so that’s what this is for for now we’ll just have it return true so it’s just going to generate all our faces it’s just going to be a little placeholder we’ll come back to it in the Future with all our blank methods set up we can start actually implementing the update method so the first thing we’re going to do is surface tool. begin which just as the name would imply tells the surface tool that we’re going to start making a mesh we’re going to start giving it Vertices and it takes an primitive type as a parameter we’re going to set that to triangles so we’re going to make up our mesh by defining every single triangle in the mesh so you see it starts with that then it goes into create block mesh in this method here we’re going to start Creating uh every single face but we’re only going to create the face if the Block in that direction that it’s facing towards is transparent so that we would say if check transparent block position plus Vector 3 i. up so if the block on top of us is Transparent and we do create face mesh top block position so I’m going to copy paste this five more times for our six total pH and then just start changing the values so if we look at the Block underneath us we render the bottom face if that’s transparent the one to the left of Us we render the left face and so on cool so now lastly we’re going to fill out the create face mesh method we’re going to come back to this a few times in the future to kind of fill it out make it a bit more detailed but for now We’re going to keep it pretty basic where first up we’re going to get the four corners so as I said before the face array contains the indices of the vertices array that make up Each corner and then this is the position of each Corner within the Entire chunk which is why we have to add on the Block position to those values next what we’re going to do is Define the vertices that make up each triangle in our Square so you can make a triangle uh you can make a square with two Triangles so we’re going to have AB c as our first triangle and as our second triangle we have a CD and again the order of these values matter uh if you imagine this Square it is that a is in the top left B is in the Bottom left C is in the bottom right and D is in the top right so this triangle ABC is sort of the bottom left triangle and then ACD is sort of the top right triangle and depending on if it’s going clockwise or counterclockwise that’ll change which direction it renders in so Uh the order is very important now with those we can do service tool. add triangle fan which inserts a triangle made of array data into the mesh and we’re going to give it the triangle one array and we’re going to duplicate that and give it the triangle two Array so that’ll make our Square out of two triangles so that finishes the create face mases method so if we go back up to update we can finish this out as well so what we’re going to want to do is to Fair mesh surface tool. commit so what this Does is the surface tool will then commit all the data into an array mesh we’re going to take that mesh that it just created we’re going to tell our mesh instance to use that mesh and then for the Collision shape we’re going to tell it to create a tri mesh Shape out of the mesh so it has to calculate a collision shape to use from this mesh and we’re going to tell it to use that and if I’m not mistaken think now if we go back to here and we rebuild our project maybe close out the chunk scene and reopen It it’s not going to work oh I forgot something very important uh we’re not calling either of these methods so for that we’ll do public override void ready so this is uh the ready method is what’s called as the node is you know entered into the scene And here we’re going to call generate we’re going to call update so now we can go back into gdau and also make sure to click on your chunk and get confused because why isn’t it why isn’t it updating I don’t know why this is happening um but gto’s kind of bugged Out for me where you can see it is not referencing the right script so I’m just going to detach the script that it has going to save the scene going to attach existing script and to reload my chunk script and okay now it’s working so hopefully that doesn’t happen to you uh But it did happen to me now that when I click the chunk node you can see that we can now assign its Collision shape and its mesh instance so we can just drag these two notes over save now if we restart the scene by closing it and opening it again now it Actually works if we zoom out you can see we have this nice chunk of uh blank sort of undefined blocks Video Information
This video, titled ‘Godot 4 C# Tutorial – Minecraft Terrain – Part 1 (Chunks)’, was uploaded by xen-42 on 2023-12-04 18:00:13. It has garnered views and [vid_likes] likes. The duration of the video is or seconds.
Get the source code on my Patreon: https://www.patreon.com/xen42 Join my Discord server idk why not: …