Creating an Octahedron Sphere in Unity

3 Jasper Flick Oct 27, 2014

octahedron sphere planet

We often use meshes to represent 3D objects, approximating surfaces with a collection of triangles. This works fine when all lines are straight, but becomes troublesome when curves are involved. A cube is easy. A sphere? Not so much.

Consider Unity's default sphere mesh. Its topology is known as a UV sphere. It's basically a rectangular grid that's wrapped around a sphere and compressed into a single point at the poles. Thus the vertical lines have constant longitude and the horizontal lines have constant latitude.

uv sphere, perspective uv sphere, top uv sphere, side

This kind of mesh is relatively easy to create and matches how textures for spheres are often stored. However, it uses a lot of triangles at the poles and it looks quite different when viewed from above than when viewed from a side. Ideally, the triangles would be distributed evenly across the sphere's surface and would look the same no matter what side you view it from.

An alternative way to create a sphere is to start with one of the platonic solids, which are all more symmetrical than a UV sphere. A tetrahedron, cube, octahedron, dodecahedron, or icosahedron. Their vertices are all placed at the same distance from their center, so in that sense they are all approximations of a sphere. Then if you split all their faces into smaller faces and push the new vertices created that way to the same distance from its center, you end up with a better approximation of a sphere. Further subdividing results in more triangles and a better approximation.

The more faces you start with, the better the distribution of the triangles. So a tetrahedron is the worst candidate to begin with, while a icosahedron is the best. The octahedron falls somewhere in the middle and strikes a balance between quality and complexity, so we'll use that one.

Octahedron

To consider the creation of an octahedron sphere in isolation, let's create a static OctahedronSphereCreator class whose only job is to create our mesh. Give it a static Create method with parameters to specify how many subdivisions you want and what radius the sphere should have.

using UnityEngine;

public static class OctahedronSphereCreator {

    public static Mesh Create (int subdivisions, float radius) {
        return null;
    }
}

And to test our mesh, let's create an OctahedronSphereTester component that simply creates the mesh when it awakens and assigns it to its MeshFilter.

using UnityEngine;

[RequireComponent(typeof(MeshFilter))]
public class OctahedronSphereTester : MonoBehaviour {

    public int subdivisions = 0;
    public float radius = 1f;

    private void Awake () {
        GetComponent<MeshFilter>().mesh = OctahedronSphereCreator.Create(subdivisions, radius);
    }
}

Now make a new game object with this component, and also give it a MeshRenderer and a default material.

tester game object

Of course, nothing shows up yet when entering play mode. Let's change the Create method so it produces an octahedron instead of retuning null.

        Vector3[] vertices = {
            Vector3.down,
            Vector3.forward,
            Vector3.left,
            Vector3.back,
            Vector3.right,
            Vector3.up
        };

        int[] triangles = {
            0, 1, 2,
            0, 2, 3,
            0, 3, 4,
            0, 4, 1,

            5, 2, 1,
            5, 3, 2,
            5, 4, 3,
            5, 1, 4
        };

        if (radius != 1f) {
            for (int i = 0; i < vertices.Length; i++) {
                vertices[i] *= radius;
            }
        }

        Mesh mesh = new Mesh();
        mesh.name = "Octahedron Sphere";
        mesh.vertices = vertices;
        mesh.triangles = triangles;
        return mesh;

This creates an octahedron by first defining its bottom four triangles, moving between the forward, left, back, and right vertices, then adding the top four triangles in the same order.

While the octahedron will now show up when entering play mode, it will have weird shading. This is because we haven't given it normals yet. Right now, we can simply use the vertices directly as normals, if we do so before setting them to the desired radius. But in general, we'd have to normalize all vertices after subdividing our mesh. So let's add a Normalize method that does this and also fills an array of normals at the same time.

    private static void Normalize (Vector3[] vertices, Vector3[] normals) {
        for (int i = 0; i < vertices.Length; i++) {
            normals[i] = vertices[i] = vertices[i].normalized;
        }
    }

We have to call this method before applying the radius and allocate the normals array before that. Oh, and don't forget to assign it to the mesh.

        Vector3[] normals = new Vector3[vertices.Length];
        Normalize(vertices, normals);

        if (radius != 1f) {
            for (int i = 0; i < vertices.Length; i++) {
                vertices[i] *= radius;
            }
        }

        Mesh mesh = new Mesh();
        mesh.name = "Octahedron Sphere";
        mesh.vertices = vertices;
        mesh.normals = normals;
        mesh.triangles = triangles;
        return mesh;

octahedron pretending to be a sphere

Now we have an octahedron that's shaded as if it were a sphere. The next step is to define the texture coordinates. We'll use the same longitude-latitude mapping as used by Unity's sphere. While this is not a great way to texture a sphere, it's easy and a lot of textures are available in this format. To see what it looks like, create a material with a test image like this one.

uv test image

And here it is applied to Unity's UV sphere. It will always be messed up at the poles. You'll either have discontinuities or extreme distortion, as shown below. You can also download it here.

uv sphere with test image

As you can see, there's a seam where opposite sides of the texture join. We'll place this seam in the forward direction, so that when looking at our mesh along the Z axis we'll see the middle of the texture.

We can convert vertex positions into texture coordinates by using inverse trigonometric functions. The vertical coordinate is easiest, it's simply asin(y) / π + ½. The horizontal case is slightly more complex, atan2(x, z) / -2π. This actually produces a range that goes from 0 to ½ on the left side, then flips to -½ and goes back to 0 on the right side. So we have to add 1 if we end up with a negative result.

    private static void CreateUV (Vector3[] vertices, Vector2[] uv) {
        for (int i = 0; i < vertices.Length; i++) {
            Vector3 v = vertices[i];
            Vector2 textureCoordinates;
            textureCoordinates.x = Mathf.Atan2(v.x, v.z) / (-2f * Mathf.PI);
            if (textureCoordinates.x < 0f) {
                textureCoordinates.x += 1f;
            }
            textureCoordinates.y = Mathf.Asin(v.y) / Mathf.PI + 0.5f;
            uv[i] = textureCoordinates;
        }
    }

Now that we have a convenient CreateUV method, we can call it in Create after the vertices have been normalized and later assign the coordinates to our mesh.

        Vector3[] normals = new Vector3[vertices.Length];
        Normalize(vertices, normals);

        Vector2[] uv = new Vector2[vertices.Length];
        CreateUV(vertices, uv);

        if (radius != 1f) {
            for (int i = 0; i < vertices.Length; i++) {
                vertices[i] *= radius;
            }
        }

        Mesh mesh = new Mesh();
        mesh.name = "Octahedron Sphere";
        mesh.vertices = vertices;
        mesh.normals = normals;
        mesh.uv = uv;
        mesh.triangles = triangles;
        return mesh;

textured octahedron

We have now wrapped the texture around our octahedron, but it doesn't look very good. There are two problems. First, because the polar vertices don't rotate along with the other vertices the texture gets twisted into a circle, just like with Unity's UV sphere. To prevent this, we have to use four vertices per pole so we rotate them along with their triangles. This means we have to change our vertices and triangles in Create.

        Vector3[] vertices = {
            Vector3.down, Vector3.down, Vector3.down, Vector3.down,
            Vector3.forward,
            Vector3.left,
            Vector3.back,
            Vector3.right,
            Vector3.up, Vector3.up, Vector3.up, Vector3.up
        };

        int[] triangles = {
            0, 4, 5,
            1, 5, 6,
            2, 6, 7,
            3, 7, 4,

             8, 5, 4,
             9, 6, 5,
            10, 7, 6,
            11, 4, 7
        };

Then we have to manually adjust the horizontal coordinates of the polar vertices after we finish our loop in CreateUV. As they should match the horizontal middle of the triangles they're part of, they're offset by ⅛ and increase in steps of ¼.

        uv[vertices.Length - 4].x = uv[0].x = 0.125f;
        uv[vertices.Length - 3].x = uv[1].x = 0.375f;
        uv[vertices.Length - 2].x = uv[2].x = 0.625f;
        uv[vertices.Length - 1].x = uv[3].x = 0.875f;

duplicated polar vertices

We have now undone the twisting, at the cost of introducing discontinuities, as part of the texture is skipped in between triangles.

The second problem is that the texture is flipped in the fourth quadrant, because the texture wraps all the way back to zero. To solve this, we have to duplicate the forward vertex, creating a seam that is 0 at the left side and 1 at the right side. So our vertex and triangle code changes once again.

        Vector3[] vertices = {
            Vector3.down, Vector3.down, Vector3.down, Vector3.down,
            Vector3.forward,
            Vector3.left,
            Vector3.back,
            Vector3.right,
            Vector3.forward,
            Vector3.up, Vector3.up, Vector3.up, Vector3.up
        };

        int[] triangles = {
            0, 4, 5,
            1, 5, 6,
            2, 6, 7,
            3, 7, 8,

             9, 5, 4,
            10, 6, 5,
            11, 7, 6,
            12, 8, 7
        };

Now we have to update CreateUV so it assigns the correct coordinates to the right side of the seam. Assuming the vertices are placed by circling around the sphere, we can detect that we passed a seam by comparing X coordinates. If we get the same twice, we just started a new triangle row, which means that the previous vertex was the right side of the seam. Then all we have to do is set the horizontal texture coordinate of that vertex to 1. Here's the complete code of CreateUV.

    private static void CreateUV (Vector3[] vertices, Vector2[] uv) {
        float previousX = 1f;
        for (int i = 0; i < vertices.Length; i++) {
            Vector3 v = vertices[i];
            if (v.x == previousX) {
                uv[i - 1].x = 1f;
            }
            previousX = v.x;
            Vector2 textureCoordinates;
            textureCoordinates.x = Mathf.Atan2(v.x, v.z) / (-2f * Mathf.PI);
            if (textureCoordinates.x < 0f) {
                textureCoordinates.x += 1f;
            }
            textureCoordinates.y = Mathf.Asin(v.y) / Mathf.PI + 0.5f;
            uv[i] = textureCoordinates;
        }
        uv[vertices.Length - 4].x = uv[0].x = 0.125f;
        uv[vertices.Length - 3].x = uv[1].x = 0.375f;
        uv[vertices.Length - 2].x = uv[2].x = 0.625f;
        uv[vertices.Length - 1].x = uv[3].x = 0.875f;
    }

duplicated seam

Subdivided Octahedron

To subdivide our octahedron means we replace each of its triangles with four smaller triangles. Each time we do this, the number of vertices and triangles increases. So one triangle turns into 4^s or 2^(2s) triangles, where s is the number of subdivision steps. As we start with eight triangles, our subdivided octahedron will end up with 2^(2s + 3) triangles.

The number of vertices is a little more complicated. First consider the resolution r of one octahedron face, which determines how many triangle rows it has. As we start with a single row and double this amount with each subdivision, r = 2^s. Now consider a single quadrant of our octahedron, which consists of two triangles that share one edge. Without subdivisions, it has a total of 1 + 2 + 1 = 4 vertices. Subdividing once increases this to 1 + 2 + 3 + 2 + 1 = 9 vertices, the next step increases to 1 + 2 + 3 + 4 + 5 + 4 + 3 + 2 + 1 = 25 vertices, and so on. This means that one quadrant requires (r + 1)^2 vertices. The entire octahedron would have four times as many vertices, except that three of the quadrants share one vertex per row, of which there are 2r - 1. So the total number of vertices is 4(r + 1)^2 - 3(2r - 1). Here are the totals up to seven subdivision steps.

  1. 32 triangles, 27 vertices.
  2. 128 triangles, 79 vertices.
  3. 512 triangles, 279 vertices.
  4. 2,048 triangles, 1,063 vertices.
  5. 8,192 triangles, 4,167 vertices.
  6. 32,768 triangles, 16,519 vertices.
  7. 131,072 triangles, 65,799 verticles.

Because Unity uses 16-bit integers to index mesh data, it isn't able to handle seven subdivisions in a single mesh. So let Create begin by clamping the amount of subdivisions and log a warning when an unsupported amount is requested.

        if (subdivisions < 0) {
            subdivisions = 0;
            Debug.LogWarning("Octahedron Sphere subdivisions increased to minimum, which is 0.");
        }
        else if (subdivisions > 6) {
            subdivisions = 6;
            Debug.LogWarning("Octahedron Sphere subdivisions decreased to maximum, which is 6.");
        }

It's now time to remove the fixed vertex and triangle array and replace them with array declarations with the appropriate size. Because computer numbers are binary we can use the bit shifting operation 1 << n to compute 2^n. We then leave the filling of the arrays to a new method, CreateOctahedron. We pass it the arrays and the resolution, which is all it should need.

        int resolution = 1 << subdivisions;
        Vector3[] vertices = new Vector3[(resolution + 1) * (resolution + 1) * 4 -
            (resolution * 2 - 1) * 3];
        int[] triangles = new int[(1 << (subdivisions * 2 + 3)) * 3];
        CreateOctahedron(vertices, triangles, resolution);

Because subdividing our octahedron is a recursive problem, we could solve it with a recursive approach. But we can use an iterative approach as well, by layering counter-clockwise circles of triangle strips from bottom to top. This matches how our other methods expect vertices to be ordered, so let's do that.

We'll use an integer v to keep track of our current vertex index and increase it each time we add another vertex. So every vertex assignment should look like vertices[v++] = followed by some value. The first four bottom vertices are easy.

    private static void CreateOctahedron (Vector3[] vertices, int[] triangles, int resolution) {
        int v = 0;

        for (int i = 0; i < 4; i++) {
            vertices[v++] = Vector3.down;
        }
    }

Let's now focus on one octahedron face first, the one that connects the forward and left main directions. In fact, let's only consider the vertices along its left edge, the one going from bottom to forward. We already have the bottom vertex, what's left is to add one vertex per triangle row until we reach the top of the face.

        for (int i = 1; i <= resolution; i++) {
            float progress = (float)i / resolution;
            vertices[v++] = Vector3.Lerp(Vector3.down, Vector3.forward, progress);
        }

Of course, we need more than one vertex per row. We need entire lines of vertices, their length increasing with each step until they reach the other edge.

        for (int i = 1; i <= resolution; i++) {
            float progress = (float)i / resolution;
            Vector3 from, to;
            vertices[v++] = from = Vector3.Lerp(Vector3.down, Vector3.forward, progress);
            to = Vector3.Lerp(Vector3.down, Vector3.left, progress);
            v = CreateVertexLine(from, to, i, v, vertices);
        }

The convenient CreateVertexLine we just thought up is a simple method that adds vertices as it moves between two points and returns the next available vertex index.

    private static int CreateVertexLine (
        Vector3 from, Vector3 to, int steps, int v, Vector3[] vertices
    ) {
        for (int i = 1; i <= steps; i++) {
            vertices[v++] = Vector3.Lerp(from, to, (float)i / steps);
        }
        return v;
    }

To have CreateOctahedron add triangles we also have to keep track of the current triangle index, just like we're doing for vertices. We will be placing triangles strips in between two rows of vertices, so it's convenient if we also keep track of the first vertex index of the previous row. We name it vBottom because it defines the bottom of the triangle strip we're adding.

We'll also assume we have a CreateLowerStrip method to take care of creating a triangle strip for a bottom octahedron face. We invoke it before actually defining the vertices it will use, because at that point we know the starting indices of the bottom and top vertex rows that it needs.

        int v = 0, vBottom = 0, t = 0;

        for (int i = 0; i < 4; i++) {
            vertices[v++] = Vector3.down;
        }

        for (int i = 1; i <= resolution; i++) {
            float progress = (float)i / resolution;
            Vector3 from, to;
            vertices[v++] = from = Vector3.Lerp(Vector3.down, Vector3.forward, progress);
            to = Vector3.Lerp(Vector3.down, Vector3.left, progress);
            t = CreateLowerStrip(i, v, vBottom, t, triangles);
            v = CreateVertexLine(from, to, i, v, vertices);
            vBottom = v - 1 - i;
        }

If we only consider the bottom row, there's only one downward-pointing triangle that CreateLowerStrip has to worry about.

    private static int CreateLowerStrip (int steps, int vTop, int vBottom, int t, int[] triangles) {
        triangles[t++] = vBottom;
        triangles[t++] = vTop - 1;
        triangles[t++] = vTop;
        return t;
    }

However, this gives us only one triangle per row. Each step before the last should add two more triangles, switching between pointing downward and pointing upward, and incrementing both vertex indices.

    private static int CreateLowerStrip (int steps, int vTop, int vBottom, int t, int[] triangles) {
        for (int i = 1; i < steps; i++) {
            triangles[t++] = vBottom;
            triangles[t++] = vTop - 1;
            triangles[t++] = vTop;

            triangles[t++] = vBottom++;
            triangles[t++] = vTop++;
            triangles[t++] = vBottom;
        }
        triangles[t++] = vBottom;
        triangles[t++] = vTop - 1;
        triangles[t++] = vTop;
        return t;
    }

Now you should be able to see the first face of the subdivided octahedron. You might need to rotate the view to find it.

no subdivisions one subdivision two subdivisions

We actually want to complete a full circle each step instead of filling only one quadrant. To be able to easily loop over all four directions, create a static array to hold them.

    private static Vector3[] directions = {
        Vector3.left,
        Vector3.back,
        Vector3.right,
        Vector3.forward
    };

Instead of adding a vertex line and triangle strip only once per row, we actually have to do this four times. We have to advance vBottom per quadrant to keep it aligned. We have to add i - 1 to it, except for the first row where it has to be 1 not 0, as those vertices are duplicated. After completing each circle we can simply reset vBottom by subtracting that circle's length from the current vertex index.

        for (int i = 1; i <= resolution; i++) {
            float progress = (float)i / resolution;
            Vector3 from, to;
            vertices[v++] = to = Vector3.Lerp(Vector3.down, Vector3.forward, progress);
            for (int d = 0; d < 4; d++) {
                from = to;
                to = Vector3.Lerp(Vector3.down, directions[d], progress);
                t = CreateLowerStrip(i, v, vBottom, t, triangles);
                v = CreateVertexLine(from, to, i, v, vertices);
                vBottom += i > 1 ? (i - 1) : 1;
            }
            vBottom = v - 1 - i * 4;
        }

Now the entire bottom hemisphere should be generated.

bottom hemisphere

To include the top hemisphere, add a similar loop below the one for the bottom hemisphere. In this case we'll be decreasing the number of steps per row, so we iterate in the opposite direction. Also, because the top four vertices are special, we won't include the top row, so there's no special case for vBottom either.

        for (int i = resolution - 1; i >= 1; i--) {
            float progress = (float)i / resolution;
            Vector3 from, to;
            vertices[v++] = to = Vector3.Lerp(Vector3.up, Vector3.forward, progress);
            for (int d = 0; d < 4; d++) {
                from = to;
                to = Vector3.Lerp(Vector3.up, directions[d], progress);
                t = CreateUpperStrip(i, v, vBottom, t, triangles);
                v = CreateVertexLine(from, to, i, v, vertices);
                vBottom += i + 1;
            }
            vBottom = v - 1 - i * 4;
        }

The CreateUpperStrip method basically acts as the opposite of CreateLowerStrip.

    private static int CreateUpperStrip (int steps, int vTop, int vBottom, int t, int[] triangles) {
        triangles[t++] = vBottom;
        triangles[t++] = vTop - 1;
        triangles[t++] = ++vBottom;
        for (int i = 1; i <= steps; i++) {
            triangles[t++] = vTop - 1;
            triangles[t++] = vTop;
            triangles[t++] = vBottom;

            triangles[t++] = vBottom;
            triangles[t++] = vTop++;
            triangles[t++] = ++vBottom;
        }
        return t;
    }

Right now, only the top row is still missing, which is solved by adding a small loop at the end of CreateOctahedron. Here is the entire method.

    private static void CreateOctahedron (Vector3[] vertices, int[] triangles, int resolution) {
        int v = 0, vBottom = 0, t = 0;

        for (int i = 0; i < 4; i++) {
            vertices[v++] = Vector3.down;
        }

        for (int i = 1; i <= resolution; i++) {
            float progress = (float)i / resolution;
            Vector3 from, to;
            vertices[v++] = to = Vector3.Lerp(Vector3.down, Vector3.forward, progress);
            for (int d = 0; d < 4; d++) {
                from = to;
                to = Vector3.Lerp(Vector3.down, directions[d], progress);
                t = CreateLowerStrip(i, v, vBottom, t, triangles);
                v = CreateVertexLine(from, to, i, v, vertices);
                vBottom += i > 1 ? (i - 1) : 1;
            }
            vBottom = v - 1 - i * 4;
        }

        for (int i = resolution - 1; i >= 1; i--) {
            float progress = (float)i / resolution;
            Vector3 from, to;
            vertices[v++] = to = Vector3.Lerp(Vector3.up, Vector3.forward, progress);
            for (int d = 0; d < 4; d++) {
                from = to;
                to = Vector3.Lerp(Vector3.up, directions[d], progress);
                t = CreateUpperStrip(i, v, vBottom, t, triangles);
                v = CreateVertexLine(from, to, i, v, vertices);
                vBottom += i + 1;
            }
            vBottom = v - 1 - i * 4;
        }

        for (int i = 0; i < 4; i++) {
            triangles[t++] = vBottom;
            triangles[t++] = v;
            triangles[t++] = ++vBottom;
            vertices[v++] = Vector3.up;
        }
    }

Now the entire subdivided octahedron should show up when you enter play mode. Although the texture is of course different when viewed from above and from the front, the shape of the mesh is identical.

top hemisphere front hemisphere

Texturing and Tangents

So how does the octahedron sphere perform when used for something practical, for example to represent a planet? Here's an example texture for a planet, both a diffuse with specular map and a normal map. You only see the water because the alpha channel is used for the specular strength, which is set to 1 for water and 0 for land. You can download larger versions here.

texture, diffuse and specular texture, normals

Here's our sphere with a diffuse material, specular material, and bumped specular material. A directional light was added as well to get more interesting lighting.

diffuse specular bumped specular, without tangents

The bumped specular version looks wrong, because we haven't given our mesh tangent vectors yet, which are needed to work with normal maps. So let's add a CreateTangents method.

The tangents are simply unit-length vectors that point along the horizontal texture direction as it is wrapped around our sphere. So they should follow flat counter-clockwise circles per vertex row. We can generate that from vertex positions by taking their X and Z coordinates, normalizing them to get the horizontal direction from the center, then rotating that 90 degrees counter-clockwise. The rotation can be done by simply converting (x, 0, z) into (-z, 0, x).

    private static void CreateTangents (Vector3[] vertices, Vector4[] tangents) {
        for (int i = 0; i < vertices.Length; i++) {
            Vector3 v = vertices[i];
            v.y = 0f;
            v = v.normalized;
            Vector3 tangent;
            tangent.x = -v.z;
            tangent.y = 0f;
            tangent.z = v.x;
            tangents[i] = tangent;
        }
    }

However, Unity uses 4D vectors to define tangents. The fourth component is used to determine how the normal and tangent should be combined to construct a 3D space in which the normal map is placed. It can be either 1 or -1. Because of the order in which Unity combines the normal and tangent, we have to use -1.

    private static void CreateTangents (Vector3[] vertices, Vector4[] tangents) {
        for (int i = 0; i < vertices.Length; i++) {
            Vector3 v = vertices[i];
            v.y = 0f;
            v = v.normalized;
            Vector4 tangent;
            tangent.x = -v.z;
            tangent.y = 0f;
            tangent.z = v.x;
            tangent.w = -1f;
            tangents[i] = tangent;
        }
    }

As this approach does not work for the polar vertices, we once again define them separately, after the loop.

        tangents[vertices.Length - 4] = tangents[0] = new Vector3(-1f, 0, -1f).normalized;
        tangents[vertices.Length - 3] = tangents[1] = new Vector3(1f, 0f, -1f).normalized;
        tangents[vertices.Length - 2] = tangents[2] = new Vector3(1f, 0f, 1f).normalized;
        tangents[vertices.Length - 1] = tangents[3] = new Vector3(-1f, 0f, 1f).normalized;
        for (int i = 0; i < 4; i++) {
            tangents[vertices.Length - 1 - i].w = tangents[i].w = -1f;
        }

All that's left to do is have Create generate the tangents and add them to the mesh.

        Vector4[] tangents = new Vector4[vertices.Length];
        CreateTangents(vertices, tangents);

        if (radius != 1f) {
            for (int i = 0; i < vertices.Length; i++) {
                vertices[i] *= radius;
            }
        }

        Mesh mesh = new Mesh();
        mesh.name = "Octahedron Sphere";
        mesh.vertices = vertices;
        mesh.normals = normals;
        mesh.uv = uv;
        mesh.tangents = tangents;
        mesh.triangles = triangles;
        return mesh;

Now we can produce a properly shaded planet.

bumped specular, with tangents

Comparing Spheres

So how does an octahedron sphere planet look compared to a UV sphere planet? Let's first examine Unity's UV sphere. It has 525 vertices and 760 triangles. It looks pretty good when viewed from the side, but its silhouette is a lot rougher when looking from above. And there is a large circular distortion visible at the pole.

uv sphere, top uv sphere, front

Let's compare it with an octahedron sphere that has been subdivided three times, which has 279 vertices and 512 triangles. That's 37% and 67% of what the UV sphere has. It has a slightly better silhouette when viewed from above and looks a bit rougher when viewed from aside. The radial distortion is gone, instead we have a smaller diamond-shaped artifact.

oct sphere 3, top oct sphere 3, front

What about if we use four subdivisions? In that case we have 1,063 (202%) vertices and 2,048 triangles (269%). This gives us a much better silhouette and the radius of the polar distortion has been roughly halved.

cot sphere 4, top cot sphere 4, front

How many subdivision steps you really need depends on how large the planet appears. If you're close you might need all six, while far away you can suffice with only two. You could use some LOD system to always pick the best option, either Unity Pro's or a custom one.

Mesh Assets

So far we have been using a test component to generate our meshes in play mode, but it would be very convenient to have them available like any other mesh asset. You can use an editor script to save meshes as asset files. Here is a simple wizard that allows you to configure and save an octahedron sphere mesh in a Unity asset file. The script should to be placed in an Editor folder.

using UnityEditor;
using UnityEngine;

public class OctahedronSphereWizard : ScriptableWizard {

    [MenuItem("Assets/Create/Octahedron Sphere")]
    private static void CreateWizard () {
        ScriptableWizard.DisplayWizard<OctahedronSphereWizard>("Create Octahedron Sphere");
    }

    [Range(0, 6)]
    public int level = 6;
    public float radius = 1f;

    private void OnWizardCreate () {
        string path = EditorUtility.SaveFilePanelInProject(
            "Save Octahedron Sphere",
            "Octahedron Sphere",
            "asset",
            "Specify where to save the mesh.");
        if (path.Length > 0) {
            Mesh mesh = OctahedronSphereCreator.Create(level, radius);
            MeshUtility.Optimize(mesh);
            AssetDatabase.CreateAsset(mesh, path);
            Selection.activeObject = mesh;
        }
    }
}

Enjoy your octahedron spheres!

The finished project and mesh assets can be downloaded here.

3 comments


Or enter your name and Email
  • B Bella_lv 9 months ago
    hi ,good Tutorial,but i am doubt about the Subdivided Octahedron,1step for example,i think it is 32 triangles & 18 Vertices,why are 27 vertices
  • JJ Jeff Johnson 2 years ago
    The north pole (4 triangles) has the wrong uv coordinates. Any suggestions on how to fix
  • G gbsr 2 years ago
    This is awesome. Thanks!