Search Unity

Changing the color of an object that shares material with many other objects

Discussion in 'General Graphics' started by pk1234dva, Dec 4, 2019.

  1. pk1234dva

    pk1234dva

    Joined:
    May 27, 2019
    Posts:
    84
    Hey guys, suppose I have a lot of objects using the same material. I'd like to do some sort of effect to one of them, without changing the others. Maybe change the material, or maybe just use the same material but with some different properties, etc.

    There's tons of options here, and I'd like to know what you'd recommend.

    1, On each object I could have two materials A,B, and by default only A is turned on. When I want to do some effect on a specific object, I could turn off A on it, turn on B, and play around with. I'm not sure how efficient something like this is - turning materials on/off often, etc. It's also messy, but it might be the fastest way for me, and it could be easy for prototyping.

    2, Maybe I could somehow change certain parameters of the object (just by normal scripting), and let the shader somehow notice it. Clearly shaders can somehow figure out things like normals, vertex positions etc, so if I could change some variable, the shader could notice this and act accordingly.

    3, Just use a replacement shader. This seems pretty much equivalent to 2, but again, I'll need 2 shaders, and it seems a bit less elegant. Should be efficient though.

    I'm sure there's other methods and I'd appreciate any recommendations.
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,634
    1 and 2 will both work. 3 is kind-of pointless.

    If you choose option 2 and change materials parameters with code, then Unity will automatically instance the material so that you can change the material properties of the one object without affecting the other objects that have the same material. The downside of this is that even if you change the properties back to their original values, the material will still be instanced so it will no longer batch with the other objects that have the same material. This may or may not matter at all depending on your performance budget.