Search Unity

[help] i'm having hard time creating meshes in c#

Discussion in 'Scripting' started by Thibault-Potier, May 28, 2015.

  1. Thibault-Potier

    Thibault-Potier

    Joined:
    Apr 10, 2015
    Posts:
    206
    Hi !
    I'm trying to create a circle in c#. (My final goal will be to draw pie graph)
    I thought i was doing it right, but here is what i got :



    Here is my code so far:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PieGraph : MonoBehaviour {
    5.  
    6.     public int resolution = 36;
    7.     float angleStep;
    8.     float curAngle=0;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.  
    13.         MeshFilter mf = GetComponent < MeshFilter>();
    14.         Mesh mesh = new Mesh ();
    15.         mf.mesh = mesh;
    16.  
    17.         //Vertices
    18.         Vector3 [] vertices = new Vector3[resolution + 1];
    19.  
    20.         vertices [0] = new Vector3 (0, 0, 0);//centre
    21.         angleStep = 360 / resolution;
    22.  
    23.         for(int i=1;i<=resolution;i++){//NB POINTS
    24.             vertices[i]=new Vector3 (Mathf.Cos(curAngle),Mathf.Sin(curAngle), 0);
    25.             curAngle+=angleStep;
    26.             //Debug.Log (vertices[i]);
    27.         }
    28.  
    29.         //Triangles
    30.         int [] triangles = new int[resolution*3];
    31.  
    32.         int temp = 0;
    33.         triangles[0]=0;
    34.         for(int i=1;i<(resolution*3);i++){//NB POINTS
    35.             if(i%3==0){
    36.                 triangles[i]=0;
    37.                 temp+=1;
    38.             }
    39.             else{
    40.                 triangles[i]=i-2*temp;
    41.                 if(i-2*temp==37)triangles[i]=1;
    42.             }
    43.         }
    44.  
    45.         /*
    46.         for (int i=0; i<(resolution*3); i++) {//NB POINTS
    47.             Debug.Log (i + "  " + triangles [i]);
    48.         }
    49.         */
    50.  
    51.         //Normals
    52.         Vector3[] normals = new Vector3[resolution+1];
    53.  
    54.         for(int i=0;i<=resolution;i++){//NB POINTS
    55.             normals[i] = Vector3.forward;
    56.         }
    57.         //UVs
    58.         Vector2[] UV= new Vector2 [resolution+1];
    59.         for(int i=0;i<=resolution;i++){//NB POINTS
    60.             UV[i] = new Vector2(vertices[i].x,vertices[i].y);
    61.         }
    62.  
    63.         //Assign arrays
    64.         mesh.vertices = vertices;
    65.         mesh.triangles = triangles;
    66.         mesh.normals = normals;
    67.         mesh.uv = UV;
    68.  
    69.  
    70.    
    71.     }
    72. }
    73.  
    When i check my vertices position / my triangles with debug.log , it seems to be right. Here are screens from the console : (those are from the Debug.log you can see in my code)
    Vertices coords:

    triangles :


    Help is required :D Thx a lot
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Looks like you're using degrees in Mathf.Sin/Cos. Those functions expect radians.
     
  3. Thibault-Potier

    Thibault-Potier

    Joined:
    Apr 10, 2015
    Posts:
    206
    I love you <3

    problem solved :)
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    As general advice for mesh-building, I recommend treating triangles as blocks of 3 (or quads as blocks of 6) rather than trying to do modulus math - it'll make your code a lot more readable and less error-prone.

    Something like this (though it may not quite be correct, I had trouble reading what your code was trying to do) ;)
    Code (csharp):
    1.  
    2. for(int i=1;i<resolution;i++){//NB POINTS
    3. int t = i*3;
    4. triangles[t+0] = 0;
    5. triangles[t+1] = i;
    6. triangles[t+2] = (i+1) % resolution;
    7. }
     
  5. Thibault-Potier

    Thibault-Potier

    Joined:
    Apr 10, 2015
    Posts:
    206
    Yeah, my code is clearly not "clean" or optimized, i just needed a first result to keep going :D now i can go on, thx a lot