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 /
avatar image
0
Question by DanaScully · Jul 03, 2017 at 01:28 PM · meshverticestrianglesmeshfilter

Save only visible part of mesh

I am trying to save only visible parts of my cloth mesh as obj file. So far I have the visible vertices and their corresponding vertex indices (triangles). But when I save my mesh, the visible part is removed and the rest is saved. i want to reach exactly the opposite but changing the if statement destroys the whole mesh. Please tell me where I go wrong.

  void Update()
     {
          clothMesh = Instantiate(cloth.gameObject.GetComponent<MeshFilter>().sharedMesh);
 
         Vector3[] vertices = clothMesh.vertices;
         int[] triangles = clothMesh.triangles;
 
             
         List<Vector3> visibleVertices = new List<Vector3>();
         List<int> visibleTriIndecies = new List<int>();
 
         //--find out the visible vertices
         Vector3 visibleVertex = new Vector3();
 
         for(int i=0; i < vertices.Length;i++)
         {
             visibleVertex = cam.WorldToScreenPoint(cloth.transform.TransformPoint( vertices[i]));
 
             //---if vertex is visible
             if(visibleVertex.x <= cam.pixelWidth && visibleVertex.x >= 0 && visibleVertex.y >= 0 && visibleVertex.y <= cam.pixelHeight)
             {
                 visibleVertices.Add(vertices[i]);
                 
             }
         }
 
         //---find out the vertex index of visible vertices
         for (int i = 0; i < triangles.Length; i+=3)
         {
             foreach(Vector3 vpoint in visibleVertices)
             {
                 if(vpoint == vertices[triangles[i]])
                 {
                     visibleTriIndecies.Add(triangles[i]);
                     visibleTriIndecies.Add(triangles[i+1]);
                     visibleTriIndecies.Add(triangles[i+2]);
                 }
             }
         }
 
         Mesh mesh =  clothMesh;
         int[] newtris = mesh.triangles;
 
         for(int i = 0; i < visibleTriIndecies.Count; i+=3)
         {
             for(int j = 0; j < newtris.Length; j+=3)
             {
                 if(newtris[j] == visibleTriIndecies[i] && newtris[j + 1] == visibleTriIndecies[i + 1] && newtris[j + 2] == visibleTriIndecies[i + 2])
                 {
                                 newtris[j] = 0;
                                 newtris[j + 1] = 0;
                                 newtris[j + 2] = 0;
                 }
             }
         }
 
      
         mesh.triangles = newtris;
 
         Debug.Log("Vertex count: " + visibleVertices.Count);
 
 
 
         SaveMeshAsObj(mesh);
         Destroy(mesh);
 
     }



  

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

Answer by DanaScully · Jul 04, 2017 at 06:52 AM

The answer was another for loop. May not be optimal but gives the result.

for(int i = 0; i < newtris.Length; i+=3) { if(newtris[i] == triangles[i] && newtris[i+1] == triangles[i+1] && newtris[i+2] == triangles[i+2]) { newtris[i] = 0; newtris[i + 1] = 0; newtris[i + 2] = 0; } else { newtris[i] = triangles[i]; newtris[i + 1] = triangles[i + 1]; newtris[i + 2] = triangles[i + 2]; } }

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

71 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

Related Questions

Generated Mesh Triangles not Being Made/Visible? 1 Answer

Extruding faces of a mold. Need to properly assign vertices to triangles 0 Answers

Null Reference Exception Assigning Vertices to Mesh 0 Answers

Why vertex positions appear (0.0, 0.0, 0.0) ? 1 Answer

Lighting up all triangles between two points in a mesh? 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