Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
2
Question by popuppirate · Nov 13, 2015 at 10:32 AM · meshproceduralverticestriangles

Generating Mesh using Points

Hello all,

I am trying to connect some generated points to form a mesh, but despite my best efforts to try and understand how to piece the mesh and triangles together, I'm still struggling!

The image below is composed of an array, 'vertices', that I wish to connect. The points consist of a 'torus_count' number of points in a torus before moving to the next one along. This is best illustrated by the diagram below. I have also included the code in the OnGUI section which enabled me to produce this diagram.

In short, what is the best way now to go from here to generating a mesh array and a triangles array?

Many thanks,

Popuppirate

Code:

  void OnGUI (){
 for (int i=0; i<vertices.Length; i++) {
                if (i < vertices.Length - torus_count)
                {
 
                    if (i % torus_count == torus_count-1)
                    {// Draws last line of each section
                        Gizmos.DrawLine(vertices[i], vertices[i + torus_count]); 
                        continue;
                    }
                    Gizmos.DrawLine(vertices[i], vertices[i + torus_count]);
                    Gizmos.DrawLine(vertices[i + torus_count], vertices[i + torus_count+1]);
                    Gizmos.DrawLine(vertices[i + torus_count], vertices[i + 1]);
                    Gizmos.DrawLine(vertices[i + 1], vertices[i]);
                }
 
            }
 }

 



alt text

screenshot-2.jpg (350.7 kB)
Comment
Add comment
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

1 Reply

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

Answer by mikelortega · Nov 13, 2015 at 01:06 PM

You can try somethig like this code. I hope you understand. You have to create a Mesh, then fill in the triangle and UV mapping data. I hope it helps:

 private GameObject GenerateMesh(Vector3[] vertices, int torus_count)
 {
     GameObject torus = new GameObject("Torus");
 
     MeshRenderer mr = torus.AddComponent<MeshRenderer>();
     mr.material = Resources.Load<Material>("YourMaterial");
 
     MeshFilter mf = torus.AddComponent<MeshFilter>();
     Mesh mesh = new Mesh();
     mesh.Clear(false);
     mf.mesh = mesh;
     mesh.name = "Torus_mesh";
 
     mesh.vertices = vertices;
 
     List<int> triangles = new List<int>();
     for (int row = 0; row < vertices.Length / torus_count - 1; ++row)
     {
         for (int col = 0; col < torus_count - 1; ++col)
         {
             triangles.Add(row * torus_count + col);
             triangles.Add(row * torus_count + col + 1);
             triangles.Add((row + 1) * torus_count + col);
 
             triangles.Add(row * torus_count + col + 1);
             triangles.Add((row + 1) * torus_count + col + 1);
             triangles.Add((row + 1) * torus_count + col);
         }
     }
     mesh.triangles = triangles.ToArray();
 
     List<Vector2> uv = new List<Vector2>();
     for (int i = 0; i < vertices.Length; ++i)
         uv.Add(new Vector2(0.0f, 0.0f)); // FIX this! to map your texture correctly
     mesh.uv = uv.ToArray();
 
     mesh.RecalculateNormals();
     mesh.Optimize();
 
     return torus;
 }


Comment
Add comment · Show 4 · 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 popuppirate · Nov 13, 2015 at 02:19 PM 0
Share

Hi mikelo,

This is almost exactly what I'm looking for - great work!

However, for whatever reason it only seems to be picking out every other triangle, see below. I'll try and figure out what's going on with it and if I can then I'll be very satisfied with this answer!

alt text

screenshot-5.jpg (332.3 kB)
avatar image mikelortega popuppirate · Nov 13, 2015 at 02:36 PM 1
Share

I think there is an error in lines 25 and 27. Try this:

      triangles.Add(row * torus_count + col + 1);
      triangles.Add((row + 1) * torus_count + col + 1);
      triangles.Add((row + 1) * torus_count + col);
avatar image popuppirate mikelortega · Nov 13, 2015 at 05:06 PM 0
Share

Thank you very much! This worked perfectly!

Show more comments

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

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

How to get triangle index of meshe's desired part? 0 Answers

How can I remove shooted mesh of object? 0 Answers

Procedural Mesh Problems 0 Answers

How can I identify the location of mesh triangles at run time? (Procedural Generation) 1 Answer

Creating triangles around a mesh using an array of vertices 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