• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by thoorne · May 19, 2013 at 07:23 PM · meshproceduralverticesgenerationtriangle

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.

alt text

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.

alt text

problem.jpg (29.6 kB)
algorytm.jpg (133.4 kB)
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · May 19, 2013 at 08:42 PM 0
Share

What's your question ?

avatar image thoorne · May 19, 2013 at 08:53 PM 0
Share

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.

avatar image Fattie · May 19, 2013 at 08:57 PM 1
Share

Do you really think, someone can read through all your code, and find the problem? Seriously ?

avatar image Fattie · May 19, 2013 at 08:58 PM 1
Share

Learn to use Debug.DrawLine, and Debug.Log

If you spend 5 minutes 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.

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · May 19, 2013 at 09:18 PM 0
Share

"I can't see an obvious mistake beside that points."

avatar image
0

Answer by WR-reddevil · Oct 18, 2019 at 06:38 AM

@thoorne Did you solve the problem?

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image misher · Oct 18, 2019 at 06:44 AM 0
Share

If you want to create a cylinder mesh quickly you can use GameObject.CreatePrimitive(PrimitiveType.Cylinder);

avatar image WR-reddevil misher · Oct 18, 2019 at 07:04 AM 0
Share

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.

avatar image misher WR-reddevil · Oct 19, 2019 at 08:17 AM 0
Share

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).

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

creating a mesh procedurally 4 Answers

Easy way to convert a bunch of vertices to triangles or uv's? 1 Answer

Procedurally generated mesh JS - explain the error please 0 Answers

Procedural Mesh Generation - Split Arrays into sections 1 Answer

How to colorize a face of a Mesh 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges