Hello everybody my name is pixel rain or rain for short i finally decided to try making some unity tutorials for youtube after spending way too much time watching them myself and what better place to start making these tutorials than with everybody’s favorite voxels yeah that means we’re gonna start with Something that looks like minecraft but it won’t be like that for too long once we’ve gotten all the base work in we’ll be switching up to a method that i’ve written or you know taking some myriad of other methods into what is ultimately a novel approach to dual Contouring but for now let’s get started with the basics today we’re going to be setting up the basic mesh structure for our container and to do that we’re going to go ahead and make a few scripts here we’re going to have a data folder and inside Yield data folder we’re going to drop in a voxel class we’re actually going to set it up as a struct because of allocations and whatnot so we can go and wipe all this fun stuff out and it’s going to be a very very very basic structure right now it is Only going to have a public byte id that’s all we’re going to use we’re using bytes for the space savings more than anything else now that we’ve got our ever so simple voxel we’re gonna go ahead and make another one another script called container all right so we’ve got our new script Called the container we’re gonna start by adding some requires so anytime you instantiate a game object and you add the container class to it or if you create an object in your scene at it it’ll automatically add the components for the mesh filter mesh renderer mesh collider And then we’re going to go ahead and add a couple variables here get our position and our references to the mesh renderer and so on and so forth then we’re going to make a function called initialize so what this will do is just assign our material to Mesh render and set our local position variable to whatever position we tell it to not super useful right this minute but a little ways down the road it’ll become the uh pretty much a pivotal point if you’re trying to make large-scale terrain we also need a method to match up with This called configure components this just gets the uh components from the game object nice and simple and then we’re going to actually start setting up our mesh data structure because i don’t personally like to have a bunch of this stuff just floating around in my container class i would rather have it Be contained in its own struct like this so what this is is it’s going to house a mesh our list for the vertices triangles uvs and whatever else we add like colors so our first function in here is going to be clear data which if it’s initialized except false we’re just Going to you know create new lists for everything and new mesh if it’s not set to false we’re going to go ahead and just clear the lists lists are not the best approach to doing this it’s just the easiest you don’t have to deal with allocations up front but not good for garbage Collection in the long run so we’ll be revisiting that at some point in the future then we’ve got a another function called upload mesh all this does is set the vertices triangles uvs and some other boilerplate mesh stuff that you traditionally want to have yeah so that’s that’s about it for our Mesh data now we’re gonna go ahead and make two functions to actually do something here because right now all we have is our components and a mesh data structure that’s not super helpful to us so we’re gonna have a generate mesh function and a upload mesh function and Before we can actually start writing the generate mesh function we’re gonna need some voxel statics or some static variables that are going to be used anytime we’re creating a block or you know just a what makes up the mesh so from the top we’ve got the vertices that Each voxel is going to be made out of um start with the bottom left corner of a cube then you go to the right corner top and then top left and then on the front side of it you have the bottom left bottom right and so on and so forth we’ve got the Vertex index which i use to uh map the face to the vertex um then you’ve got your uvs and then the triangles themselves this is just used to index into one of these four vertices and add it to our array of triangles so now that we’ve got that sorted out We’re going to go ahead and do our generate mesh function just going to look something like this to start with helps i guess if you actually have some uh mesh data declared all right so when we start this function we’re going to go ahead and call clear Data which as i said before we’ll just clear all the data in the struct you always want to clear the data before you regenerate we might move it around when we work on threading but for right now that’ll be good enough we’re doing a single block just to draw You know our good old friend the cube so we’ll just set a block position of 888 um we’ll make a new voxel with an id of one wherein zero is going to be air essentially not a solid block and then you’ve got just a couple variables to help down here these are Arrays so for each face we can set the uh zero through three in that array to be what the vertices in the face is with some clever mapping and then when we do the when we add the triangles we can actually add the vertices in using that flat array it In my experience that makes it a little bit easier for me you can do this any number of ways this isn’t really what matters in the long run but this is just what i do then we’re going to iterate over each of the face of the cube um Technically we aren’t checking the faces of the cube just yet we’ll be getting to that in the next part of the video when we set up our data structure then we’ve got so here’s where we map from our array of voxel vertices we map by the index we’re on by face And then by vertice so i would be face j is the vertices we’re at in that quadrilateral phase and you add in the block position that we set here and we also map the uvs from each corner of the quadrilateral then we’ll iterate over each of the six points for our triangles Going again through our remapping here and for the triangle itself we’re just going to increment a basic counter because we’re doing um we’re not doing shared vertices here so each point of the triangle is going to be its own vertices it’ll it ends in 36 vertices for a cube instead of eight We’ll eventually add in shared vertices but i want to wait on that until we get to our dual contouring method so now that we’ve got that sorted out we’re going to go ahead and do our upload mesh which is just going to call that upload mesh function which Just sets their vertices and everything and does the basic mesh functionality we’re gonna make sure our local variable for mesh renderer and mesh filter and everything is set up and if it’s not we’ll call configure components which will go through and just get components for each of them And we want to assign our mesh filter with the mesh we just made mesh collider as well you don’t have to do the mesh glider right now it is the longest part of this whole process realistically but that is what it is um now we’re gonna go ahead and Come back to our project and in case you didn’t know when you hit play it always lags a little bit and there’s a way to speed that up if you just come into your project settings editor and then you toggle interplay mode options it’ll help with the uh Lag when you hit play on a scene so now that we’ve got that set up we’re going to go ahead and create a world manager class and eventually this will handle the instantiation of chunks and everything else but for right now all we needed to do is take a material that we set And run through this little bit of code so this is going to just like top everything that’s important so we’re going to create a new object called container we’re going to set the transform to be the same as the world manager object then the container is going to be added to it We’ll call initialize on it just to set that local material in the chunk then we’ll call the generate mesh and upload mesh function just like that helps if i make an actual scene doesn’t it yeah do that real quick scene all right then we’ll go ahead and create an empty object Call it world manager and then we want to go ahead and make a world material you can name it whatever you want doesn’t really matter right now but just for the sake of it and then if you hit play you should have a handy dandy cube first one Don’t worry i know this isn’t a lot that’s why the second video will be releasing at the same time as this one and that one will actually go over the data structure i’m going to use at least to start and we’ll get a little bit more going on than just a Single cube i just didn’t want this video to be 45 minutes long and 300 lines in the container to start off with so um i hope everybody enjoyed that this is my first time doing a video like this so if you have any comments on other ways i should do things If you’d prefer me to actually write the code instead of copying it in and explaining it i know i prefer that in some instances but it’s i know it’s a personal thing usually so also i would be more than happy to work on other series in the process So if anyone has any suggestions on something else i should work on and make tutorials for feel free to let me know in the comments down below until next time thank you guys for watching Video Information
This video, titled ‘Unity Voxels – Procedural Generation Tutorial – Part 1: The First Cube’, was uploaded by PixelReyn on 2022-03-09 22:40:16. It has garnered 7253 views and 142 likes. The duration of the video is 00:13:20 or 800 seconds.
Hello everyone! I know what you’re thinking, “Not another Minecraft Clone, ugh.” While you are correct about that for now, it won’t stay that way for too long! Once the basics are in and working, we’ll get to implement a novel Dual Contouring system. (or Naive Surface Nets, since I don’t use a QEF) But for now, I present: Cube.
Github Repo: https://github.com/pixelreyn/VoxelProjectSeries/tree/Part1
My Links: Twitch: https://www.twitch.tv/pixelreyn Discord: https://discord.gg/WCW3QwgY3c Patreon: https://www.patreon.com/pixelreyn