본문으로 바로가기

Unity fragment self shadow casting

category Technical Report/Unity Shader 2017. 9. 18. 14:08
반응형

  


Properties {
            
       [NoScaleOffset]    _MainTex  ("Albedo", 2D) = "white" {}
      
        }

 
SubShader
    {
        Pass
        {
            Tags {"LightMode"="ForwardBase"}

            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            #include "Lighting.cginc"
           

           // shadow helper functions and macros
            #include "AutoLight.cginc"        
            #pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap novertexlight         

            struct v2f
            {
                float2 uv : TEXCOORD0;
                SHADOW_COORDS(1) // put shadows data into TEXCOORD1
                fixed3 diff    : COLOR0;
                fixed3 ambient : COLOR1;
                float4 pos : SV_POSITION;
            };

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

                half3 worldNormal = UnityObjectToWorldNormal(v.normal);
                half nl = max(0, dot(worldNormal, _WorldSpaceLightPos0.xyz));
               

               o.diff = nl * _LightColor0.rgb;
            

                o.ambient = ShadeSH9(half4(worldNormal,1));
                // compute shadows data
             

               TRANSFER_SHADOW(o)
               

               return o;
            }

            sampler2D _MainTex;

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

                // compute shadow attenuation (1.0 = fully lit, 0.0 = fully shadowed)
                fixed shadow = SHADOW_ATTENUATION(i);
               

               // darken light's illumination with shadow, keep ambient intact
                fixed3 lighting = i.diff * shadow + i.ambient;
                col.rgb *= lighting;
               
               
                return col;
            }
            ENDCG
        }



        // shadow casting support
       // UsePass "Legacy Shaders/VertexLit/SHADOWCASTER"



        Pass
        {
         
          //Shadow pass

            Tags {"LightMode"="ShadowCaster"}


            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_shadowcaster
            #include "UnityCG.cginc"

            struct v2f {
                V2F_SHADOW_CASTER;
            };

            v2f vert(appdata_base v)
            {
                v2f o;
                TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
                return o;
            }

            float4 frag(v2f i) : SV_Target
            {
                SHADOW_CASTER_FRAGMENT(i)
            }
            ENDCG
        }
    }
}

반응형