Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Sphere out of planes

Discussion in 'Scripting' started by Josenifftodd, Mar 29, 2016.

  1. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    Hello, so I'm trying to create my own sphere cube seen as the tutorials on the net just mess up when trying to apply perlin to them.

    So far I have this, where do I want to go from here? It makes a flat circle. I'm not 100% on how I'm going to make the Y make it rounded.


    Code (CSharp):
    1. void Start () {
    2. Mesh mesh = GetComponent<MeshFilter>().mesh;
    3. Vector3[] vertices = mesh.vertices;
    4. for (var i=0; i<vertices.Length; i++) {
    5. vertices[i] = vertices[i].normalized*radius;
    6. }
    7. mesh.vertices = vertices;
    8. mesh.RecalculateNormals();
    9. mesh.RecalculateBounds ();
    10. }
    11. }
    and this is my mesh generator.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public static class MeshGenerator
    5. {
    6.  
    7.     public static MeshData GenerateTerrainMesh(float[,] heightMap, float heightMultiplier, AnimationCurve heightCurve, int levelOfDetail)
    8.     {
    9.         int width = heightMap.GetLength(0);
    10.         int height = heightMap.GetLength(1);
    11.         float topLeftX = (width - 1) / -2f;
    12.         float topLeftZ = (height - 1) / 2f;
    13.  
    14.         int meshSimplificationIncrement = (levelOfDetail == 0) ? 1 : levelOfDetail * 2;
    15.         int verticesPerLine = (width - 1) / meshSimplificationIncrement + 1;
    16.  
    17.         MeshData meshData = new MeshData(verticesPerLine, verticesPerLine);
    18.         int vertexIndex = 0;
    19.  
    20.         for (int y = 0; y < height; y += meshSimplificationIncrement)
    21.         {
    22.             for (int x = 0; x < width; x += meshSimplificationIncrement)
    23.             {
    24.                 meshData.vertices[vertexIndex] = new Vector3(topLeftX + x, heightCurve.Evaluate(heightMap[x, y]) * heightMultiplier, topLeftZ - y);
    25.                 meshData.uvs[vertexIndex] = new Vector2(x / (float)width, y / (float)height);
    26.  
    27.                 if (x < width - 1 && y < height - 1)
    28.                 {
    29.                     meshData.AddTriangle(vertexIndex, vertexIndex + verticesPerLine + 1, vertexIndex + verticesPerLine);
    30.                     meshData.AddTriangle(vertexIndex + verticesPerLine + 1, vertexIndex, vertexIndex + 1);
    31.                 }
    32.  
    33.                 vertexIndex++;
    34.             }
    35.         }
    36.  
    37.         return meshData;
    38.  
    39.     }
    40. }
    41.  
    42. public class MeshData
    43. {
    44.     public Vector3[] vertices;
    45.     public int[] triangles;
    46.     public Vector2[] uvs;
    47.  
    48.     int triangleIndex;
    49.  
    50.     public MeshData(int meshWidth, int meshHeight)
    51.     {
    52.         vertices = new Vector3[meshWidth * meshHeight];
    53.         uvs = new Vector2[meshWidth * meshHeight];
    54.         triangles = new int[(meshWidth - 1) * (meshHeight - 1) * 6];
    55.     }
    56.  
    57.     public void AddTriangle(int a, int b, int c)
    58.     {
    59.         triangles[triangleIndex] = a;
    60.         triangles[triangleIndex + 1] = b;
    61.         triangles[triangleIndex + 2] = c;
    62.         triangleIndex += 3;
    63.     }
    64.  
    65.     public Mesh CreateMesh()
    66.     {
    67.         Mesh mesh = new Mesh();
    68.         mesh.vertices = vertices;
    69.         mesh.triangles = triangles;
    70.         mesh.uv = uvs;
    71.         mesh.RecalculateNormals();
    72.         return mesh;
    73.     }
    74.  
    75. }
    All I'm trying to do is make it so I can generate planets but it doesn't seem like anyone wants to help or know what the hell I'm suppose to do...
     
  2. XukeLho

    XukeLho

    Joined:
    Dec 30, 2014
    Posts:
    199
  3. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    Cheers but the only problem with that is that the poles aren't right according to the comments so when I use perlinNoise it'll just go crazy :( I do have a script for making a sphere out of planes, then when I apply my perlin to it, it does raise the terrain but it has gaps all around it and deforms my sphere, doesn't seem to want to keep the shape it starts off with. :/
     
  4. XukeLho

    XukeLho

    Joined:
    Dec 30, 2014
    Posts:
    199
    can you show a print of what you have and another of what you want (aprox)
     
  5. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    The script I attached above, the mesh is generated from perlin noise and then it it makes a (plane) mesh which is converted into a terrain (this is all done in editor including colors) then I add my sphere script to the 'Terrain Plane' then this happens. It is getting to the shape I want it's just so deformed my head is gunna explode lol.

    Pic is attached below.
     
  6. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    I ideally want to make ^^ into a sphere that has the terrain inputted onto it which is generated onto the mesh, I can show you how my terrain is generated if you want with an image or what it looks like before I press play and the sphere script kicks in.
     
  7. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    As stated above, here is how I start off, my terrain is generated like so inside the editor, then I add the sphere script and boosh it does what the image above is showing lol.

    I don't even mind if it's all the same terrain all around the sphere as long as I can somehow get the sphere to look like a 3d planet that can be exploded. I have my Flaux Gravity and Spacecraft ready.

    the sphere in the game scene is just a test to see if the poles messed up which they did, but im thinking of using it as a sphere for if distance is miles away. :) Hope you can help pal & hopefully I've explained everything properly, like I said the sphere script seems to be wanting to make a sphere it just isn't :(

     
  8. XukeLho

    XukeLho

    Joined:
    Dec 30, 2014
    Posts:
    199
    So, you have a plane and want to transform that into a sphere? is that it?
     
  9. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    Pretty much but I want it to keep the terrain style thats generated so it generates a planet from the plane... if that makes sense? So how I generate my terrain using a plane, I want to then be able to attach a sphere script which then makes it into a planet that has mountains etc like on the plane thats shown above. :) don't so much need a script making for me but more directing in the right directions with what I should be using to get my result I want. :)
     
  10. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    I do have this script that makes a sphere but I can't seem to make it so it's all one color it's 3 different colors. (basic I know but I haven't really looked into colors yet been focusing on getting perlin noise to do its main part)
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. [RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
    4. public class CubeSphere : MonoBehaviour
    5. {
    6.     public int gridSize;
    7.     public float radius = 1f;
    8.     private Mesh mesh;
    9.     public Vector3[] vertices;
    10.     public Vector3[] normals;
    11.     public Color32[] cubeUV;
    12.     private void Awake()
    13.     {
    14.         Generate();
    15.     }
    16.     private void Generate()
    17.     {
    18.         GetComponent<MeshFilter>().mesh = mesh = new Mesh();
    19.         mesh.name = "Procedural Sphere";
    20.         CreateVertices();
    21.         CreateTriangles();
    22.         CreateColliders();
    23.     }
    24.     private void CreateVertices()
    25.     {
    26.         int cornerVertices = 8;
    27.         int edgeVertices = (gridSize + gridSize + gridSize - 3) * 4;
    28.         int faceVertices = (
    29.             (gridSize - 1) * (gridSize - 1) +
    30.             (gridSize - 1) * (gridSize - 1) +
    31.             (gridSize - 1) * (gridSize - 1)) * 2;
    32.         vertices = new Vector3[cornerVertices + edgeVertices + faceVertices];
    33.         normals = new Vector3[vertices.Length];
    34.         cubeUV = new Color32[vertices.Length];
    35.         int v = 0;
    36.         for (int y = 0; y <= gridSize; y++)
    37.         {
    38.             for (int x = 0; x <= gridSize; x++)
    39.             {
    40.                 SetVertex(v++, x, y, 0);
    41.             }
    42.             for (int z = 1; z <= gridSize; z++)
    43.             {
    44.                 SetVertex(v++, gridSize, y, z);
    45.             }
    46.             for (int x = gridSize - 1; x >= 0; x--)
    47.             {
    48.                 SetVertex(v++, x, y, gridSize);
    49.             }
    50.             for (int z = gridSize - 1; z > 0; z--)
    51.             {
    52.                 SetVertex(v++, 0, y, z);
    53.             }
    54.         }
    55.         for (int z = 1; z < gridSize; z++)
    56.         {
    57.             for (int x = 1; x < gridSize; x++)
    58.             {
    59.                 SetVertex(v++, x, gridSize, z);
    60.             }
    61.         }
    62.         for (int z = 1; z < gridSize; z++)
    63.         {
    64.             for (int x = 1; x < gridSize; x++)
    65.             {
    66.                 SetVertex(v++, x, 0, z);
    67.             }
    68.         }
    69.         mesh.vertices = vertices;
    70.         mesh.normals = normals;
    71.         mesh.colors32 = cubeUV;
    72.     }
    73.     private void SetVertex(int i, int x, int y, int z)
    74.     {
    75.         Vector3 v = new Vector3(x, y, z) * 2f / gridSize - Vector3.one;
    76.         float x2 = v.x * v.x;
    77.         float y2 = v.y * v.y;
    78.         float z2 = v.z * v.z;
    79.         Vector3 s;
    80.         s.x = v.x * Mathf.Sqrt(1f - y2 / 2f - z2 / 2f + y2 * z2 / 3f);
    81.         s.y = v.y * Mathf.Sqrt(1f - x2 / 2f - z2 / 2f + x2 * z2 / 3f);
    82.         s.z = v.z * Mathf.Sqrt(1f - x2 / 2f - y2 / 2f + x2 * y2 / 3f);
    83.         normals[i] = s;
    84.         vertices[i] = normals[i] * radius;
    85.         cubeUV[i] = new Color32((byte)x, (byte)y, (byte)z, 0);
    86.     }
    87.     private void CreateTriangles()
    88.     {
    89.         int[] trianglesZ = new int[(gridSize * gridSize) * 12];
    90.         int[] trianglesX = new int[(gridSize * gridSize) * 12];
    91.         int[] trianglesY = new int[(gridSize * gridSize) * 12];
    92.         int ring = (gridSize + gridSize) * 2;
    93.         int tZ = 0, tX = 0, tY = 0, v = 0;
    94.         for (int y = 0; y < gridSize; y++, v++)
    95.         {
    96.             for (int q = 0; q < gridSize; q++, v++)
    97.             {
    98.                 tZ = SetQuad(trianglesZ, tZ, v, v + 1, v + ring, v + ring + 1);
    99.             }
    100.             for (int q = 0; q < gridSize; q++, v++)
    101.             {
    102.                 tX = SetQuad(trianglesX, tX, v, v + 1, v + ring, v + ring + 1);
    103.             }
    104.             for (int q = 0; q < gridSize; q++, v++)
    105.             {
    106.                 tZ = SetQuad(trianglesZ, tZ, v, v + 1, v + ring, v + ring + 1);
    107.             }
    108.             for (int q = 0; q < gridSize - 1; q++, v++)
    109.             {
    110.                 tX = SetQuad(trianglesX, tX, v, v + 1, v + ring, v + ring + 1);
    111.             }
    112.             tX = SetQuad(trianglesX, tX, v, v - ring + 1, v + ring, v + 1);
    113.         }
    114.         tY = CreateTopFace(trianglesY, tY, ring);
    115.         tY = CreateBottomFace(trianglesY, tY, ring);
    116.         mesh.subMeshCount = 3;
    117.         mesh.SetTriangles(trianglesZ, 0);
    118.         mesh.SetTriangles(trianglesX, 1);
    119.         mesh.SetTriangles(trianglesY, 2);
    120.     }
    121.     private int CreateTopFace(int[] triangles, int t, int ring)
    122.     {
    123.         int v = ring * gridSize;
    124.         for (int x = 0; x < gridSize - 1; x++, v++)
    125.         {
    126.             t = SetQuad(triangles, t, v, v + 1, v + ring - 1, v + ring);
    127.         }
    128.         t = SetQuad(triangles, t, v, v + 1, v + ring - 1, v + 2);
    129.         int vMin = ring * (gridSize + 1) - 1;
    130.         int vMid = vMin + 1;
    131.         int vMax = v + 2;
    132.         for (int z = 1; z < gridSize - 1; z++, vMin--, vMid++, vMax++)
    133.         {
    134.             t = SetQuad(triangles, t, vMin, vMid, vMin - 1, vMid + gridSize - 1);
    135.             for (int x = 1; x < gridSize - 1; x++, vMid++)
    136.             {
    137.                 t = SetQuad(
    138.                     triangles, t,
    139.                     vMid, vMid + 1, vMid + gridSize - 1, vMid + gridSize);
    140.             }
    141.             t = SetQuad(triangles, t, vMid, vMax, vMid + gridSize - 1, vMax + 1);
    142.         }
    143.         int vTop = vMin - 2;
    144.         t = SetQuad(triangles, t, vMin, vMid, vTop + 1, vTop);
    145.         for (int x = 1; x < gridSize - 1; x++, vTop--, vMid++)
    146.         {
    147.             t = SetQuad(triangles, t, vMid, vMid + 1, vTop, vTop - 1);
    148.         }
    149.         t = SetQuad(triangles, t, vMid, vTop - 2, vTop, vTop - 1);
    150.         return t;
    151.     }
    152.     private int CreateBottomFace(int[] triangles, int t, int ring)
    153.     {
    154.         int v = 1;
    155.         int vMid = vertices.Length - (gridSize - 1) * (gridSize - 1);
    156.         t = SetQuad(triangles, t, ring - 1, vMid, 0, 1);
    157.         for (int x = 1; x < gridSize - 1; x++, v++, vMid++)
    158.         {
    159.             t = SetQuad(triangles, t, vMid, vMid + 1, v, v + 1);
    160.         }
    161.         t = SetQuad(triangles, t, vMid, v + 2, v, v + 1);
    162.         int vMin = ring - 2;
    163.         vMid -= gridSize - 2;
    164.         int vMax = v + 2;
    165.         for (int z = 1; z < gridSize - 1; z++, vMin--, vMid++, vMax++)
    166.         {
    167.             t = SetQuad(triangles, t, vMin, vMid + gridSize - 1, vMin + 1, vMid);
    168.             for (int x = 1; x < gridSize - 1; x++, vMid++)
    169.             {
    170.                 t = SetQuad(
    171.                     triangles, t,
    172.                     vMid + gridSize - 1, vMid + gridSize, vMid, vMid + 1);
    173.             }
    174.             t = SetQuad(triangles, t, vMid + gridSize - 1, vMax + 1, vMid, vMax);
    175.         }
    176.         int vTop = vMin - 1;
    177.         t = SetQuad(triangles, t, vTop + 1, vTop, vTop + 2, vMid);
    178.         for (int x = 1; x < gridSize - 1; x++, vTop--, vMid++)
    179.         {
    180.             t = SetQuad(triangles, t, vTop, vTop - 1, vMid, vMid + 1);
    181.         }
    182.         t = SetQuad(triangles, t, vTop, vTop - 1, vMid, vTop - 2);
    183.         return t;
    184.     }
    185.     private static int
    186.     SetQuad(int[] triangles, int i, int v00, int v10, int v01, int v11)
    187.     {
    188.         triangles[i] = v00;
    189.         triangles[i + 1] = triangles[i + 4] = v01;
    190.         triangles[i + 2] = triangles[i + 3] = v10;
    191.         triangles[i + 5] = v11;
    192.         return i + 6;
    193.     }
    194.     private void CreateColliders()
    195.     {
    196.         gameObject.AddComponent<SphereCollider>();
    197.     }
    198.     internal class SerializedFieldAttribute : Attribute
    199.     {
    200.     }
    201. }
     
  11. XukeLho

    XukeLho

    Joined:
    Dec 30, 2014
    Posts:
    199
    yea, now that you've explained it its a prety tough thing to do.
    You generate the terrain on a plain and then want to wrap it around a sphere. Let me think about it. No promises though
     
  12. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    No worries pal any help will help :p haha, I'll keep messing about with a few things and see how it works.
    The script I attached above if I could make that so it's one texture all around the planet sphere that probably will be the direction I need to go in as I could apply the perlin noise to all the vertices on the sphere but It displays like this...

     
  13. XukeLho

    XukeLho

    Joined:
    Dec 30, 2014
    Posts:
    199
  14. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    Already checked that out days ago lol the script doesn't do what the guy says, it don't even make a sphere :/ and a cube is in the inspector but they isn't anything for the cube in the script, so not even sure why he declared it...
     
  15. XukeLho

    XukeLho

    Joined:
    Dec 30, 2014
    Posts:
    199
    my bad