我想在 Unity 中渲染流线。流线是许多空间曲线,每个顶点都有自己的颜色。像下图:
Unity 的 LineRenderer 似乎无法为单个节点分配颜色。所以我该怎么做?
您可以使用MeshTopology.Lines或MeshTopology.LineStrip创建网格
直接来自文档:
...在某些情况下,您可能想要渲染由线或点组成的复杂事物。使用该拓扑创建网格并使用它进行渲染通常是最有效的方法。
下面是一个创建线带网格的脚本。只需将它放在一个空的游戏对象上。
网格看起来像这样:
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class LineStrip : MonoBehaviour {
void Start() {
GetComponent<MeshRenderer>().material = new Material(Shader.Find("Sprites/Default"));
int n = 512;
Vector3[] verts = new Vector3[n];
Color[] colors = new Color[n];
int[] indices = new int[n];
for (int i = 0; i < n; i++)
{
// Indices in the verts array. First two indices form a line,
// and then each new index connects a new vertex to the existing line strip
indices[i] = i;
// Vertex colors
colors [i] = Color.HSVToRGB( (float)i/n, 1, 1 );
// Vertex positions
verts[i] = new Vector3( i / 64f, Mathf.Sin( i/32f ), 0);
}
Mesh m = new Mesh
{
vertices = verts,
colors = colors
};
m.SetIndices(indices, MeshTopology.LineStrip, 0, true);
GetComponent<MeshFilter>().mesh = m;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
829 次 |
| 最近记录: |