Shader入门精要读书笔记 - CH10.1.4_折射

发布时间 2023-05-24 00:58:37作者: yanghui01

最终效果

 

关于折射

光的反射和折射区别:
反射:光线在介质表面反弹后进入人眼
折射:光线穿透介质后进入人眼,所以看到的是介质后面的物体

 

为什么最终效果看着是一种放大的效果?

如果不发生折射,将看到灰色光线对应的区域,而现在看到的区域变小了。就相当于,本来是100x100的图片用100x100的Image显示;现在80x80的图片用100x100的Image显示。

 

Shader "My/Tex2/Refract"
{
    Properties
    {
        _MainTex("Main Tex", 2D) = "white" {}
        _Color("Diffuse Color Tint", Color) = (1, 1, 1, 1)

        _Cubemap("Refraction Cubemap", Cube) = "_Skybox" {} //折射环境时从这个贴图采样环境

        _RefractColor("Refraction Tint Color", Color) = (1, 1, 1, 1)
        _RefractRatio("Refraction Ratio", Range(0.01, 1)) = 0.5 //两种介质的折射率比值, 比如: 通过玻璃看到后面扭曲的物体时, 空气折射率/玻璃的折射率
        _RefractAmount("Refraction Amount", Range(0, 1)) = 1 //折射程度
    }

    SubShader
    {
        Tags { "RenderType" = "Opaque" "Queue" = "Geometry"}

        Pass
        {
            Tags { "LightMode" = "ForwardBase" }

            CGPROGRAM

            #pragma multi_compile_fwdbase

            #pragma vertex vert
            #pragma fragment frag

            #include "Lighting.cginc"

            sampler2D _MainTex;
            float4 _MainTex_ST;
            fixed4 _Color;

            samplerCUBE _Cubemap;

            fixed4 _RefractColor;
            fixed _RefractRatio;
            float _RefractAmount;

            struct a2v
            {
                float4 vertex : POSITION;
                float4 texcoord : TEXCOORD0;
                float3 normal : NORMAL; //顶点法线
            };

            struct v2f
            {
                float4 pos : SV_POSITION;
                float2 uv : TEXCOORD0;
                float3 worldPos : TEXCOORD1;
                fixed3 worldNormal : TEXCOORD2;
                fixed3 worldViewDir : TEXCOORD3;
                fixed3 worldRefr : TEXCOORD4;
            };

            v2f vert(a2v v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
                o.worldNormal = UnityObjectToWorldNormal(v.normal); //法线(世界空间)
                o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz; //世界坐标
                o.worldViewDir = UnityWorldSpaceViewDir(o.worldPos); //观看方向(世界空间), 顶点指向观察点

                o.worldRefr = refract(-normalize(o.worldViewDir), normalize(o.worldNormal), _RefractRatio); //折射方向(世界空间)

                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                fixed3 worldNormal = normalize(i.worldNormal);
                fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
                fixed3 worldViewDir = normalize(i.worldViewDir);

                fixed3 albedo = tex2D(_MainTex, i.uv).rgb * _Color.rgb; //取贴图颜色作为漫反射颜色

                fixed lambert = max(0, dot(worldNormal, worldLightDir));
                fixed3 diffuse = _LightColor0.rgb * albedo * lambert; //漫反射一般是吸收反射(乘)主光源颜色

                // Use the refract dir in world space to access the cubemap
                fixed3 refraction = texCUBE(_Cubemap, i.worldRefr).rgb * _RefractColor.rgb;

                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo; //吸收反射(乘)环境光颜色

                fixed3 color = ambient + lerp(diffuse, refraction, _RefractAmount); //各种反射颜色叠加
                return fixed4(color, 1.0);
            }

            ENDCG
        }
    }
    FallBack "Diffuse"
}