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..

2 comments:

burmeister said...

i like what you're doing man

the draw exceptions you're making are really smart

this is especially interesting:

"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."

and then when you break one, it renders the newly exposed faces? that makes a lot of sense.

Roy said...

Thanks dude. I'm sure other games are doing it.. I'm catching up years on game development progress here haha. I think once I figure that out, my render times should drop significantly.