我已根据this tutorial编写了代码。
这是代码:
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;
}
首先,我想知道您认为哪一个是最优化的一个?
第二,如果他们都不是你的建议么?