Search Unity

Shader Alpha

Discussion in 'Shaders' started by Kamajabu, Feb 18, 2016.

  1. Kamajabu

    Kamajabu

    Joined:
    Feb 14, 2016
    Posts:
    3
    Hey,

    I have pretty straightforward question but I'm completely lost in shaders :/ I'm trying to make a cutoff layer using stencil but I don't want to make that layer solid, but use alpha so it'll cover graphics behind it in only 50% for example (best if I could dynamically choose it). I have cutoff, I have black covering layer, but when I change alpha from 255 to 254 layer dissapears completly. Any ideas how to change it?

    Code (CSharp):
    1. Shader "Custom/Stencil NotEqual" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,0)
    4.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    5.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    6.         _Metallic ("Metallic", Range(0,1)) = 0.0
    7.         _Cutoff("Cutoff", Range(0,1)) = 0.5
    8.     }
    9.     SubShader {
    10.         Tags {  "Queue" = "Transparent"
    11.                  "RenderType"="Transparent"}
    12.         Blend SrcAlpha DstAlpha
    13.         LOD 200
    14.  
    15.         Stencil {
    16.             Ref 1
    17.             Comp NotEqual
    18.         }
    19.        
    20.         CGPROGRAM
    21.         #pragma surface surf Lambert alpha:fade
    22.  
    23.         // Use shader model 3.0 target, to get nicer looking lighting
    24.         #pragma target 3.0
    25.  
    26.         sampler2D _MainTex;
    27.  
    28.         struct Input {
    29.             float2 uv_MainTex;
    30.         };
    31.  
    32.         half _Glossiness;
    33.         half _Metallic;
    34.          float _Cutoff;
    35.         fixed4 _Color;
    36.  
    37.         void surf (Input IN, inout SurfaceOutputStandard o) {
    38.             // Albedo comes from a texture tinted by color
    39.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    40.             o.Albedo = c.rgb;
    41.             // Metallic and smoothness come from slider variables
    42.             o.Metallic = _Metallic;
    43.             o.Smoothness = _Glossiness;
    44.             o.Alpha = c.a;
    45.             clip(c.a - _Cutoff);
    46.         }
    47.         ENDCG
    48.     }
    49.     FallBack "Diffuse"
    50. }
    51.  
    Thanks a lot!