Search Unity

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

Discussion in 'Scripting' started by mrdatsun, Jan 2, 2021.

  1. mrdatsun

    mrdatsun

    Joined:
    Apr 8, 2020
    Posts:
    26
    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.

    Code (CSharp):
    1.  
    2. Mesh mesh = GetComponent< MeshFilter >().mesh;
    3. Color32 newColorA = new Color32(0,255,255,255);
    4. Color32 newColorB = new Color32(255,255,0,255);
    5. Color32 newColorC = new Color32(255,0,255,255);
    6.      
    7. Color32[] colours = {newColorA,newColorB,newColorC,newColorA,newColorB,newColorC};
    8.    
    9.  
    10.    
    11.  Color32[] newColors = new Color32[mesh.vertices.Length];
    12.  int vertexIndex = 0;
    13.  for (int faceIndex =0; faceIndex < 6; faceIndex ++)
    14. {
    15.             Color32 newColor = colours[faceIndex];
    16.             for(int cornerIndex = 0; cornerIndex < 4; cornerIndex++)
    17.             {
    18.                 Debug.Log(cornerIndex + (4*faceIndex));
    19.                 newColors[cornerIndex + (4*faceIndex)] = newColor;
    20.                 vertexIndex++;
    21.             }
    22.  
    23.   }
    24.  // Apply the color
    25. mesh.colors32 = newColors;
    26.  
     
  2. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    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.
     
  3. mrdatsun

    mrdatsun

    Joined:
    Apr 8, 2020
    Posts:
    26
    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://forum.unity.com/threads/cube-mesh-order-of-vertices-and-triangles.873901/
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,993
    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
     
    Last edited: Jan 2, 2021
    mrdatsun likes this.
  5. mrdatsun

    mrdatsun

    Joined:
    Apr 8, 2020
    Posts:
    26
    Very helpful. Thank-you!
     
  6. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    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
     
    mrdatsun likes this.
  7. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,997
    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:
    Code (CSharp):
    1. public Text3D T3Dprefab; // should be centered both ways
    2.  
    3. void Start() {
    4.   Vector3[] V=GetComponent<MeshRenderer>().mesh.vertices;
    5.   for(int i=0; i<V.Length; i++) {
    6.     Text3D cornerT=Instantiate(T3Dprefab, V[i]);
    7.     cornerT.text=""+i;
    8.   }
    9. }
     
    mrdatsun likes this.
  8. mrdatsun

    mrdatsun

    Joined:
    Apr 8, 2020
    Posts:
    26
    Nice answer. A lot easier than what i had been planning. And generally, a nice debug technique. Thank-you.
     
  9. mrdatsun

    mrdatsun

    Joined:
    Apr 8, 2020
    Posts:
    26
    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.