一、天空盒
使用立方体贴图最直接的目的就是展现天空盒,或者更进一步用于实现环境映射(反射与折射)
关于立方体贴图的采样原理:https://blog.csdn.net/Jaihk662/article/details/107379399
Unity 中天空盒的设置:
一样可以对于不同的摄像机使用不同的天空盒:
二、简单的反射实现
关于反射和折射的原理:https://blog.csdn.net/Jaihk662/article/details/107491144
代码和效果:
Shader "Jaihk662/Reflection1"
{
Properties
{
_DiffuseColor ("DiffuseColor", Color) = (1, 1, 1, 1)
_ReflectColor ("ReflectColor", Color) = (1, 1, 1, 1)
_ReflectAmount ("ReflectAmount", Range(0, 1)) = 1
_Cubemap ("ReflectionCubemap", Cube) = "_Skybox" {}
}
SubShader
{
LOD 200
PASS
{
Tags { "LightMode" = "ForwardBase" }
CGPROGRAM
#pragma vertex vert //声明顶点着色器的函数
#pragma fragment frag //声明片段着色器的函数
#include "UnityCG.cginc"
#include "Lighting.cginc"
fixed4 _DiffuseColor;
fixed4 _ReflectColor;
float _ReflectAmount;
samplerCUBE _Cubemap;
struct _2vert
{
float4 vertex: POSITION;
float3 normal: NORMAL;
float4 texcoord: TEXCOORD0;
};
struct vert2frag
{
float4 pos: SV_POSITION;
float3 wPos: TEXCOORD0;
float3 wNormal: TEXCOORD1;
float3 wViewDir: TEXCOORD2;
float3 wReflect: TEXCOORD3;
};
vert2frag vert(_2vert v)
{
vert2frag o;
o.pos = UnityObjectToClipPos(v.vertex);
o.wNormal = normalize(UnityObjectToWorldNormal(v.normal));
o.wPos = mul(unity_ObjectToWorld, v.vertex).xyz;
o.wViewDir = normalize(UnityWorldSpaceViewDir(o.wPos));
o.wReflect = reflect(-o.wViewDir, o.wNormal); //在片段着色器中计算反射向量,性能优先
return o;
}
fixed4 frag(vert2frag i): SV_Target
{
fixed3 wLightDir = normalize(UnityWorldSpaceLightDir(i.wPos));
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed3 diffuse = _LightColor0.rgb * _DiffuseColor.rgb * (0.5 * dot(i.wNormal, wLightDir) + 0.5);
fixed3 reflection = texCUBE(_Cubemap, i.wReflect).rgb * _ReflectColor.rgb;
return fixed4(ambient + lerp(diffuse, reflection, _ReflectAmount), 1.0);
}
ENDCG
}
}
FallBack "Specular"
}
这种反射属于假反射,只能反射天空盒,不能反射其它物体
三、简单的折射效果
代码和效果:
Shader "Jaihk662/Refraction1"
{
Properties
{
_DiffuseColor ("DiffuseColor", Color) = (1, 1, 1, 1)
_RefractColor ("RefractColor", Color) = (1, 1, 1, 1)
_RefractAmount ("RefractAmount", Range(0, 1)) = 1
_RefractRatio ("RefractiRatio", Range(0.1, 1)) = 0.5 //折射率
_Cubemap ("ReflectionCubemap", Cube) = "_Skybox" {}
}
SubShader
{
LOD 200
PASS
{
Tags { "LightMode" = "ForwardBase" }
CGPROGRAM
#pragma vertex vert //声明顶点着色器的函数
#pragma fragment frag //声明片段着色器的函数
#include "UnityCG.cginc"
#include "Lighting.cginc"
fixed4 _DiffuseColor;
fixed4 _RefractColor;
float _RefractAmount;
float _RefractRatio;
samplerCUBE _Cubemap;
struct _2vert
{
float4 vertex: POSITION;
float3 normal: NORMAL;
float4 texcoord: TEXCOORD0;
};
struct vert2frag
{
float4 pos: SV_POSITION;
float3 wPos: TEXCOORD0;
float3 wNormal: TEXCOORD1;
float3 wViewDir: TEXCOORD2;
float3 wRefract: TEXCOORD3;
};
vert2frag vert(_2vert v)
{
vert2frag o;
o.pos = UnityObjectToClipPos(v.vertex);
o.wNormal = normalize(UnityObjectToWorldNormal(v.normal));
o.wPos = mul(unity_ObjectToWorld, v.vertex).xyz;
o.wViewDir = normalize(UnityWorldSpaceViewDir(o.wPos));
o.wRefract = refract(-o.wViewDir, o.wNormal, _RefractRatio); //在片段着色器中计算反射向量,性能优先
return o;
}
fixed4 frag(vert2frag i): SV_Target
{
fixed3 wLightDir = normalize(UnityWorldSpaceLightDir(i.wPos));
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed3 diffuse = _LightColor0.rgb * _DiffuseColor.rgb * (0.5 * dot(i.wNormal, wLightDir) + 0.5);
fixed3 refraction = texCUBE(_Cubemap, i.wRefract).rgb * _RefractColor.rgb;
return fixed4(ambient + lerp(diffuse, refraction, _RefractAmount), 1.0);
}
ENDCG
}
}
FallBack "Specular"
}