# Procedural Cylinder Generation

I wanted to create procedural cylinder with some noise. Here is my code:

``````var mf: MeshFilter;
var cube : GameObject;
var mesh : Mesh;

var iter : int;
var num : int;
var leng : int;

function Start()
{
leng = 10;
iter = 5;
num=0;

var mf: MeshFilter = GetComponent(MeshFilter);
mesh = new Mesh();
mf.mesh = mesh;
MakingVertices(1,iter,leng,0.5f,0.1);
}

function MakingVertices (radius : int, iterations : int, lenggth : int, gap : float, noise : float)
{
var noise_x : float;
var noise_y : float;
var noise_z : float;
var x : float;
var y : float;
var z : float;
var i : int;
var p : int;
var angle : float;

var vertices: Vector3[] = new Vector3[iterations*lenggth];

while(p < lenggth)
{
i=0;
while(i<iterations)
{
noise_x = Random.Range(-noise,noise);
noise_y = Random.Range(-noise,noise);
noise_z = Random.Range(-noise,noise);
angle = (i * 1.0) / iterations * Mathf.PI * 2;
x = Mathf.Sin(angle) * radius;
y = Mathf.Cos(angle) * radius;
vertices *= new Vector3(x+noise_x,y+noise_y,z+noise_z);*
``````
• `````` 	Instantiate(cube,new Vector3(x+noise_x,y+noise_y,z+noise_z),Quaternion.identity);*
``````
• `````` 	i++;*
``````
• `````` 	num++;*
``````
• `````` }*
``````
• `````` z = z + gap;*
``````
• `````` p++;*
``````
• }*
• Debug.Log("Vertices: "+num);*
• mesh.vertices = vertices;*
• MakingTrianges();*
}

function MakingTrianges()
{

• var i : int=0;*
• var j : int=0;*
• var r : int = 0;*
_ var tris: int = new int[3*(leng-1)*iter];_
• if(tris.Length % 3 == 0) Debug.Log(“Array Correct”);*
_ while(i<(leng-1)*iter)_
• {*
_ tris[i*3] = i;_
_ if((i+1) % iter == 0) { Debug.Log(“Podzielnosc!”); tris[i*3+1] = 1+i -iter; }_
_ else { tris[i*3+1] = 1+i; }_
_ tris[i*3+2] = iter+i;_
• `````` i++;*
``````
• } *
• mesh.triangles = tris;*
• MakingNormals();*
}

function MakingNormals()
{

• var i : int=0;*
• var normals: Vector3 = new Vector3[num];*
• while(i<num)*
• {*
_ normals = Vector3.forward;_
* i++;*
* }*
* mesh.normals = normals;*
}
I think that there is a problem with MakingTriangles function.

[11185-problem.jpg|11185]_
As you can see, cubes are instantiated in right position so the vertices should be.
Here is visualization of algorithm that i tried to implement.
_
[11186-algorytm.jpg*|11186]*
_*
_*

Well, i see several problems here:

• First of all your helper-indication-cubes are instantiated in world-space coordinates while your vertices are of course in local space. Unless the object which has this script attached is at position 0,0,0 and not rotated or scaled the position won’t match the actual vertices.
• It seems you create one triangle per “face” but you actually need two triangles to form a quad.
• You don’t create the caps ( maybe on purpose? )
• Your choosen variable names and function parameters as well as the strange calling order makes it not easy to follow the actual code flow. A function called “MakingVertices” should “make vertices” and not triangles and normals.
• Your normals are quite useless and can produce strange behaviour since they all just point along the local z axis. Since they point along the cylinder the shader will always should the faces black shaded. If you don’t know how to create the normals, just use at least RecalculateNormals. They might not be perfect but should be enough for testing.

I can’t see an obvious mistake beside that points. It’s hard to tell where it goes wrong. Since it’s a quite easy shape you might want to test with an iteration count of 3 and print out the triangles array to check your indices manually.

@thoorne Did you solve the problem?