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 /
This question was closed Sep 19, 2017 at 10:14 AM by Jenny_Wasja for the following reason:

I found the problem: Simple variable mistake!

avatar image
0
Question by Jenny_Wasja · Sep 13, 2017 at 10:51 PM · meshverticesindex

Creating Mesh - Vertices out of boundries

Hello everyone o/

I'm to make a program that initializes an Icosphere (A sphere that consists out of triangles that have the same size and distance) from the script, where I can set the number of iterations for the sphere. The first creation of the sphere with 20 triangles works fine. But if I try to add more triangles recursively, I get this error:

Failed setting triangles. Some indices are referencing out of bounds vertices. IndexCount: 240, VertexCount: 42

I'm confused about that: My vertices and my indices range from 0-41 and from 0-239. But there should be no further Vertex at the next position. Has anybody an idea, what I could have done wrong? What do I not see? :/

Here is my code:

 public class GenerateIcosphere : MonoBehaviour {
 
     public List<Vector3> newVertices;
     public List<Vector2> newUV;
     public List<int> newTriangles;
     public Material wireframe;
     public int recrusionLevel = 0;
 
     private int triLevel = 0;
     private Dictionary<Int64, int> middlePointIndexCache;
     private Mesh iSphere;
 
     private void createIcosphere()
     {
         middlePointIndexCache = new Dictionary<long, int>();
 
         float t = (float)(1.0 + Mathf.Sqrt(5.0f) / 2.0f);
 
         newVertices = new List<Vector3> { new Vector3(-1, t, 0), new Vector3(1, t, 0),
                                        new Vector3(-1, -t, 0),new Vector3(1, -t, 0),
 
                                        new Vector3(0, -1, t), new Vector3(0, 1, t),
                                        new Vector3(0,-1, -t), new Vector3(0, 1, -t),
 
                                        new Vector3(t, 0, -1), new Vector3(t, 0, 1),
                                        new Vector3(-t,0, -1), new Vector3(-t, 0, 1)};
 
 
         newTriangles = new List<int> {0, 11, 5, 0, 5, 1, 0, 1, 7, 0, 7, 10, 0, 10, 11,
                                   1, 5, 9, 5, 11, 4, 11, 10, 2, 10, 7, 6, 7, 1, 8,
                                   3, 9, 4, 3, 4, 2, 3, 2, 6, 3, 6, 8, 3, 8, 9,
                                   4, 9, 5, 2, 4, 11, 6, 2, 10, 8, 6, 7, 9, 8, 1};
     }
 
     bool checkForDifference(int n)
     {
         if(n > triLevel || n < triLevel)
         {
             triLevel = n;
             return true;
         }
 
         return false;
     }
 
     void iterateIcosphere()
     {
         
         for (int i = 0; i < recrusionLevel; i++)
         {
             List<int> addTriangles = new List<int> { };
             for (int j = 0; j < newTriangles.Count; j = j+3)
             {
                 int a = getMiddlePoint(newTriangles[j], newTriangles[j+1]);
                 int b = getMiddlePoint(newTriangles[j+1], newTriangles[j+2]);
                 int c = getMiddlePoint(newTriangles[j+2], newTriangles[j]);
 
                 addTriangles.Add(newTriangles[j]);
                 addTriangles.Add(a);
                 addTriangles.Add(c);
 
                 addTriangles.Add(newTriangles[j+1]);
                 addTriangles.Add(b);
                 addTriangles.Add(a);
 
                 addTriangles.Add(newTriangles[j+2]);
                 addTriangles.Add(c);
                 addTriangles.Add(b);
 
                 addTriangles.Add(a);
                 addTriangles.Add(b);
                 addTriangles.Add(c);
                 
             }
 
 
             newTriangles = addTriangles;
         }
     }
 
     private int addVertex(Vector3 p)
     {
         float length = Mathf.Sqrt(p.x * p.x + p.y * p.y + p.z * p.z);
         newVertices.Add(new Vector3(p.x / length, p.y / length, p.z / length));
         return newVertices.Count;
     }
 
     private int getMiddlePoint(int p1, int p2)
     {
         //check if the point already exists
 
         bool firstIsSmaller = p1 < p2;
         Int64 smallerIndex = firstIsSmaller ? p1 : p2;
         Int64 greaterIndex = firstIsSmaller ? p2 : p1;
         Int64 key = (smallerIndex << 32) + greaterIndex;
 
         int ret;
         if (middlePointIndexCache.TryGetValue(key, out ret))
         {
             return ret;
         }
 
 
 
         //if not calculate new point
         Vector3 point1 = newVertices[p1];
         Vector3 point2 = newVertices[p2];
         Vector3 middle = new Vector3(((point1.x + point2.x) /2.0f), 
                                      ((point1.y + point2.y) / 2.0f), 
                                      ((point1.z + point2.z) / 2.0f));
 
         int i = addVertex(middle);
 
         middlePointIndexCache.Add(key, i);
 
         return i;
     }
 
     void Start () {
         createIcosphere();
         iSphere = new Mesh();
         gameObject.AddComponent<MeshFilter>().mesh = iSphere;
         iSphere.vertices = newVertices.ToArray();
         iSphere.uv = newUV.ToArray();
         iSphere.triangles = newTriangles.ToArray();
         iSphere.RecalculateNormals();
         gameObject.AddComponent<MeshRenderer>();
         gameObject.GetComponent<MeshRenderer>().material = wireframe;
     }
 
     // Update is called once per frame
     void Update () {
         if (checkForDifference(recrusionLevel))
         {
             iterateIcosphere();
             iterateUVs();
             iSphere = new Mesh();
             gameObject.GetComponent<MeshFilter>().mesh = iSphere;
             iSphere.Clear();
             iSphere.vertices = newVertices.ToArray();
             iSphere.uv = newUV.ToArray();
             iSphere.triangles = newTriangles.ToArray();
             iSphere.RecalculateNormals();
             
         }   
     }

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

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

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

Vertex coordinates of mesh imported from Blender as fbx are all equal to 0,0,0? 0 Answers

Adding Vertices to Coded Mesh? 0 Answers

Seperating isolated triangles into mesh 0 Answers

getting the height of the nearest vertices 0 Answers

Reload meshes with readable = true? 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