Beispiel #1
0
        void RecursivePartition(List <int> triangles, int depth, Node parent)
        {
            Vector3 partitionPoint = Vector3.zero;

            Vector3 maxExtents = new Vector3(-float.MaxValue, -float.MaxValue, -float.MaxValue);
            Vector3 minExtents = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);

            foreach (int triangle in triangles)
            {
                partitionPoint += vertices[tris[triangle]] + vertices[tris[triangle + 1]] + vertices[tris[triangle + 2]];

                minExtents.x = Mathf.Min(minExtents.x, vertices[tris[triangle]].x, vertices[tris[triangle + 1]].x, vertices[tris[triangle + 2]].x);
                minExtents.y = Mathf.Min(minExtents.y, vertices[tris[triangle]].y, vertices[tris[triangle + 1]].y, vertices[tris[triangle + 2]].y);
                minExtents.z = Mathf.Min(minExtents.z, vertices[tris[triangle]].z, vertices[tris[triangle + 1]].z, vertices[tris[triangle + 2]].z);

                maxExtents.x = Mathf.Max(maxExtents.x, vertices[tris[triangle]].x, vertices[tris[triangle + 1]].x, vertices[tris[triangle + 2]].x);
                maxExtents.y = Mathf.Max(maxExtents.y, vertices[tris[triangle]].y, vertices[tris[triangle + 1]].y, vertices[tris[triangle + 2]].y);
                maxExtents.z = Mathf.Max(maxExtents.z, vertices[tris[triangle]].z, vertices[tris[triangle + 1]].z, vertices[tris[triangle + 2]].z);
            }

            // Centroid of all vertices
            partitionPoint /= vertexCount;

            // Better idea? Center of bounding box
            partitionPoint = minExtents + Math3d.SetVectorLength((maxExtents - minExtents), (maxExtents - minExtents).magnitude * 0.5f);

            Vector3 extentsMagnitude = new Vector3(Mathf.Abs(maxExtents.x - minExtents.x), Mathf.Abs(maxExtents.y - minExtents.y), Mathf.Abs(maxExtents.z - minExtents.z));

            Vector3 partitionNormal;

            if (extentsMagnitude.x >= extentsMagnitude.y && extentsMagnitude.x >= extentsMagnitude.z)
            {
                partitionNormal = Vector3.right;
            }
            else if (extentsMagnitude.y >= extentsMagnitude.x && extentsMagnitude.y >= extentsMagnitude.z)
            {
                partitionNormal = Vector3.up;
            }
            else
            {
                partitionNormal = Vector3.forward;
            }

            List <int> positiveTriangles;
            List <int> negativeTriangles;

            Split(triangles, partitionPoint, partitionNormal, out positiveTriangles, out negativeTriangles);

            parent.partitionNormal = partitionNormal;
            parent.partitionPoint  = partitionPoint;

            Node posNode = new Node();

            parent.positiveChild = posNode;

            Node negNode = new Node();

            parent.negativeChild = negNode;

            if (positiveTriangles.Count < triangles.Count && positiveTriangles.Count > 3)
            {
                RecursivePartition(positiveTriangles, depth + 1, posNode);
            }
            else
            {
                posNode.triangles = positiveTriangles.ToArray();

                if (drawMeshTreeOnStart)
                {
                    DrawTriangleSet(posNode.triangles, DebugDraw.RandomColor());
                }
            }

            if (negativeTriangles.Count < triangles.Count && negativeTriangles.Count > 3)
            {
                RecursivePartition(negativeTriangles, depth + 1, negNode);
            }
            else
            {
                negNode.triangles = negativeTriangles.ToArray();

                if (drawMeshTreeOnStart)
                {
                    DrawTriangleSet(negNode.triangles, DebugDraw.RandomColor());
                }
            }
        }
Beispiel #2
0
 public static void DrawVector(Vector3 position, Vector3 direction, float raySize, float markerSize, Color color, float duration, bool depthTest = true)
 {
     Debug.DrawRay(position, direction * raySize, color, 0, false);
     DebugDraw.DrawMarker(position + direction * raySize, markerSize, color, 0, false);
 }