In this video i want to start exploring how we can place trees on our map using again purely noise function there is a great video called discrete constructs in endless worlds on gdc vault and i think it is on youtube as well and basically it shows multiple Techniques on how you can create things like structures rivers but i think it also covers trees and that’s where i get the idea so apparently noise when generating a 2d pearly noise it has minimas and maximas in local points so we can establish that in this terrain we can see multiple Maximas that are local maximas and the idea is that if we generate a pearly noise value different from our train for our trees then we can simply split the terrain into chunks which we already have then takes the maximum around this terrain and this would be calculated by simply selecting each point calculate Calculating the noise value and simply checking if the neighbors of this concrete position are lower or equal value to this concrete position that we have selected now if they are less or equal then it means that our current point is a local maximum and if we have those local Maximas then we can simply say okay let’s place the trees in those local maximas it will not represent our terrain because it will be a separate pairing noise calculation but basically we can integrate it with our world for example by preventing the tree being spawned inside the water or at Certain height in our case i think the only limitations will be that i do not want to place trees on the rock surface but basically this is the idea of how we can create our trees pretty easily and place them around the map so overall they look pretty nice and placed pretty Randomly i’m sure that there are better ways to do it but basically this is the whole notion of it that we can still use burning noise and just by processing the data that we receive for each chunk and we can find the local maximas and place their specific things So we have handled one of the problems which is placement of the trees the second problem is as you can see that trees as far as goes for the trunk of the tree it is in one space so it is possibly in one chunk but the leaves can be shared between two Chunks so the second issue that we have is that we need to place the leaves and we are not always certain that all the chunks so for example that this chunk is rendered so we need to place the data but when rendering we need to split the Rendering for example here this tree is rendered across four chunks so basically we need to put the data about the three leaves into the chunk data of ours and then when we are rendering we are simply rendering trees as well as our terrain in one go Okay so this is the issue with the rendering and we are going to tackle this as well okay we are back in our project so first of all we need to adjust our generation and our generation mainly happens in the train generator so let’s open this script up okay great So in our train generator we have our biome generator we’re going to select the biome generator later on based on what is the biome that we have in this position but basically the biome generator should have a way to generate trees if we want to generate the tree data Now to avoid the issue of maybe for our desert we do not want to have any trees we are going to simply have something like this we want to have three data objects three data equals and we are going to call our biome generator dot get tree data And we are going to pass here our data and also we may want to include the map seed offset so that we can generate different tree data depending on the map c that we have provided in our game okay so now first of all we need to Create this class so let’s create a below public class three data okay and we are going to now be able to generate this gets tree data right click quick actions and generate method in our biome generator now we may want to select our public class 3 data right click quick actions And move type to tree data script so this will be in a separate script basically this is how we can quickly generate our logic and we when we have this three data we may want to set our data dot three data equals our three data that we Have calculated so we are going to store the data in our chunk data about the trees this is exactly because later on when we are rendering our chunks we need to include only the leaves that are visible on this specific chunk so this is why we need to have this data Before we can render our world so let’s right click on this three data quick actions and generate field tree data in our chunk data class okay and now we have saved this data so now later on when we are processing our columns we are going to be able to Include this tree data in this generation just to clarify we can put this code before those two for loops because we only want to calculate the noise values once and then when we process our data we want to check the data against what we have calculated in Each position but we do not need to regenerate those values we only want to calculate those once for now let’s focus on how we are going to generate those points using the local maximas of our purlin noise values so let’s save it let’s go back to unity okay So somewhere around here we should have the tree data so i’m going to right click create a new folder let’s call it trees okay i’m going to drag here our tree data this new folder okay and let’s open this folder up because we will need to have couple more scripts Here so let’s right click c sharp script and we are going to call it tree generator okay so this will be the mono behavior that will store all the settings for the tree generation for a specific biome now let’s maybe start by filling in the three data class that we have already created Let’s open it up okay and here we are going to have three fields now we can create public list of vector to int and we need to import the vector to insert click on it quick actions and using unity engine because when we have generated our class we have not Added any libraries by default and we need to also import the list so right click quick actions and using system collections generic and now we are going to have a list of vector to end and those will be 3 positions because they will be independent of the Height also of from the basically y-axis in our game they will be generated by selecting the random position through the pearly noise next you are going to have also a list but of vector 3 in type and those will be 3 leaves solid and i have planned to add a tree leaves Transparent but i didn’t at the end so we can have only two of those here and basically three leaf solids will be an indicator way where should we put the voxel representing our three leaves and this will be important for the generation of our different chunks and rendering them Correctly and this is it for the tree data let’s save it let’s go back to unity great now let’s open up our tree generator script okay now this will be a model behavior because we are going to have to pass it noise settings and domain warping so create public noise settings tree noise Settings field and public domain warping domain warping and we are going to use both of those to generate our trees so first we are going to have a method called public three data so we want to return tree data generate three data we are going to take in the Chunk data chunk data and vector to int map seed offset this will be called i think from the biome generator script now in here we are going to set the three noise settings world offset to be the map seed offset so that we can generate appropriate version of our Three positions depending on the offset that the user has provided next we are going to create a new tree data and we are going to create float so the 2d array of noise data and we are going to call generate tree noise method we are going to pass chunk Data and the tree noise settings right click quick actions and generate this method and at the end we are going to set three data three positions and we are going to have a new class called the data processing and this will be a static class so let’s Right click quick actions and we can generate a class data processing okay and this is generated for us this class next we have this find local maxima so we can right click on actually let’s fill it in before it generated and find local maximal take the noise data That we have generated because we need to have this noise this data to be able to analyze it in terms of where are the what are the values of the neighbors of a specific point now when we have this noise data we need to pass chunk data worldposition.x and chunk dataworldposition.z values because Basically we are generating this in the x and z direction we don’t care about the y direction at this point when we are generating the positions that our trees might be placed at so now if we have passed all of this right click on this find local maxima Quick actions and we are going to generate the method in our data processing class that we have generated as a temporary class that we are going to fill in later and we are going to at the end return our tree data okay so this is one method the second One the private one will be generate tree noise which will generate as a float to the array of the noise values for each position in our chunk now to generate it all we need to do is create a new float to the array that will be of Size new float chunk data chunk size chunk data chunk size so this will be our 2d load array now we need to calculate x max x min z max z means so those are the points that are defining the bounding box of our chunk and for those we want To calculate our noise values and those will be calculated by getting the chunk data worldposition.x plus chunk size and chunk data worldposition.z plus chunk size and the min and max will be simply position.x and position.z now this is because we need to calculate the noise based on the world position Of our in our chunk next we are going to have x index 0 and z index 0 and we are going to look for each x equals mean x less than x max x plus plus and the same for the z and we are going to call noise max x Index and z index equals and we are going to call our domain warping to generate our domain and noise using those x and z values from those for loops and the three noise settings now we are going to increase the z index and x index so that we can place the correct The value in the correct spot in our noise max array this is simply because our x and z values will be something some coordinates in the world space but our values in the noise array are from zero to the chunk size and we are going to return this noise max array and this Is all that we need to do in our tree generator now last thing that we need to fill in is this data processing find local maxima method so let’s try to click on this and go to it this definition now let’s maybe take a break here and in The next video we are going to fill in this data processing class as well as handle placing our trees on our map okay again if you are enjoying this tutorial leave a like leave a comment subscribe to the channel it helps me a lot and also you can support me by becoming a Patron okay see you in the next video Video Information
This video, titled ‘Create MINECRAFT in Unity – S3 – P7 Adding Trees P1’, was uploaded by Sunny Valley Studio on 2021-08-17 18:40:11. It has garnered 1164 views and 47 likes. The duration of the video is 00:13:29 or 809 seconds.
In the Section 3 we will explore how easy it is to make our code for the procedural voxel world tutorial multithreaded when we have separated data creation from the rendering of the chunks.
Section 1 – Voxel Terrain Engine https://youtube.com/playlist?list=PLcRSafycjWFdYej0h_9sMD6rEUCpa7hDH
Section 2 – Procedural generation https://youtube.com/playlist?list=PLcRSafycjWFesScBq3JgHMNd9Tidvk9hE
Section 3 – Multithreading https://youtube.com/playlist?list=PLcRSafycjWFceHTT-m5wU51oVlJySCJbr
Learn more from about Unity Game Dev in my video courses at: https://courses.sunnyvalleystudio.com/
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
Articles: GDC – https://www.gdcvault.com/play/1025192/Math-for-Game-Programmers-Discrete Perlin noise – https://adrianb.io/2014/08/09/perlinnoise.html Domain Warping – http://www.nolithius.com/articles/world-generation/world-generation-techniques-domain-warping
Join the discord if you have any questions! https://discord.gg/RQEtYHz