标签:结构体 att 查看 having var rdb poi for 产生
运行环境:Shader "Custom/DifSpecPoint" {
Properties {
_Spec ("Spec", Color) = (1,1,1,1)
_Shin ("Shin", range(1,32)) = 2
}
SubShader {
pass {
tags{ "lightmode" = "forwardbase" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "unitycg.cginc"
#include "lighting.cginc"
#include "autolight.cginc" //1.
#pragma multi_compile_fwdbase //2.
fixed4 _Spec;
float _Shin;
struct v2f{
float4 pos:POSITION;
float3 normal:NORMAL;
float4 vertex:TEXCOORD2;
SHADOW_COORDS(0) //3.
};
v2f vert(appdata_base v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.normal = normalize(v.normal);
o.vertex = v.vertex;
TRANSFER_SHADOW(o) //4.
return o;
}
fixed4 frag(v2f IN):COLOR
{
float3 wpos = mul(unity_ObjectToWorld, IN.vertex).xyz;
UNITY_LIGHT_ATTENUATION(atten, IN, wpos) //5.
//diffuse
float3 N = UnityObjectToWorldNormal(IN.normal);
float3 L = normalize(_WorldSpaceLightPos0).xyz;
float ndotl = saturate(dot(N, L));
fixed4 col = _LightColor0*ndotl;
//specular
float3 V = normalize(WorldSpaceViewDir(IN.vertex));
float3 R = 2 * dot(N, L)*N - L; //phong
float3 H = normalize(V + L); //blinphong
float specScale = pow(saturate(dot(R, V)), _Shin); //phong
specScale = pow(saturate(dot(H, N)), _Shin); //blinphong
col += _Spec*specScale;
//pointlight
float3 pL = Shade4PointLights(unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0,
unity_LightColor[0].rgb, unity_LightColor[1].rgb, unity_LightColor[2].rgb, unity_LightColor[3].rgb,
unity_4LightAtten0,
wpos, N);
col.rgb += pL;
//shadow
col.rgb *= atten; //6.
col += UNITY_LIGHTMODEL_AMBIENT;
return col;
}
ENDCG
}
}
fallback "Diffuse"
}
#include "autolight.cginc" //1.
引用autolight.cginc
#pragma multi_compile_fwdbase //2.
添加这一行,见unity手册“Making multiple shader program variants"中相关描述: compiles all variants needed by ForwardBase (forward rendering base) pass type. The variants deal with different lightmap types and main directional light having shadows on or off.这会对应主平行光产生的阴影。
以下3~5见autolight.cginc
SHADOW_COORDS(0) //3.
TRANSFER_SHADOW(o) //4.
UNITY_LIGHT_ATTENUATION(atten, IN, wpos) //5.
//shadow
col.rgb *= atten; //6.
效果如下图:
可见plane可以接受主平行光照射球体产生的阴影了
从UNITY_LIGHT_ATTENUATION倒推,查看autolight.cginc,在5.0helper中
#ifdef DIRECTIONAL
#define UNITY_LIGHT_ATTENUATION(destName, input, worldPos) fixed destName = SHADOW_ATTENUATION(input);
#endif
其中SHADOW_ATTENUATION(input)的定义,查找可知对应不同的函数,但参数一样
a._ShadowCoord
再查
_ShadowCoord
在autolight.cginc开头Shadow helpers中
#define SHADOW_COORDS(idx1) unityShadowCoord4 _ShadowCoord : TEXCOORD##idx1;
定义了,那么在v2f结构体中就要写入
SHADOW_COORDS(0) //3.
注意括号里的0,这个是代表TEXCOORD0,如果0被占了,则依次向后找,比如改成1、2什么的
看
TRANSFER_SHADOW(o) //4.
个人理解是把计算的阴影要从vertex程序传至fragment程序中,然后fragment中的UNITY_LIGHT_ATTENUATION
才能起作用(不信可以把TRANSFER_SHADOW(o)注释掉,不报错,但阴影没了)
而TRANSFER_SHADOW在autolight.cginc中的定义也是需要
a._ShadowCoord
的,所以在v2f结构体中定义了SHADOW_COORDS(0)满足了TRANSFER_SHADOW和UNITY_LIGHT_ATTENUATION两段宏的需要。
标签:结构体 att 查看 having var rdb poi for 产生
原文地址:http://blog.51cto.com/shuxiayeshou/2065497