- Home /
How can I add another "layer" to my shader?
I am developing a multiple diffuse texture shader where multiple textures are overlayed over the top of one another. I've studied the builtin decal shader to see how to overlay one texture over the top of another, but now I can't work out how to overlay a third, fourth etc over the top of that. Here is my shader code:
Shader "Player/Human Skin 2" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 0)
_Shininess ("Shininess", Range (0.01, 1)) = 0.078125
_SkinTex ("Skin Texture (RGB)", 2D) = "white" {}
_TattooTex ("Tattoo Texture (RGBA)", 2D) = "white" {}
_BeardTex ("Beard Texture (RGBA)", 2D) = "white" {}
_AlphaMap ("Transparency", 2D) = "white" {}
_Cutoff ("Alpha cutoff", Range(0,1)) = 0.1
_BumpMap ("First Normal Map", 2D) = "bump" {}
// Slider to control fading between normalmaps
_BumpMapSlider ("Normal Slider", Range (0, 1)) = 0
// Second normal map
_BumpMap2 ("Second Normal Map", 2D) = "bump" {}
}
SubShader {
Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
LOD 400
CGPROGRAM
#pragma surface surf BlinnPhong alphatest:_Cutoff
#pragma exclude_renderers flash
#pragma target 3.0
sampler2D _SkinTex;
sampler2D _TattooTex;
sampler2D _BeardTex;
sampler2D _AlphaMap;
sampler2D _BumpMap;
fixed4 _Color;
half _Shininess;
// Remember to add your properties in here also. Check ShaderLab References from manual for more info
float _BumpMapSlider;
sampler2D _BumpMap2;
struct Input {
float2 uv_SkinTex;
float2 uv_TattooTex;
float2 uv_BeardTex;
float2 uv_BumpMap;
};
void surf (Input IN, inout SurfaceOutput o) {
half4 c = tex2D(_SkinTex, IN.uv_SkinTex) * _Color;
half4 tattoo = tex2D(_TattooTex, IN.uv_TattooTex);
half4 beard = tex2D(_BeardTex, IN.uv_BeardTex);
c.rgb = lerp (c.rgb, beard.rgb, beard.a);
o.Albedo = c.rgb * _Color.rgb;
o.Gloss = c.a;
o.Alpha = c.a * tex2D(_AlphaMap, IN.uv_SkinTex).r;
o.Specular = _Shininess;
// Read values from _BumpMap and _BumpMap2
fixed3 normal1 = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
fixed3 normal2 = UnpackNormal(tex2D(_BumpMap2, IN.uv_BumpMap));
// Interpolater between them and set the result to the o.Normal
o.Normal = lerp(normal1, normal2, _BumpMapSlider);
}
ENDCG
}
FallBack "Transparent/Cutout/Bumped Specular"
FallBack "Transparent/Cutout/VertexLit"
}
I cannot get the third texture to show and I suspect it may be something to do the the combining of the alpha channels for each texture. I have considered doing a new pass but I'm trying to make the shader work all in one pass to limit drawcalls. Any suggestions as to how I could add the next layers?
Answer by tanoshimi · Dec 05, 2013 at 07:15 AM
Are you trying to add _TattooTex over the top of _BeardTex? If so, try simply adding the following line at line 49:
half4 beard = tex2D(_BeardTex, IN.uv_BeardTex);
c.rgb = lerp (c.rgb, beard.rgb, beard.a);
// Add this line
c.rgb = lerp (c.rgb, tattoo.rgb, tattoo.a);
o.Albedo = c.rgb * _Color.rgb;