Search Unity

How to layer textures in shader?

Discussion in 'Image Effects' started by resetme, Mar 3, 2017.

  1. resetme

    resetme

    Joined:
    Jun 27, 2012
    Posts:
    204
    How do i properly layer textures in unity using fragment shaders?

    In photoshop


    Right now what i do in the shader is lerp the BG,TOP, using TOP ALPHA as T. But the result is not the same when i put the BG on sprite and the TOP in other sprite at top (what im trying to achieve).

    Any ideas how to do this in a shader? can i print the BF in one pass and the TOP in another pass?

    Help!
     
    Talhayousuf likes this.
  2. resetme

    resetme

    Joined:
    Jun 27, 2012
    Posts:
    204
    ok i did it with 2 pass.

    Using the lerp technique always gave some small artifacts (black borders).

    now im layering like this.

    Code (CSharp):
    1. SubShader
    2.     {
    3.         Tags { "RenderType"="Opaque" }
    4.  
    5.         Pass
    6.         {
    7.             CGPROGRAM
    8.             #pragma vertex vert_img
    9.             #pragma fragment frag
    10.             #include "UnityCG.cginc"
    11.  
    12.             struct appdata
    13.             {
    14.                 float4 vertex : POSITION;
    15.                 float2 uv : TEXCOORD0;
    16.             };
    17.  
    18.             struct v2f
    19.             {
    20.                 float2 uv : TEXCOORD0;
    21.                 float4 vertex : SV_POSITION;
    22.             };
    23.  
    24.             sampler2D _MainTex;
    25.            
    26.             fixed4 frag (v2f_img i) : SV_Target
    27.             {
    28.                 fixed4 col = tex2D(_MainTex, i.uv);
    29.                 return col;
    30.             }
    31.             ENDCG
    32.         }
    33.  
    34.         Tags {  "Queue"="Transparent" "RenderType"="Transparent" }
    35.         Blend SrcAlpha OneMinusSrcAlpha
    36.         Pass
    37.         {
    38.             CGPROGRAM
    39.             #pragma vertex vert_img
    40.             #pragma fragment frag
    41.             #include "UnityCG.cginc"
    42.  
    43.             struct appdata
    44.             {
    45.                 float4 vertex : POSITION;
    46.                 float2 uv : TEXCOORD0;
    47.             };
    48.  
    49.             struct v2f
    50.             {
    51.                 float2 uv : TEXCOORD0;
    52.                 float4 vertex : SV_POSITION;
    53.             };
    54.  
    55.             sampler2D _MainTex2;
    56.    
    57.             fixed4 frag (v2f_img i) : SV_Target
    58.             {
    59.                 fixed4 col = tex2D(_MainTex2, i.uv);
    60.                 return col;
    61.             }
    62.             ENDCG
    63.         }
    64.     }
     
    l_racher and Talhayousuf like this.