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 Tharzzat · Jan 18 at 09:53 AM · script.

How do I add a plane to every triangle in a mesh?

I'm trying to do something like point in polyhedron with planes. Adding a plane to every triangle in a mesh. I don't know how to fix it.

         Mesh Mesh = GetComponent<MeshFilter>().mesh;
 
         for (int i = 0; i < Mesh.triangles.Length; i += 3)
         {
             p1.Add(Mesh.vertices[Mesh.triangles[i + 0]]);
             p2.Add(Mesh.vertices[Mesh.triangles[i + 1]]);
             p3.Add(Mesh.vertices[Mesh.triangles[i + 2]]);
 
             Triangles.Add(new Plane(p1[0], p2[0], p3[0]));
         }
 
         int TriCount = Triangles.Count;
 
         for (int i = 0; i < TriCount; i++)
         {
             //  Set active is true when inside the Cube
             if (Triangles[i].GetDistanceToPoint(Camera.main.transform.position) <= 0)
             {
                 Cube.SetActive(true);
             }
             else
             {
                 Cube.SetActive(false);
             }
         }
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

2 Replies

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

Answer by Bunny83 · Jan 18 at 10:27 AM

Well, first of all your generation of your plane list is extremely inefficient. Each time you access Mesh.triangles or Mesh.vertices you will create a new triangle / vertex array. So your first loop creates tons of garbage. You should always store those two arrays in a local variable (so you only generate those arrays once) and then use those local variables to read the array elements.

Second: It seems really strange that you called your List "Triangles" when it contains Planes. If you read your own code in a year you will be maximally confused when you try to make sense of it. So I highly recommend you refactor the name of your List to something meaningful, like "Planes"?


Though your actual issue is that your second loop doesn't do what you think it does. In order for a point to be inside a convex polyhedron the point has to be "behind" every face of the polyhedron. Currently you're switching back and forth for each individual plane you're processing. Another strange thins is your use of the p0, p1 and p2 lists. You add the points to the end of those lists, but you always read the first elements from those lists. So you don't really create seperate planes. You create the same plane from the first 3 points several times.


What you want to do is knowing if the point is behind all planes. The easiest solution is to reverse the question. What does it take to know that a point is outside the polyhedra? Exactly, whenever the point you want to test is outside / before any one of the planes, you know that the point is outside. You can even stop checking the other planes because we already know it's outside. That's why such an implementation usually looks like this:

 // changed "mesh" to "sharedMesh". Accessing "mesh" would duplicate the mesh for this MeshFilter
 Mesh mesh = GetComponent<MeshFilter>().sharedMesh;
 // as explained, store the triangles and vertices arrays in local variables
 var triangles = mesh.triangles;
 var vertices = mesh.vertices;
 for (int i = 0; i < triangles.Length; i += 3)
 {
     Vector3 p1 = Mesh.vertices[Mesh.triangles[i + 0]];
     Vector3 p2 = Mesh.vertices[Mesh.triangles[i + 1]];
     Vector3 p3 = Mesh.vertices[Mesh.triangles[i + 2]];
     Planes.Add(new Plane(p1, p2, p3));
 }

 // start with the assumption we're inside
 bool isInside = true;
 // store the camera position once because accessing Camera.main is expensive.
 Vector3 camPos = Camera.main.transform.position;
 // check plane by plane
 foreach(var plane in Planes)
 {
     // if camera position is outside / in front of a plane, we know we're outside and can stop
     if (plane.GetDistanceToPoint(camPos) > 0)
     {
         isInside = false;
         break;
     }
 }
 // when we get here and isInside is still true, we know that none of the planes have stopped the loop
 // so we're inside
 Cube.SetActive(isInside);
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
avatar image
0

Answer by Tharzzat · Jan 18 at 02:38 PM

Thanks.

I had to change the points to world space.

             Vector3 tp1 = transform.TransformPoint(p1);
             Vector3 tp2 = transform.TransformPoint(p2);
             Vector3 tp3 = transform.TransformPoint(p3);
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

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

How can i make character head to rotate according to the mouse cursor position ? 0 Answers

Why when getting component the variable is null ? 1 Answer

need some help with this things 0 Answers

Can I make a mod for a unity game with non-unity software? 0 Answers

Create Text file and open - write on it after button click 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