Best way to dye hair (i.e. apply a color gradient to a grayscale texture)?

Consider the following…

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?

Do that.

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.

1 Like

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!

Thanks,

  • Joe

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:
2894300--212858--gradient_hair.png

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.

2894300--212859--hair.png

And this is all 4 pixels of the gradient: 2894300--212860--hair_gradient_brown.png

And I didn’t spend a lot of time on that so the colors don’t quite match the original.

Shader "Unlit/GradientRemap"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _Gradient ("Gradient", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
         
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            sampler2D _Gradient;
            float4 _Gradient_TexelSize;
         
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                return o;
            }
         
            fixed4 frag (v2f i) : SV_Target
            {
                fixed alpha = tex2D(_MainTex, i.uv).a;
                float halfTexel = _Gradient_TexelSize.x * 0.5;
                float2 gradientUV = float2(_Gradient_TexelSize.x * 0.5 + alpha * (1.0 - _Gradient_TexelSize.x), 0.5);
                fixed4 col = tex2D(_Gradient, gradientUV);
                return col;
            }
            ENDCG
        }
    }
}

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.

1 Like

Well, you rock! It didn’t occur to me to rely on the color interpolation inherent in the texture look-up.

You’ve gone above and beyond the call of duty here… Thank you very much!

And here it is in a diffuse (i.e. lit) shader.

Thanks again!

2 Likes

Hi! I suggest use UVPaint, it can add detail and bake them into the hair texture, more pissibilities to customization:

https://www.youtube.com/watch?v=ds4miuj7fAs

https://www.youtube.com/watch?v=BPlYJgDk5O8

It is runtime solution