How to generate cylinder through code c#?

I am trying to generate a pie chart, but it can not be scale in Z-axis. Following is my code

 using UnityEngine;
using System.Collections;
public class PieChartMesh : MonoBehaviour
{
     float[] mData;
     int mSlices;
     float mRotationAngle;
     float mRadius;
     Material[] mMaterials;
     Vector3[] mVertices;
     Vector3[] mNormals;
     Vector3[] mWRNormals;
     Vector3 mNormal = new Vector3(0f, 0f, -1f);
     public Vector3 mWRNormal = new Vector3(1, 1, 1);
     Vector2[] mUvs;
     int[] mTriangles;
     MeshRenderer mMeshRenderer;
     float delay = 0.1f;
     GameObject TempObject;
     public void Init(float[] data, int slices, float rotatioAngle, float radius, Material[] materials, float speed, GameObject otherobject)
     {
         TempObject = otherobject;
         mData = data;
         mSlices = slices;
         mRotationAngle = rotatioAngle;
         mRadius = radius;
         delay = speed;
         // Get Mesh Renderer
         mMeshRenderer = gameObject.GetComponent("MeshRenderer") as MeshRenderer;
         if (mMeshRenderer == null)
         {
             gameObject.AddComponent<MeshRenderer>();
             mMeshRenderer = gameObject.GetComponent("MeshRenderer") as MeshRenderer;
         }
         mMeshRenderer.materials = materials;
         mMaterials = materials;
         Init(data);
     }
     public void Init(float[] data)
     {
         mSlices = 100;
         mRotationAngle = 90f;
         mRadius = 0.3f;
         mData = data;
     }
     public void Draw(float[] data)
     {
         mData = data;
         StopAllCoroutines();
         StartCoroutine(Draw());
     }
     public IEnumerator Draw()
     {
         //Check data validity for pie chart...
         while (mData == null)
         {
             print("PieChart: Data null");
             yield return null;
         }
         for (int i = 0; i < mData.Length; i++)
         {
             if (mData[i] < 0)
             {
                 print("PieChart: Data < 0");
                 yield return null;
             }
         }
         // Calculate sum of data values
         float sumOfData = 0;
         foreach (float value in mData)
         {
             sumOfData += value;
         }
         if (sumOfData <= 0)
         {
             print("PieChart: Data sum <= 0");
             yield return null;
         }
         // Determine how many triangles in slice
         int[] slice = new int[mData.Length];
         int numOfTris = 0;
         int numOfSlices = 0;
         int countedSlices = 0;
         // Caluclate slice size
         for (int i = 0; i < mData.Length; i++)
         {
             numOfTris = (int)((mData[i] / sumOfData) * mSlices);
             slice[numOfSlices++] = numOfTris;
             countedSlices += numOfTris;
         }
         // Check that all slices are counted.. if not -> add/sub to/from biggest slice..
         int idxOfLargestSlice = 0;
         int largestSliceCount = 0;
         for (int i = 0; i < mData.Length; i++)
         {
             if (largestSliceCount < slice[i])
             {
                 idxOfLargestSlice = i;
                 largestSliceCount = slice[i];
             }
         }
         // Check validity for pie chart
         if (countedSlices == 0)
         {
             print("PieChart: Slices == 0");
             yield return null;
         }
         // Adjust largest dataset to get proper slice
         slice[idxOfLargestSlice] += mSlices - countedSlices;
         // Check validity for pie chart data
         if (slice[idxOfLargestSlice] <= 0)
         {
             print("PieChart: Largest pie <= 0");
             yield return null;
         }
         // Init vertices and triangles arrays
         mVertices = new Vector3[mSlices * 3];
         mNormals = new Vector3[mSlices * 3];
         mWRNormals = new Vector3[mSlices * 3];
         mUvs = new Vector2[mSlices * 3];
         mTriangles = new int[mSlices * 3];
         //gameObject.AddComponent("MeshFilter");
         //gameObject.AddComponent("MeshRenderer");
         Mesh mesh = ((MeshFilter)GetComponent("MeshFilter")).mesh;
         mesh.Clear();
         mesh.name = "Pie Chart Mesh";
         // Roration offset (to get star point to "12 o'clock")
         float rotOffset = mRotationAngle / 360f * 2f * Mathf.PI;
         // Calc the points in circle
         float angle;
         float[] x = new float[mSlices];
         float[] y = new float[mSlices];
         float[] z = new float[mSlices];
         for (int i = 0; i < mSlices; i++)
         {
             angle = i * 2f * Mathf.PI / mSlices;
             x[i] = (Mathf.Cos(angle + rotOffset) * mRadius);
             y[i] = (Mathf.Sin(angle + rotOffset) * mRadius);
             z[i] = (Mathf.Cos(angle ) * mRadius);
            
         }
         // Generate mesh with slices (vertices and triangles)
         for (int i = 0; i < mSlices; i++)
         {
             mVertices[i * 3 + 0] = new Vector3(0f, 0f, 0f);
             mVertices[i * 3 + 1] = new Vector3(x[i], y[i], 0f);
             // This will ensure that last vertex = first vertex..
             mVertices[i * 3 + 2] = new Vector3(x[(i + 1) % mSlices], y[(i + 1) % mSlices], 0);
             mNormals[i * 3 + 0] = mNormal;
             mNormals[i * 3 + 1] = mNormal;
             mNormals[i * 3 + 2] = mNormal;
             mWRNormals[i * 3 + 0] = mWRNormal;
             mWRNormals[i * 3 + 1] = mWRNormal;
             mWRNormals[i * 3 + 2] = mWRNormal;
             mUvs[i * 3 + 0] = new Vector2(0f, 0f);
             mUvs[i * 3 + 1] = new Vector2(x[i], y[i]);
             // This will ensure that last uv = first uv..
             mUvs[i * 3 + 2] = new Vector2(x[(i + 1) % mSlices], y[(i + 1) % mSlices]);
             mTriangles[i * 3 + 0] = i * 3 + 0;
             mTriangles[i * 3 + 1] = i * 3 + 1;
             mTriangles[i * 3 + 2] = i * 3 + 2;
         }
         // Assign verts, norms, uvs and tris to mesh and calc normals
         mesh.vertices = mVertices;
         mesh.normals = mNormals;
         mesh.uv = mUvs;
         mesh.uv2 = mUvs;
         //mesh.triangles = triangles;
         mesh.subMeshCount = mData.Length;
         int[][] subTris = new int[mData.Length][];
         countedSlices = 0;
         // Set sub meshes
         for (int i = 0; i < mData.Length; i++)
         {
             GameObject go = Instantiate(TempObject);
             go.name = i.ToString();
             Mesh gomesh = ((MeshFilter)go.GetComponent("MeshFilter")).mesh;
             gomesh.vertices = mesh.vertices;
             gomesh.normals = mWRNormals;
             gomesh.uv = mesh.uv;
             //gomesh.subMeshCount = mData.Length;
             // Every triangle has three veritces..
             subTris[i] = new int[slice[i] * 3];
             // Add tris to subTris
             for (int j = 0; j < slice[i]; j++)
             {
                 subTris[i][j * 3 + 0] = mTriangles[countedSlices * 3 + 0];
                 subTris[i][j * 3 + 1] = mTriangles[countedSlices * 3 + 1];
                 subTris[i][j * 3 + 2] = mTriangles[countedSlices * 3 + 2];
                 if (j % 5 == 0)
                     yield return new WaitForSeconds(delay);
                 mesh.SetTriangles(subTris[i], i);
                 gomesh.SetTriangles(subTris[i], 0, true);
                 countedSlices++;
             }
             gomesh.Optimize();
             gomesh.RecalculateNormals();
         }
     }
     // Properties
     public float[] Data { get { return mData; } set { mData = value; } }
     public int Slices { get { return mSlices; } set { mSlices = value; } }
     public float RotationAngle { get { return mRotationAngle; } set { mRotationAngle = value; } }
     public float Radius { get { return mRadius; } set { mRadius = value; } }
     public Material[] Materials { get { return mMaterials; } set { mMaterials = value; } }
}

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.

1 Like

I did that here is the code and it’s working fine

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CyliderGenerate : MonoBehaviour
{
    public MeshFilter mf;
    public Mesh mesh;

    public GameObject cube;

    [Range(10, 20)]
    public int iter;
    int num;
    public int leng;
    Vector3[] vertices;
    int[] tris;
    int[] FinalTri;
    int[] firstplane;
    void Start()
    {
        MeshFilter mf = GetComponent<MeshFilter>();
        mesh = new Mesh();
        MakingVertices(1, iter, leng, 0.5f, 0.1f);
    }

    void MakingVertices(int radius, int iterations, int lenggth, float gap, float noise)
    {
        float noise_x;
        float noise_y;
        float noise_z;
        float x;
        float y;
        float z = 0;
        int i;
        int p = 0;
        float angle;

        vertices = new Vector3[(iterations * lenggth) + 2];
        int tempo = 0;
        vertices[vertices.Length - 2] = Vector3.zero;

        while (p < lenggth)
        {
            i = 0;
            while (i < iterations)
            {
                angle = (i * 1.0f) / iterations * Mathf.PI * 2;
                x = Mathf.Sin(angle) * radius;
                y = Mathf.Cos(angle) * radius;
                vertices[tempo] = new Vector3(x, y, z);
                //GameObject go = Instantiate(cube, vertices[tempo], Quaternion.identity);
                //go.name = num.ToString();
                i++;
                num++;
                tempo += 1;
            }
            z += gap;
            p++;
        }


        vertices[vertices.Length - 1] = new Vector3(0, 0, vertices[vertices.Length - 3].z);
        Debug.Log("Vertices: " + num);
        mesh.vertices = vertices;
        MakingNormals();
    }
    void MakingNormals()
    {
        int i = 0;
        Vector3[] normals = new Vector3[num + 2];
        while (i < num)
        {
            normals[i] = Vector3.forward;
            i++;
        }
        mesh.normals = normals;

        MakingTrianges();
    }
    void MakingTrianges()
    {
        int i = 0;
        tris = new int[((3 * (leng - 1) * iter) * 2) + 3];
        while (i < (leng - 1) * iter)
        {
            tris[i * 3] = i;
            if ((i + 1) % iter == 0)
            {
                tris[i * 3 + 1] = 1 + i - iter;
            }
            else
            {
                tris[i * 3 + 1] = 1 + i;
            }
            tris[i * 3 + 2] = iter + i;
            i++;
        }
        int IndexofNewTriangles = -1;

        for (int u = (tris.Length - 3) / 2; u < tris.Length - 6; u += 3)
        {
            //mesh.RecalculateTangents();
            if ((IndexofNewTriangles + 2) % iter == 0)
            {
                tris[u] = IndexofNewTriangles + iter * 2 + 1;
            }
            else
                tris[u] = IndexofNewTriangles + iter + 1;

            tris[u + 1] = IndexofNewTriangles + 2;
            tris[u + 2] = IndexofNewTriangles + iter + 2;
            IndexofNewTriangles += 1;
        }
        tris[tris.Length - 3] = 0;
        tris[tris.Length - 2] = (iter * 2) - 1;
        tris[tris.Length - 1] = iter;

        firstplane = new int[(iter * 3) * 2];
        int felmnt = 0;
        for (int h = 0; h < firstplane.Length / 2; h += 3)
        {

            firstplane[h] = felmnt;

            if (felmnt + 1 != iter)
                firstplane[h + 1] = felmnt + 1;
            else
                firstplane[h + 1] = 0;
            firstplane[h + 2] = vertices.Length - 2;
            felmnt += 1;
        }

        felmnt = iter * (leng - 1);
        for (int h = firstplane.Length / 2; h < firstplane.Length; h += 3)
        {

            firstplane[h] = felmnt;

            if (felmnt + 1 != iter * (leng - 1))
                firstplane[h + 1] = felmnt + 1;
            else
                firstplane[h + 1] = iter * (leng - 1);
            firstplane[h + 2] = vertices.Length - 1;
            felmnt += 1;
        }

        firstplane[firstplane.Length - 3] = iter * (leng - 1);
        firstplane[firstplane.Length - 2] = vertices.Length - 3;
        firstplane[firstplane.Length - 1] = vertices.Length - 1;

        FinalTri = new int[tris.Length + firstplane.Length];

        int k = 0, l = 0;
        for (k = 0, l = 0; k < tris.Length; k++)
        {
            FinalTri[l++] = tris[k];
        }
        for (k = 0; k < firstplane.Length; k++)
        {
            FinalTri[l++] = firstplane[k];
        }

        mesh.triangles = FinalTri;
        mesh.Optimize();
        mesh.RecalculateNormals();
        mf.mesh = mesh;
    }
}
5 Likes