Beispiel #1
0
        /// <summary>
        /// Renders the octree to the scene
        /// </summary>
        public void OnSceneGUI(Transform rTransform)
        {
#if UNITY_EDITOR
            if (Root != null)
            {
                Root.OnSceneGUI(rTransform);
            }
#endif
        }
Beispiel #2
0
        /// <summary>
        /// Renders the octree to the scene
        /// </summary>
        public void OnSceneGUI()
        {
            if (!RenderOctree && !RenderMesh && !RenderTestNode && !RenderTestTriangle)
            {
                return;
            }

            Color lHandlesColor = Handles.color;

            if (MeshOctree == null || MeshOctree.Root == null)
            {
                Stopwatch lTimer = new Stopwatch();
                lTimer.Start();

                Mesh lMesh = ExtractMesh();
                if (lMesh != null)
                {
                    MeshOctree = new MeshOctree(lMesh);
                }

                lTimer.Stop();
                ParseTime = lTimer.ElapsedTicks / (float)TimeSpan.TicksPerSecond;
            }

            if (MeshOctree != null && MeshOctree.Root != null)
            {
                if (RenderOctree)
                {
                    MeshOctree.OnSceneGUI(transform);
                }

                if (RenderMesh)
                {
                    Handles.color = new Color(0f, 1f, 0f, 0.5f);

                    int[]     lTriangles = MeshOctree.Root.MeshTriangles;
                    Vector3[] lVertices  = MeshOctree.Root.MeshVertices;

                    int lTriangleCount = lTriangles.Length / 3;
                    for (int i = 0; i < lTriangleCount; i++)
                    {
                        int     lIndex   = i * 3;
                        Vector3 lVertex1 = transform.TransformPoint(lVertices[lTriangles[lIndex]]);
                        Vector3 lVertex2 = transform.TransformPoint(lVertices[lTriangles[lIndex + 1]]);
                        Vector3 lVertex3 = transform.TransformPoint(lVertices[lTriangles[lIndex + 2]]);

                        Handles.DrawLine(lVertex1, lVertex2);
                        Handles.DrawLine(lVertex2, lVertex3);
                        Handles.DrawLine(lVertex3, lVertex1);
                    }
                }

                if (RenderTestTriangle)
                {
                    Handles.color = Color.magenta;

                    Vector3 lLocalPoint = transform.InverseTransformPoint(TestTransform != null ? TestTransform.position : TestPosition);

                    // First, let's see if our point is inside the root node. If not, make it so.
                    if (!MeshOctree.ContainsPoint(lLocalPoint))
                    {
                        if (lLocalPoint.x < MeshOctree.Root.Min.x)
                        {
                            lLocalPoint.x = MeshOctree.Root.Min.x;
                        }
                        else if (lLocalPoint.x > MeshOctree.Root.Max.x)
                        {
                            lLocalPoint.x = MeshOctree.Root.Max.x;
                        }

                        if (lLocalPoint.y < MeshOctree.Root.Min.y)
                        {
                            lLocalPoint.y = MeshOctree.Root.Min.y;
                        }
                        else if (lLocalPoint.y > MeshOctree.Root.Max.y)
                        {
                            lLocalPoint.y = MeshOctree.Root.Max.y;
                        }

                        if (lLocalPoint.z < MeshOctree.Root.Min.z)
                        {
                            lLocalPoint.z = MeshOctree.Root.Min.z;
                        }
                        else if (lLocalPoint.z > MeshOctree.Root.Max.z)
                        {
                            lLocalPoint.z = MeshOctree.Root.Max.z;
                        }
                    }

                    // Grab the closest triangle
                    int lTriangleIndex = MeshOctree.Root.ClosestTriangle(lLocalPoint);
                    if (lTriangleIndex >= 0)
                    {
                        Vector3 lVertex1 = transform.TransformPoint(MeshOctree.Root.MeshVertices[MeshOctree.Root.MeshTriangles[lTriangleIndex]]);
                        Vector3 lVertex2 = transform.TransformPoint(MeshOctree.Root.MeshVertices[MeshOctree.Root.MeshTriangles[lTriangleIndex + 1]]);
                        Vector3 lVertex3 = transform.TransformPoint(MeshOctree.Root.MeshVertices[MeshOctree.Root.MeshTriangles[lTriangleIndex + 2]]);

                        Handles.DrawLine(lVertex1, lVertex2);
                        Handles.DrawLine(lVertex2, lVertex3);
                        Handles.DrawLine(lVertex3, lVertex1);

#if UNITY_4 || UNITY_5_0 || UNITY_5_1 || UNITY_5_2 || UNITY_5_3 || UNITY_5_4 || UNITY_5_5
                        Handles.SphereCap(0, transform.TransformPoint(lLocalPoint), Quaternion.identity, 0.05f);
#else
                        Handles.SphereHandleCap(0, transform.TransformPoint(lLocalPoint), Quaternion.identity, 0.05f, EventType.Layout | EventType.Repaint);
#endif
                    }
                }

                if (RenderTestNode)
                {
                    Vector3 lLocalPoint = transform.InverseTransformPoint(TestTransform != null ? TestTransform.position : TestPosition);

                    // Grab the closest node
                    MeshOctreeNode lNode = MeshOctree.Root.ClosestNode(lLocalPoint);
                    if (lNode != null)
                    {
                        Handles.color = Color.white;
                        lNode.OnSceneGUI(transform);

                        if (lNode.TriangleIndexes != null)
                        {
                            Handles.color = Color.magenta;
                            for (int i = 0; i < lNode.TriangleIndexes.Count; i++)
                            {
                                int     lTriangleIndex = lNode.TriangleIndexes[i];
                                Vector3 lVertex1       = transform.TransformPoint(MeshOctree.Root.MeshVertices[MeshOctree.Root.MeshTriangles[lTriangleIndex]]);
                                Vector3 lVertex2       = transform.TransformPoint(MeshOctree.Root.MeshVertices[MeshOctree.Root.MeshTriangles[lTriangleIndex + 1]]);
                                Vector3 lVertex3       = transform.TransformPoint(MeshOctree.Root.MeshVertices[MeshOctree.Root.MeshTriangles[lTriangleIndex + 2]]);

                                Handles.DrawLine(lVertex1, lVertex2);
                                Handles.DrawLine(lVertex2, lVertex3);
                                Handles.DrawLine(lVertex3, lVertex1);
                            }
                        }
                    }
                }
            }

            Handles.color = lHandlesColor;
        }