unity3d - 如何在 Unity 项目中获得顶点颜色?
问题描述
我正在尝试以编程方式创建网格,并且在使用 UV 时能够看到网格。但现在我正在尝试在网格上使用颜色,以便每 4 条线的颜色不同(例如图形的短轴和长轴),但现在网格不会出现。
我尝试过的事情:
我相信我已经实现了通用渲染管道 (Universal RP) 来创建顶点颜色着色器。但这没有用。
我也尝试过使用内置的 Particals/Standard Unit 着色器。这不起作用,当我更改为通用 RP 时,我相信它会破坏它,因为材料在预览窗格中显示为粉红色。
还从其他应该解决此问题的 stackoverflow 答案中复制了几个着色器。
这是我生成网格的代码。
public class Grid : MonoBehaviour
{
public float thickness = 0.001f;
public float spacing = 2f;
public int major = 4;
public Color majorColor;
public Color minorColor;
private Vector3[] vertices;
private int[] triangles;
private Color[] colors;
private void Start()
{
transform.localScale = new Vector3(216f, 279.5f, 0.01f);
DrawGrid();
}
public void DrawGrid()
{
Vector2Int numLines = new Vector2Int
{
x = Mathf.FloorToInt(transform.localScale.x / (spacing + thickness)),
y = Mathf.FloorToInt(transform.localScale.y / (spacing + thickness))
};
int totalLines = numLines.x + numLines.y;
int verticesPerLine = 4;
int triangleIdxPerLine = 6;
int numVertices = totalLines * verticesPerLine;
int numTriangles = totalLines * triangleIdxPerLine;
vertices = new Vector3[numVertices];
triangles = new int[numTriangles];
colors = new Color[numVertices];
var minorSpacing = spacing / transform.localScale.x;
int i = 0;
for (float x = 0.0f; x < 1; x += minorSpacing + thickness, i++)
DrawLine(i, new Vector2(x, 0.0f), new Vector2(x + thickness, 1.0f));
for (float y = 0.0f; y < 1; y += minorSpacing + thickness, i++)
DrawLine(i, new Vector2(0.0f, y), new Vector2(1.0f, y + thickness));
print(colors.Length);
print(majorColor);
GetComponent<MeshFilter>().mesh = new Mesh
{
vertices = vertices,
colors = colors,
triangles = triangles
};
}
private void DrawLine(int idx, Vector2 point1, Vector2 point2) {
int vOffset = idx * 4;
vertices[vOffset] = new Vector3(point1.x, point1.y);
vertices[vOffset + 1] = new Vector3(point2.x, point2.y);
vertices[vOffset + 2] = new Vector3(point1.x, point2.y);
vertices[vOffset + 3] = new Vector3(point2.x, point1.y);
Color color = idx % major == 0 ? majorColor : minorColor;
colors[vOffset] = color;
colors[vOffset + 1] = color;
colors[vOffset + 2] = color;
colors[vOffset + 3] = color;
int tOffset = idx * 6;
triangles[tOffset] = vOffset;
triangles[tOffset + 1] = vOffset + 1;
triangles[tOffset + 2] = vOffset + 2;
triangles[tOffset + 3] = vOffset + 1;
triangles[tOffset + 4] = vOffset;
triangles[tOffset + 5] = vOffset + 3;
}
}
解决方案
我在这个 YT 教程https://www.youtube.com/watch?v=eJEpeUH1EMg的帮助下弄明白了。
我在相机指向的另一侧渲染三角形。
只是改变了三角形索引的顺序。
triangles[tOffset] = vOffset;
triangles[tOffset + 1] = vOffset + 2;
triangles[tOffset + 2] = vOffset + 3;
triangles[tOffset + 3] = vOffset + 2;
triangles[tOffset + 4] = vOffset + 1;
triangles[tOffset + 5] = vOffset + 3;
推荐阅读
- python - 如何为与 PyInstaller 捆绑的 Windows、MacOS 和 Linux 应用程序创建快捷方式图标
- android - 无法导航到片段 - kotlin
- typescript - 如何为此去抖动功能编写测试?
- python - 查找一个文件上的行是否在 Python 中作为另一个文件的行中的单词出现
- python - 如何通过python脚本在Debian OS的控制台中填写docker登录信息
- flutter - 错误:flutter/lib/ui/ui_dart_state.cc(199) 未处理的异常:无法启动 Flutter 中的调用
- sql - 丰富一行取决于 Spark SQL 中另一行中的值
- python - 无法使用 sqlalchemy 调用存储过程?
- javascript - WebStorm:直接用箭头提取函数
- php - 通过 $_POST 传递参数时 wp_clear_scheduled_hook 不起作用