使用Unity shader制作阴影的教程 VR资源

清风 2017-08-16 17:04:55

Unity阴影采用的是 shadow map 的技术,即把摄像机放到光源位置上,看不到的地方就有阴影。
 

前向渲染中,若一光源开启了阴影,Unity会计算它的阴影映射纹理(shadow map),它其实是一张深度图,记录了从光源位置出发,能看到的场景中距离它最近的表面位置的深度信息。
 

Unity中采用LightMode为ShadowCaster的Pass来生成这张 shadow map,首先会从本shader中找这个pass,若没有就从fallback的shader中层层寻找。
 

然后在fragment shader中,比较该点与shadow map中对应的点的深度,若大于,则处在阴影区域,否则不在阴影区域。
 

阴影包括投射阴影和接收阴影2个部分。
 

投射阴影,则需将该物体加入到光源的shadow map的计算中。
 

接收阴影,则需要对shadow map进行采样,把采样结果和最后的光照结果相乘来产生阴影效果。
 

shader如下:

 
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
 
Shader "Custom/Forward Rendering"
{
    Properties
    {
        _MainTex("Main Texture", 2D) = "white" {}
        _Specular("Specular", Color) = (1,1,1,1)
        _Gloss("Gloss", Range(8,256)) = 8
    }
 
    SubShader
    {
        Pass
        {
            Tags
            {
                "LightMode" = "ForwardBase"
            }
 
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_fwdbase
 
            #include "UnityCg.cginc"
            #include "Lighting.cginc"
            #include "AutoLight.cginc"
 
            sampler2D _MainTex;
            fixed4 _Specular;
            float _Gloss;
 
            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
                float3 normal : NORMAL;
            };
 
            struct v2f
            {
                float4 pos : SV_POSITION;
                float2 uv : TEXCOORD0;
                float3 worldNormal : TEXCOORD1;
                float4 worldPos: TEXCOORD2;
                SHADOW_COORDS(3)
            };
 
            v2f vert(appdata v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                o.worldNormal = UnityObjectToWorldNormal(v.normal);
                o.worldPos = mul(unity_ObjectToWorld, v.vertex);
                TRANSFER_SHADOW(o);
                return o;
            }
 
            fixed4 frag(v2f i) : SV_TARGET
            {
                fixed4 albedo = tex2D(_MainTex, i.uv);
                fixed4 ambient = albedo * UNITY_LIGHTMODEL_AMBIENT;
 
                float3 worldLight = normalize(UnityWorldSpaceLightDir(i.worldPos.xyz));
                float3 worldView = normalize(UnityWorldSpaceViewDir(i.worldPos.xyz));
                fixed4 diff = albedo * _LightColor0 * max(0, dot(i.worldNormal, worldLight));
 
                float3 halfDir = normalize(worldView + worldLight);
                fixed4 spec = albedo * _Specular * pow(max(0, dot(halfDir, i.worldNormal)), _Gloss);
 
                float shadow = SHADOW_ATTENUATION(i);
                fixed4 col = ambient + (diff + spec) * shadow;
                return col;
            }
 
            ENDCG
        }
 
        Pass
        {
            Tags
            {
                "LightMode" = "ForwardAdd"
            }
            Blend One One
 
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_fwdadd
 
            #include "UnityCg.cginc"
            #include "Lighting.cginc"
            #include "AutoLight.cginc"
 
            sampler2D _MainTex;
            fixed4 _Specular;
            float _Gloss;
             
            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
                float3 normal : NORMAL;
            };
 
            struct v2f
            {
                float4 pos : SV_POSITION;
                float2 uv : TEXCOORD0;
                float3 worldNormal : TEXCOORD1;
                float4 worldPos: TEXCOORD2;
                SHADOW_COORDS(3)
            };
 
            v2f vert(appdata v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                o.worldNormal = UnityObjectToWorldNormal(v.normal);
                o.worldPos = mul(unity_ObjectToWorld, v.vertex);
                TRANSFER_SHADOW(o);
                return o;
            }
 
            fixed4 frag(v2f i) : SV_TARGET
            {
                fixed4 albedo = tex2D(_MainTex, i.uv);
 
                float3 worldLight = normalize(UnityWorldSpaceLightDir(i.worldPos.xyz));
                float3 worldView = normalize(UnityWorldSpaceViewDir(i.worldPos.xyz));
                fixed4 diff = albedo * _LightColor0 * max(0, dot(i.worldNormal, worldLight));
 
                float3 halfDir = normalize(worldView + worldLight);
                fixed4 spec = albedo * _Specular * pow(max(0, dot(halfDir, i.worldNormal)), _Gloss);
 
                // 参考 AutoLight.cginc
                float atten;
                #ifdef USING_DIRECTIONAL_LIGHT
                atten = 1;
                #else
                    float4 lightCoord = mul(unity_WorldToLight, i.worldPos);
                    #ifdef POINT
                    atten = tex2D(_LightTexture0, dot(lightCoord.xyz,lightCoord.xyz).xx).UNITY_ATTEN_CHANNEL;
                    #elif SPOT
                    atten = (lightCoord.z > 0) * tex2D(_LightTexture0, lightCoord.xy / lightCoord.w + 0.5).w * tex2D(_LightTextureB0, dot(lightCoord, lightCoord).xx).UNITY_ATTEN_CHANNEL;
                    #endif
                #endif
 
                float shadow = SHADOW_ATTENUATION(i);
 
                fixed4 col = (diff + spec) * atten * shadow;
                return col;
            }
 
            ENDCG
        }
    }
 
    Fallback "VertexLit"
}

效果如下:
 
99VR视界二维码
热门推荐
Hot Recommended
在线客服