Updated January 29, 2014, 4:42 pm

Icosphere Planet

This post is going to be about rendering an icosphere planet.

I had two pages bookmarked for a while but never had the chance to sit down and implement this, well until last night that is.

First part of the implementation was to render a icosphere. This blog post explains how it can be done. The code there was in C# so I ported it to AS3 and used Stage3D to render the sphere. One thing that annoyed me while working with Stage3D was that I had to use Vector.<Number> types to store the vertex data. In C++, I create a structure for a vertex like :

struct IcoSphereVertex
    Vector3 position;
    Vector3 normal;
    Vector3 color;
Then instances of this structure are stored in a std::vector and can be passed to Direct3D to create vertex buffers. In Stage3D you can't pass a Vector.<IcoSphereVertex> so all the vertices are stored in a Vector.<Number>. In my case I had 9 Number elements per vertex so my Vector that holds all the vertices has vertexCount * 9 elements.

Also at first AGAL which is the shading language used in Stage3D was like magic to me. This is mainly because it is assembly. After I found this which has a nice list of all the vertex/fragment shader registers and constants it all made more sense. You just need to be aware of which registers you pass in each matrix/vector/constant and use the correct registers in the shaders.

Second part of the implementation is to offset the vertices of the sphere so that you get a planet like mesh. Again the idea for this is really simple and comes from here. Simply put, after the icosphere is created the algorithm iterates a certain number of times. Each iteration a random plane is created that divides the sphere in two parts. Vertices of the sphere that lie in front of this plane are moved up(or down) and vertices that lie behind the plane are moved down(or up). The direction the vertices will be moved is randomized every iteration.

Finally I colored the vertices based on their height from the center of the sphere. This gives the impression of oceans, plains, mountains etc.

Here is how it looks after running it for 500 iterations

I also embedded the swf here so you can play around with it. You can input the number of iterations you want into the textbox, it takes a while to generate the planet as you increase the iteration count. You can zoom in/out with the mouse wheel and rotate the planet by dragging.

At some point I might port this to Direct3D and think about texturing the planet.

Check out the code at GitHub https://github.com/barisusakli/icosphere