Search Unity

Color mesh triangle

Discussion in 'Documentation' started by lAntonin, May 1, 2022.

  1. lAntonin

    lAntonin

    Joined:
    Jan 26, 2021
    Posts:
    7
    Hello, i wanna know how it is possible to color a custom mesh triangles per triangles.
    Like if it where :
    Code (CSharp):
    1. mesh Mesh;
    2. Vector3[] verticles = new Vector3[10];
    3. int[] triangles = new int[verticles.Length * 3];
    4. void Start()
    5. {
    6.     mesh = new Mesh();
    7.     GetComponent<MeshFiter>().mesh = mesh;
    8.      //set verticles and triangles values...
    9.      for (int i = 0; i < triangles Length; i++)
    10.      {
    11.           if (i = 0)
    12.           {
    13.                triangle[i].color = Color.red;
    14.            }
    15.            else
    16.            {
    17.                 triangle[i].color = Color.blue;
    18.            }
    19.      }
    20.      mesh.vertices = verticles;
    21.      mesh.triangles = triangles;
    22. }
    This exemple is for color only one triangle of the mesh (there it is the triangle number 0).

    Thanks you for help...
     
    Last edited: May 1, 2022
    alinajosephclass likes this.
  2. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,928
    Triangles don't have any property of their own (color, position, normal, etc). They're just triads of indices into the vertex array.

    Vertices however, can have colors. So you can give each vertex in your mesh a color, and they will be interpolated across triangle faces. See: https://docs.unity3d.com/ScriptReference/Mesh-colors.html

    Note that in order to actually see the vertex colors, you need to render the mesh using a shader that doesn't ignore them. Most Unity built-in shaders will ignore vertex colors.