边缘发光效果
2020-06-07 本文已影响0人
LEO_青蛙
边缘发光效果
GitHub项目地址
边缘发光的原理:视觉方向与模型正面的法线方向趋近于平行,视觉方向与模型边界的法线方向趋近于垂直。
视觉方向 = 摄像机的坐标 - 模型各个点的坐标
法线方向 = 模型各个点的法线方向
注意:
(1)在同一个空间下进行计算
(2)必须归一化处理
ObjSpaceViewDir方法的源码:
// Computes object space view direction
inline float3 ObjSpaceViewDir( in float4 v )
{
float3 objSpaceCameraPos = mul(unity_WorldToObject, float4(_WorldSpaceCameraPos.xyz, 1)).xyz;
return objSpaceCameraPos - v.xyz;
}
在世界空间下计算视觉方向:
//get the view direction in world space
fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);
Shader "Custom/RimLightShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_RimColor ("Rim Color", COLOR) = (1,1,1,1)
_RimPower ("Rim Power", float) = 1
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal : NORMAL;
};
struct v2f
{
float2 uv : TEXCOORD0;
float rimLight : TEXCOORD1;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
fixed4 _RimColor;
float _RimPower;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
//注意一定要归一化
float3 viewDir = normalize(ObjSpaceViewDir(v.vertex));
float3 normal = normalize(v.normal);
o.rimLight = 1.0 - saturate(dot(normal, viewDir));
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
fixed3 rimColor = _RimColor.rgb * pow(i.rimLight, 1 / _RimPower);
col.rgb += rimColor;
return col;
}
ENDCG
}
}
}