- Home /
Generating spherical terrain from a spherical heightmap
Hi all,
I'm using libnoise to generate a spherical heightmap, which is easy enough. However, I'm stuck on mapping this heightmap onto the sphere in order to get a nice terrain going. I'm familiar with the basics, like going through the vertices, using Texture2D.GetPixel(), etc. but actually implementing this is causing me a headache.
There are already several questions about this on the internet but none of them really go in depth about the implementation. I imagine it's not as simple as just looping through the vertices and applying the y-value to it like you do with planes.
I also thought about using regular heightmaps and using a cube-sphere but the implementation beind that confuses me too.
Any help is appreciated!
Can you say more about that heightmap? What kind of layout does it use? Is it just latitude and longitude mapped to U and V texture coordinates? If your terrain is simply a unit sphere mesh then you just need to multiply each vertex with (1 + m * h) where h is the height from the heightmap and m is the maximum height. The main question is how you would sample the texture.
@coeusueoc
These are two examples of the heightmap, the first picture is how it looks flat, second is how it looks on a sphere. https://imgur.com/a/RVyxD And yes it's simply a unit sphere.
Answer by BastianUrbach · Apr 11, 2018 at 05:00 PM
Seems like you just need to calculate latitude and longitude for each vertex, then sample your texture with those values, then translate the vertices outwards depending on the value.
float latitude = Vector3.Angle(Vector3.up, vertex); float longitude = Vector3.SignedAngle(Vector3.right, vertex, Vector3.up); float height = texture.GetPixelBilinear(latitude / 180, longitude / 360).r; vertex *= 1 + maxHeight * height;
Hey, this seems to be working good so far, however, it seems that it only changes the vertices on roughly have of the shape.
https://i.imgur.com/JZRdVr1.png
Set "wrap mode" to "repeat" in the texture import settings or add the line
longitude = (longitude + 360) % 360;That should fix it.
Oh wait no, I think the problem is that latitude and longitude are the wrong way round. Use
GetPixelBilinear(longitude / 360, latitude / 180)ins$$anonymous$$d.
Your answer
Follow this Question
Related Questions
UV-ing a sphere procedurally 1 Answer
How to create a spherical navigation? 1 Answer
Mesh collider trigger 2 Answers
Setting triangles failing 0 Answers
How do I guarantee a generated sphere's mesh is facing outward? 1 Answer