• 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
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

People who like this

0 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. My 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

  • Sort: 
avatar image
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
AlucardJay
Rabwin
adrianadrian

People who like this

3 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

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

@thoorne Did you solve the problem?

Comment

People who like this

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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

18 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 avatar image

Related Questions

Mesh extrusion overlapping triangles problem 0 Answers

Unity3D question about the Procedural Generation engine for creatures in Spore? 1 Answer

Creating a mesh out of a linerenderer - how do you convert revolved vertices set to triangles and uv's 1 Answer

By Code generated Mesh (problems with visibility) 4 Answers

Mesh.uv is out of range 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges