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*.GetDistanceToPoint(Camera.main.transform.position) <= 0)*
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);