Suppose I want to provide all these options for the same hair model, and let the player pick their hair color at runtime.
Conceptually, I could use a grayscale hair texture, and then map those shades of gray to a color gradient designed from each of the above options. Each gradient is probably defined by 3 or 4 color keys.
What would be the best way to actually implement that in Unity?
I’d love to actually use the Gradient class to define each option, but I don’t see any easy way to make use of that in a shader. I could just generate a new Texture2D on the fly, by iterating over pixels of the original texture and looking them up in the gradient. But that hardly seems elegant, does it?
I guess I could instead make a shader that takes a color look-up table in the form of a texture. So I’d convert my Gradient into a (1x256 pixel) texture, and give the shader that as well as the grayscale texture map, and could look up the appropriate color for each pixel that way.
But I can’t shake the feeling that I’m missing some simpler solution here… anyone?
Doesn’t even need to be 1x256, could probably be 1x16 for most cases. You could even just create a single 16x16 texture with all 16 colors encoded into it and then the shader just needs a single float value to change the color.
Some things to remember. When using a grey-scale texture for UVs of a gradient texture you don’t really want to go fully from 0.0 to 1.0 (or black to white), but +/- 0.5 * TexelSize.x. The reason for this is if you sample at 0.0 or 1.0 that’s actually going to be the same color value, halfway between the first and last pixels (if the texture isn’t clamped). Setting the wrap mode to clamp also works, but it means the first and last colors will appear slightly “early” in the lookup.
But this would mean our hair has only 16 different shades in it… 4-bit color. Holy SNES batman! Or am I missing something? Maybe sampling two neighboring colors and interpolating within the shader? But that seems like making the shader work harder than it really needs to.
That’s a really good point (though I still think it should be 256x16). Of course it means I’d have to commit to exactly those 16 hair colors up front… I have a feeling my future self would be cursing my present self for constraining him like that.
Yep, sample in the middle of the texel, not on the edge — I’m with you!
The shader is going to interpolate anyway, there’s no extra work there, especially when Unity’s Gradient is limited to 16 keys anyway there’s not a great reason to use a full 256 wide gradient texture. It’s more a question of how sharp your colors are going to be in the gradient. Most likely they’ll be fairly simple gradients.
For example:
That’s a grey scale (Alpha8) hair, just stolen from the above image converted to greyscale and auto-leveled in Photoshop, and a 1x4 gradient.
And this is all 4 pixels of the gradient:
And I didn’t spend a lot of time on that so the colors don’t quite match the original.
Btw, the above 4 pixel gradient was set to be an uncompressed / 16bit texture, but GPUs will still do the bilinear interpolation using 24 bit color (or better), and DXT is also only storing colors with 16 bit precision.