Hi everyone!
I’m working on a color replacement shader for my game, and I’m having some issues with my system.
I’d like a shader that (ideally) replaces the pixels of several colors for another ones. Ideally I’d do that using a ‘hue’ change and keeping the Saturation and Value of the given color. But due to the limit of 64 instructions for shader model 2, I just multiply the base color and the new color. For that I’m using a grayscale mask so I just don’t tint the whole image. In fact, this grayscale will determine the color I’ll be replacing it for.
And on top of that, I need it to be cutout so I have transparency if the texture used has the alpha channel.
Here’s the code I’ve got so far:
Shader "Transparent/Cutout/Diffuse Tint" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
_Mask ("Mask (RGB) Trans (A)", 2D) = "white" {}
_Cutoff ("Alpha cutoff", Range(0,1)) = 0.0
_SWColor1 ("Swap to 1", Color) = (1.00,1.00,1.00,0.5)
_SWColor2 ("Swap to 2", Color) = (0.75,0.75,0.75,0.5)
_SWColor3 ("Swap to 3", Color) = (0.50,0.50,0.50,0.5)
_SWColor4 ("Swap to 4", Color) = (0.25,0.25,0.25,0.5)
}
SubShader {
Tags {"IgnoreProjector"="True" "RenderType"="TransparentCutout"}
LOD 600
CGPROGRAM
#pragma surface surf BlinnPhong alphatest:_Cutoff
//#pragma target 3.0
sampler2D _MainTex;
sampler2D _Mask;
float4 _Color;
float3 _SWColor1;
float3 _SWColor2;
float3 _SWColor3;
float3 _SWColor4;
struct Input {
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o) {
half4 base = tex2D(_MainTex, IN.uv_MainTex) * _Color;
half4 mask = tex2D(_Mask, IN.uv_MainTex);
half3 col = base.rgb*( (mask.r>=0.900)? _SWColor1.rgb :
(mask.r>=0.650)? _SWColor2.rgb :
(mask.r>=0.400)? _SWColor3.rgb :
(mask.r>=0.150)? _SWColor4.rgb :
half3(1,1,1)
);
o.Albedo = col.rgb;
o.Alpha = base.a;
}
ENDCG
}
Fallback "Transparent/Cutout/Diffuse"
}
And now, let’s explain the issues I’m having:
- The mask Texture if it has its filter mode set to bilinear, there’ll be some artifacts in the texture, since it’ll interpolate between black and white. Otherwise (filter mode set to Point), it’ll be just too pixelated.
Here’s an example (maybe if it’s too small to see the details).
Here the color equivalence would be (passed as shader parameters):
SwapColor 1 = White → Red
SwapColor 2 = Grey 75% → Green
SwapColor 3 = Grey 50% → Light blue
SwapColor 4 = Grey 25% → Yellow
- Also, when building the project as webplayer, I’m having some issues I’m not having in the actual game view inside the editor, it seems it put to white some details of the texture that I’m not modifying at all.
Any ideas on how to achieve the result?
I’d just like to change the hue of a given texture in the areas indicated by the mask.
