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
0
Question by cclaypool1 · May 17, 2013 at 11:05 PM · terrainmeshprocedurallod

Simple LOD not working correctly.

I have created a simple LOD method here:

 public static void SphereLOD(Vector3 camPos, Mesh mesh)
     {
         newVectices = new Dictionary<uint,int>();
         List<Vector3> meshverts = new List<Vector3>(mesh.vertices);
         normals = new List<Vector3>(mesh.normals);
         uvs = new List<Vector2>(mesh.uv);
         indices = new List<int>();
         Vector3[] closest = new Vector3[9];
         closest = mesh.vertices.OrderBy(v=>(v - camPos).sqrMagnitude).Take(9).ToArray();
         Vector3 originate = (closest[0] + closest[1] + closest[2] + closest[3] + closest[4] + closest[5]
              + closest[6] + closest[7] + closest[8]) / 9;
         float length = ((closest[0] - originate).magnitude + (closest[1] - originate).magnitude +
              (closest[2] - originate).magnitude + (closest[3] - originate).magnitude +
              (closest[4] - originate).magnitude + (closest[5] - originate).magnitude +
              (closest[6] - originate).magnitude + (closest[7] - originate).magnitude +
              (closest[8] - originate).magnitude) / 9;
         closestlist = new List<Vector3>(closest);
         vertices = new List<Vector3>(closest);
     
         oldclose = closestlist;
         int[] triangles = mesh.triangles;
         if ((closest[1] - camPos).magnitude > (2 * length))
         {
             
             for (int i = 0; i < triangles.Length; i += 3)
                 {
                 int i1 = triangles[i + 0];
                 int i2 = triangles[i + 1];
                 int i3 = triangles[i + 2];
  
                 int a = GetNewVertex(i1, i2);
                 int b = GetNewVertex(i2, i3);
                 int c = GetNewVertex(i3, i1);
                 indices.Add(i1);   indices.Add(a);   indices.Add(c);
                 indices.Add(i2);   indices.Add(b);   indices.Add(a);
                 indices.Add(i3);   indices.Add(c);   indices.Add(b);
                 indices.Add(a );   indices.Add(b);   indices.Add(c); // center triangle
                 }
         }
         for(int i = 1; i < vertices.Count; i++)
         {
             meshverts.Add (vertices[i]);
         }
         
         mesh.vertices = meshverts.ToArray();
         //mesh.normals = normals.ToArray();
         //mesh.uv = uvs.ToArray();
         mesh.triangles = indices.ToArray();
         
         newVectices = null;
         vertices = null;
         normals = null;
         indices = null;
         
         
     }

From here, I go into the a script and call MeshHelper.SphereLOD(camPos, mesh); and it should subdivide the closes 9 vertices to the camera when the camera gets close enough. The problem is that when I apply this to any object, strange things happen. The object's triangles are thrown about and the mesh is destroyed. Can anyone help me isolate the problem?

Edit: Here is the GetNewVertices code:

 static int GetNewVertex(int i1, int i2)
     {
 
         uint t1 = ((uint)i1 << 16) | (uint)i2;
         uint t2 = ((uint)i2 << 16) | (uint)i1;
         if (newVectices.ContainsKey(t2))
             return newVectices[t2];
         if (newVectices.ContainsKey(t1))
             return newVectices[t1];
 
         int newIndex = vertices.Count;
         newVectices.Add(t1,newIndex);
  
  
         vertices.Add((vertices[i1] + vertices[i2]) * 0.5f);
         normals.Add((normals[i1] + normals[i2]).normalized);
         uvs.Add((uvs[i1] + uvs[i2]) * 0.5f);
  
         return newIndex;
     }


These images show that the object is responding to the camera position (see triangles pulling toward it) but not in the intended manner.

Image 1

Image 2

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

· Add your reply
  • Sort: 

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

13 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

Related Questions

Rivers on procedurally terrain 0 Answers

How to load/save 'Chunks' near cam for mesh terrain? 0 Answers

Mesh based on noise 1 Answer

Huge world without huge lag? 1 Answer

Discrete divisions on terrain? 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