본문으로 바로가기

Fragment Lambert Lit AlphaBlend

category Technical Report/Unity Shader 2019. 2. 9. 18:40
반응형


앞 포스팅에서 (https://illu.tistory.com/1371) Lambert 라이팅을 적용한 이후 이제 라이팅에 영향을 받는 알파블렌드를 구현하는 포스팅이다.





 Shader "Test/FragmentLambertAlpha"
 
   {
    Properties
    {
      _TintColor("Color", color) = (1,1,1,1)
      _MainTex("Texture", 2D) = "white" {}
      _Alpha("Alpha Range", Range(0,1)) = 0.5
      [Enum(UnityEngine.Rendering.BlendMode)] _SrcBlend("Src Blend", Float) = 1
      [Enum(UnityEngine.Rendering.BlendMode)] _DstBlend("Dst Blend", Float) = 1
 
      [Enum(Off, 0, On, 1)] _ZWrite("ZWrite", Float) = 0
      [Enum(UnityEngine.Rendering.CompareFunction)] _ZTest("ZTest", Float) = 0
      [Enum(UnityEngine.Rendering.CullMode)] _Cull("Cull Mode", Float) = 1

    }

    SubShader
    {
    Tags { "Queue"="Transparent" "RenderType"="Transparent" }
      
      Pass
        {                 
            Tags {"LightMode"="ForwardBase"}       
          
            Blend[_SrcBlend] [_DstBlend]
            ZWrite[_ZWrite]
            ZTest[_ZTest]
            Cull[_Cull]


            CGPROGRAM
              #pragma vertex vert
              #pragma fragment frag
              # include "UnityCG.cginc"
               # include "UnityLightingCommon.cginc"   

            struct v2f
         {
                float2 uv : TEXCOORD0;
                fixed4 diff : COLOR0;
                float4 vertex : SV_POSITION;
                };

         v2f vert(appdata_base v)
         {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.texcoord;

               half3 worldNormal = UnityObjectToWorldNormal(v.normal);
               half nl = max(0, dot(worldNormal, _WorldSpaceLightPos0.xyz));
               o.diff = nl * _LightColor0;
               o.diff.rgb += ShadeSH9(half4(worldNormal, 1));

 
               return o;
    }

               sampler2D _MainTex;
               half _Alpha;
               fixed4 _TintColor;

               fixed4 frag(v2f i) : SV_Target
                {
              
                fixed4 col = tex2D(_MainTex, i.uv) * _TintColor;

           
               col *= i.diff;
              
               return fixed4(col.rgb, col.a * _Alpha);
                 }

            ENDCG
        }
  
    }
}




반응형