Rim 邊緣光

邊緣光:計算眼睛和模型頂點法線的點積,結果做爲強度,和材質輸出;頂點和法線平行時,強度最大,垂直時,強度最小。所以將他取反,即同一方向時,強度最小,垂直時,強度最大。spa

 float rim= 1-dot(normalize(viewDirection),normalDirection); code

 

源代碼:orm

 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 }
相關文章
相關標籤/搜索