shader如下:
Shader "Custom/Refraction" { Properties { _Cubemap("Cubemap", Cube) = "" {} _RefractRatio("Refract Ratio", Range(0,1)) = 0.9 _RefractionAmount("Reflection Amount", Range(0, 1)) = 0.5 } SubShader { Pass { Tags { "LightMode" = "ForwardBase" } Cull Off CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma multi_compile_fwdbase #include "UnityCG.cginc" #include "Lighting.cginc" #include "AutoLight.cginc" samplerCUBE _Cubemap; float _RefractRatio; float _RefractionAmount; struct appdata { float4 vertex : POSITION; fixed4 color : COLOR; float3 normal : NORMAl; }; struct v2f { float4 pos : SV_POSITION; fixed4 color : COLOR; float3 worldNormal : TEXCOORD0; float3 worldPos : TEXCOORD1; SHADOW_COORDS(2) }; v2f vert(appdata v) { v2f o; o.pos = mul(UNITY_MATRIX_MVP, v.vertex); o.color = v.color; o.worldNormal = normalize(UnityObjectToWorldNormal(v.normal)); o.worldPos = mul(unity_ObjectToWorld, v.vertex); TRANSFER_SHADOW(o); return o; } fixed4 frag(v2f i) : SV_TARGET { fixed3 albedo = i.color.rgb; fixed3 ambient = albedo * UNITY_LIGHTMODEL_AMBIENT.rgb; float3 worldLight = UnityWorldSpaceLightDir(i.worldPos); fixed3 diffuse = albedo * _LightColor0.rgb * max(0, dot(worldLight, i.worldNormal)); float3 worldView = UnityWorldSpaceViewDir(i.worldPos); float3 refDir = refract(-normalize(worldView), normalize(i.worldNormal), _RefractRatio); fixed3 refCol = texCUBE(_Cubemap, refDir); UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos); fixed3 col = ambient + lerp(diffuse, refCol, _RefractionAmount) * atten; return fixed4(col, 1); } ENDCG } } Fallback "VertexLit" }
效果如下: