Sunday, July 3, 2016

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.


enter image description here


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:


enter image description here


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:


enter image description here


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.



Answer




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.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...