Rim 边缘光
边缘光:计算眼睛和模型顶点法线的点积,结果作为强度,和材质输出;顶点和法线平行时,强度最大,垂直时,强度最小。因此将他取反,即同一方向时,强度最小,垂直时,强度最大。
float rim= 1-dot(normalize(viewDirection),normalDirection);
源代码:
1 Shader "JQM/Rim_1" 2 { 3 Properties 4 { 5 _Color("Color", color) = (1.0,1.0,1.0,1.0) 6 _SpecColor("Specular Color", color) = (1.0,1.0,1.0,1.0) 7 _Shininess("Shininess",float) = 10 8 _RimColor("Rim Coloe Color", color) = (1.0,1.0,1.0,1.0) 9 _RimPower("Rim Power",Range(0.1,10.0)) = 3.0 10 11 } 12 SubShader{ 13 Pass{ 14 15 Tags { "LightMode" = "ForwardBase"} 16 17 CGPROGRAM 18 #pragma vertex vert 19 #pragma fragment frag 20 21 //使用自定义变量 22 uniform float4 _Color; 23 uniform float4 _SpecColor; 24 uniform float4 _RimColor; 25 uniform float _Shininess; 26 uniform float _RimPower; 27 28 //使用Unity定义的变量 29 uniform float4 _LightColor0; 30 31 struct vertexInput{ 32 float4 vertex:POSITION; 33 float3 normal:NORMAL; 34 }; 35 36 struct vertexOutput{ 37 float4 pos:SV_POSITION; 38 float4 posWorld:TEXCOORD0; 39 float3 normalDir:TEXCOORD1; 40 }; 41 42 //顶点程序 43 vertexOutput vert(vertexInput v) 44 { 45 vertexOutput o; 46 o.posWorld = mul(_Object2World, v.vertex); 47 o.normalDir = normalize( mul(float4(v.normal,0.0),_World2Object).xyz); 48 o.pos = mul(UNITY_MATRIX_MVP, v.vertex); 49 return o; 50 } 51 52 //片段程序 53 float4 frag(vertexOutput i):COLOR 54 { 55 float3 normalDirection = i.normalDir; 56 float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz- i.posWorld.xyz); 57 58 float rim=1 -dot(normalize(viewDirection),normalDirection); 59 return float4(rim*_Color.xyz,1.0); 60 } 61 62 ENDCG 63 } 64 } 65 66 }