Search Unity

How to change color of a game object the right way?

Discussion in 'Shaders' started by jsmall81, Jan 25, 2021.

  1. jsmall81

    jsmall81

    Joined:
    Jan 25, 2016
    Posts:
    22
    First off, I barely know anything of how to program... But.. I'm curious what is the right way to change the color of a game object. Specifically one section of it. I originally thought changing the material was the right way. Then I thought material blocks would work. Then I thought textures...

    Now what I want to do is change a section of a model to be a certain color. For example, one color of a flag. Let's say it's blue and green. If I wanted to change the green color to match a certain players team color, what's the right way to do it? Change a material instance, texture, or what? This would be a networked object that all other players would see. Max of around 8 players. I don't feel like 8 new materials would be horrible, but I also don't fully understand the limitations of unity. Thanks.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    Could be done with hand authored textures. Common for games with only 2 or 3 team colors and no user defined colors.
    Could be done with vertex colors. Useful if you have different models per team already, or a low poly style.
    Could be done with multiple materials per mesh. Useful if you have high poly characters, or are going for a specific style with lots of "flat" color areas.
    Could be done with color replacement shaders. Common for 2D games with pixelated sprites.

    But the most common method is to use a mask texture that defines what parts of the main texture you want to change the color of. Basically have your base texture be greyscale in the areas you want to change the color, and have a second texture that's just black & a solid white, or one of the three main color channels (Red, Green, or Blue) of the area you want to change the color of. An improvement on that is to have the base texture be black and the "mask" texture be the greyscale version of the base texture you want to color.

    https://bgolus.medium.com/the-team-color-problem-b70ec69d109f

    Realistically, for characters, you're going to have multiple materials no matter what since Unity doesn't have support for batching or instancing skinned meshes. So there's no real extra cost to having unique materials per character*.

    * As long as you don't have multiple hundreds of characters, at which case you might want to look at techniques for instancing skinned meshes, or instanced impostors. At which point there'll also be ways to set an instanced color value per object.
     
  3. jsmall81

    jsmall81

    Joined:
    Jan 25, 2016
    Posts:
    22
    Thank you. Gives me something to work with.