KITASENJU DESIGN BLOG

memo, html, javascript, unity

shader only shadow caster

影だけ出すシェーダー

//screen mapping
//https://light11.hatenadiary.com/entry/2018/06/13/235543

//lighting
//http://alfa.hatenablog.jp/entry/2015/08/16/195933



Shader "Unlit/ScreenMappingShadowTrans"
{
    Properties
    {
        //camer texture
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" "LightMode" = "ForwardBase" }
        LOD 100
        cull off
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_fwdbase

            #include "UnityCG.cginc"
            #include "AutoLight.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
            };

            struct v2f
            {
                float4 vertex   : SV_POSITION;
                float4 pos      : TEXCOORD0;
                float3 lightDir : TEXCOORD1;
                float3 normal   : TEXCOORD2;
                LIGHTING_COORDS(3, 4)
            };

            sampler2D _MainTex;
            uniform fixed4 _LightColor0;

            v2f vert (appdata_full v)
            {
                v2f o;
                
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.pos = ComputeScreenPos(o.vertex);

                o.lightDir = normalize(ObjSpaceLightDir(v.vertex));
                o.normal   = normalize(v.normal).xyz;

                TRANSFER_VERTEX_TO_FRAGMENT(o);
                TRANSFER_SHADOW(o);

                return o;
            }
                        
            fixed4 frag (v2f i, bool IsFacing:SV_IsFrontFace) : SV_Target
            {
                fixed atten = LIGHT_ATTENUATION(i);

                //fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT;
                //fixed4 lightColor = _LightColor0 * saturate(dot(i.lightDir, i.normal));

                half2 uv = i.pos.xy / i.pos.w;
                fixed4 col = tex2D(_MainTex, uv);

                //col.rgb = (col.rgb * lightColor * atten) + ambient;
            
                clip( (1-atten) - 0.5 );//0に近い方があかるい 明るいときはclip


                return float4(col.rgb*0.8,1.0);
            }
            ENDCG
        }
    }
}
"FOOTER"