新建一个表面着色器shader文件
Shader "Custom/GlassRefraction"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Color ("Main Color", Color) = (1,1,1,1)
_RefractAmount ("Refraction Amount", Range(0.1, 1.0)) = 0.5
}
SubShader
{
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
LOD 200
CGPROGRAM
#pragma surface surf Lambert alpha:fade
sampler2D _MainTex;
float4 _Color;
float _RefractAmount;
struct Input
{
float2 uv_MainTex;
float3 worldNormal;
float3 worldPos;
};
void surf (Input IN, inout SurfaceOutput o)
{
// 计算折射向量
float3 viewDir = normalize(_WorldSpaceCameraPos - IN.worldPos);
float cosTheta = dot(viewDir, IN.worldNormal);
float3 refractDir = refract(viewDir, IN.worldNormal, 1.0 / 4.6); // 1.5是玻璃的折射率
refractDir = normalize(refractDir);
// 获取折射的颜色
float2 perturbUV = IN.uv_MainTex + refractDir.xy * _RefractAmount;
float4 refractionColor = tex2D(_MainTex, perturbUV);
// 设置表面颜色和透明度
o.Albedo = _Color.rgb * refractionColor.rgb;
o.Alpha = refractionColor.a;
}
ENDCG
}
FallBack "Transparent/Specular"
}
写好之后保存,,导入一张你想要反射的图片,点击shader文件,在inspector面板中将导入的图片拖拽至Texture旁边的小框里
接着新建一个材质,将刚写好的shader文件拖拽添加至该材质中。创建一个板状物体,将该材质添加至该物体即可
效果: