Thursday, March 03, 2011

Generating Mesh

It doesn't look like much of anything, but I'm generating those meshes.  I've got the vertices and the uvs part of the Mesh object figured out, but not the triangles.

I'm already detecting what part of the square is exposed to air, so now I just have to selectively draw that part instead of the whole cube and I'll be good to go (after figuring out the triangles part too).  The entire inside will be totally hollow.

I'm very excited for this.

Tuesday, March 01, 2011

Some Simplex Noise

I've had family visiting for the last week, so I haven't gotten as much time as I'd like to program.  Since the last post I've been reading all about Perlin and Simplex noise and how to implement it.  I made the decision to stick with Simplex noise since it's apparently faster.  Luckily someone converted libnoise over to Unity's C#, so I've been using that.

When I first started, I was simply getting the y value for the (x,z) position and drawing a block there.  This was neat in that it generated a rolling hill kind of terrain, but I wanted more.

The next thing I did was foreach (x,y,z), I was getting the noise value from the Simplex algorithm.  If it was > 0, I would set that block to visible.  This generated some awesome caves to explore but it wasn't suitable as a terrain because there was always something over you.

As I played more and more and sped things up, I noticed my largest bottleneck being in the most predictable spot:  my draw function.  I was still doing the same old Instantiate() a cube dealie.  I tried several CombineMesh scripts and got my draw calls down from 60 to 4, but I feel like I can do better.  For a 16x16x16 chunk it still pauses for a second while it generates/draws.  One thing I'm considering is generating a huge chunk of the world and storing it somewhere.  When you need to generate a new chunk, all the game would have to do is pull the world data instead of generate it.  Generating the terrain is extremely fast so doing a huge chunk in the beginning isn't so bad.

On top of generating the world, I'm also doing a second pass to detect which cubes have sides visible to the player and which ones are surrounded.  If the cubes are surrounded, they're set to hidden so we don't draw them.  I also add the cubes that are visible to a List<> of their own.  Originally my draw function was going through every single (x,y,z) coord and checking if that cube needs to be drawn.  Now my draw function just goes through that list.  This has cut my loop iterations down considerably (more than half in a lot of cases).

Anyway, this is all probably newbie stuff, but I'm learning it as I go along.

My current tasking is generating my own mesh instead of instantiating a whole cube.  I want to do this so I only have to draw the side of the cube that's facing the air instead of the whole thing.

So until next time..