본문으로 바로가기

Unity Unlit Receive Shadow

category Technical Report/Unity Shader 2018. 2. 5. 14:18
반응형


// include AlphaTest Code



Shader "UnityKorea/UnlitTest" {

    Properties {
        _Color ("Main Color", Color) = (1,1,1,1)
        _MainTex ("Base (RGB)", 2D) = "white" {}
      //  _Cutoff  ("Alpha cutoff", Range(0,1)) = 0.5


    }

    SubShader {
       

    Tags {"Queue" = "Geometry" "RenderType" = "Opaque"}

//   Tags {"Queue" = "AlphaTest" "RenderType" = "TransparentCutout"}

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

                #pragma vertex vert
                #pragma fragment frag
                #pragma multi_compile_fwdbase
                #pragma fragmentoption ARB_fog_exp2
                #pragma fragmentoption ARB_precision_hint_fastest
          //    #pragma alphatest:_Cutoff
              
                #include "UnityCG.cginc"
                #include "AutoLight.cginc"
              
                struct v2f
                {
                    float4  pos         : SV_POSITION;
                    float2  uv          : TEXCOORD0;
                    LIGHTING_COORDS(1,2)
                };
 
                v2f vert (appdata_tan v)
                {
                    v2f o;
                  
                    o.pos = UnityObjectToClipPos( v.vertex);
                    o.uv = v.texcoord.xy;
                    TRANSFER_VERTEX_TO_FRAGMENT(o);
                    return o;
                }
 
                sampler2D _MainTex;
             // float _Cutoff;
                fixed4 _Color;
              
                fixed4 frag(v2f i) : COLOR
                {
                    fixed4 color = tex2D(_MainTex, i.uv);
                // clip(color.a - _Cutoff);
                  
                    fixed atten = LIGHT_ATTENUATION(i); // Light attenuation + shadows.
                    //fixed atten = SHADOW_ATTENUATION(i); // Shadows ONLY.
                   // fixed4 ambient = UNITY_LIGHTMODEL_AMBIENT.rgba;


                    return color * atten * _Color ;
                }

            ENDCG
        }

 
        Pass {

            Tags {"LightMode" = "ForwardAdd"}
            //Blend One One


            CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
                #pragma multi_compile_fwdadd_fullshadows
                #pragma fragmentoption ARB_fog_exp2
                #pragma fragmentoption ARB_precision_hint_fastest
              
                #include "UnityCG.cginc"
                #include "AutoLight.cginc"
              
                struct v2f
                {
                    float4  pos         : SV_POSITION;
                    float2  uv          : TEXCOORD0;
                    LIGHTING_COORDS(1,2)
                };
 
                v2f vert (appdata_tan v)
                {
                    v2f o;
                  
                    o.pos = UnityObjectToClipPos( v.vertex);
                    o.uv = v.texcoord.xy;
                    TRANSFER_VERTEX_TO_FRAGMENT(o);
                    return o;
                }
 
                sampler2D _MainTex;
                float _Cutoff;
                fixed4 _Color;
              
                fixed4 frag(v2f i) : COLOR
                {
                    fixed4 color = tex2D(_MainTex, i.uv);
              //   clip(color.a - _Cutoff);  
                                  
                    fixed atten = LIGHT_ATTENUATION(i); // Light attenuation + shadows.
                   //fixed atten = SHADOW_ATTENUATION(i); // Shadows ONLY.       
                    return tex2D(_MainTex, i.uv) * atten * _Color ;
                }

            ENDCG
        }
    }

    Fallback "Diffuse"  

   // Fallback "Transparent/Cutout/VertexLit"


}
 


be test below codes from https://forum.unity.com/threads/modifying-vertex-position-shader-in-a-surface-shader-shadow-problems.216276/


    Shader "Custom/Curved" {
            Properties {
                _MainTex ("Base (RGB)", 2D) = "white" {}
                _QOffset ("Offset", Vector) = (0,0,0,0)
                _Dist ("Distance", Float) = 100.0
            }
            SubShader {
                Tags { "RenderType"="Opaque" }
                Pass
                {
                    CGPROGRAM
                    #pragma vertex vert
                    #pragma fragment frag
                    #include "UnityCG.cginc"
      
                    sampler2D _MainTex;
                    float4 _QOffset;
                    float _Dist;
                  
                    struct v2f {
                        float4 pos : SV_POSITION;
                        float4 uv : TEXCOORD0;
                    };
      
                    v2f vert (appdata_base v)
                    {
                        v2f o;
                        float4 vPos = mul (UNITY_MATRIX_MV, v.vertex);
                        float zOff = vPos.z/_Dist;
                        vPos += _QOffset*zOff*zOff;
                        o.pos = mul (UNITY_MATRIX_P, vPos);
                        o.uv = v.texcoord;
                        return o;
                    }
      
                    half4 frag (v2f i) : COLOR
                    {
                        half4 col = tex2D(_MainTex, i.uv.xy);
                        return col;
                    }
                    ENDCG
                }
            }
            FallBack "Diffuse"
        }




Shader "Curved Diffuse" {
 
Properties {
 
    _MainTex ("Base (RGB)", 2D) = "white" {}
     _Color ("Main Color", Color) = (1,0.5,0.5,1)
}
 
SubShader {
 
    Tags { "RenderType"="Opaque" }
 
    LOD 150
 
    CGPROGRAM
 
    #pragma exclude_renderers flash
    #pragma surface surf Lambert noforwardadd
    #pragma vertex vert addshadow
 
    sampler2D _MainTex;
    float4 _QOffset;
    float _Dist;
    float4 _Color;
 
    struct Input {
    float2 uv_MainTex;
    };
 
    void vert (inout appdata_full v) {
 
        float4 vertex_view = mul(UNITY_MATRIX_MV, v.vertex);
        float zOff = vertex_view.z / _Dist;
        v.vertex.xyz += mul(_QOffset*zOff*zOff, UNITY_MATRIX_IT_MV).xyz;
 
    }
 
    void surf (Input IN, inout SurfaceOutput o) {
 
        fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
        o.Albedo =c.rgb;
        o.Albedo *= _Color.rgb;
        o.Alpha = c.a;
 
    }
 
    ENDCG
}
 
}



also additional below shader pass into fragment shaders



Pass {
    Name "ShadowCaster"
    Tags { "LightMode" = "ShadowCaster" }
          
    Fog {Mode Off}
    ZWrite On ZTest LEqual Cull Off
    Offset 1, 1
    
    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag
    #pragma multi_compile_shadowcaster
    #pragma fragmentoption ARB_precision_hint_fastest
    #include "UnityCG.cginc"
 
    float4 _QOffset;
    float _Dist;
            
    struct v2f {
        V2F_SHADOW_CASTER;
    };
            
    v2f vert( appdata_base v ) {
        float4 vPos = mul (UNITY_MATRIX_MV, v.vertex);
        float zOff = vPos.z/_Dist;
        vPos += _QOffset*zOff*zOff;
        v.vertex = mul (vPos, UNITY_MATRIX_IT_MV);
        v2f o;
        TRANSFER_SHADOW_CASTER(o)
        return o;
    }
            
    float4 frag( v2f i ) : COLOR {
        SHADOW_CASTER_FRAGMENT(i)
    }
    ENDCG
}


      
// Pass to render object as a shadow collector
Pass {
    Name "ShadowCollector"
    Tags { "LightMode" = "ShadowCollector" }
          
    Fog {Mode Off}
    ZWrite On ZTest LEqual
    
    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag
    #pragma fragmentoption ARB_precision_hint_fastest
    #pragma multi_compile_shadowcollector
            
    #define SHADOW_COLLECTOR_PASS
    #include "UnityCG.cginc"
 
    float4 _QOffset;
    float _Dist;
 
    struct appdata {
        float4 vertex : POSITION;
    };
            
    struct v2f {
        V2F_SHADOW_COLLECTOR;
    };
            
    v2f vert (appdata v) {
        float4 vPos = mul (UNITY_MATRIX_MV, v.vertex);
        float zOff = vPos.z/_Dist;
        vPos += _QOffset*zOff*zOff;
        v.vertex = mul (vPos, UNITY_MATRIX_IT_MV);
        v2f o;
        TRANSFER_SHADOW_COLLECTOR(o)
        return o;
    }
            
    fixed4 frag (v2f i) : COLOR {
        SHADOW_COLLECTOR_FRAGMENT(i)
    }
    ENDCG
}



반응형