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

Shader and Camera help

Discussion in 'Shaders' started by INSANE_BR, Oct 27, 2009.

  1. INSANE_BR

    INSANE_BR

    Joined:
    Sep 10, 2009
    Posts:
    142
    Hi there, I'm using this Shader to mask a health bar in my game.

    Code (csharp):
    1. Shader "Insane/Transparent/Mask (Pos)" {
    2.    Properties {
    3.       _MainTex ("Base (RGB)", 2D) = "white" {}
    4.       _MaskTex ("Mask (RGB)", 2D) = "white" {}
    5.       _Rotation ("Rotation (-2Pi, 2Pi)", Float) = 0.0
    6.       _PositionX ("PositionX", Float) = 0.0
    7.       _PositionY ("PositionY", Float) = 0.0
    8.      
    9.       _PositionX_Mask ("PositionX Mask", Float) = 0.0
    10.       _PositionY_Mask ("PositionY Mask", Float) = 0.0
    11.    }
    12.    SubShader {
    13.        
    14.       Tags {Queue=Transparent}
    15.       Lighting Off
    16.       ColorMask RGB
    17.        
    18.        
    19.       Pass {
    20.          blend Zero OneMinusSrcColor
    21.          SetTexture [_MaskTex] {
    22.             Combine texture
    23.          }
    24.       }
    25.        
    26.        
    27.       Pass {
    28.          blend One OneMinusSrcColor
    29.          
    30.              
    31.         CGPROGRAM
    32.         #pragma vertex vert
    33.         #pragma fragment frag
    34.         #include "UnityCG.cginc"
    35.  
    36.         struct v2f {
    37.             V2F_POS_FOG;
    38.             float2  uv : TEXCOORD0;
    39.             float2  uv2 : TEXCOORD1;
    40.         };
    41.  
    42.         uniform float4 _MainTex_ST;
    43.         uniform float4 _MaskTex_ST;
    44.  
    45.         uniform float _Rotation;
    46.         uniform float _PositionX;
    47.         uniform float _PositionY;
    48.  
    49.         uniform float _Rotation_Mask;
    50.         uniform float _PositionX_Mask;
    51.         uniform float _PositionY_Mask;
    52.  
    53.         v2f vert (appdata_base v)
    54.         {
    55.             v2f o;
    56.             PositionFog( v.vertex, o.pos, o.fog );
    57.  
    58.         // TEXTURE
    59.  
    60.             float2 uv1 = TRANSFORM_TEX(v.texcoord, _MainTex);
    61.            
    62.             uv1 -= float2( 0.5, 0.5 );
    63.             o.uv.x =  cos( _Rotation )*uv1.x + sin(_Rotation)*uv1.y;
    64.             o.uv.y = -sin( _Rotation )*uv1.x + cos(_Rotation)*uv1.y;
    65.  
    66.             o.uv.x +=  _PositionX;
    67.             o.uv.y +=  _PositionY;
    68.            
    69.             o.uv += float2( 0.5, 0.5 );
    70.  
    71.         // MASK
    72.  
    73.             float2 uv2 = TRANSFORM_TEX(v.texcoord, _MaskTex);
    74.  
    75.             uv2 -= float2( 0.5, 0.5 );
    76.            
    77.             o.uv2.x =  uv2.x + _PositionX_Mask;
    78.             o.uv2.y =  uv2.y + _PositionY_Mask;
    79.  
    80.             o.uv2 += float2( 0.5, 0.5 );
    81.  
    82.  
    83.             return o;
    84.         }
    85.  
    86.         uniform sampler2D _MainTex;
    87.         uniform sampler2D _MaskTex;
    88.  
    89.         half4 frag (v2f i) : COLOR
    90.         {
    91.            half4 mainCol = tex2D( _MainTex, i.uv );
    92.            half4 maskCol = tex2D( _MaskTex, i.uv2 );
    93.            
    94.             return mainCol * maskCol;
    95.         }
    96.         ENDCG
    97.       }
    98.      
    99.    }
    100. }
    Its working pretty good, but I still have one problem.

    I'm using this shader to make a material for a plane, and I have a camera rendering to a texture that I use GUI.DrawTexture().

    The problem is:
    If I set the Camera Clear Flags to Skybox I get a 50% alpha black square behind my health bar.
    If I set anything difrent from Skybox I can't see anything.
    :(

    Any clue?

    thanks in advance
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Does it make any difference if you set the camera's background color to zero alpha?
     
  3. INSANE_BR

    INSANE_BR

    Joined:
    Sep 10, 2009
    Posts:
    142
    It won't show anything :( it feels like everything is a background, solid color.

    I'm actually trying to make it another way.

    I will use 2 PNGs and a Alpha Mask. I will use the mask to mask a PNG that represents a Full Health bar, and by changing the mask offset I it will show the empty health bar.

    I'm using this shader now:

    Code (csharp):
    1. Shader "Insane/Transparent/TwoTextureMasked" {
    2.     Properties {
    3.         _Color ("Main Color", Color) = (1,1,1,1)
    4.         _FrontTex ("Front Texture (RGB)", 2D) = "white" {}
    5.         _BackTex ("Back Texture (RGB)", 2D) = "white" {}
    6.         _Mask ("Mask (A)", 2D) = "white" {}
    7.     }
    8.     SubShader {
    9.        Pass {
    10.            Tags {Queue=Transparent}
    11.            Alphatest Greater 0
    12.            ColorMask RGB
    13.            Lighting On
    14.            
    15.            Material {
    16.                Diffuse [_Color]
    17.                Ambient [_Color]
    18.            }
    19.            
    20.            SetTexture [_FrontTex] {
    21.                combine texture
    22.            }
    23.            SetTexture [_Mask] {
    24.                combine previous, texture
    25.            }
    26.            SetTexture [_BackTex] {
    27.               combine previous lerp (previous) texture
    28.            }
    29.            SetTexture [_FrontTex] {
    30.                combine previous * primary
    31.            }
    32.        }
    33.     }
    34.     FallBack " Diffuse", 1
    35. }

    Two problems still:

    The transparent part of the PNGs are not transparent... :(

    And I need the PNGs not to be affected by ambient light, They have colors on their own.

    In the image you can see the mask is working, its masking like 60% of the image.

    Well, any help would be appretiated :)

    Thanks,
     

    Attached Files: