Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
0
Question by bart1259 · May 06, 2016 at 02:32 PM · vector3verticestrianglesgeometrysubdivision

Unity subdividing icosohedron script problem

So I wrote this script to subdivide an icosohedron, but when I try to execute it unity crashes. Here is the code

 using UnityEngine;
 using System.Collections;
 using System;
 using System.Collections.Generic;
 
 [RequireComponent(typeof(MeshRenderer)), RequireComponent(typeof(MeshFilter))]
 
 public class Icosohedron : MonoBehaviour {
 
     public int recursionLevel;
     private float t = (1 + Mathf.Sqrt(5)) / 2;
     private MeshFilter mf;
     // Use this for initialization
     void Start () {
         #region
         List<Vector3> verticies = new List<Vector3>();
         
         Vector3 aa = new Vector3(-1,  t,  0);
         Vector3 ab = new Vector3(1, t, 0);
         Vector3 ac = new Vector3(-1, -t, 0);
         Vector3 ad = new Vector3(1, -t, 0);
 
         Vector3 ae = new Vector3(0, -1, t);
         Vector3 af = new Vector3(0, 1, t);
         Vector3 ag = new Vector3(0, -1, -t);
         Vector3 ah = new Vector3(0, 1, -t);
 
         Vector3 ai = new Vector3(t, 0, -1);
         Vector3 aj = new Vector3(t, 0, 1);
         Vector3 ak = new Vector3(-t, 0, -1);
         Vector3 al = new Vector3(-t, 0, 1);
 
         verticies.Add(aa);
         verticies.Add(ab);
         verticies.Add(ac);
         verticies.Add(ad);
         verticies.Add(ae);
         verticies.Add(af);
         verticies.Add(ag);
         verticies.Add(ah);
         verticies.Add(ai);
         verticies.Add(aj);
         verticies.Add(ak);
         verticies.Add(al);
 
         List<int> triangles = new List<int>();
 
         triangles.AddRange(new 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
         });
         #endregion
         mf = GetComponent<MeshFilter>();
         mf.mesh = CreateIcosohedron(verticies, triangles);
         print(mf.mesh.triangles.Length);
         print(mf.mesh.vertices.Length);
         for (int tri = 0; tri < triangles.Count/3; tri+=3)
         {
             Vector3 tempV1 = new Vector3();
             Vector3 tempV2 = new Vector3();
             Vector3 tempV3 = new Vector3();
             Vector3 NewVS12 = new Vector3();
             Vector3 NewVS23 = new Vector3();
             Vector3 NewVS13 = new Vector3();
             int[] TrianglesArray = triangles.ToArray();
             Vector3[] VertsArray = verticies.ToArray();
             tempV1 = VertsArray[tri];
             tempV2 = VertsArray[tri + 1];
             tempV3 = VertsArray[tri + 2];
             triangles.Remove(0);
             triangles.Remove(1);
             triangles.Remove(2);
             NewVS12 = Vector3.Slerp(tempV1, tempV2, 0.5f);
             NewVS23 = Vector3.Slerp(tempV2, tempV3, 0.5f);
             NewVS13 = Vector3.Slerp(tempV1, tempV3, 0.5f);
 
             verticies.Add(NewVS12);
             verticies.Add(NewVS23);
             verticies.Add(NewVS13);
 
             int VertNum1 = verticies.Count-2; //12
             int VertNum2 = verticies.Count - 1; //23
             int VertNum3 = verticies.Count; //13
 
             triangles.Add(VertNum1);
             triangles.Add(VertNum2); //Middle triangle
             triangles.Add(VertNum3);
 
             triangles.Add(VertNum1);
             triangles.Add(tri);     // adds 12
             triangles.Add(tri + 1);
 
             triangles.Add(VertNum2);
             triangles.Add(tri + 2); // adds 23
             triangles.Add(tri + 1);
 
             triangles.Add(VertNum3);
             triangles.Add(tri); // adds 13
             triangles.Add(tri + 2);
         }
         /*  Cycle through each triangle /
             turn lists into arrays /
             store old verts /
             Delete the triangle /
             make new verticies /
             Slerp verticies /
             Add Verts to list /
             Get what number the verticies are /
             Make 4 triangles ?
         */
         mf.mesh = CreateIcosohedron(verticies, triangles);
     }
 
     Mesh CreateIcosohedron(List<Vector3> Verts, List<int> triangles)
     {
         Mesh m;
         m = new Mesh();
         float t = (1 + Mathf.Sqrt(5)) / 2;
         Vector3[] array = new Vector3[99999];
         array = Verts.ToArray();
         m.vertices = array;
         int[] arrayt = new int[99999];
         arrayt = triangles.ToArray();
         m.triangles = arrayt;
         return m;
     }
 }
 

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
1
Best Answer

Answer by tanoshimi · May 06, 2016 at 02:40 PM

That's because you have an infinite loop:

 for (int tri = 0; tri < triangles.Count/3; tri+=3)

In the body of that loop you're removing 3 elements from triangles, but adding 12 new ones, so your counter is never going to reach triangles.Count/3.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Find closest point in triangle to point? 1 Answer

How do I get the color of a certain mesh vertex? 0 Answers

Sketchup models import with 0 verts/tris? 0 Answers

Best way to optimize mesh updating through code? 1 Answer

Connecting flatshaded vertices 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