- Home /
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[i] = 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[i] = Vector3.forward;
i++;
}
mesh.normals = normals;
}
I think that there is a problem with MakingTriangles function.
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.
As i said i wanted to create procedurally generated cylinder with some noise but i can't achieve that. $$anonymous$$y code should be good but i dont know why it isn't. Where is mistake? It generates only first row of vertices and they are misplaced in z axis.
Do you really think, someone can read through all your code, and find the problem? Seriously ?
Learn to use Debug.DrawLine, and Debug.Log
If you spend 5 $$anonymous$$utes doing this you will instantly find the problem.
It is utterly impossible to weave mesh without continually drawing indicators (use Debug. Gizmo, etc) as you work.
This is the actual answer to your problem.
Answer by Bunny83 · May 19, 2013 at 09:15 PM
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.
Answer by WR-reddevil · Oct 18, 2019 at 06:38 AM
@thoorne Did you solve the problem?
If you want to create a cylinder mesh quickly you can use GameObject.CreatePrimitive(PrimitiveType.Cylinder);
that I know but what I want is I will create a cylinder first and cut it to some other pices to create a 3D Pie chart.
I think you should work with the front plate, a circle, make all your sectors as separate meshes. After that, create the second "Wall" of vertices behind, link everything with triangles (this is the hardest part, you might want to use tons of debug draw features to understand if it works correctly starting with simple geometries).