Search Unity

How to generate cylinder through code c#?

Discussion in 'Scripting' started by Vinayak-VC, Oct 18, 2019.

  1. Vinayak-VC

    Vinayak-VC

    Joined:
    Apr 16, 2019
    Posts:
    60
    I am trying to generate a pie chart, but it can not be scale in Z-axis. Following is my code

    Code (CSharp):
    1.  using UnityEngine;
    2. using System.Collections;
    3. public class PieChartMesh : MonoBehaviour
    4. {
    5.      float[] mData;
    6.      int mSlices;
    7.      float mRotationAngle;
    8.      float mRadius;
    9.      Material[] mMaterials;
    10.      Vector3[] mVertices;
    11.      Vector3[] mNormals;
    12.      Vector3[] mWRNormals;
    13.      Vector3 mNormal = new Vector3(0f, 0f, -1f);
    14.      public Vector3 mWRNormal = new Vector3(1, 1, 1);
    15.      Vector2[] mUvs;
    16.      int[] mTriangles;
    17.      MeshRenderer mMeshRenderer;
    18.      float delay = 0.1f;
    19.      GameObject TempObject;
    20.      public void Init(float[] data, int slices, float rotatioAngle, float radius, Material[] materials, float speed, GameObject otherobject)
    21.      {
    22.          TempObject = otherobject;
    23.          mData = data;
    24.          mSlices = slices;
    25.          mRotationAngle = rotatioAngle;
    26.          mRadius = radius;
    27.          delay = speed;
    28.          // Get Mesh Renderer
    29.          mMeshRenderer = gameObject.GetComponent("MeshRenderer") as MeshRenderer;
    30.          if (mMeshRenderer == null)
    31.          {
    32.              gameObject.AddComponent<MeshRenderer>();
    33.              mMeshRenderer = gameObject.GetComponent("MeshRenderer") as MeshRenderer;
    34.          }
    35.          mMeshRenderer.materials = materials;
    36.          mMaterials = materials;
    37.          Init(data);
    38.      }
    39.      public void Init(float[] data)
    40.      {
    41.          mSlices = 100;
    42.          mRotationAngle = 90f;
    43.          mRadius = 0.3f;
    44.          mData = data;
    45.      }
    46.      public void Draw(float[] data)
    47.      {
    48.          mData = data;
    49.          StopAllCoroutines();
    50.          StartCoroutine(Draw());
    51.      }
    52.      public IEnumerator Draw()
    53.      {
    54.          //Check data validity for pie chart...
    55.          while (mData == null)
    56.          {
    57.              print("PieChart: Data null");
    58.              yield return null;
    59.          }
    60.          for (int i = 0; i < mData.Length; i++)
    61.          {
    62.              if (mData[i] < 0)
    63.              {
    64.                  print("PieChart: Data < 0");
    65.                  yield return null;
    66.              }
    67.          }
    68.          // Calculate sum of data values
    69.          float sumOfData = 0;
    70.          foreach (float value in mData)
    71.          {
    72.              sumOfData += value;
    73.          }
    74.          if (sumOfData <= 0)
    75.          {
    76.              print("PieChart: Data sum <= 0");
    77.              yield return null;
    78.          }
    79.          // Determine how many triangles in slice
    80.          int[] slice = new int[mData.Length];
    81.          int numOfTris = 0;
    82.          int numOfSlices = 0;
    83.          int countedSlices = 0;
    84.          // Caluclate slice size
    85.          for (int i = 0; i < mData.Length; i++)
    86.          {
    87.              numOfTris = (int)((mData[i] / sumOfData) * mSlices);
    88.              slice[numOfSlices++] = numOfTris;
    89.              countedSlices += numOfTris;
    90.          }
    91.          // Check that all slices are counted.. if not -> add/sub to/from biggest slice..
    92.          int idxOfLargestSlice = 0;
    93.          int largestSliceCount = 0;
    94.          for (int i = 0; i < mData.Length; i++)
    95.          {
    96.              if (largestSliceCount < slice[i])
    97.              {
    98.                  idxOfLargestSlice = i;
    99.                  largestSliceCount = slice[i];
    100.              }
    101.          }
    102.          // Check validity for pie chart
    103.          if (countedSlices == 0)
    104.          {
    105.              print("PieChart: Slices == 0");
    106.              yield return null;
    107.          }
    108.          // Adjust largest dataset to get proper slice
    109.          slice[idxOfLargestSlice] += mSlices - countedSlices;
    110.          // Check validity for pie chart data
    111.          if (slice[idxOfLargestSlice] <= 0)
    112.          {
    113.              print("PieChart: Largest pie <= 0");
    114.              yield return null;
    115.          }
    116.          // Init vertices and triangles arrays
    117.          mVertices = new Vector3[mSlices * 3];
    118.          mNormals = new Vector3[mSlices * 3];
    119.          mWRNormals = new Vector3[mSlices * 3];
    120.          mUvs = new Vector2[mSlices * 3];
    121.          mTriangles = new int[mSlices * 3];
    122.          //gameObject.AddComponent("MeshFilter");
    123.          //gameObject.AddComponent("MeshRenderer");
    124.          Mesh mesh = ((MeshFilter)GetComponent("MeshFilter")).mesh;
    125.          mesh.Clear();
    126.          mesh.name = "Pie Chart Mesh";
    127.          // Roration offset (to get star point to "12 o'clock")
    128.          float rotOffset = mRotationAngle / 360f * 2f * Mathf.PI;
    129.          // Calc the points in circle
    130.          float angle;
    131.          float[] x = new float[mSlices];
    132.          float[] y = new float[mSlices];
    133.          float[] z = new float[mSlices];
    134.          for (int i = 0; i < mSlices; i++)
    135.          {
    136.              angle = i * 2f * Mathf.PI / mSlices;
    137.              x[i] = (Mathf.Cos(angle + rotOffset) * mRadius);
    138.              y[i] = (Mathf.Sin(angle + rotOffset) * mRadius);
    139.              z[i] = (Mathf.Cos(angle ) * mRadius);
    140.            
    141.          }
    142.          // Generate mesh with slices (vertices and triangles)
    143.          for (int i = 0; i < mSlices; i++)
    144.          {
    145.              mVertices[i * 3 + 0] = new Vector3(0f, 0f, 0f);
    146.              mVertices[i * 3 + 1] = new Vector3(x[i], y[i], 0f);
    147.              // This will ensure that last vertex = first vertex..
    148.              mVertices[i * 3 + 2] = new Vector3(x[(i + 1) % mSlices], y[(i + 1) % mSlices], 0);
    149.              mNormals[i * 3 + 0] = mNormal;
    150.              mNormals[i * 3 + 1] = mNormal;
    151.              mNormals[i * 3 + 2] = mNormal;
    152.              mWRNormals[i * 3 + 0] = mWRNormal;
    153.              mWRNormals[i * 3 + 1] = mWRNormal;
    154.              mWRNormals[i * 3 + 2] = mWRNormal;
    155.              mUvs[i * 3 + 0] = new Vector2(0f, 0f);
    156.              mUvs[i * 3 + 1] = new Vector2(x[i], y[i]);
    157.              // This will ensure that last uv = first uv..
    158.              mUvs[i * 3 + 2] = new Vector2(x[(i + 1) % mSlices], y[(i + 1) % mSlices]);
    159.              mTriangles[i * 3 + 0] = i * 3 + 0;
    160.              mTriangles[i * 3 + 1] = i * 3 + 1;
    161.              mTriangles[i * 3 + 2] = i * 3 + 2;
    162.          }
    163.          // Assign verts, norms, uvs and tris to mesh and calc normals
    164.          mesh.vertices = mVertices;
    165.          mesh.normals = mNormals;
    166.          mesh.uv = mUvs;
    167.          mesh.uv2 = mUvs;
    168.          //mesh.triangles = triangles;
    169.          mesh.subMeshCount = mData.Length;
    170.          int[][] subTris = new int[mData.Length][];
    171.          countedSlices = 0;
    172.          // Set sub meshes
    173.          for (int i = 0; i < mData.Length; i++)
    174.          {
    175.              GameObject go = Instantiate(TempObject);
    176.              go.name = i.ToString();
    177.              Mesh gomesh = ((MeshFilter)go.GetComponent("MeshFilter")).mesh;
    178.              gomesh.vertices = mesh.vertices;
    179.              gomesh.normals = mWRNormals;
    180.              gomesh.uv = mesh.uv;
    181.              //gomesh.subMeshCount = mData.Length;
    182.              // Every triangle has three veritces..
    183.              subTris[i] = new int[slice[i] * 3];
    184.              // Add tris to subTris
    185.              for (int j = 0; j < slice[i]; j++)
    186.              {
    187.                  subTris[i][j * 3 + 0] = mTriangles[countedSlices * 3 + 0];
    188.                  subTris[i][j * 3 + 1] = mTriangles[countedSlices * 3 + 1];
    189.                  subTris[i][j * 3 + 2] = mTriangles[countedSlices * 3 + 2];
    190.                  if (j % 5 == 0)
    191.                      yield return new WaitForSeconds(delay);
    192.                  mesh.SetTriangles(subTris[i], i);
    193.                  gomesh.SetTriangles(subTris[i], 0, true);
    194.                  countedSlices++;
    195.              }
    196.              gomesh.Optimize();
    197.              gomesh.RecalculateNormals();
    198.          }
    199.      }
    200.      // Properties
    201.      public float[] Data { get { return mData; } set { mData = value; } }
    202.      public int Slices { get { return mSlices; } set { mSlices = value; } }
    203.      public float RotationAngle { get { return mRotationAngle; } set { mRotationAngle = value; } }
    204.      public float Radius { get { return mRadius; } set { mRadius = value; } }
    205.      public Material[] Materials { get { return mMaterials; } set { mMaterials = value; } }
    206. }
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    First, ignore Z axis completely. Generate two similar circles for top and bottom. After that add triangles between them to be the side cylinder surface. Imagine top and bottom circles have both 100 vertices except center point. This means vertex #100 is right above #0, so first triangle will be like 0, 100, 1, and second will be like 1, 100, 101.
     
    Vinayak-VC likes this.
  3. Vinayak-VC

    Vinayak-VC

    Joined:
    Apr 16, 2019
    Posts:
    60
    I did that here is the code and it's working fine


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CyliderGenerate : MonoBehaviour
    6. {
    7.     public MeshFilter mf;
    8.     public Mesh mesh;
    9.  
    10.     public GameObject cube;
    11.  
    12.     [Range(10, 20)]
    13.     public int iter;
    14.     int num;
    15.     public int leng;
    16.     Vector3[] vertices;
    17.     int[] tris;
    18.     int[] FinalTri;
    19.     int[] firstplane;
    20.     void Start()
    21.     {
    22.         MeshFilter mf = GetComponent<MeshFilter>();
    23.         mesh = new Mesh();
    24.         MakingVertices(1, iter, leng, 0.5f, 0.1f);
    25.     }
    26.  
    27.     void MakingVertices(int radius, int iterations, int lenggth, float gap, float noise)
    28.     {
    29.         float noise_x;
    30.         float noise_y;
    31.         float noise_z;
    32.         float x;
    33.         float y;
    34.         float z = 0;
    35.         int i;
    36.         int p = 0;
    37.         float angle;
    38.  
    39.         vertices = new Vector3[(iterations * lenggth) + 2];
    40.         int tempo = 0;
    41.         vertices[vertices.Length - 2] = Vector3.zero;
    42.  
    43.         while (p < lenggth)
    44.         {
    45.             i = 0;
    46.             while (i < iterations)
    47.             {
    48.                 angle = (i * 1.0f) / iterations * Mathf.PI * 2;
    49.                 x = Mathf.Sin(angle) * radius;
    50.                 y = Mathf.Cos(angle) * radius;
    51.                 vertices[tempo] = new Vector3(x, y, z);
    52.                 //GameObject go = Instantiate(cube, vertices[tempo], Quaternion.identity);
    53.                 //go.name = num.ToString();
    54.                 i++;
    55.                 num++;
    56.                 tempo += 1;
    57.             }
    58.             z += gap;
    59.             p++;
    60.         }
    61.  
    62.  
    63.         vertices[vertices.Length - 1] = new Vector3(0, 0, vertices[vertices.Length - 3].z);
    64.         Debug.Log("Vertices: " + num);
    65.         mesh.vertices = vertices;
    66.         MakingNormals();
    67.     }
    68.     void MakingNormals()
    69.     {
    70.         int i = 0;
    71.         Vector3[] normals = new Vector3[num + 2];
    72.         while (i < num)
    73.         {
    74.             normals[i] = Vector3.forward;
    75.             i++;
    76.         }
    77.         mesh.normals = normals;
    78.  
    79.         MakingTrianges();
    80.     }
    81.     void MakingTrianges()
    82.     {
    83.         int i = 0;
    84.         tris = new int[((3 * (leng - 1) * iter) * 2) + 3];
    85.         while (i < (leng - 1) * iter)
    86.         {
    87.             tris[i * 3] = i;
    88.             if ((i + 1) % iter == 0)
    89.             {
    90.                 tris[i * 3 + 1] = 1 + i - iter;
    91.             }
    92.             else
    93.             {
    94.                 tris[i * 3 + 1] = 1 + i;
    95.             }
    96.             tris[i * 3 + 2] = iter + i;
    97.             i++;
    98.         }
    99.         int IndexofNewTriangles = -1;
    100.  
    101.         for (int u = (tris.Length - 3) / 2; u < tris.Length - 6; u += 3)
    102.         {
    103.             //mesh.RecalculateTangents();
    104.             if ((IndexofNewTriangles + 2) % iter == 0)
    105.             {
    106.                 tris[u] = IndexofNewTriangles + iter * 2 + 1;
    107.             }
    108.             else
    109.                 tris[u] = IndexofNewTriangles + iter + 1;
    110.  
    111.             tris[u + 1] = IndexofNewTriangles + 2;
    112.             tris[u + 2] = IndexofNewTriangles + iter + 2;
    113.             IndexofNewTriangles += 1;
    114.         }
    115.         tris[tris.Length - 3] = 0;
    116.         tris[tris.Length - 2] = (iter * 2) - 1;
    117.         tris[tris.Length - 1] = iter;
    118.  
    119.         firstplane = new int[(iter * 3) * 2];
    120.         int felmnt = 0;
    121.         for (int h = 0; h < firstplane.Length / 2; h += 3)
    122.         {
    123.  
    124.             firstplane[h] = felmnt;
    125.  
    126.             if (felmnt + 1 != iter)
    127.                 firstplane[h + 1] = felmnt + 1;
    128.             else
    129.                 firstplane[h + 1] = 0;
    130.             firstplane[h + 2] = vertices.Length - 2;
    131.             felmnt += 1;
    132.         }
    133.  
    134.         felmnt = iter * (leng - 1);
    135.         for (int h = firstplane.Length / 2; h < firstplane.Length; h += 3)
    136.         {
    137.  
    138.             firstplane[h] = felmnt;
    139.  
    140.             if (felmnt + 1 != iter * (leng - 1))
    141.                 firstplane[h + 1] = felmnt + 1;
    142.             else
    143.                 firstplane[h + 1] = iter * (leng - 1);
    144.             firstplane[h + 2] = vertices.Length - 1;
    145.             felmnt += 1;
    146.         }
    147.  
    148.         firstplane[firstplane.Length - 3] = iter * (leng - 1);
    149.         firstplane[firstplane.Length - 2] = vertices.Length - 3;
    150.         firstplane[firstplane.Length - 1] = vertices.Length - 1;
    151.  
    152.         FinalTri = new int[tris.Length + firstplane.Length];
    153.  
    154.         int k = 0, l = 0;
    155.         for (k = 0, l = 0; k < tris.Length; k++)
    156.         {
    157.             FinalTri[l++] = tris[k];
    158.         }
    159.         for (k = 0; k < firstplane.Length; k++)
    160.         {
    161.             FinalTri[l++] = firstplane[k];
    162.         }
    163.  
    164.         mesh.triangles = FinalTri;
    165.         mesh.Optimize();
    166.         mesh.RecalculateNormals();
    167.         mf.mesh = mesh;
    168.     }
    169. }
    170.  
    171.