Home > Back-end >  Apply mesh color or mesh color32 and render it for procedural generation
Apply mesh color or mesh color32 and render it for procedural generation

Time:11-25

I am here cause I have a problem with my project.

Actually I want to create an unicolor cube named voxel. I have created 6 quads for building a cube and there's the result:

My fabulous cube with no texture or color

During this creation, I give to my quad a color with mesh.color. But it doesn't affect the quad color, nor take the color.

I try to follow the documentations below for the mesh color:

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

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

So my question is: how can I use color or color32 on my mesh for create a cube with color ?

You can look my code here:

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()
    {
    }
}

And this script is linked to my empty object "World" here :

hierarchy of my project

Thanks for your answers.

CodePudding user response:

Unity doesn't know what to do with vertex colors without a shader to handle them, and Unity uses the material of the mesh to determine which shader to use.

So, you will need to assign a material to your meshrenderer with a shader appropriate for your needs.

You can either write your own shader which uses the vertex colors and assign that to your material, or you can use a builtin shader which does that already, such as the default or diffuse sprite shaders.

If you write your own shader you could write one which does not make use of a texture, and only uses the vertex colors to determine the albedo, as well as other customizations.

However you decide to create your shader & material, once you have your material, you could add a [SerializeField] voxelMat; field to your World, assign your material to it in the inspector, then use renderer.material = voxelMat; to assign it.

  • Related