Search Unity

Different colors per object with a replacement shader?

Discussion in 'Scripting' started by Damocles, Jul 25, 2013.

  1. Damocles

    Damocles

    Joined:
    Jul 21, 2012
    Posts:
    46
    I'm creating a system where characters and objects that are behind walls, have their obscured part drawn in outline form. Fairly straightforward using replacement shaders, but I would really like to be able to colour code these objects, so that players can quickly assess the situation from the colours.

    The problem is, replacement shaders don't use materials, so the outlines are all one colour. Does anyone know of a way to set different colours per object with replacement shaders?

    Layers are no good, as I'm already close to the layer limit from other game aspects/requirements, and cannot afford the several layers needed to separate the outlines. Plus some of the objects will need to be on the same layer for physics/game reasons, but different colours in their outlines.

    Is there perhaps some kind of pre-render event called per-object that I could use to change a shader variable?
     
  2. tomvds

    tomvds

    Joined:
    Oct 10, 2008
    Posts:
    1,028
    If you swap a shader in the inspector, all variables that share the same name and type are copied over to the new shader. Replacement shaders work the same way. All variables that it shares with the original shader will be copied. To have a per-object color, add a color variable to all your original shaders (this color does not need to be actually used by the shader). Add a color variable with the same name to the replacement shader and use that to color the outline. You can now select the replacement color in the original materials of your objects.
     
  3. Damocles

    Damocles

    Joined:
    Jul 21, 2012
    Posts:
    46
    Excellent, it worked like a charm. Thanks Tom.