Hello and welcome back to core blaze this is going to be the last episode of the series creating voxel chunks in Unreal Engine using blueprints in the last episode we finished up with this whole big piece of blueprint code that’s responsible for our noise map generation in this episode we’ll be tackling mesh Building which is the final thing that we need to do so we are going to be implementing the remaining helper functions is empty get position in direction and the check functions now if you take a closer look on the names of these functions you can get kind of the Idea of how much building is going to work so basically we’ll be iterating over the whole blocks array and for each block we’ll be checking the neighboring blocks in all the six direction whether they are empty or not if they are empty we’ll be creating the faces and we will Start by implementing the gate position in direction and this is something that I’ve already implemented and don’t get intimated by all these value simple function so the inputs are a direction of the type a Direction enum and a position which is an interactive and this outputs result which is going to Give position of type int vector so basically we switch on the direction so depending upon the type of direction we either add one or subtract one to the corresponding axis so for here we have the forward direction which you can see is this return moon and basically we add One to the x value and return y&z as it is now if you take a look at the direction enum that we created in the first episode and mapped it out so the forward direction corresponds to plus X right direction corresponds to plus buy-back direction to minus X left to Minus y up to plus dead and down to minus n so according to that map I basically implemented this function adding 1 or minus 1 on the corresponding axis and there’s nothing special about this function so you can implement this on your own next we are going to Implement the is empty function also this get position in Direction isn’t modifying any of this member variables so it’s a pure function four is empty we are going to have an input which is called block this would be of that type of a block enum and what We’ll be returning would be a boolean which will be the result so we’ll basically take this block and compare it to an enum value which would be all of the empty blocks that you have in your blocks enum so for me that’s the null block and err block and if you have More you can compare to those and you will basically be adding them or use the or operator on all of them and this is what will be returned here so that’s our is empty function and then this isn’t modifying anything on our state so this will also be a pure function and finally We will be implementing the check function now this check function is is empty function are depending on the use case it depends on the type of generation you want for my use case I want there to be faces on the edges of the chunk so that is these faces here so On all the five directions that is forward back left right and down I am going to have these faces except for the up direction because of direction is mostly comprised of a block so it won’t make practical to have faces here on the up direction so to do that the first Thing that will input would be a position again this would be able to type in collector and will be returning a boolean which would be a result okay so I break this position up and we’ll be checking the five directions whether it’s exceeding the size or going below Zero since our junk indexes should be between 0 and 16 on each of this axis you get this size variable we’ll take X and see whether it’s greater than equal to size since when we are checking the neighboring cheong maybe blocks and if you’re on a block which is At the edge of a chunk and the neighboring blocks index on either of the axis may be greater than 16 or less than zero and in those cases we want to create a face since that would be the edges so if it’s greater than the size I Will copy this block here on X or Y okay or if it’s less than minus 1 less than equal to minus 1 I mean these comparisons depend upon the type of generation you want so you can modify them according to your use case so the comparison that -1 happens on all three So these are 5 comparisons corresponding the 5 directions basically all them I’ll add 3 more pins and this is true any one of them will be returning to so the face will be created so we do a branch here and connect this this is true we’ll Return true this is false we do another branch here we’ll be checking the up direction if this is true we’ll return false I have a return if we don’t check here it will return false so to check top direction we basically say if Y is greater that is greater than equal to The size so that means this branch condition is true with return false and if it’s something that has not been trapped by these two branch condition then that means the block is somewhere in the middle of the chunk in that case we’ll be getting the block using the Gate block index function split the structure we’ll get the x y&z value then using the blocks we’ll get a copy and then we’ll check whether this block is empty or not I mean is empty is the function that we implemented and whatever will be this value will be Coming that so we will add another return node and plug this in here so that’s our check function again this is not modifying anything on the street so it would be a pure function now if you take a look at our helper functions other than create phase every other Function is a pure function so don’t make a point of that next we will go to our construction script and finally implement the mesh building so we will break this second execution pin of the sequence this pin also and move this out of the way for now and we’ll bring in The execution pin and do a for loop so first thing we need to do is iterate over the blocks for that we need three four loops corresponding the x y&z direction so we connect them so these are three nested four loops and that last index is going to be size minus one Since the for loop runs inclusive of the last index using this indexes we will be able to get the block index so we use the gate block index function split it pass in the x y&z value from the indexes of the for loop take the blocks array Get a copy and with this we are basically iterating over all of the blocks now we are going to check whether the block we have gotten is empty or not so if the current block that we are at is empty we don’t need to do any generation since air blocks and blocks Won’t have any faces so we can do a branch statement here and reduce a bit of computation so if this condition is true we don’t do nothing if this condition is false then only we go into this for each loop with generates faces I will recombine the structure Since we’ll be splitting that now when we are going over this direction so we’ll be checking the neighboring blocks to do that we will be using the gate position in direction so direction is this enum value and position is our x y&z values that we have here you can Even rather than splitting everything we can use make vector and do that so that will actually reduce the number of hours and clean it up a bit but we do it this way now so we get the position in that direction and then we check that Position and we get a boolean value and depending upon this boolean value will be doing the graph so if the check is true we will be creating our face now the only thing that’s left is providing the position value and since our cube is of 100 units length we need to offset a Position by 100 to do that we’ll be multiplying x y&z by 100 and again if I would have just created a vector this would have been a single multiplication but whatever it doesn’t matter now so we’ll be multiplying that XY and z values by 100 that’s this splitting the Position and winding this up you can obviously clean up the blueprints as much as possible creating macros and everything so there is practically no into it it’s a good idea if you are doing some big project which this certainly is it now if I compile this it May look like stuck but that’s because it’s going through a huge number of iterations here and if we take a look at the viewport zoom out a bit you’ll see we have our chunk being generated you may need to zoom out a lot depending upon your speed so with that We have a chunk mesh being generated and if we go inside we don’t have any of the internal phases so that is a really optimized way so we don’t have any hidden vertex position or rate increases and if I just save this and go into the world you’ll see this is our chunk Whitey the factor that we just created in the series if I move this around in x and y position you can see the mesh is modifying it may be a laggy bit laggy since it’s going through a large number of iterations to regenerate the mesh but you can see the mesh is changing Depending upon the position of the chunk actor in the world so you can place around 1020 of these actors it will only lag during the placement and once the generation it’s done it’s pretty performant rather than the previous tutorial in which I had hundreds of cubes being spawn which stacks the GPU Also but once the mesh is created it’s not as heavy on the system so that’s it for this tutorial series and I hope you liked it I will be doing the similar tutorial series in unity using the new burst compiler and the job system so that would be a pretty performant Multi-threaded way of doing this gem generation and multi-threading is something that can be done in Unreal Engine but I have no idea how to do that and I may do another video going on for the ideas that you can implement in expanding this whole voxel system also If I am able to do the UV and textures thing I may make a video on that so do subscribe so that you get notified on when these videos come up if you have any other suggestions or if you want me to make tutorial series on something Else leave them down below in the comments and share this video with the friends it really helps me out so thanks for watching bye Video Information
This video, titled ‘UE4 Blueprint Tutorial – Minecraft like Voxel Terrain Generation : Part 4 Mesh Building’, was uploaded by CodeBlaze on 2020-06-21 12:04:52. It has garnered 3865 views and 66 likes. The duration of the video is 00:13:10 or 790 seconds.
Hope you liked the series, do subscribe for the unity series
Playlist – https://www.youtube.com/playlist?list=PLgji-9GMuqkKHBbUZroj_h7rd36Jdtygf
Voxel Chunks in Unity – https://www.youtube.com/playlist?list=PLgji-9GMuqkI77VmFk0Rol4AWKp-OgkGr
Project Source – https://github.com/BLaZeKiLL/UE4-Voxel-Tutorial/tree/master
Follow me on Insta – https://www.instagram.com/code.blaze/
#UnrealEngine4 #Minecraft #WithMe