In the previous video we have created this chunk renderer what we will need to have is a new game object in our hierarchy so right click here and create an empty and let’s call it chunk let’s reset its trials from using those three dots and reset And we are going to drag here our chunk renderer the remaining components has appeared and this is due to the fact that we have used the required component attribute now we now have here the mesh filter mesh renderer and the mesh collider our chunk render And what we will need to have here is also a material so what we are going to do is select the materials from our mesh renderer and we have no materials what we will need to do is select our underscore textures folder and we are going we are going to drag our main Material as the element 0 it was added at the bottom of our inspector we are going to add another material in our mesh renderer drag as a second material the water material this is because we’re using the sub meshes so element 0 is equivalent to the submerged 0 Element 1 to the sub 1 and accordingly those two materials will be assigned to those two sub-meshes and if you explore those materials the main material is opaque render phase is front water material is transparent using their blending mode alpha and is rendering both faces since if we are underwater we Also would like to see the water we will soon be able to visualize our chunk for now let’s drag our chunk as a prefab to our assets we’re going to go back to our underscore scripts folder and we will want to open our chunk script okay now here we have our Previously created method internal static let’s change it maybe to public but it doesn’t really matter for our project public static mesh data get chunk data chunk data as an argument and we are throwing an exception instead we are going to create our mesh data and i have previously mentioned our mesh data Will take a bull flag and it is taking a true parameter so this is the main mesh data and it will generate a water mesh data inside it and then it will stop the recursion now for now we do not have the additional class that we will need to Have to generate our mesh data so we are going to simply return our mesh data this will be the empty mesh data object but later in the course we are going to fill it in so let’s maybe add command fill later and this will be when we have additional block Helper class so we are going to have a couple methods here one of those will be loop through the blocks and we are going to have the action delegate here and the action delegate needs to have using system statement at the top and this is A delegate so that we can pass here a method name this method needs to take three arguments of type int and this will allow us to for example make some changes to each voxel in our chunk now we have passed chunk data as first parameter and action delegate that takes Three integers action to perform and we are going to simply loop for each index 0 to blocks.length so for each voxel for each block type and we are going to generate var position since we need to generate a position from the index of our blocks i would not have This method so right click quick actions and generate this method and after we receive this position so this will be instead of object a vector3 in and since we have created this class using the viewer studio it didn’t import our unity engine libraries so right click here quick Actions and we are going to say using in the engine and we should be good to go so this method will convert the index of our blocks array into a position with the x y and z coordinates and we are going to simply call this action On this position we are going to use this method when we want to access the mesh data from each block each voxel and this will be implemented soon now let’s focus on creating this get position from index method now it will take a chunk data as a parameter And an integer value and what we are going to do here is we are going to calculate the x y and z position using those formulas i have taken them from stack overflow convert 1d array index to a 3d array index and the answer here is this for those formulas for z Y and x coordinates and simply we are going to return the new vector 3 in it with those integer values for x y and z coordinates calculated based on the index of the blocks array great with those two methods done we will need to have a bit more so We will need to be able to check if the index is in a range and actually i believe that this should be called something like axis coordinate so right click and maybe rename it to axis coordinate and this will be the axis coordinate so x y and z In the uh chunk coordinate system okay so if we pass it 15 and the chunk size is 16 it will be good if we pass it 16 it will be out of a range okay and with the in the range height we’re going to rename this to be Y coordinate so we are going to pass the y coordinate and if the y coordinate again this is in chunk coordinate system so it is from zero to the chunk height and it if the value is less than zero or greater than a chunk height we are going to return False else we are going to return true and this will be useful when we are setting the value for a voxel in our chunk and if the index or another if the coordinate is outside of the bounds of our chunk we will know that we will need to access a neighboring chunk Instead of this one so now having those two methods we can implement a set block so i will paste this method and this will be our public static void set block again we will need to pass the chunk data because this is a static method we’re going to pass the locate local Position of the block that we want to set and the block type that we want to set this block to we’re going to use our in range and in range hide to check if our local position is indeed this chunk if it is we will need to have a method That gets the index from a position so right click quick action and let’s generate this method and if we have this position if it is in range we are going to get this position from our index or other index from this position and we are going to set our chunk data.blocks With the index to be the new type of block else we will implement a new code but only when we actually have a world that contains the references to the other chunks and this will be at the end of this section for now we are going to throw an Exception so that we know that we do not have this code yet okay so let’s fill in the get index from position it should be created as a private static int method and it will take the chunk data and x y and z coordinate all we need to return here Is a x plus chunk size a times y plus chunk size times chunk height times z and this is just the reversed calculation of the previous calculations for the get position from index again if you go to the stack overflow to the convert a 1d array index to a 3d Array index at the bottom you can see that there is this calculation that i have used okay we are back in our code sorry about all this code but without the it we are not going to be able to create our voxel engine so you need to bear with me a bit more Before we can actually test our code beside those methods we will need to have one more so let’s create it above our get chunk mesh data and this one will be public static vector 3 in get block in chunk coordinates as i have previously said we are going to distinguish between the world Coordinates and the chunk coordinates so if we receive the position in the world coordinates we’re going to get the position.x minus chunkdata.worldposition.x so if word position is 10 and our chunk is at 0 0 0 we are going to get 10. if this is 30 and the chunk position is on x Is 16 we are going to subtract 30 minus 16 and it will give us 14 which will be the position inside our chunk so basically we are going to convert the world position into the chunk coordinates so that we can access the correct index in our blocks Array and again we are going to return a new vector3 int so that we want to have integer values we are almost done with the script we had this set block method so we can set the block value now we will want to have a get block from chunk coordinates method Now this will access the concrete block type from our blocks array this will be public static method that returns us a block type and as the parameters it will take the chunk data as this is a static method so we need to take it always And it will take x y and z parameters similarly to what we had in our chunk data but this took vector 3 int so if we want to access this lock at those x y and z coordinates we’re going to first check if those coordinates are in Range now this takes the values of the chunk coordinate so we cannot really pass here the world core that we need to convert them into this is why we needed this get block in chunk coordinates method to convert our vector3 position into the chunk coordinate position we’ll simply perform the checks if the Block or the voxel is in range of our chunk if it is we calculate simply the index equals get index from position x y and z and passing the chunk data and we are going to return the chunk data.block with the index and again if this is not the correct Chunk we are going to need to access the world script which we did not have so again we are throwing an exception for now now to make our live a bit easier we are going to create an overload of the same method so we can copy the same name But instead of passing x y and z separately we will pass vector 3 int and we are going to simply call this return the overloaded method by passing in chunk data and the chunk coordinates the vector3 in parameter with x y and z and this will work the same but Now we will not have to convert our vector3 into separate parameters okay again we are going to revisit this script when we have more classes to work with next three videos we are going to be finishing the scripts and setting up textures data so that we can access the created data Depending on our block type and in the fourth video from now we are going to be finally able to create our world script that will allow us to generate our world and to visualize it for now in the next video we are going to create a blog data so that will contain The information that will describe how to paint correct textures depending on the block type see you in the next video Video Information
This video, titled ‘Create MINECRAFT in Unity – S1 – P7 Chunk class’, was uploaded by Sunny Valley Studio on 2021-05-25 12:55:19. It has garnered 7007 views and 161 likes. The duration of the video is 00:12:45 or 765 seconds.
In the Section 1 of this procedural voxel world generation Unity 2020 tutorial we will start by creating a codebase to generate the terrain using voxels. We will need to write a bit of code so instead of retyping the code copy it from github or use the end project from google drive (links below).
Section 1 – Voxel Terrain Engine https://youtube.com/playlist?list=PLcRSafycjWFdYej0h_9sMD6rEUCpa7hDH
Section 2 – https://www.youtube.com/playlist?list=PLcRSafycjWFesScBq3JgHMNd9Tidvk9hE
Section 3 – https://www.youtube.com/playlist?list=PLcRSafycjWFceHTT-m5wU51oVlJySCJbr
Learn more from about Unity Game Dev in my video courses at: https://courses.sunnyvalleystudio.com/
Starter project files: https://github.com/SunnyValleyStudio/Unity-2020-Voxel-World-Tutorial-Section-1-starter-project
Code files for everyone: https://www.subscribepage.com/voxel_world_tutorial_section_2
Project / code files for patreons: https://www.patreon.com/posts/voxel-world-end-53323870?utm_medium=clipboard_copy&utm_source=copy_to_clipboard&utm_campaign=postshare
Project / code files for YT members: Link in members only community post: https://www.youtube.com/c/SunnyValleyStudio/community
You can support me through Patreon: https://www.patreon.com/sunnyvalleystudio
I have used this tutorial by AlexStv as a reference to create this series: https://forum.unity.com/threads/tutorial-procedural-meshes-and-voxel-terrain-c.198651/
Join the discord if you have any questions! https://discord.gg/RQEtYHz