Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
1
Question by AVividLight · Aug 31, 2012 at 04:23 AM · c#meshproceduralgeneration

Help in understanding Mesh Generation

Hey guys,

I'm kinda' sick and I don't feel like typing much, so please excuse the brief question! I've been trying to figure out Mesh Generation, and I think I understand it a tiny bit, but I'm having a really hard time grasping this. I've been reading tutorials online about it, but they all seem to be written to people who know a bit about it... I pretty much know nothing about this, so that may be the issue.

After reading Morten Nobel's blog post on procedurally creating a Tetrahedron, I copy-and-pasted the code provided into my project and played around with it for a bit. Now I've got it creating a two-dimensional plane (what I wanted), but the code is still very long. I guess I'm just having a hard time understanding that all this is necessary to make a simple plane, and that there isn't an easier way to do it (while still making it in code).

I'll post the code I've edited bellow so you guys can read it, and maybe give me some suggestions; tell me if there is a simpler way to do this (again, while still making it in code. I don't want to use CreatePrimitive or something similar), or if I'm not understanding it and left some unnecessary code in there. Basically, please just help me understand how to make a plane procedurally!

From Morten Nobel's blog post "Procedural generated mesh in Unity":

     void Start ()
     {
         
         MeshFilter meshFilter = gameObject.GetComponent<MeshFilter>();
         Mesh mesh = new Mesh ();
         meshFilter.mesh = mesh;
 
         mesh.vertices = new Vector3[]
         {
             
             new Vector3(0,1,0), 
             new Vector3(1,1,0), 
             new Vector3(1,1,1), 
             new Vector3(0,1,1), 
         };
 
         int faces = 1;
 
         List<int> triangles = new List<int>();
         List<Vector2> uvs = new List<Vector2>();
 
         for (int i = 0; i < faces; i++)
         {
             
             int triangleOffset = i*1;
             triangles.Add(0+triangleOffset);
             triangles.Add(2+triangleOffset);
             triangles.Add(1+triangleOffset);
 
             triangles.Add(0+triangleOffset);
             triangles.Add(3+triangleOffset);
             triangles.Add(2+triangleOffset);
 
             uvs.Add(new Vector2(0,0));
             uvs.Add(new Vector2(1,0));
             uvs.Add(new Vector2(1,1));
             uvs.Add(new Vector2(0,1));
         }
 
         mesh.triangles = triangles.ToArray();
         mesh.uv = uvs.ToArray();
 
         renderer.material = new Material(Shader.Find("Diffuse"));
 
         mesh.RecalculateNormals(); 
         mesh.RecalculateBounds (); 
         mesh.Optimize();
     }

Well, I started this saying I wasn't going to type much, and here I've written three paragraphs! Anyway, thank you for taking the time to read this, indoubtedly, confusing post. I really appreciate all your help, guys, thanks so much!

-Gibson

Comment
Add comment · Show 2
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 Apples_mmmmmmmm · Aug 31, 2012 at 05:11 AM 1
Share

Looks good to me, however I am curious why you are using int triangle offset ins$$anonymous$$d of just using i in the for loop. Since you are multiplying i by 1 for the value of triangleOffset you may as well not create the extra variable.

avatar image AVividLight · Aug 31, 2012 at 05:27 AM 0
Share

Thanks for the reassurance, apples_mmmmmmmm! I am using int triangle offset simply because that's how it was when I coppied it, and when I tried to take it out, I got an error. Please, how would you suggest I would do it? Thanks again!

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Apples_mmmmmmmm · Aug 31, 2012 at 05:31 AM

I think you should be able to replace

 for (int i = 0; i < faces; i++) {
 
  int triangleOffset = i*1;
  triangles.Add(0+triangleOffset);
  triangles.Add(2+triangleOffset);
  triangles.Add(1+triangleOffset);
 
  triangles.Add(0+triangleOffset);
  triangles.Add(3+triangleOffset);
  triangles.Add(2+triangleOffset);
 
  uvs.Add(new Vector2(0,0));
  uvs.Add(new Vector2(1,0));
  uvs.Add(new Vector2(1,1));
  uvs.Add(new Vector2(0,1));
 
 }

with

 for (int i = 0; i < faces; i++)
    {
 
      triangles.Add(0+i);
      triangles.Add(2+i);
      triangles.Add(1+i);
 
      triangles.Add(0+i);
      triangles.Add(3+i);
      triangles.Add(2+i);
 
      uvs.Add(new Vector2(0,0));
      uvs.Add(new Vector2(1,0));
      uvs.Add(new Vector2(1,1));
      uvs.Add(new Vector2(0,1));
    }

I don't think you should get any errors through doing this.

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

9 People are following this question.

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

Related Questions

Procedurally Generated Cube Mesh 3 Answers

Flat shading procedural generated mesh? 1 Answer

Mesh generated from bezier curve loop going outside loop 0 Answers

how to find the distance between two points? 1 Answer

Mesh creation by code not working? 0 Answers


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