c# - Unity 中安卓设备上的战争迷雾
问题描述
我已经根据本教程编写了代码。
这是代码:
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;
}
首先我想知道你认为哪一个是最优化的?
第二如果他们都没有,你的建议是什么?
解决方案
推荐阅读
- react-admin - 事件 onBlur 是否与自定义字段上的 React Admin useInput 挂钩?
- debugging - Intellij 中的项目 reactor 调试
- python - 在 boxplot python 中添加是或否元素
- java - 卸载 Open JDK Zulu - 安装 Oracle JDK / Mac
- algorithm - A* 的可接受启发式
- javascript - JavaScript 过滤掉一个元素
- r - R离线包安装
- javascript - 制表符分隔树到 JSON
- sql - 如何选择重复两个属性的行
- autodesk-forge - 如何从 Forge Viewer 中检索文件名?