Search Unity

Mapping a cube to a sphere

Discussion in 'Scripting' started by aubergine, Dec 6, 2010.

  1. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,878
    Hello,

    According to the formula in this site, the following code should work. But i get funny results as the result is not a sphere but more like an oval. Can anyone see what the problem is?

    I am using an average box object with 10 subdivisions on every side exported from 3d max.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlanetLOD : MonoBehaviour {
    5.  
    6.     Mesh m;
    7.    
    8.     // Use this for initialization
    9.     void Start () {
    10.         m = GetComponent<MeshFilter>().mesh;
    11.        
    12.         Vector3[] vertices = m.vertices;
    13.         Vector3[] verticesN = m.vertices;
    14.         Vector3[] normals = m.normals;
    15.        
    16.         for (int i = 0; i < vertices.Length; i++) {
    17.             verticesN[i].x = SphericalX (vertices[i].x, vertices[i].y, vertices[i].z);
    18.             verticesN[i].y = SphericalY (vertices[i].x, vertices[i].y, vertices[i].z);
    19.             verticesN[i].z = SphericalZ (vertices[i].x, vertices[i].y, vertices[i].z);
    20.         }
    21.         m.vertices = verticesN;
    22.         m.RecalculateNormals();
    23.     }
    24.  
    25.    
    26.     float SphericalX (float x, float y, float z) {
    27.         return x * Mathf.Sqrt ( 1.0f - y * y * 0.5f - z * z * 0.5f + y * y * z * z / 3.0f);
    28.     }
    29.     float SphericalY (float x, float y, float z) {
    30.         return y * Mathf.Sqrt ( 1.0f - z * z - 0.5f - x * x * 0.5f + z * z * x * x / 3.0f);
    31.     }
    32.     float SphericalZ (float x, float y, float z) {
    33.         return z * Mathf.Sqrt ( 1.0f - x * x - 0.5f - y * y * 0.5f + x * x * y * y / 3.0f);
    34.     }
    35. }

    EDIT: Attached is a photo of the problem.
     

    Attached Files:

    Last edited: Dec 6, 2010
  2. fededevi

    fededevi

    Joined:
    Jun 11, 2010
    Posts:
    29
    I don't know why it doesnt work but I think it is much easier to transform the position with this formula (assuming cube is centered in (0,0,0)):

    float Spherical (Vector3 vertex)
    {
    return vertex.normalized * radius;
    }
     
    Last edited: Dec 6, 2010
  3. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,878
    Okay, this code works. But still not quite right like a total sphere. I think im missing some scale thing here.
    Can anyone help?

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlanetLOD : MonoBehaviour {
    5.  
    6.     Mesh m;
    7.    
    8.     // Use this for initialization
    9.     void Start () {
    10.         m = GetComponent<MeshFilter>().mesh;
    11.        
    12.         Vector3[] vertices = m.vertices;
    13.         Vector3[] verticesN = m.vertices;
    14.         Vector3[] normals = m.normals;
    15.        
    16.         for (int i = 0; i < vertices.Length; i++) {
    17.             verticesN[i].x = SphericalX (vertices[i].x, vertices[i].y, vertices[i].z) *1.3F;
    18.             verticesN[i].y = SphericalY (vertices[i].x, vertices[i].y, vertices[i].z) *1.3F;
    19.             verticesN[i].z = SphericalZ (vertices[i].x, vertices[i].y, vertices[i].z) *1.3F;
    20.         }
    21.         m.vertices = verticesN;
    22.         m.RecalculateNormals();
    23.  
    24.  
    25.     }
    26.    
    27.    
    28.     float SphericalX (float x, float y, float z) {
    29.         return x * Mathf.Sqrt (1.0F - y*y * 0.5F - z*z * 0.5F + y*y*z*z/3.0F);
    30.     }
    31.     float SphericalY (float x, float y, float z) {
    32.         return y * Mathf.Sqrt (1.0F - z*z * 0.5F - x*x * 0.5F + z*z*x*x/3.0F);
    33.     }
    34.     float SphericalZ (float x, float y, float z) {
    35.         return z * Mathf.Sqrt (1.0F - x*x * 0.5F - y*y * 0.5F + x*x*y*y/3.0F);
    36.     }
    37. }
     
  4. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,878
    Well, the formula itself is supposed to return a normalized * radius result. There is somethin else.

    EDIT: Oh sorry, now i understand what you mean :) But i need to use this formula.
     
    Last edited: Dec 6, 2010