菲涅尔反射 :视线垂直于表面时,反射较弱,而当视线非垂直表面时,夹角越小,反射越明显.
例如:如果你站在湖边,低头看脚下的水,你会发现水是透明的,反射不是特别强烈;如果你看远处的湖面,你会发现水并不是透明的,但反射非常强烈
Schlick菲涅尔近似等式!
F0是一个反射系数,用于控制菲涅尔反射的强度,v是视角方向,n是表面法线
Shader "QShader/FresnelReflection_schlick"{Properties{_BaseColor("Base Color",Color) = (1.0,1.0,1.0,1.0)_FresnelScale("Fresnel Scale",Range(0,1)) = 0.5_Cubemap("Cubemap",Cube) = "skybox"{}}SubShader{pass {Tags{"LightMode" = "ForwardBase"}CGPROGRAM#pragma vertex Vertex#pragma fragment Fragment#include "Lighting.cginc"#include "AutoLight.cginc"struct a2v {float4 vertex : POSITION;float3 normal : NORMAL;};struct v2f {float4 pos : SV_POSITION;float3 worldNormal : TEXCOORD0;float3 worldPos : TEXCOORD1;float3 worldViewDir : TEXCOORD2;float3 worldReflectDir : TEXCOORD3;SHADOW_COORDS(4)};fixed4 _BaseColor;fixed _FresnelScale;samplerCUBE _Cubemap;v2f Vertex(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.worldReflectDir = reflect(-o.worldViewDir,o.worldNormal);TRANSFER_SHADOW(o)return o;}fixed4 Fragment(v2f i) :SV_TARGET{//世界下法线向量fixed3 worldNormal = normalize(i.worldNormal);//光源向量fixed3 lightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));//视角向量fixed3 worldViewDir = normalize(i.worldViewDir);//环境光fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * _BaseColor.xyz;//漫反射fixed3 diffuse = _LightColor0.xyz * _BaseColor.xyz * saturate(dot(worldNormal,lightDir));//schlick菲涅尔反射公式float fresnel = _FresnelScale + (1 - _FresnelScale) * pow(1 - dot(worldNormal,worldViewDir),5);//反射fixed3 reflection = texCUBE(_Cubemap,i.worldReflectDir).xyz;//计算光照衰减UNITY_LIGHT_ATTENUATION(atten,i,i.worldPos);//将菲涅尔反射系数作为线性插值的权重在物体原本的漫反射颜色和反射颜色之间进行过度fixed3 color = ambient + lerp(diffuse,reflection,saturate(fresnel)) * atten;return fixed4(color,1.0);}ENDCG}}}