Hello and welcome back to code blaze so i’m back with voxel chunks in unreal engine 5 and last time we completed all of the declaration part so the member variables and the functions now we will implement those functions one by one so by the end of this video we’ll have something like this Where we’ll have voxel meshes being generated as per our previous blueprint code so let’s get started Do we are going to start with by implementing the constructor the first thing we do is add our include statements for the enums file the noise light and the procedural mesh component next i disable the tick since i don’t need it and then i initialize the procedural mesh component Using the create default sub object then i initialize my noise object basically set the frequency so this is something you can play with now actually all these three parameters you can play with and expose them as u properties if you want then i resize the blocks array since i know What the final size of block set is going to be which is basically the cube of the chunk size so it’s better to resize it at once rather than it expanding during the run time and finally i disable the shadows so this is kind of a personal preference Since we would be going for baked ambient occlusion so i want that effect to stand out more so i don’t want shadows right now again this could be a you property so totally up to You So now we’ll implement the utility methods and the first one is the get block index so it’s pretty simple so we’re basically flattening a three-dimensional area index to one dimension and first we’ll move across the x-axis then the y-axis and finally the z-axis so you can just consider this a Three-dimensional array in which first the x axis or the x dimension is filled then the y dimension and then the z dimension and next method we implement is the get position in direction this again is a pretty simple method so depending upon the direction we add that directions unit vector to the position And it should always be out of this six cases only so by default if it hits though it shouldn’t hit we throw an invalid argument exception and finally we implemented this check method so this basically checks whether the given position contains an air block or not so if the Given positions any of dimension is greater than the size so that means we are checking something that’s outside the current chunk we return true because we want faces to be drawn in that scenario otherwise we use the get doc index to flatten the position to the proper block index and we compare that With the air block so if the if that block is an air block we return true because in that case we want a face to be drawn and if it’s not an air block then it’s something solid so we don’t need a face in that direction Now we’ll implement the get face vertices method so this method will basically query the block vertex data to get the four vertices so we define a vertices array and have a for loop which runs still four and we basically add the correct vertex data so the way this logic works Is so first we query the triangle data right we have the triangle data here so depending upon the direction so we cast the direction to an end and multiply that by four because there are four elements per direction so with that any one of this direction will come and depending upon the i Index which is from zero to four so considering the right direction uh the direction will be cast to one one into four will point to this element and then i would be added which is from zero to three right so that are the four elements and we will get these Four indexes and finally we’ll use these four indexes like five zero three six to query the vertex data so fifth would be this one uh yeah fifth would be this one then the zero then the third and finally the sixth now actually fifth would be this one but You get the idea right we will use the triangle data to query the vertex data so once we get that we multiply that by a scale so in our case the scale is one so it wouldn’t matter and finally we offset it by the current blocks position and return that vertex array Or the array of the four vertices and this method is used in the create phase method where we are actually populating the mesh datas so we start with the vertex data and append the face vertices for the given direction and position next we populate the uv data so this is Going to be currently this only once we start adding textures this is this will change so for now we can just add in the simple squares uv data or faces uv data and finally we add in the triangle data so do make a note of the initializers syntax that i’m using here And the way we get the triangles is from the vertex count and this is something i explained in my blueprint video but the way this happens is depending upon the vertex count we can offset the vertex count by these few numbers like three and two to get the Triangle two triangles for the given face so if you need a proper explanation i’ll link uh i’ll put a link to a video going over the anatomy of the cube down below and you can go through it because that will explain in a much better way that Than i can and finally we increment the vertex count by four because every time a face is added we are adding four vertices Finally we’ll implement the get blocks and the generate mesh part so for the generate blocks what we’ll do is get the current location of the chunk using the get actor location and then we’ll iterate in two dimensions the x and y so we’ll basically be generating the height map kind of So we get the x and y position for the map so that we’ll get by taking the x and y indexes of the loop multiplying that by hundred so this multiplication by 100 is something specific to unreal only because unreal works the basic single unit in Angle is of 100 centimeters so we need to multiply stuff by 100 so we multiply x by 100 add in the location that is offset of the current chunk and divide this by 100 since we want to query our noise map or the height map using this You know the single steps in integers like 1 2 3 4 that’s why we divide again this by 100 so we don’t have that unreal offset now you could skip this division but then you’ll have to play with the noise frequency and i think you can still get a decent result but yeah This way we get the x and y position which we are going to use to query the height map and the way we are doing this is by simply using the noise object and passing in get noise which we pass the x and y value now get noise will return a value Between minus 1 and 1 so we need to scale it for to the correct value that we want so we add 1 and now the value will be between 0 and 2 then we multiply it by half of size so the final value out of all this would be between 0 and size Since like the 2 and 2 gets cancelled and then we round that to an integer since we want proper steps because we are doing a blocky world and just to be on the safe side i clamp this value between zero and size so that way it doesn’t give go out of bounds or So we don’t get any out of array index errors so once we have our height we fill basically the block still height till the height with stone and the blocks above height with the air blocks and that is all we need to do to generate a block setting Next we implemented the generate mesh so this is also pretty simple so we iterate through all the blocks in x y and z direction and then we check if the current block is a air block or a opaque block if it’s an air block we can just ignore it And if it’s something else we basically store the position with which just the loop indexes here next we iterate over the direction enum now this order of the enum values is again should be the same in which they are declared and in which the triangle data has been declared so We basically trade through all directions for the current block and we check if the position in that direction so basically we are checking the neighboring six blocks around the current block so if the neighboring block is an air block we create a face otherwise we don’t do anything And to create a face we passing the direction and again we need to offset the position by 100 because of the unreal units and with this we have everything implemented except the apply mesh and apply mesh is pretty simple so with all our mesh data created all we need to do is Go to the mesh and we can call the create mesh section method uh create mesh section and we’ll be creating the mesh section zero next we by pass it the vertex data then the triangle data then you can pass in a normal but uh we don’t we are not generating normals For now so we pass in an empty array so that would be np array of f vectors so just like this next we can pass in the uv data then we can pass in vertex colors also so this is also something you can work with if you don’t want Uh you know texture so we don’t want this so we’ll again pass in an empty array here t array and this should be of f color an empty array for this next we need to pass in the tangents again we are not necessarily concerned with that so again a t array Of f proc mesh tangents so all these values you can actually see the documentation of the create mesh section method and from there only you will get this and finally we need to pass in a bool value whether to create the collision mesh or not so ideally you would want a collision mesh If you want your character to run around over your mesh but for now we will set it to false since well it’s not needed for us and with this we have the complete chunk class implemented so it’s quite around like good 140 lines and now We’ll test this in the engine to see if it works now before diving into the engine and trying to figure out whether it’s why it’s not working so make sure you actually call this methods in the begin play which i totally forgot so with the super begin play we can call The generate blocks then we generate the mesh and finally we apply the mesh so with all that you can compile your code and i just added the chunk actor to my world now if i play i’ll see my chunk actually i spawn inside my chunk So you can see we get uh our waximesh here and if you are using the same frequency and the seed you will be getting the exact same mesh and you can go inside and see we don’t have any extra faces being drawn and yeah that’s it whatever blueprint Functionality we had to generate the mesh we are doing that in c plus now and just to have a little bit of fun we will create a world class which will allow us to basically instantiate multiple of such chunks uh depending upon the draw distance we set so This is something optional like it will be used in the future to test out the different meshing strategies and it won’t take much time so just stick around and i’ll talk about the next things coming up in the series so here i have created a new class based on Actor called chunk world and this is going to be pretty simple so first off we again don’t need the tick method and we have some new properties here uh you property this one will we will have it anywhere for now everywhere we don’t need this extra braces Add it anywhere and we’ll just supply a category that would be junk world okay and Yeah so the first property would be the actual class reference so the reference to the chunk class that we want to initialize so the way you store this would be t subclass or and this can be actor only since the chunk is a subclass of actor now This isn’t the correct way to do it ideally you would have a base class for all your chunk types and have that be the subclass here but for now we can work with this and i’ll just call it chunk next we’ll have another you property again edit edit anywhere The category would be chunk world and this will be the draw distance so we’ll basically be drawing uh basically a rectangle of chunks around the origin so we can have a draw distance you can set it to 5 by default and finally the last property would be the chunk size So this is this should be the same value as what we have as the size variable in our chunk class so we’ll have chunk world and this will be chunk size so we have that as 32 and if we change it and that place we need to change it here also And that’s all the properties we need so we can go to the c plus plus class we disable the tick uh remove the tick method and all we need to do is now do a two dimensional for loop go from we’ll call this x and it will go till the draw distance Uh this would be equal to and this would be it will start from minus draw distance to the draw distance so that it is all around and similarly we’ll do another loop in y so we’ll change this to y everywhere and now we just spawn our actor to do That we need to get the world reference so get world dot spawn actor and we’ll use the first overload which takes the u class property and this will be an a after again if you have a chunk place base class that is what you need to use here So this would be chung and then we need to give it a position so that would be f vector and this would be x into the chunk size and it’s not showing up chunk size in 200 that’s the default unreal engine single unit similarly we’ll do i into Size into hundred and for now z can be zero so that’s our position and finally we need to give it a rotation so that can be zero rotator so f rotator zero rotator yeah so that’s it with this we’ll have one basically a rectangular field of chunks being drawn around the origin so Let’s compile and see it in action so the code compiled successfully and i have added the chunk world to my scene next we need to select the chunk class so we’ll just search here and we can find a chunk class and for now the draw distance can be 5 and the chunk Size is 32 which is same as our size in the chunk class now if i just play so i’ll have my chunks drawing actually i’m spawning inside it but if i go out you can see we have our beautiful chunks spawning everywhere it’s around the origin so technically this would be An 11 by 11 chunk field so around 1 21 chunks there are and it’s started pretty fine like the performance right now is pretty good but then again it can be a lot better than this with multi-threading and greedy machine you know so yeah that’s it for this video And you can see we have culling also by default from unreal engine and in the next few episodes we’ll start by implementing 3d machine so that’s a better machine algorithm currently we are doing very simple meshing to generate these chunks with bd meshing we can get a more Optimized mesh which will reduce the number of vertices and triangles and yeah from there we’ll go to textures i suppose and finally to ambient occlusion also i plan to like have been getting comments regarding some other meshing algorithms that people want to see like marching cubes or something like that so yeah we’ll Look at that also so thanks for watching if you have any suggestions please leave them down below in the comments subscribe for the upcoming content and your feedback is always appreciated bye Video Information
This video, titled ‘UE5 C++Tutorial – Minecraft like Voxel Terrain Generation : Part 2 Mesh Generation’, was uploaded by CodeBlaze on 2021-06-12 12:23:31. It has garnered 5969 views and 112 likes. The duration of the video is 00:22:05 or 1325 seconds.
UE5 Source Code – https://github.com/BLaZeKiLL/UE5VoxelTutorial UE4 Source Code – https://github.com/BLaZeKiLL/UE4VoxelTutorial
UE4 Blueprint Series – https://youtube.com/playlist?list=PLgji-9GMuqkKHBbUZroj_h7rd36Jdtygf
To learn more about runtime mesh generation of a cube :- Anatomy of a cube – https://www.youtube.com/watch?v=bnmr_At2R0s
CBSL is a unity library that contains reusable generic components, utils & frameworks that I develop while working on my projects Give it a look at – https://cbsl.netlify.app/
Follow me on Insta – https://www.instagram.com/code.blaze/
#unrealengine5 #ue5 #Minecraft #gamedevelopment #programming
► Music Credit: ‘Low Frequency Music’ Track Name: ‘Good Day’ Music By: Low Frequency Music @ https://soundcloud.com/user-551516820 Official “Low Frequency Music” YouTube Channel HERE – https://www.youtube.com/LowFrequencyM… DOWNLOAD @ https://www.chilloutrecordsmusic.com/ License for commercial use: Creative Commons Attribution 3.0 Unported “Share Alike” (CC BY-SA 3.0) License. Full License HERE – https://creativecommons.org/licenses/… Music promoted by NCM https://goo.gl/fh3rEJ @ https://www.LoFi-HipHop.com