Shader "_MyShader/7_Advanced/2_Fresnel"
{
Properties
{
_Color("Color", COLOR) = (1,1,1,1)
_CubeMap("CubeMap", Cube) = "SkyBox" {}
_FresnelScale("FresnelScale", Range(0,1.5)) = 0.5
}
SubShader
{
Tags { "RenderType"="ForwardBase" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
#include "AutoLight.cginc"
struct a2v
{
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f
{
float4 pos : SV_POSITION;
float4 worldPos : TEXCOORD1;
float3 worldNormal : TEXCOORD2;
float3 worldLight : TEXCOORD3;
float3 worldView : TEXCOORD4;
float3 worldReflection : TEXCOORD5;
SHADOW_COORDS(5)
};
fixed4 _Color;
samplerCUBE _CubeMap;
float _FresnelScale;
v2f vert (a2v v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.worldPos = mul(_Object2World,v.vertex);
o.worldNormal = UnityObjectToWorldNormal(v.normal);
o.worldLight = UnityWorldSpaceLightDir(o.worldPos);
o.worldView = UnityWorldSpaceViewDir(o.worldPos);
o.worldReflection = reflect(-o.worldView, o.worldNormal);
TRANSFER_SHADOW(o);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed3 worldNormalDir = normalize(i.worldNormal);
fixed3 worldLightDir = normalize(i.worldLight);
fixed3 worldViewDir = normalize(i.worldView);
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed3 diffuseColor = _LightColor0.rgb * _Color.rgb * max(0, dot(worldNormalDir, worldLightDir));
fixed3 refraction = texCUBE(_CubeMap, i.worldReflection).rgb;
fixed3 fresnel = _FresnelScale + (1 - _FresnelScale) * pow((1 - dot(worldViewDir, worldNormalDir)), 5);
UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);
fixed3 color = ambient + lerp(diffuseColor, refraction, fresnel) * atten;
return fixed4(color, 1);
}
ENDCG
}
}
}
UnityShader 学习笔记 13 高级纹理之菲涅尔反射
最新推荐文章于 2024-06-25 15:15:00 发布