Can shader attributes be defined by material?

I’m looking to create a means of using palettized colors to replace r/g/b/a values in textures. I’ve began tinkering around with global shader variables to define colors to call up in the shaders, but then I realized I was missing something:

Let’s say that, in theory, I had 16 colors to choose from and 4 colors used per object. If I wanted to support every possible combination of colors, I’d need 65536 different shaders, each built with each combination of colors.

To be specific, I’m currently using Shader.SetGlobalColor() from a script to be able to define the colors used in the palette, so that modifying one would actively update the shaders without going in and tweaking them by hand.

So, my question is: Is there any way to have global shader variables available for use like this, but then which ones are called by an object/material is determined through a material (or even script), or would it only be possible by using global variables (non-shader) and updating the color per object for every object every time the palette’s color changes?

That’s actually the default case. Almost all shader parameters are defined per material. Shader parameters include textures, colors and other settings like a float value used as cutoff limit (like in the cutout shaders).

That a look at the Material documentation. See all the SetXXX and GetXXX methods.

Those are the supported types:

  • SetBuffer
  • SetColor
  • SetFloat
  • SetInt
  • SetMatrix
  • SetTexture
  • SetVector

The methods SetTextureOffset and SetTextureScale just set the values for the Texture matrix.

Of course for the shader to use the properties you have to define them in the properties block. If you write your own vertex and fragment shaders or if your write surface shaders in cg you might also have a look at How to use shader properties in CG programs