Search Unity

4 layer shader with alpha

Discussion in 'Shaders' started by raoul, Jan 18, 2008.

  1. raoul

    raoul

    Joined:
    Jul 14, 2007
    Posts:
    6,735
    Hi,

    I tried the 3 and four layer shaders on the wiki and they work great. At the moment I have 3 questions:

    1. I remember reading that the 4 layer shader has problems on some graphic cards. This is issue was supposed to get solved in Unity 2. Is this indeed solved now?

    2. In the 4 layer shader it seems that the texture in the 4th layer (alpha channel) covers the whole object and therefor affecting the textures in the first 3 layers. I am probably doing something wrong here but I am pretty sure there are no areas with opacity in the splatmap. I used solid colors for the 1st 3 layers.

    3. What I am looking for is an additional option to have the layers (or at least 1 of them) fade into opacity (using the alpha channel) to blend the part of the object using that layer with the background. Is this possible? Or maybe this can be done with the 4 layer shader?

    Many thanks,
    Raoul
     
  2. raoul

    raoul

    Joined:
    Jul 14, 2007
    Posts:
    6,735
    Had my first steps into shaders this weekend and managed to make some small changes to the three layer shader:

    Code (csharp):
    1.  
    2. Shader "Terrain/Three Layer" {
    3. Properties {
    4.     _Color ("Main Color", Color) = (1,1,1,1)
    5.     _MainTex ("Color 1 (RGB)", 2D) = "grey" {}
    6.     _MainTex2 ("Color 2 (RGB)", 2D) = "grey" {}
    7.     _MainTex3 ("Color 3 (RGB)", 2D) = "grey" {}
    8.     _Mask ("Mixing Mask (RGBA)", 2D) = "white" {}
    9. }
    10. SubShader {
    11.     Pass {
    12.         Blend SrcAlpha OneMinusSrcAlpha
    13.         Lighting On
    14.        
    15.         Material {
    16.             Diffuse [_Color]
    17.             Ambient [_Color]
    18.         }
    19.    
    20. CGPROGRAM
    21. // profiles arbfp1
    22. #pragma fragment frag
    23.  
    24. // fragmentoption ARB_fog_exp2
    25.  
    26. sampler2D _MainTex : register(s0);
    27. sampler2D _MainTex2 : register(s1);
    28. sampler2D _MainTex3 : register(s2);
    29. sampler2D _Mask : register(s3);
    30.  
    31. struct v2f {
    32.     float4 uv[5] : TEXCOORD0;
    33.     float4 diffuse : COLOR;
    34.  
    35. };
    36.  
    37. half4 frag( v2f i ) : COLOR
    38. {
    39.     // get the four layer colors
    40.     half4 color1 = tex2D( _MainTex, i.uv[0].xy );
    41.     half4 color2 = tex2D( _MainTex2, i.uv[1].xy );
    42.     half4 color3 = tex2D( _MainTex3, i.uv[2].xy );
    43.     // get the mixing mask texture
    44.     half4 mask = tex2D( _Mask, i.uv[3].xy );
    45.     // mix the four layers
    46.     half4 color = color1 * mask.r + color2 * mask.g + color3 * mask.b;
    47.     // match mask alpha
    48.     color.a = mask.a;    
    49.     //multiply and double by the lighting
    50.     return color * i.diffuse * 2.0;
    51. }
    52. ENDCG
    53.         SetTexture[_MainTex] {}
    54.         SetTexture[_MainTex2] {}
    55.         SetTexture[_MainTex3] {}
    56.         SetTexture[_Mask] {}
    57.     }
    58. }
    59. FallBack " VertexLit", 1
    60. }
    61.  
    added:
    Code (csharp):
    1.  
    2. Blend SrcAlpha OneMinusSrcAlpha
    3.  
    and
    Code (csharp):
    1.  
    2. color.a = mask.a;
    3.  
    This actually does blend the object with the background. See result below:
    The red is the background, the bricks and wood are 3 textures on a plane using the 3 layer shader. The mixing mask texture has opacity on the outside.

    As you can see the brick texture is much brighter in the parts with opacity. The color level of the material is set to white. If I move this to grey the brightness disappears but the overall look is too dark. I also tried controlling the brightness by changing:

    return color * i.diffuse * 2.0;

    to

    return color * i.diffuse * 4.0;

    In the shader. Unfortunately this leads to a similar result as below. Anyone knows how to fix this?
     

    Attached Files:

  3. raoul

    raoul

    Joined:
    Jul 14, 2007
    Posts:
    6,735
    Actually this is not true, when changing the color to grey, the transparent area is still brighter. When I remove the "color.a = mask.a;" part from the shader the edges with full transparency have this same bright color. It seems that "color.a = mask.a;" does apply the alpha of the splat texture correctly to the output and the blending properties are set correctly. I belief the problem is in this line:

    half4 color = color1 * mask.r + color2 * mask.g + color3 * mask.b;

    The initial color of the areas with transparency is not set correctly.

    The only solution I, as a complete shader noob, can think of is to use a separate texture for the alpha channel? Or can all of this actually be done with one splat texture?