边缘光:计算眼睛和模型顶点法线的点积,结果作为强度,和材质输出;顶点和法线平行时,强度最大,垂直时,强度最小。因此将他取反,即同一方向时,强度最小,垂直时,强度最大。
float rim= -dot(normalize(viewDirection),normalDirection);
源代码:
Shader "JQM/Rim_1"
{
Properties
{
_Color("Color", color) = (1.0,1.0,1.0,1.0)
_SpecColor("Specular Color", color) = (1.0,1.0,1.0,1.0)
_Shininess("Shininess",float) =
_RimColor("Rim Coloe Color", color) = (1.0,1.0,1.0,1.0)
_RimPower("Rim Power",Range(0.1,10.0)) = 3.0 }
SubShader{
Pass{ Tags { "LightMode" = "ForwardBase"} CGPROGRAM
#pragma vertex vert
#pragma fragment frag //使用自定义变量
uniform float4 _Color;
uniform float4 _SpecColor;
uniform float4 _RimColor;
uniform float _Shininess;
uniform float _RimPower; //使用Unity定义的变量
uniform float4 _LightColor0; struct vertexInput{
float4 vertex:POSITION;
float3 normal:NORMAL;
}; struct vertexOutput{
float4 pos:SV_POSITION;
float4 posWorld:TEXCOORD0;
float3 normalDir:TEXCOORD1;
}; //顶点程序
vertexOutput vert(vertexInput v)
{
vertexOutput o;
o.posWorld = mul(_Object2World, v.vertex);
o.normalDir = normalize( mul(float4(v.normal,0.0),_World2Object).xyz);
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
return o;
} //片段程序
float4 frag(vertexOutput i):COLOR
{
float3 normalDirection = i.normalDir;
float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz- i.posWorld.xyz); float rim= -dot(normalize(viewDirection),normalDirection);
return float4(rim*_Color.xyz,1.0);
} ENDCG
}
} }