折射
折射是當光線從一種介質斜射到另一種介質時,傳播方向會發生改變。在給定入射角的情況下可以使用斯涅耳定律(Snell’s Law)來計算反射角,計算公式如下:
θ1是光在介質1中和表面法線的夾角,θ2是折射光線和法線的夾角,η1和η2分別是兩個介質的折射率。真空的折射率是1,玻璃的折射率是1.5。在渲染的過程中更準確的是進行兩次折射,一次是光線進入物體內部時折射,一次是光線從物體內部射出,但實際進行一次折射的效果也看起來是對的,所以一般只進行第一次折射。實現效果代碼如下:
Properties {
_Color ("Color Tint", Color) = (1, 1, 1, 1)
_RefractColor("Refraction Color", Color) = (1, 1, 1, 1)
_RefractAmount("Refraction Amount", Range(0, 1)) = 1
_RefractRatio("Refraction Ratio", Range(0.1, 1)) = 0.5
_Cubemap ("Reflraction Cubemap", Cube) = "_Skybox" {}
}
SubShader{
Tags{ "RenderType" = "Opaque" "Queue" = "Geometry" }
Pass{
Tags { "LightMode" = "ForwardBase" }
CGPROGRAM
#pragma multi_compile_fwdbase
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
#include "AutoLight.cginc"
fixed4 _Color;
fixed4 _RefractColor;
fixed _RefractAmount;
fixed _RefractRatio;
samplerCUBE _Cubemap;
struct a2v
{
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f
{
float4 pos : SV_POSITION;
fixed3 worldNormal : TEXCOORD0;
fixed3 worldPos : TEXCOORD1;
fixed3 worldViewDir : TEXCOORD2;
fixed3 worldRefr : TEXCOORD3;
SHADOW_COORDS(4)
};
v2f vert(a2v v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.worldNormal = UnityObjectToWorldNormal(v.normal);
o.worldPos = mul(unity_ObjectToWorld, v.vertex);
o.worldViewDir = UnityWorldSpaceViewDir(o.worldPos);
o.worldRefr = refract(-normalize(o.worldViewDir), normalize(o.worldNormal), _RefractRatio);
TRANSFER_SHADOW(o);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
fixed3 worldNormal = normalize(i.worldNormal);
fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
fixed3 worldViewDir = normalize(i.worldViewDir);
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed3 diffuse = _LightColor0.rgb * _Color.rgb * max(0, dot(worldNormal, worldLightDir));
fixed3 refraction = texCUBE(_Cubemap, i.worldRefr).rgb * _RefractColor.rgb;
UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);
fixed3 color = ambient + lerp(diffuse, refraction, _RefractAmount) * atten;
return fixed4(color, 1.0);
}
ENDCG
}
}
FallBack "Reflective/VertexLit"
在上面的代碼中,使用CG的refract函數來計算折射方向,第一個參數是入射光線方向,第二個參數是法線方向,第三個參數用_RefractRatio來表示,是入射光線所在的介質的折射率和折射光線所在的介質的折射率的比值。_RefractAmount用來計算漫反射和折射的混合。實現效果如下: