Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Coloring seperate faces of a cube

Discussion in 'General Graphics' started by foxvalleysoccer, May 15, 2015.

  1. foxvalleysoccer

    foxvalleysoccer

    Joined:
    May 11, 2015
    Posts:
    108
    How can I apply different colors to a cube face?

    Can i apply a color through code also?

    Creating a Q-bert type game clone.

    Thanks for your help in getting me started with my first project.

    Josh
     
  2. shaderbytes

    shaderbytes

    Joined:
    Nov 11, 2010
    Posts:
    900
    Create the cube via code and then use vertex colors is your best option. If you want hard edges remember each face needs its own vertices. Once the cube is created its easy to then set a new colors array for the vertex colors each time you need a face to change color.
     
    foxvalleysoccer likes this.
  3. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    Or you make a mesh with a different sub-mesh for every face , a different material for each sub-mesh.
    That way can can change the material / color for each face.
     
    foxvalleysoccer likes this.
  4. DougMcFarlane

    DougMcFarlane

    Joined:
    Apr 25, 2009
    Posts:
    197
  5. foxvalleysoccer

    foxvalleysoccer

    Joined:
    May 11, 2015
    Posts:
    108
    Thanks guys
     
  6. shaderbytes

    shaderbytes

    Joined:
    Nov 11, 2010
    Posts:
    900
    I would not do that since that means 6 draw calls + when you have multiple cubes on stage and need to change colors it means you have to create a new material each time. If you had a mere 20 cubes you would end up with 120 draw calls. With vertex colors you can use one material for all the cubes with every face a different color and have only 1 draw call.
     
    theANMATOR2b likes this.
  7. DougMcFarlane

    DougMcFarlane

    Joined:
    Apr 25, 2009
    Posts:
    197
    What if you had 6 submeshes all sharing the same vertex color material. At least now you can access a specific face quick and easy, then modify it's four vertices' color array. No extra draw calls. But then again, a cube only has 24 unique vertices, and just reading, modifying the correct 4, and then writing all 24 back each time may be tidier.

    I would create a new class (or two) to handle keeping track of this for you. The 'cube' class would contain an array of 6 'face' class instances which store each face's vertices, normals, colors, etc. Then have a 'build' method that assembles a cube mesh based on the 6 face's data.