[fixed] problems with creating a disk shaped mesh (C#)

I have made a function to make flat disk. the weird thing is: it works with some input parameters but doesn’t with others. when it doesn’t work it doesn’t give any errors, just nothing shows up.

this is my Code:


public Mesh Disk(int radius,int radiusTiles,int tilesAround)
	{

		Vector3[] vertices 	= new Vector3	[radiusTiles*tilesAround*6];
		Vector3[] triangles 	= new int		[radiusTiles*tilesAround*6];
		Vector3[] UV 			= new Vector2	[radiusTiles*tilesAround*6];
		int currentVertex = 0;

		float tileLength = radius / radiusTiles;	//the length of a tile parallel to the radius
		float radPerTile = 2 * Mathf.PI / tilesAround; //the radians the tile takes

		for(int angleNum = 0; angleNum < tilesAround; angleNum++)//loop around
		{
			float angle = radPerTile*angleNum;	//the current angle in radians


			for(int offset = 0; offset < radiusTiles; offset++)//loop from the center outwards
			{

				vertices[currentVertex]		=	new Vector3(Mathf.Cos(angle)*offset*tileLength 				, Mathf.Sin(angle)*offset*tileLength				,0);
				vertices[currentVertex + 1]	=	new Vector3(Mathf.Cos(angle + radPerTile)*offset*tileLength , Mathf.Sin(angle + radPerTile)*offset*tileLength	,0);
				vertices[currentVertex + 2]	=	new Vector3(Mathf.Cos(angle)*(offset + 1)*tileLength 		, Mathf.Sin(angle)*(offset + 1)*tileLength			,0);
					
				vertices[currentVertex + 3]	=	new Vector3(Mathf.Cos(angle + radPerTile)*offset*tileLength 		, Mathf.Sin(angle + radPerTile)*offset*tileLength		,0);
				vertices[currentVertex + 4]	=	new Vector3(Mathf.Cos(angle + radPerTile)*(offset + 1)*tileLength 	, Mathf.Sin(angle + radPerTile)*(offset + 1)*tileLength	,0);
				vertices[currentVertex + 5]	=	new Vector3(Mathf.Cos(angle)*(offset + 1)*tileLength 				, Mathf.Sin(angle)*(offset + 1)*tileLength				,0);

				currentVertex += 6;
			}
		}

		for(int j = 0; j < triangles.Length; j++)	//set the triangles
		{
			triangles[j] = j;
		}

		//create the mesh and apply vertices/triangles/UV
		Mesh disk = new Mesh();
		disk.vertices = vertices;
		disk.triangles = triangles;
		disk.RecalculateNormals();
		disk.uv = UV;	//the UV doesnt need to be set
		
		return disk;
	}

for example:
Disk (5, 7, 15);
doesn’t work, but:
Disk (4, 4, 15);
does.

If anyone knows what i’m doing wrong it would be really helpfull.


Edit:

changed

float tileLength = radius / radiusTiles;

into

float tileLength = (float)radius / (float)radiusTiles;

and:

float angle = radPerTile*angleNum;

into

float angle = (float)radPerTile*(float)angleNum;

Hi,
you are dividing the parameters radius and radiusTile but they are integers, maybe you need to cast to float in that operation or it gives wrong parameters to your mesh…

I think that’s it but when you say it doesn’t work try to give more precisions (error logs, crash, weird meshes? screenshot could help too).
Think of it for your future posts :wink:

see you later

After a quick glance at your code I’d agree with @Nerevar - the problem is with your integer division. 4/4 is 1, which works. 5/7 is 0, so all your subsequent calculations are based on zero.

Simple test if this is the problem… change:

float angle = radPerTile*angleNum; 

to:

float angle = (float)radPerTile * (float)angleNum;