Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

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:
    2,063
    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.