unity 2d 网格三角形

时间:2021-04-24 08:15:11

标签: c# unity3d

我是 Unity 的初学者。我想创建一个三角形。因为我还没有找到任何预先构建的三角形精灵。我试过用一个带网格的。

这是代码:

public class meshgenerator : MonoBehaviour
{
    // Start is called before the first frame update
    private float h = 10;
    private float my_base = 10;
    void Start()
    {
        Mesh mesh = new Mesh();
        Vector3[] vertices = new Vector3[3];
        vertices[0] = new Vector3(0, 0);
        vertices[1] = new Vector3(my_base, 0);
        vertices[2] = new Vector3(my_base / 2, h);
        mesh.vertices = vertices;
        mesh.triangles = new int[] {0, 1, 2 };
        GetComponent<MeshFilter>().mesh = mesh;
    }
}

我对网格的教程了解不多,但我认为最终的形状应该由多个三角形组成(一个正方形需要 4 个顶点和 2 个三角形)。

另外,我不知道紫外线有什么帮助。谁能告诉我为什么当我运行这段代码时什么都不显示?我创建了一个空对象,添加了 MeshFilter、MeshRenderer 和这个脚本。

谢谢。

1 个答案:

答案 0 :(得分:0)

Untiy has a clockwise triangle winding order

你的三角形看起来像

        2 (mybase/2, h)


0 (0,0)         1 (mybase, 0)

所以你的顶点的顺序等于这个图像:

enter image description here

这会导致面部法线指向远离您的方向,只能从另一侧看到!

你更喜欢这张图片中的顺序

enter image description here

向你做出正常的脸。

=>

你想要

mesh.triangles = new[]{ 0, 2, 1 };

就你而言。