Search Unity

Trying to blend 2 textures along a gradient in CG

Discussion in 'Shaders' started by Maethor, Apr 11, 2009.

  1. Maethor

    Maethor

    Joined:
    Apr 2, 2009
    Posts:
    27
    OK so I'm trying my hand at writing a shader in CG. It's supposed to use two textures that blend together along inverse gradients.

    The first version here is with only the first texture worked in:

    Code (csharp):
    1. Shader "Tutorial/Textured Colored v1" {
    2. Properties {
    3.     _MainTex ("Texture", 2D) = "white" { }
    4.     _BlendText ("Texture", 2D) = "black" { }
    5. }
    6. SubShader {
    7.     Pass {
    8.  
    9. CGPROGRAM
    10. #pragma vertex vert
    11. #pragma fragment frag
    12. #pragma fragmentoption ARB_fog_exp2
    13.  
    14. #include "UnityCG.cginc"
    15.  
    16. sampler2D _MainTex;
    17. sampler2D _BlendTex;
    18.  
    19. struct v2f {
    20.     V2F_POS_FOG;
    21.     float2  uv : TEXCOORD0;
    22. };
    23.  
    24. v2f vert (appdata_base v)
    25. {
    26.     v2f o;
    27.     PositionFog( v.vertex, o.pos, o.fog );
    28.     o.uv = TRANSFORM_UV(0);
    29.     return o;
    30. }
    31.  
    32. half4 frag (v2f i) : COLOR
    33. {
    34.     float alpha = i.uv;
    35.     half4 texcol = tex2D( _MainTex, i.uv );
    36.     return texcol * (1,1,1,alpha);
    37. }
    38. ENDCG
    39.  
    40.     }
    41. }
    42. Fallback "VertexLit"
    43. }
    It seems to work pretty well: http://www.ohlonegdc.com/file-host/shader_v1.jpg

    Ok, but when I try to include the second texture along the inverse gradient, here is the code:

    Code (csharp):
    1. Shader "Tutorial/Textured Colored" {
    2. Properties {
    3.     _MainTex ("Texture", 2D) = "white" { }
    4.     _BlendText ("Texture", 2D) = "black" { }
    5. }
    6. SubShader {
    7.     Pass {
    8.  
    9. CGPROGRAM
    10. #pragma vertex vert
    11. #pragma fragment frag
    12. #pragma fragmentoption ARB_fog_exp2
    13.  
    14. #include "UnityCG.cginc"
    15.  
    16. sampler2D _MainTex;
    17. sampler2D _BlendTex;
    18.  
    19. struct v2f {
    20.     V2F_POS_FOG;
    21.     float2  uv : TEXCOORD0;
    22.     float2 uv2 : TEXCOORD1;
    23. };
    24.  
    25. v2f vert (appdata_base v)
    26. {
    27.     v2f o;
    28.     PositionFog( v.vertex, o.pos, o.fog );
    29.     o.uv = TRANSFORM_UV(0);
    30.     o.uv2 = TRANSFORM_UV(1);
    31.     return o;
    32. }
    33.  
    34. half4 frag (v2f i) : COLOR
    35. {
    36.     float alpha = i.uv;
    37.     float inverseAlpha = 1 - i.uv2;
    38.     half4 texcol = tex2D( _MainTex, i.uv ) * (1,1,1,alpha);
    39.     half4 texcolblend = tex2D( _BlendTex, i.uv2) * (1,1,1,inverseAlpha);
    40.     return texcol + texcolblend;
    41. }
    42. ENDCG
    43.  
    44.     }
    45. }
    46. Fallback "VertexLit"
    47. }
    And the result looks like the second texture is just being interpreted as a solid color: http://www.ohlonegdc.com/file-host/shader_v2.jpg

    Any idea what's happening here? I appreciate any help in advance.
     
  2. Maethor

    Maethor

    Joined:
    Apr 2, 2009
    Posts:
    27
    Turns out I had a typo!

    Anyone know a way to rotate a texture along existing UVs? That would be a time-saver.