示例#1
0
    private void OnDrawGizmos()
    {
        if (binarySpacePartition == null)
        {
            Start();
        }

        // Draw a line between the probe and the closest triangle vertex via the BSP...
        Vector3 queryPoint = probe.position;

        if (drawNaiveClosestPoint)
        {
            Profiler.BeginSample("Naive Closest Point on Mesh");
            float naiveMinDist = 1000f; Vector3 naiveMinPos = Vector3.one;
            for (int i = 0; i < meshTriangles.Length; i += 3)
            {
                Vector3 trianglePoint = Constraints.ConstrainToTriangle(queryPoint,
                                                                        meshVertices[meshTriangles[i + 0]],
                                                                        meshVertices[meshTriangles[i + 1]],
                                                                        meshVertices[meshTriangles[i + 2]]);
                float dist1 = Vector3.Distance(queryPoint, trianglePoint);
                if (dist1 < naiveMinDist)
                {
                    naiveMinDist = dist1; naiveMinPos = trianglePoint;
                }
            }
            Gizmos.color = Color.red;
            Gizmos.DrawLine(queryPoint, naiveMinPos);
            Gizmos.DrawSphere(naiveMinPos, 0.005f);

            Profiler.EndSample();
        }

        Profiler.BeginSample("BSP Closest Point on Mesh", this);
        Gizmos.color = Color.white;
        Vector3 bspMinPos = Vector3.zero; float bspMinSqDist = 100000f;

        binarySpacePartition.QueryClosestPointRecursive(queryPoint, ref bspMinPos, ref bspMinSqDist);
        Gizmos.DrawLine(queryPoint, bspMinPos);
        Gizmos.DrawSphere(bspMinPos, 0.005f);
        Profiler.EndSample();

#if G3_USING_UNITY
        if (g3MeshTree != null && g3MeshTree.IsValid)
        {
            Profiler.BeginSample("D3 Closest Point on Mesh", this);
            Gizmos.color = Color.magenta;
            int     nearestTri     = g3MeshTree.FindNearestTriangle(new g3.Vector3d(queryPoint.x, queryPoint.y, queryPoint.z));
            Vector3 trianglePointt = Constraints.ConstrainToTriangle(queryPoint,
                                                                     meshVertices[meshTriangles[(nearestTri * 3)]],
                                                                     meshVertices[meshTriangles[(nearestTri * 3) + 1]],
                                                                     meshVertices[meshTriangles[(nearestTri * 3) + 2]]);
            Gizmos.DrawLine(queryPoint, trianglePointt);
            Gizmos.DrawSphere(trianglePointt, 0.005f);
            Profiler.EndSample();
        }
#endif
    }