Hey, I may not know much, but I know people like to see results from what they are learning! I find this source slightly laggy, but this makes it perfect for learning as you should REWRITE the script to a better standard once you understand it!
Anyway, I highly recommend you to download the link I posted last blog berfore you continue :
http://unitycoder.com/download/index.php?link=178&uid=un1t3c0d5rAs this is the source I am basing off and decripting right now, in the following words.
But before we get to ahead of ourselves and start blindly copy/pasting code, we need to understand it.
What exactly does this source do?This source is a great implementation of optimisation in a Voxel Engine, it cuts the verts from a
HUMUNGUS (Not a real word but necessary) amount (104.5k) to 6.1k, quite a difference for exactly the same result.
So what does this have to do with anything, you may ask, this code makes it so that only the faces that need to be rendered
(ones that are next to open space) are displayed while the rest are unrendered saving mass amounts of rendering power.
Perhaps this needs a visual example, so:
Here is the chunk that has been slightly destroyed (from outside) :
And here it is from the inside.... As you can see, It has only rendered very few of the faces, the ones that are
visible!I'm excited so now how do I do it?Getting to it, don't worry!
Now the best way to understand this is to decript the code. For your own learning benifit I would like you to check out the source and try and understand the code on your own.... If you get lost, come back and continue.
I also strongly recommend reading up on:
Lists/Array Types : http://robotduck.wordpress.com/2009/11/04/88/I take no credit for the code below, it belongs to : ChunkRenderer testing in unity javascript - mgear - http://unitycoder.com/blog
*ORIGINAL SOURCE: Rendering sorcery II by "paulp" > http://www.blocksters.com/node/57Firstly the set variables, a basic rundown of what they mean. - Code:
-
private var s:int=16;
private var lineRenderer : LineRenderer;
private var chunk:int[,,] = new int[s,s,s];
Let us pick this code apart and make it easy to understand - Code:
-
private var s:int=10;
This is the chunk size, edit it as you will to find the best results.
- Code:
-
private var lineRenderer : LineRenderer;
This has nothing to do with the actual chunk generation, but the ray(shot with the lmb). Ignore this for now.
- Code:
-
private var chunk:int[,,] = new int[s,s,s];
This sets up a 3-Dimensional array, notice the
[s,s,s] on the end. This sets the array size to
[10,10,10] (This i dependant on the size of the chunks (s)). This array keeps ALL the values of the blocks within the chunk.
Whew! That was a difficult part wasn't it? Well the worst is yet to come. Next up is the Start() function. - Code:
-
function Start ()
{
for (var x:int=0; x< s; x )
{
for (var y:int=0; y< s; y )
{
for (var z:int = 0; z < s; z )
{
chunk[x,y,z] = 1;
}
}
}
gameObject.AddComponent ("MeshCollider");
ChunkRender(chunk);
lineRenderer = Camera.main.transform.GetComponent(LineRenderer);
}
Once again, lets break it up. - Code:
-
function Start ()
{
for (var x:int=0; x< s; x )
{
for (var y:int=0; y< s; y )
{
for (var z:int = 0; z < s; z )
{
chunk[x,y,z] = 1;
}
}
}
Now this script is
very simple. All the for loops run through as long as they are less than 16, the chunk size. Thus setting up the chunks blocks location within the array. Thus there will be 16x16x16 blocks =
4096 blocks in a chunk!The reason for the
chunk[x,y,z] = 1;. This code sets every single block to "solid" (Shown Later!). This code is not used just yet.
- Code:
-
gameObject.AddComponent ("MeshCollider");
This piece of code does the obvious.... It adds a Physics MeshCollider to the gameobject in charge of the chunk.
- Code:
-
ChunkRender(chunk);
ChunkRender() is a script that I will go into detail about next. It takes a argument of
(chunk:int[,,]). Chunk being our 3-Dimensional array and
:int so that it shall only take int values.
- Code:
-
lineRenderer = Camera.main.transform.GetComponent(LineRenderer);
Once again.... Ignore this
I am going to split this post into 2 parts as the content is WAY too long to put in one.....
Tue Oct 09, 2012 2:18 pm by landon912