效果图

资源
代码
Shader "DC/Lighting/Fresnel"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_FresnelScale("FresnelScale", float) = 0.5
_Environment("Envionment", Cube) = "defaulttexture" {}
_SurfaceCol("SurfaceCol", Color) = (1,1,1,1)
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal : NORMAL;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float3 worldViewDir : TEXCOORD1;
float3 worldNormal : NORMAL;
float3 worldRefl: TEXCOORD2;
};
sampler2D _MainTex;
float4 _MainTex_ST;
samplerCUBE _Environment;
float _FresnelScale;
fixed4 _SurfaceCol;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.worldViewDir = normalize(UnityWorldSpaceViewDir(mul(unity_ObjectToWorld, v.vertex).xyz));
o.worldNormal = normalize(mul(unity_ObjectToWorld, v.normal));
o.worldRefl = reflect(-o.worldViewDir, o.worldNormal);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
/*
菲涅尔反射指物体表面镜面反射和漫反射同时以一定比例发生的现象
菲涅尔反射formular
fresnel = fresnelScale + (1 - fresnelScale) * pow(1 - dot(worldViewDir, worldNormal), 5)
finalCol = lerp(diffuseCol, reflectionCol, saturate(fresnel))
*/
float fresnel = _FresnelScale + (1 - _FresnelScale) * pow(1 - dot(i.worldViewDir, i.worldNormal), 5);
float3 lDir = normalize(UnityWorldSpaceLightDir(_WorldSpaceLightPos0.xyz));
fixed3 diffuseCol = _LightColor0 * saturate(dot(i.worldNormal, lDir)) * _SurfaceCol.rgb;
fixed3 reflectionCol = texCUBE(_Environment, i.worldRefl).rgb;
fixed3 col = lerp(diffuseCol, reflectionCol, fresnel);
return fixed4(col,1);
}
ENDCG
}
}
}