Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Multiple Transparent Textures on a single object

Discussion in 'Shaders' started by jessee03, Aug 21, 2014.

  1. jessee03

    jessee03

    Joined:
    Apr 27, 2011
    Posts:
    729
    I am trying to get a layered effects with multiple transparent or Alpha textures on a single object. When creating things like a waterfall, flowing river ect. I like to use multiple planes layered on top of each other with different alpha values, scroll direction, ect. to give the effect of flowing water. I'm curious if this can all be done on a single plane instead?
     
  2. brianasu

    brianasu

    Joined:
    Mar 9, 2010
    Posts:
    369
    You can with a shader that samples multiple textures and blends them together. You can scroll the uvs to make the water flow. I think it's better to do it all in one shader so you won't have any z-fighting and it probably helps the performance.
     
  3. brianasu

    brianasu

    Joined:
    Mar 9, 2010
    Posts:
    369
    This is a simple surface shader that blends A B and C in that order.

    Code (CSharp):
    1. Shader "Custom/Blend 3"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Color", COLOR) = (1, 1, 1, 1)
    6.         _MainTex ("Texture A (RGB)", 2D) = "black" {}
    7.         _TextureB ("Texture B (RGB)", 2D) = "black" {}
    8.         _TextureC ("Texture C (RGB)", 2D) = "black" {}
    9.         _FlowSpeedA ("Flow Speed A (UV X, UV Y)", VECTOR) = (0, 0, 0, 0)
    10.         _FlowSpeedB ("Flow Speed B (UV X, UV Y)", VECTOR) = (0, 0, 0, 0)
    11.         _FlowSpeedC ("Flow Speed C (UV X, UV Y)", VECTOR) = (0, 0, 0, 0)
    12.     }
    13.  
    14.     SubShader
    15.     {
    16.         Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
    17.         LOD 200
    18.         ZWrite Off
    19.      
    20.         CGPROGRAM
    21.         #pragma surface surf Lambert alpha
    22.  
    23.         fixed4 _Color;
    24.  
    25.         sampler2D _MainTex;
    26.         sampler2D _TextureB;
    27.         sampler2D _TextureC;
    28.         float4 _FlowSpeedA;
    29.         float4 _FlowSpeedB;
    30.         float4 _FlowSpeedC;
    31.      
    32.  
    33.         struct Input
    34.         {
    35.             float2 uv_MainTex;
    36.             float2 uv_TextureB;
    37.             float2 uv_TextureC;
    38.         };
    39.  
    40.         void surf (Input i, inout SurfaceOutput o)
    41.         {
    42.             fixed4 srcA = tex2D(_MainTex,  i.uv_MainTex  + _FlowSpeedA.xy * _Time.x);
    43.             fixed4 srcB = tex2D(_TextureB, i.uv_TextureB + _FlowSpeedB.xy * _Time.x);      
    44.             fixed4 srcC = tex2D(_TextureC, i.uv_TextureC + _FlowSpeedC.xy * _Time.x);
    45.          
    46.             fixed4 col = fixed4(
    47.                 srcA.rgb * srcA.a + srcB.rgb * srcB.a * (1 - srcA.a),
    48.                 srcA.a + srcB.a * (1 - srcA.a));
    49.              
    50.             fixed4 colB = fixed4(
    51.                 col.rgb * col.a + srcC.rgb * srcC.a * (1 - col.a),
    52.                 col.a + srcC.a * (1 - col.a)) * _Color;
    53.              
    54.             o.Albedo = colB.rgb;
    55.             o.Alpha = colB.a;
    56.         }
    57.         ENDCG
    58.     }
    59.     FallBack "Transparent/VertexLit"
    60. }
     
  4. jessee03

    jessee03

    Joined:
    Apr 27, 2011
    Posts:
    729
    Thank you for taking the time to write this. I rarely write code for shaders and you made it really simple to follow :)