应用网格颜色或网格颜色 32 并渲染它以进行程序生成

Apply mesh color or mesh color32 and render it for procedural generation

我来这里是因为我的项目有问题。

实际上我想创建一个名为voxel 的单色立方体。我创建了 6 个四边形来构建立方体,结果如下:

My fabulous cube with no texture or color

在创作过程中,我为我的四边形赋予了 mesh.color 的颜色。但是不影响四色,也不取色

我尝试按照下面的网格颜色文档:

https://docs.unity3d.com/ScriptReference/Mesh-colors.html

https://docs.unity3d.com/ScriptReference/Mesh-colors32.html

所以我的问题是:如何在我的网格上使用 color 或 color32 来创建带有颜色的立方体?

你可以在这里查看我的代码:

public class World : MonoBehaviour
{
    enum Cubeside { Bottom, Top, Left, Right, Front, Back };
    //public void SetColors(List<Color32> inColors);

    public float quadSize = 0.5f;

    private int id;
    // Red , Green , Blue
    private List<int> RGBColor;
    private float Opacity;
    //public Color32[] VoxelColors = new Color32[] { new Color32(255, 105, 205, 225)};
    //public Color32 VoxelColors = new Color32(255, 105, 205, 225);
    public Color[] VoxelColorsSimple;

    /// <summary>
    /// Permet de créer une face du cube
    /// </summary>
    /// <param name="side">Définie le coté que l'on doit créer</param>
    /// <returns>Aucun return</returns> 
    void CreateQuad(Cubeside side)
    {
        int colorRed = 255;
        int ColorGreen = 105;
        int ColorBlue = 205;

        Color32 C32Red = new Color32(255, 0, 0, 255);
        Color CRed = new Color(1f, 0f, 0f, 1f);
        Color32[] VoxelColors = new Color32[] { C32Red };
        Color mycolor = Color.red;

        Mesh mesh = new Mesh();
        mesh.name = "ScriptedMesh";

        Vector3[] vertices = new Vector3[4];
        Vector3[] normals = new Vector3[4];
        int[] triangles = new int[6];


        //all possible vertices
        //Positionnement des 8 vertices nécessaire à notre cube ( les 8 cotés du cube )
        Vector3 p0 = new Vector3(-quadSize, -quadSize, quadSize);
        Vector3 p1 = new Vector3(quadSize, -quadSize, quadSize);
        Vector3 p2 = new Vector3(quadSize, -quadSize, -quadSize);
        Vector3 p3 = new Vector3(-quadSize, -quadSize, -quadSize);
        Vector3 p4 = new Vector3(-quadSize, quadSize, quadSize);
        Vector3 p5 = new Vector3(quadSize, quadSize, quadSize);
        Vector3 p6 = new Vector3(quadSize, quadSize, -quadSize);
        Vector3 p7 = new Vector3(-quadSize, quadSize, -quadSize);

        //+------+.      +------+       +------+       +------+      .+------+
        //|`.    | `.    |\     |\      |      |      /|     /|    .' |    .'|
        //|  `+--+---+   | +----+-+     +------+     +-+----+ |   +---+--+'  |
        //|   |  |   |   | |    | |     |      |     | |    | |   |   |  |   |
        //+---+--+.  |   +-+----+ |     +------+     | +----+-+   |  .+--+---+
        // `. |    `.|    \|     \|     |      |     |/     |/    |.'    | .'
        //   `+------+     +------+     +------+     +------+     +------+'
        //Création des différents carré à partir de 2 triangle rectangle
        
        switch(side)
        {
            case Cubeside.Front :
                //Nous prenons 4 coté du cube
                vertices = new Vector3[] { p4, p5, p1, p0 };
                //nous rajoutons des normals avec la direction
                normals = new Vector3[] { Vector3.forward, Vector3.forward, Vector3.forward, Vector3.forward };
                //Nous créons les triangles sur la face du cube 3 1 0 étant en haut à droite et 3 2 1 en bas à gauche
                triangles = new int[] { 3, 1, 0, 3, 2, 1 };
                break;
            case Cubeside.Bottom :
                vertices = new Vector3[] { p0, p1, p2, p3 };
                normals = new Vector3[] { Vector3.down, Vector3.down, Vector3.down, Vector3.down };
                triangles = new int[] { 3, 1, 0, 3, 2, 1 };
                break;
            case Cubeside.Top: 
                vertices = new Vector3[] { p7, p6, p5, p4 };
                normals = new Vector3[] { Vector3.up, Vector3.up, Vector3.up, Vector3.up };
                triangles = new int[] { 3, 1, 0, 3, 2, 1 };
                break;
            case Cubeside.Left: 
                vertices = new Vector3[] { p7, p4, p0, p3 };
                normals = new Vector3[] { Vector3.left, Vector3.left, Vector3.left, Vector3.left };
                triangles = new int[] { 3, 1, 0, 3, 2, 1 };
                break;
            case Cubeside.Right: 
                vertices = new Vector3[] { p5, p6, p2, p1 };
                normals = new Vector3[] { Vector3.right, Vector3.right, Vector3.right, Vector3.right };
                triangles = new int[] { 3, 1, 0, 3, 2, 1 };
                break;
            case Cubeside.Back:
                vertices = new Vector3[] { p6, p7, p3, p2 };
                normals = new Vector3[] { Vector3.back, Vector3.back, Vector3.back, Vector3.back };
                triangles = new int[] { 3, 1, 0, 3, 2, 1 };
                break;
        }
        
        mesh.vertices = vertices;
        mesh.normals = normals;
        mesh.triangles = triangles;
        Color[] tempColor = new Color[mesh.vertices.Length];
        for ( int cpt = 0; cpt < mesh.vertices.Length; cpt++)
        {
            tempColor[cpt] = Color.red;
        }
        VoxelColorsSimple = tempColor;
        mesh.colors = VoxelColorsSimple;

        GameObject quad = new GameObject("quad");
        quad.transform.parent = this.gameObject.transform;
        MeshFilter meshFilter = (MeshFilter)quad.AddComponent(typeof(MeshFilter));
        meshFilter.mesh = mesh;
        MeshRenderer renderer = quad.AddComponent(typeof(MeshRenderer)) as MeshRenderer;
        
        /* //I don't want to use material
        Material mat = Resources.Load("VoxelMaterial") as Material;
        renderer.material = mat;*/

    }

    // Use this for initialization
    void Start()
    {
        CreateQuad(Cubeside.Front);
        CreateQuad(Cubeside.Bottom);
        CreateQuad(Cubeside.Top);
        CreateQuad(Cubeside.Left);
        CreateQuad(Cubeside.Right);
        CreateQuad(Cubeside.Back);
    }

    // Update is called once per frame
    void Update()
    {
    }
}

并且此脚本链接到我的空对象“世界”:

hierarchy of my project

感谢您的回答。

Unity 不知道在没有着色器处理的情况下如何处理顶点颜色,Unity 使用网格的 material 来确定使用哪个着色器。

因此,您需要使用适合您需要的着色器为网格渲染器分配 material。

您可以编写自己的使用顶点颜色的着色器并将其分配给您的 material,或者您可以使用已经执行此操作的内置着色器,例如默认或漫反射精灵着色器。

如果您编写自己的着色器,您可以编写一个不使用纹理,只使用顶点颜色来确定反照率以及其他自定义项的着色器。

无论您决定创建着色器 & material,一旦您拥有 material,您就可以将 [SerializeField] voxelMat; 字段添加到 World,分配您的 material 到检查器中,然后使用 renderer.material = voxelMat; 分配它。