Set different color for each face

Hi everyone, I try to color the faces of my hexagon.
My problem it colors the hexagon vertices and then interpolate the color… I would like to color each face with a different color and not blend them.

My code:

 using UnityEngine;
  using System.Collections;
  
  public class SetCubeColor : MonoBehaviour {
  public Shader shaders;
  // Use this for initialization
  void Start()
  {
  
  
  Mesh mesh = GetComponent<MeshFilter>().mesh;
  GetComponent<MeshRenderer>().material = new Material(shaders);
  Vector3[] vertices = mesh.vertices;
  Debug.Log("taille vercitces, MMMMMMMMMMMMMMMMMM" + mesh.colors.Length + "  " + mesh.colors32.Length);
  Color[] colors = new Color[vertices.Length];
  
  int i = 0;
  while (i < vertices.Length)
  {
  colors[i] = Color.Lerp(Color.blue, Color.green, vertices[i].y);
  i++;
  }
  mesh.colors = colors;
  }
  
  }

The shader code:

  Shader "VertexColour"
  {
     SubShader{
       Pass{
       Fog{ Mode Off }
       CGPROGRAM
   
  #pragma vertex vert
  #pragma fragment frag
   
       // vertex input: position, color
     struct appdata {
       float4 vertex : POSITION;
       fixed4 color : COLOR;
     };
   
     struct v2f {
       float4 pos : SV_POSITION;
       fixed4 color : COLOR;
     };
   
     v2f vert(appdata v) {
       v2f o;
       o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
       o.color = v.color;
       return o;
     }
   
     fixed4 frag(v2f i) : SV_Target{ return i.color; }
       ENDCG
     }
     }
  }

Current rendering :


I simply want to colorate each face of the hexagon with a different color.

Thanks in advance.

Hi!
Colors are part of the vertices’ components, not triangles’ which actually don’t exist by themselves.
Triangles are just defined by three vertices index and their order define the orientation of the face (front-face or back-face). To render the triangles, the graphic card actually interpolate the vertices components (uvs, colors, positions, normals, …) from each others.
So if two faces shared the same vertex, their components cannot be different or you will have to duplicate the vertices (same position but different components).
So if you want to render two adjacent faces with two different colors without interpolation, you’ll have to duplicate the vertices fore each face and set the color to the vertices of each faces.
You can easily duplicate the vertices in your 3D editing software by breaking the normals. (Harden Normals in Maya, Remove Smoothing Group in Max, …).

Cheerz :slight_smile: