Spherical procedural terrain shader based on slope

I've created a spherical terrain object out of 6 sphere projected (normalised) planes, each plane has been heightmapped post to being normalised.

I'm looking to create a CG shader which will firstly calculate the slope of the heightmapped terrain, I understand this will need to look at the normals to calculate this can anyone elaborate on this for me? The next problem is that I want to do the slope calculations based upon the terrain "flat" rather than normalised.

Would it be best to either reverse the normalisation of the vertices during the slope calculations, or pre calculate and store the vertices slope data (on the flat plane) during the mesh generation?

This is an example of what I want to achieve with the shader:

Any suggestions would be brilliant.

Thanks, C.

Update in response to opatut answer:

I've output the current slope data based of how I "think" was suggested and these were the results:

I think my issue is that as each plane begins to curve that curve is includes as a slope which is the obvious thing to happen. I want to "flatten" out each of the 6 planes again for the slope calculations.

Why does it have to be 6 height-mapped planes? It would probably be a lot easier if you geometry-shaded a sphere and did all your calculations based on that sphere - you can easily compare the normal vector to the radius vector (basically the location of the vertex relative to the sphere center) to get your slope angle.

Also, I think it'd be easier to generate the heigh maps, or at least you could cubemap the sphere as well... Also, you will definitely get higher resolution heightmaps, since you don't have to use part of the value range (0..255) to achieve the sphere shape.

Update due to more information from comment below:

Would it be best to either reverse the normalisation of the vertices during the slope calculations, or pre calculate and store the vertices slope data (on the flat plane) during the mesh generation?

Depends on what you need the slope calculations for. If you require them for your simulation (e.g. driving physics), it won't help you if you have them in the shader only. Then you will need them in the program anyway, so you can implement your calculations once and store them in the vertex data - if the mesh does not get too high-resolution, it should be fine.

But if you just want to apply the slope-based texture effect, calculating them in the shader should be fine. You should have the exact (heightmap-transformed) vertex position in the shader, so you can just take that position, calculate the radius vector from that (i.e. just normalize the vertex position since your sphere center is 0,0,0) and calculate the angle to the normal vector. No need to transform the vertex position again.

• Thank you for the reply, interesting suggestion but I'm using Unity3D and it doesn't support Geometry shaders. The sphere's center is just world space 0, knowing that can I still make the comparison easily enough? – Caius Eugene Jan 3 '13 at 16:18
• Updated my answer, see if that's what you want. I don't understand why you would want to reverse the normalization, and what exactly you mean by "normalization" of the plane... – opatut Jan 3 '13 at 16:33
• Thank you for the update thats very informative. To get the plane to project spherically I am moving its center point downwards and applying Vector3.Normalize to each vertices. I believe it does something like this: length = Mathf.Sqrt(xx + yy + z*z); sx = x / length sy = y / length sz = z / length – Caius Eugene Jan 3 '13 at 19:34
• Sorry I think I may not being clear on my goal, I want the slope to be based of the planes up, rather than the overall sphere. – Caius Eugene Jan 3 '13 at 21:22