Saturday, April 7, 2012

Earth Moving

So one of the technological hurdles I needed to make this game was the ability to change the texture of the ground mesh based on height. This post got me started and led me to my first successful prototypes of digging and terrain deformation/texturing. While the results made me giggle with delight, I've only dealt with half the issue...digging.  A new challenge still awaits solution...and that is 'piling' new dirt on top of the terrain and texturing it 'differently' than something that has been 'dug'.

Right now, the texturing is done using a combination of 4 different textures...one for grass, bareness, dirt and rock (the actual textures are more detailed and bigger..1024x1024 that what is seen in this post)  They are 'merged' using a WebGL fragment shader based on the height of the earth at that particular point.  The fine grained details are that I maintain an array of each of the vertices in the ground mesh.(a plane) in the form of a shader 'uniform' (essentially an array variable that I can share between the WebGL shader and my javascript code.  I made an arbitrary decision that grass exists for x units from height 1.0 downward.  Bare earth for the next y units....dirt for the next z and bedrock for the final z.

If the user piled dirt on top of grass, my current technique could deal with it just fine...I could simply create the rule that anything 'above' the height of the baseline grass would be textured with dirt.   But....imagine if I were to leave that logic alone....and a user dug down to a certain level (say bedrock) and then began piling dirt there.  The current texturing algorithm would dutifully apply the dirt texture...then bare earth.,..and eventually grass where the user was adding earth!!  NOT what we wanted.   So...my current plan is to create another uniform (array variable) that indicates that a particular vertice is 'dirty' (pardon the pun) and to NOT apply the normal texture blending algorithm...but instead texture as dirt REGARDLESS of height.  This array would be updated whenever the terrain was deformed (dug or piled).  That's next on my experimentation list...to ensure the technique works and looks reasonable.

No comments:

Post a Comment