反射 - 对物体表面进行反射处理
折射 - 对物体表面进行折射处理
菲涅尔反射 - 对物体入射角越小的反射效果越好,越大的边界反射亮度越大。(大概就是这样,具体查百度)
菲涅尔反射的效果跟接近现实中环境反射
它们都需要使用立方体纹理,也就是环境映射纹理
反射
Shader "Unlit/ReflectionShader"
{
Properties
{
_Color ("Color Tint", Color) = (1, 1, 1, 1)
_ReflectColor ("Reflect Color", Color) = (1, 1, 1, 1)
_ReflectAmount ("Reflect Amount", Range(0, 1)) = 1
_Cubemap ("Reflection", 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>
struct a2v
{
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f
{
float4 pos : SV_POSITION;
float3 worldPos : TEXCOORD0;
float3 worldNormal : TEXCOORD1;
SHADOW_COORDS(4)
};
fixed4 _Color;
fixed4 _ReflectColor;
fixed _ReflectAmount;
samplerCUBE _Cubemap;
v2f vert(a2v v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex.xyz);
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
o.worldNormal = UnityObjectToWorldNormal(v.normal);
TRANSFER_SHADOW(o);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
fixed3 worldNormal = normalize(i.worldNormal);
fixed3 litDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));
fixed3 refl = reflect(-viewDir, worldNormal);
//环境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
//漫反射
fixed3 diffuse = _LightColor0.rgb * _Color.rgb * (dot(litDir, worldNormal) * 0.5 + 0.5);
//读取环境纹理(使用反射)
fixed3 reflection = texCUBE(_Cubemap, refl).rgb * _ReflectColor.rgb;
UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);
return fixed4(ambient + lerp(diffuse, reflection, _ReflectAmount) * atten, 1.0);
}
ENDCG
}
}
//FallBack "Standard"
}
折射
Shader "Unlit/RefractionShader"
{
Properties
{
_Color ("Color Tint", Color) = (1, 1, 1, 1)
_RefractColor ("Refract Color", Color) = (1, 1, 1, 1)
_RefractAmount ("Refract Amount", Range(0, 1)) = 1
_RefractRatio ("Refract Ratio", Range(0, 1)) = 0.5
_Cubemap ("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>
struct a2v
{
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f
{
float4 pos : SV_POSITION;
float3 worldPos : TEXCOORD0;
float3 worldNormal : TEXCOORD1;
SHADOW_COORDS(4)
};
fixed4 _Color;
fixed4 _RefractColor;
fixed _RefractAmount;
fixed _RefractRatio;
samplerCUBE _Cubemap;
v2f vert(a2v v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
o.worldNormal = UnityObjectToWorldNormal(v.normal);
TRANSFER_SHADOW(o);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
fixed3 worldNormal = normalize(i.worldNormal);
fixed3 litDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));
fixed3 refr = normalize(refract(-viewDir, worldNormal, _RefractRatio));
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed3 diffuse = _LightColor0.rgb * _Color.rgb * (dot(litDir, worldNormal) * 0.5 + 0.5);
fixed3 refraction = texCUBE(_Cubemap, refr).rgb * _RefractColor.rgb;
UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);
return fixed4(ambient + lerp(diffuse, refraction, _RefractAmount) * atten, 1.0);
}
ENDCG
}
}
//FallBack "Standard"
}
菲涅尔反射
Shader "Unlit/Fresnel"
{
Properties
{
_Color ("Color Tint", Color) = (1, 1, 1, 1)
_FresnelScale ("Fresnel Scale", Range(0, 1)) = 0.5
_Cubemap ("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>
struct a2v
{
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f
{
float4 pos : SV_POSITION;
float3 worldPos : TEXCOORD0;
float3 worldNormal : TEXCOORD1;
SHADOW_COORDS(4)
};
fixed4 _Color;
fixed _FresnelScale;
samplerCUBE _Cubemap;
v2f vert(a2v v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
o.worldNormal = UnityObjectToWorldNormal(v.normal);
TRANSFER_SHADOW(o);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
fixed3 worldNormal = normalize(i.worldNormal);
fixed3 litDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));
fixed3 refl = normalize(reflect(-viewDir, worldNormal));
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed3 diffuse = _LightColor0.rgb * _Color.rgb * (dot(litDir, worldNormal) * 0.5 + 0.5);
fixed fresnel = _FresnelScale + (1 - _FresnelScale) * pow(1 - dot(viewDir, worldNormal), 5);
fixed3 reflection = texCUBE(_Cubemap, refl);
UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);
return fixed4(ambient + lerp(diffuse, reflection, fresnel) * atten, 1.0);
}
ENDCG
}
}
}
<<Unity Shader 入门精要>> - 高级纹理中反射,折射,菲涅尔反射的实现。