Tuesday, July 10, 2012

The Grid

No, this isn't a Tron post... Heh.  Anyway, a while back I read a question on (I believe it was on gamedev.StackExchange.com) about drawing a grid over terrain.

There were some questionable responses, far more complicated that it needed to be.  I've implemented this quite simply in my terrain shader:



 

if (xGridEnabled && PSIn.Normal.y > .4)
 if ((int)(PSIn.PosCoords.z*xGridThinnessFactor) % (xVoxelBlockSize*xGridThinnessFactor) == 0 
  || (int)(PSIn.PosCoords.x*xGridThinnessFactor) % (xVoxelBlockSize*xGridThinnessFactor) == 0)   
  output.Color.rgb = output.Color.rgb * 0.90f;

 

So if the grid is enabled (xGridEnabled) and the slop isn't too steep, since we don't want to muck up verticals with the grid, then it draws.  Ok, so the PSIn.Normal.y > .4 is bad.  I need to make that a parameter or something...

In any case what this does is it looks at the coordinates passed in from the Vertex shader (PSIn.PosCoords) and takes the modulus of the X or Z coordinates and xVoxelBlockSize.  xVoxelBlockSize is passed in to the shader, that's the unit width of a terrain cell.

So if the modulus is zero, it simply darkens the pixel (color *-= .9f).  I should probably make that a configurable item, too.

xGridThinnessFactor is another passed in value (default = 2).  The larger this value the thinner the line gets. Yeah, that seems backwards.  You know what?  It is.  I'm changing that crap right now.  And that .4 is now xGridNormalLimit.

Ok, so now it is xGridThicknessFactor, the larger the thicker.  The default is .5f.


Anyway, this is pretty clean, simple, and just works.

No comments:

Post a Comment