Fog of War на андроид устройствах в Unity - PullRequest
0 голосов
/ 28 мая 2018

Я написал код в соответствии с этого урока .

Это код:

public GameObject fogOfWarPlane;
    public LayerMask fogLayer;
    public float radius = 5f;

    private float radiusSqr { get { return radius * radius; } }
    private Transform target;
    private Mesh mesh;
    private Vector3[] vertices;
    private Color[] colors;

    private void Awake()
    {
        mesh = fogOfWarPlane.GetComponent<MeshFilter>().mesh;
    }

    // Use this for initialization
    void Start()
    {
        Initialize();
    }

    // Update is called once per frame
    void Update()
    {
        if (target)
        {
            //Ray from camera to target and find collided vertex
            Ray r = new Ray(transform.position, target.position - transform.position);
            RaycastHit hit;
            if (Physics.Raycast(r, out hit, 1000, fogLayer, QueryTriggerInteraction.Collide))
            {
                //Find vertices in specific area and fade alpha from center to border
                for (int i = 0; i < vertices.Length; i++)
                {
                    Vector3 v = fogOfWarPlane.transform.TransformPoint(vertices[i]);
                    float dist = Vector3.SqrMagnitude(v - hit.point);
                    if (dist < radiusSqr)
                    {
                        float alpha = Mathf.Min(colors[i].a, dist / radiusSqr);
                        colors[i].a = alpha;
                    }
                }
                UpdateColor();
            }
        }
    }

    void Initialize()
    {
        vertices = mesh.vertices;
        colors = new Color[vertices.Length];
        for (int i = 0; i < colors.Length; i++)
        {
            colors[i] = Color.black;
        }
        UpdateColor();
    }

    void UpdateColor()
    {
        mesh.colors = colors;
    }

    public void SetTarget(Transform target)
    {
        this.target = target;
    }

Кроме того, я хочу вернуться к непрозрачнымчерный через некоторое время.

Итак, я добавил 3 метода, которые я хочу решить, какой из них является наиболее оптимизированным.

Новый код (Просто обратите внимание, я просто использую один изэти методы в моей игре не одновременно):

public GameObject fogOfWarPlane;
    public LayerMask fogLayer;
    public float radius = 5f;
    public float fadeSpeed;

    private float radiusSqr { get { return radius * radius; } }
    private Transform target;
    private Mesh mesh;
    private Vector3[] vertices;
    private Color[] colors;
    private List<int> transparentVerticesIndexList = new List<int>();
    private int[] transparentVerticesIndexArray;

    private void Awake()
    {
        mesh = fogOfWarPlane.GetComponent<MeshFilter>().mesh;
    }

    // Use this for initialization
    void Start()
    {
        Initialize();
    }

    // Update is called once per frame
    void Update()
    {
        if (target)
        {
            //Ray from camera to target and find collided vertex
            Ray r = new Ray(transform.position, target.position - transform.position);
            RaycastHit hit;
            if (Physics.Raycast(r, out hit, 1000, fogLayer, QueryTriggerInteraction.Collide))
            {
                //Find vertices in specific area and fade alpha from center to border
                for (int i = 0; i < vertices.Length; i++)
                {
                    Vector3 v = fogOfWarPlane.transform.TransformPoint(vertices[i]);
                    float dist = Vector3.SqrMagnitude(v - hit.point);
                    if (dist < radiusSqr)
                    {
                        float alpha = Mathf.Min(colors[i].a, dist / radiusSqr);
                        /******Related to both method 1 and 3******/
                        if (!transparentVerticesIndexList.Contains(i))
                        {
                            transparentVerticesIndexList.Add(i);
                        }
                        /************/
                        colors[i].a = alpha;
                    }
                }
                /******Related to both method 1******/
                transparentVerticesIndexArray = transparentVerticesIndexList.ToArray();
                /************/
                UpdateColor();
            }
        }

        //Method 1
        //Fading transparent vertices to opaque
        if (transparentVerticesIndexArray != null)
        {
            for (int i = 0; i < transparentVerticesIndexArray.Length; i++)
            {
                if (colors[transparentVerticesIndexArray[i]].a < 1)
                {
                    colors[transparentVerticesIndexArray[i]].a += Time.deltaTime * fadeSpeed;
                }
                else
                {
                    colors[transparentVerticesIndexArray[i]].a = 1;
                    transparentVerticesIndexList.Remove(transparentVerticesIndexArray[i]);
                    transparentVerticesIndexArray = transparentVerticesIndexList.ToArray();
                }
            }
        }

        //Assign it to actual vertices color
        if (transparentVerticesIndexList.Count != 0)
        {
            mesh.colors = colors;
        }

        //Method 2
        for (int i = 0; i < colors.Length; i++)
        {
            if (colors[i].a < 1)
            {
                colors[i].a += Time.deltaTime * fadeSpeed;
            }
            else
            {
                colors[i].a = 1;
            }
        }

        mesh.colors = colors;

        //Method 3
        //Fading transparent vertices to opaque
        for (int i = 0; i < transparentVerticesIndexList.Count; i++)
        {
            if (colors[transparentVerticesIndexList[i]].a < 1)
            {
                colors[transparentVerticesIndexList[i]].a += Time.deltaTime * fadeSpeed;
            }
            else
            {
                colors[transparentVerticesIndexList[i]].a = 1;
                transparentVerticesIndexList.Remove(transparentVerticesIndexList[i]);
            }
        }

        //Assign it to actual vertices color
        if (transparentVerticesIndexList.Count != 0)
        {
            mesh.colors = colors;
        }
    }

    void Initialize()
    {
        vertices = mesh.vertices;
        colors = new Color[vertices.Length];
        for (int i = 0; i < colors.Length; i++)
        {
            colors[i] = Color.black;
        }
        UpdateColor();
    }

    void UpdateColor()
    {
        mesh.colors = colors;
    }

    public void SetTarget(Transform target)
    {
        this.target = target;
    }

Прежде всего, я хочу знать, какой из них вы считаете наиболее оптимизированным?

Второй Если ни один из нихкаково ваше предложение?

...