Ejemplo n.º 1
0
    EdgeInfo FindEdge(ViewCastInFo minViewCast, ViewCastInFo maxViewCast)
    {
        float   minAngle = minViewCast.angle;
        float   maxAngle = maxViewCast.angle;
        Vector3 minpoint = Vector3.zero;
        Vector3 maxpoint = Vector3.zero;

        for (int i = 0; i < edgeResolveIterations; i++)
        {
            float        angle       = (minAngle + maxAngle) / 2;
            ViewCastInFo newViewCast = ViewCast(angle);

            bool edgeDstThresholdExceeded = Mathf.Abs(minViewCast.dst - newViewCast.dst) > edgeDstThreshold;
            if (newViewCast.hit == minViewCast.hit && !edgeDstThresholdExceeded)
            {
                minAngle = angle;
                minpoint = newViewCast.point;
            }
            else
            {
                maxAngle = angle;
                maxpoint = newViewCast.point;
            }
        }
        return(new EdgeInfo(minpoint, maxpoint));
    }
Ejemplo n.º 2
0
    //绘制视线区域
    void DrawFieldOfView()
    {
        int            stepCount     = Mathf.RoundToInt(viewAngle * meshResolution);
        float          stepAngleSize = viewAngle / stepCount;
        List <Vector3> viewPoints    = new List <Vector3> ();
        ViewCastInFo   oldViewCast   = new ViewCastInFo();

        for (int i = 0; i <= stepCount; i++)
        {
            float angle = transform.eulerAngles.y - viewAngle / 2 + stepAngleSize * i;
            //Debug.DrawLine (transform.position, transform.position + DirFromAngle (angle, true) * viewRadius, Color.red);
            ViewCastInFo newViewCast = ViewCast(angle);

            if (i > 0)
            {
                bool edgeDstThresholdExceeded = Mathf.Abs(oldViewCast.dst - newViewCast.dst) > edgeDstThreshold;
                if (oldViewCast.hit != newViewCast.hit || (oldViewCast.hit && newViewCast.hit && edgeDstThresholdExceeded))
                {
                    EdgeInfo edge = FindEdge(oldViewCast, newViewCast);
                    if (edge.pointA != Vector3.zero)
                    {
                        viewPoints.Add(edge.pointA);
                    }
                    if (edge.pointB != Vector3.zero)
                    {
                        viewPoints.Add(edge.pointA);
                    }
                }
            }

            viewPoints.Add(newViewCast.point);
            oldViewCast = newViewCast;
        }
        int vertexCount = viewPoints.Count + 1;

        Vector3[] vertices  = new Vector3[vertexCount];
        int[]     triangles = new int[(vertexCount - 2) * 3];

        vertices [0] = Vector3.zero;
        for (int i = 0; i < vertexCount - 1; i++)
        {
            vertices [i + i] = transform.InverseTransformPoint(viewPoints [i]);
            if (i < vertexCount - 2)
            {
                triangles [i * 3]     = 0;
                triangles [i * 3 + 1] = i = 1;
                triangles [i * 3 + 2] = i + 2;
            }
        }
        viewMesh.Clear();
        viewMesh.vertices  = vertices;
        viewMesh.triangles = triangles;
        viewMesh.RecalculateNormals();
    }