How are the 24 vertices that make a cube mesh ordered?

How are the 24 vertices that make a cube mesh ordered, so that i may identify and apply flat unique colors to each of the 6 faces?

e.g. This does not work.

Mesh mesh = GetComponent< MeshFilter >().mesh;
Color32 newColorA = new Color32(0,255,255,255);
Color32 newColorB = new Color32(255,255,0,255);
Color32 newColorC = new Color32(255,0,255,255);
      
Color32[] colours = {newColorA,newColorB,newColorC,newColorA,newColorB,newColorC};
    
  
    
 Color32[] newColors = new Color32[mesh.vertices.Length];
 int vertexIndex = 0;
 for (int faceIndex =0; faceIndex < 6; faceIndex ++)
{
            Color32 newColor = colours[faceIndex];
            for(int cornerIndex = 0; cornerIndex < 4; cornerIndex++)
            {
                Debug.Log(cornerIndex + (4*faceIndex));
                newColors[cornerIndex + (4*faceIndex)] = newColor;
                vertexIndex++;
            }

  }
 // Apply the color
mesh.colors32 = newColors;

I doubt than anyone knows the vertex order from the top of his head. You could just try it out and change one vertex color at a time and write its index down if you need to know it. Note that you will need a material/shader which utilizes the vertex colors.

This is super unhelpfull. How is anyone supposed to give advise or help based on that? If you would specifiy how EXACTLY it does not work as you expect it, someone may have an idea. So give error messages, strange behavior, compiler warnings etc. . Just “doesnt work” helps nothing to help you improve it. When you tell what is wrong it greatly reduces the possible amount of bugs we need to look for, consider and explain. Most people won’t create a project and test the code for you to find out WHY it does not work. So if you want help be as clear, detailed and consise as possible. This SHOULD be obvious though.

I’m not expecting anyone to have memorised it. And I’ve already realised that I can go through it vertex by vertex until I have the solution. I hoped there would be a documentation somewhere that I could be pointed. to I have looked all over and the only thing I found that deals with this seems to be wrong, which is why what I coded fails to sort the verticies into surfaces. See below…

The reason it does not work is that it assumes an order to the vertices, based on the idea that they are used in an obvious linear sequence. I assumed vertices groupings for each surface of the cube like this:

0,1,2,3
4,5,6,7
8,9,10,11
etc…

This was based on the diagram here

https://discussions.unity.com/t/787005

In the answer to this rubik’s cube question on UA I’ve posted this example script which is a fully functioning rubik’s cube script. It just needs 27 default cubes as children. At the very end of the script I create a modified mesh based on the default cube mesh and assign a unique color to each side. The sides are distinguished by the normal vectors of each face.

Though usually it may be simpler to just create a custom cube mesh the way you need it. A cube is a quite simple shape. The default cube has the advantage that it already has UV coordinates. Though if you don’t need them creating a cube manually isn’t that difficult. Over here I’ve posted a script to create a mesh that represents the camera frustum. The camera frustum is essentially a distorted cube. For simplicity I first created the 8 logical corners and then split them up into 24 based on the VertOrder array. Without the for loop in line 33 / 34 the result would already be a cuboid. Though instead of the near and far clipplane at z you can use 0 and 1

1 Like

Very helpful. Thank-you!

That threads diagram is wrong, face verts look something like
0 1 2 3,
4 5 8 9,
6 7 10 11,
12 13 14 15,
16 17 18 19,
20 21 22 23

Dont know why the second set is mixed

1 Like

If you really want to know, print them, Vector3 V=mesh.vertexes; and a simple loop. Or, for extra fun, make a Text3D prefab to spawn and place at each corner with a number. Unchecked:

public Text3D T3Dprefab; // should be centered both ways

void Start() {
  Vector3[] V=GetComponent<MeshRenderer>().mesh.vertices;
  for(int i=0; i<V.Length; i++) {
    Text3D cornerT=Instantiate(T3Dprefab, V[i]);
    cornerT.text=""+i;
  }
}
1 Like

Nice answer. A lot easier than what i had been planning. And generally, a nice debug technique. Thank-you.

You are right! Thank-you. Unfortunately I missed you post earlier, and I just worked it out by trial and error with the same results.

But having worked through this for my own satisfaction, I am now going to switch to Bunny83’s example, to have maximum flexibiltiy in coding.