After a good two weeks welcome back to the series of how minecraft works this video will go over creating procedural meshes based off of the data generated in the last video in the series before we start coding let’s understand how this works first so in the last video we substantiated that the Block id of 0 is an error block knowing this depending on the block data around air blocks we’ll create faces facing towards the air blocks therefore creating a hull of the chunk which is many times more performant than instantiating a bunch of cubes to represent the block data So now without further ado let’s code the mesh generation of chunks in minecraft so first in the scripts folder create a new c sharp script called chunk mesh creator once done open up visual studio now in the chunk mesh creator script remove the model behavior inheritance and remove the start and update method Next up add in this dictionary the vector 3 is used to store the direction in which we will check for air blocks and the face data type is something we will make that will store arrays for the different faces of a cube next up let’s make the face data class Which will store the different face data for the cubes for more information on mesh generation there will be a link in the description that goes more in depth on the matter when you are finished with the face data class there will be a text doc in the description that contains all the Variables that store the different face vertices offsets indices and uv order which will be for another video all you got to do is copy and paste this into your chunk mesh creator class and you now have lines of code that have a history of sleepless nights since and create an empty constructor Now with that complete we are now going to populate the cube faces dictionary with the face data we just pasted so in order to do this first initialize the cube faces dictionary in your constructor and then loop over the check directions array that you pasted in this Loop pull a yandere dev and do this this large chunk of code populates the cube faces dictionary with face coordinates mesh triangles and uv vector order based on the direction it is looping over with that complete create a new function called create mesh from data which takes a 3d integer matrix for The arguments now in this function create two lists one that stores the vertices and one that stores the triangles for our mesh after that create a for loop that will iterate over each point in a 3d array in order to get the specified dimension of Each chunk we can make the chunk size in the world generator a public static read-only variable now inside of this for loop make a vector3 variable that stores the block position the loops are going over and then make another for loop that iterates over each check direction Now in this for loop create another vector3 variable that is the block position variable plus the direction being looped over now make a try catch statement that will prevent any out of bounds exceptions from occurring and in this try catch statement we’ll do an if statement that Checks a specific point in the data being passed into the function whether or not it is zero otherwise known as an air block also in this if statement check if the block being looped over is an air block itself so you don’t create just a giant rectangle with hundreds of thousands of vertices Now in this if statement using the check direction currently being looped over use it to get the face data instance from the cube face dictionary now with the face data instance add to the vertices and triangles lists for the vertices don’t forget the offset using coordinates from the 3d loop we made Now with that complete copy this section of code into the catch statement so the chunk is completely drawn this will be changed in the future when generating whole grids of chunks now for the last thing that needs to be done we need to make this function Return a mesh to do this at the top of the create mesh function add in a new temporary mesh variable and initialize it as a new mesh object now at the very bottom of the function set the vertices and triangles of the mesh variable to be the vertices and Indices list we made in this function and after that simply return the mesh as the last line now the mesh generation is complete for now i save for now because we still have to apply a texture on the mesh using uvs but that’ll be for another video Now back in the world generator class from the first video in the series go to the part where the generation of some temporary chunk data finishes and add in a line that creates a new game object with the mesh render and mesh filter preloaded onto it Now get the mesh filter of the new temp chunk and set the mesh to be the create mesh from data in the chunk creator class use the temp data created in the last video as a data argument for the function when that line is finished go back into Unity and if you hit play you should see an untextured minecraft chunk well that is all for this video we cover procedural mesh generation and you got to use the code that took me a ridiculously long amount of time to figure out so leave a like and subscribe And with that said i hope to see you all in the next video goodbye Video Information
This video, titled ‘How Minecraft Works | Chunk Mesh Creation [C#] [Unity3D]’, was uploaded by Rytech on 2022-01-13 12:51:25. It has garnered 6419 views and 164 likes. The duration of the video is 00:05:47 or 347 seconds.
In this video, I go over how I recreated Minecraft’s chunk mesh generation. This series of ‘How Minecraft Works’ will go over how I recreated many features of Minecraft on my own, and the code behind it so you can learn from the process.
More Info on Meshes ► https://docs.unity3d.com/Manual/GeneratingMeshGeometryProcedurally.html
Cube Face Data ► https://www.mediafire.com/file/bddxncr8i78x706/Minecraft_FaceData.txt/file Join my Discord! ► https://discord.gg/jrRJgfBz2y
-= Music =-
***************************************** Music: Dopamine Artist : DZGRIMX link to the video: https://youtu.be/3S6uyNk9HpY channel: https://www.youtube.com/channel/UCBhW… *****************************************