MESH GENERATION in Unity - Basics

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
one of the really powerful things you can do with programming is generating your own meshes this is used in a bunch of games from minecraft to no man's sky to create effects like water or entire procedurally generated walls so in this video we'll start by creating our first mesh through code but first this video is sponsored by specialist from improvable special Louis is a cloud platform that makes it easier than ever to build and host online games it comes with a set of tools to host your game globally and deployed within seconds they just launched a really awesome integration with unity called the spatial OS game development kit the GDK comes with the FPS starter project and a set of pre-made feature modules that enable online gameplay out of the box you get libraries for it transforms shooting and a character controller all working in a multiplayer environment all the source is there and you can get started now building on top of the FPS started project it is completely free to download and start developing but you will of course need a valid unity license to use to TDK so if you want to make your own online multiplayer game simply click the link in description and get started also special thanks to Andrew Cullen Enco art Arman true VR systems simmer I owe extender player Cheeta 3d weather maker and infinity PPR for that support on patreon alright let's generate some objects so the first thing that we need to do is create an object for our mesh to do this we'll go create empty and let's name it something like mesh generator we can also reset our transform here and we now need to add two components to this object we need to add a mesh filter as well as a mesh renderer the mesh filter stores the mesh itself which contains all the information about the shape of our object if you're going here we can see that there are some default meshes to choose from such as a cubic capsule a cylinder and a plane the mesh renderer is responsible for taking this data and rendering the objects so we can actually see it this is also where we can apply a material so to generate our own mesh we of course need to create a script that does this let's hit add component and let's create a script called mesh generator and let's double click this to open it up in visual studio now a mesh can contain a bunch of different data that effects our object but the most important data which we're going to focus on in this video is vertices and triangles and unity any mesh is made up of a bunch of points called vertices each one of these vertices has a position in the world if we look at a quad we can see that it is made up of four vertices one for each corner and again each vertex has a position we can store these positions one after the other in an array this is what we call the vertex array now of course objects and Yundi are made up of more than points we still need to fill in the shape to do this we use triangles in the case of our quad here we can split up into two triangles the first triangle is made up of point 0 1 & 2 to show this triangle we would add the numbers 0 1 & 2 to another array this is the triangle array the second triangle is made up of point 1 2 & 3 to also show this triangle we add these numbers to our array as well notice how the two triangles have no problem sharing the same points we'll probably talk more about this in the future now that's one slight issue with the way we've set up our triangle array here and that is something called back face culling back face culling is a process that eliminates the backsides of triangles in other words if a triangle isn't facing in the right direction it's not going to be drawn most engines use this process for performance reasons because normally there's no reason to draw the inside of an object after all it's not going to be seen the way that unity determines what direction a triangle is facing is the order in which we feed it the points of our triangle in unity triangles are drawn clockwise this is fine for our first triangle here we go from 0 to 1 to 2 which is in the clockwise direction however for our second triangle we're currently going from 1 to 2 to 3 which is counterclockwise and so this triangle is facing the wrong way to fix this we can simply reorder the points for a second triangle to something like 1 2 3 to 2 you can see we're now feeding the points clockwise in fact the order doesn't matter as long as it's clockwise we could just as well input 2 2 1 2 3 and it would work great now I know this can be super hard to wrap your head around in the beginning but as you start to experiment with it on your own I promise it gets much easier to visualize so inside of our script we can now create two arrays we can create an array of vertices and of course each vertex has three points so we'll make it a vector3 array and let's call it vertices we'd also create an array of triangles and this just needs to be an integer array let's call it triangles of course we also need to create a mesh objects luckily creating a new mesh is super easy let's first create a variable at the top here that we can store it in it's gonna be of type mesh and let's just call it mesh with a non-capital M then inside of our start method we can simply set mesh equal to a new mesh and that's all we need to do we've now created a new mesh object of course we also need to make sure to add this mesh that we just created to our mesh filter and doing this is pretty much just as easy we simply use getcomponent to get our mesh filter and we can then go dot mesh and set the mesh equal to the mesh we just created I'm just gonna go to the top of the class here and add require component type of mesh filter and this is just an attribute to make sure that there's always a mesh filter on the same object as the script just so we don't try to add a mesh to nothing also I'm just gonna get rid of the update method here so now we're ready to create the shape of our object let's create a separate function for this called create shape we then go void create shape there we go and inside this function we can first define some vertices and then some triangles to create vertices we go vertices equals a new vector3 array and we'll make sure to include the brackets we then open and close some curly brackets and at the end of these we'll add a semicolon as well and if you haven't seen this syntax before it's pretty handy it basically just allows us to specify a bunch of elements we want in our array so let's start by creating the first triangle here for that we'll need three points so I'm gonna create a new vector3 and this is going to be the first point it's going to start at zero zero zero I'm gonna create a comma here and now we can take this and copy it and paste it two more times now make sure to remove the last comma here so now we have three points but they're all currently at the center the second point here will offset by one on the Z so we'll go zero zero one and the third point that's offset that by one on the x going to be one zero zero and the y is going to be the same for all of them then we need to specify some triangles and we'll use the exact same syntax for this so we'll go triangles equals a new integer array we'll open a close some curly brackets add a semicolon and in here we can now specify the first triangle so this is going to go from point zero two one two two and that should actually be it we've now added a triangle however we haven't actually told our mesh to use any of this data let's do that in a separate function so we'll create a function here called update mesh and let's put it down here so void update mesh and in here we first want to make sure to clear our mesh for any previous data so we'll go mesh that clear and we can then simply input our vertex and triangle arrayed so mesh dot vertices equals vertices and mesh triangles equals triangles it's that symbol if we save this now and go into unity and hit play voila we're now generating a simple triangle mesh now of course notice that it's currently lacking a material and such shows this pretty ugly purple color but to change this we can simply go to the mesh renderer and choose a material if we just choose default diffuse there we go it's now affected by lighting now lighting is currently going to look a bit weird on our objects this might not be that clear here because we're just using a simple flat triangle but if you create more complex objects you're definitely going to see it and even just by changing around the light here you can see that it behaves pretty weirdly in fact it gets really bright when we shine it from underneath the reason for this is that unity uses another set of data called normals to calculate how lighting should look we could create this data ourselves much in the same way that we've done for vertices and triangles but 99% of the time we can have unity do this automatically which is really cool in fact doing this is as simple as going mesh dot recalculate normals and that's it if we say that make sure to assign a material again here because we assigned it in play mode and so it wasn't saved and play we can see that already it looks much brighter because the light is actually shining down on the surface now as we change around the lighting it actually reacts to the direction a fairly realistic fashion awesome so let's add an extra triangle to make our object into a quad doing this is super simple all we need to edit is the create shape method in fact for our vertices we just need to add one more point for the fourth corner of our quad so let's add a comma here we can go new vector3 and this is going to go one on the X zero on the Y and one on the Z as well and as for our triangles as we talked about earlier we now need to go 1 3 then 2 and if we save this and head into unity and play there we go we now have a quad pretty cool and as with any other object we can rotate it we can scale it we can move it around it's basically a completely normal unity object we can even add physics to it without any problem awesome and just to show you what would have happened if we didn't do the triangles in the correct order say we just went 1 2 and then 3 for the second triangle here you can see the result is that we're only drawing one triangle and if I go ahead and flip to the other side you can see the other triangle appears you can also see that it appears completely black because unity can't really calculate lighting on this because how do you light an object that looks like this so yeah we'll definitely just put that back awesome that's pretty much it for this video if you enjoyed it make sure to subscribe and ring that notification bell so you don't miss the next one also if you want to make your own online multiplayer game don't forget to click the link in the description and check out spacial OS also let us know if you like this type of more advanced tutorial this is of course just the tip of the iceberg when it comes to mesh generation there's still a bunch of stuff we could cover it like how to generate terrain or how to add color data to your meshes so again if you want to see anything like that definitely let us know in the comments on that thanks for watching and I will see you in the next video thanks of the awesome patreon supporters who donated in September and especial thanks to Andrew Kononenko art arming to VR systems simmer IO extended player cheetah 3d Jeff Johnson infinity PBR Sybok mummy danny sullivan chris sheriff abdullah pizza murphy thanks alone clear the set Vincent and skewer shreya ste der Kim's Kirk Ronan team a polar bear Bruins cat Noah Kiyosaki brother Larry tweed James Rogers Rob Baron become Vania Erasmus Robert bunker Jackson James P Anthony patent Q Swedish key and every see you guys ruff
Info
Channel: Brackeys
Views: 291,791
Rating: undefined out of 5
Keywords: brackeys, unity, unity3d, model, texture, models, material, materials, how, to, howto, learn, course, series, tutorial, tutorials, game, development, develop, games, programming, coding, basic, basics, C#, mesh, generation, procedural, terrain, generate, cube, quad, vertices, triangles, code, shapes, custom
Id: eJEpeUH1EMg
Channel Id: undefined
Length: 11min 10sec (670 seconds)
Published: Sun Oct 28 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.