UnityShader 学习笔记 14 高级纹理之模拟玻璃的折射

版权声明:本文为博主原创文章,未经博主允许不得转载。如有问题,欢迎指正。 https://blog.csdn.net/qq_37352817/article/details/85233947


Shader "_MyShader/7_Advanced/4_GlassRefraction"

{

    Properties {

        _MainTex ("MainTex", 2D) = "white" {}

            _BumpMap ("BumpMap", 2D) = "white" {}

            _CubeMap ("CubeMap", Cube) = "sky" {}

            _Distortion ("Distortion", Range(0, 200)) = 50

            _RefractAmount ("RefractAmount", Range(0, 1)) = 1

    }

    SubShader {

        Tags {  "Queue" = "Transparent" "RenderType"="Opaque"}



        GrabPass {"_RefractionTex"}



        Pass

        {

            Tags{"LightMode"="ForwardBase"}

            CGPROGRAM

            #pragma vertex vert

            #pragma fragment frag



            #include "UnityCG.cginc"

            #include "Lighting.cginc"



            sampler2D _MainTex;

            float4 _MainTex_ST;

            sampler2D _BumpMap;

            float4 _BumpMap_ST;

            samplerCUBE _CubeMap;

            float _Distortion;

            float _RefractAmount;

            sampler2D _RefractionTex;

            float4 _RefractionTex_TexelSize;



            struct a2v

            {

                float4 vertex:POSITION;

                float4 texcoord:TEXCOORD0;

                float3 normal:NORMAL;

                float4 tangent:TANGENT;

            };

            struct v2f

            {

                float4 pos:SV_POSITION;

                float4 scrPos:TEXCOORD1;

                float4 uv:TEXCOORD2;

                float3 TtoW0:TEXCOORD3;

                float3 TtoW1:TEXCOORD4;

                float3 TtoW2:TEXCOORD5;

                float3 worldPos:TEXCOORD6;

            };



            v2f vert(a2v v)

            {

                v2f o;

                o.pos = mul(UNITY_MATRIX_MVP,v.vertex);

                o.scrPos = ComputeGrabScreenPos(o.pos);

                o.uv.xy = TRANSFORM_TEX(v.texcoord, _MainTex);

                o.uv.zw = TRANSFORM_TEX(v.texcoord, _BumpMap);



                float3 worldNormal = UnityObjectToWorldNormal(v.normal);

                float3 worldTangent = UnityObjectToWorldDir(v.tangent);

                float3 worldBinormal = cross(worldNormal, worldTangent) * v.tangent.w;



                o.TtoW0 = float3(worldTangent.x, worldBinormal.x, worldNormal.x);

                o.TtoW1 = float3(worldTangent.y, worldBinormal.y, worldNormal.y);

                o.TtoW2 = float3(worldTangent.z, worldBinormal.z, worldNormal.z);



                o.worldPos = (_Object2World, v.vertex);



                return o;

            }



            fixed4 frag(v2f i):SV_Target

            {

                fixed3 worldViewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));

                fixed3 texColor = tex2D(_MainTex, i.uv.xy).rgb;

                fixed3 bump = UnpackNormal(tex2D(_BumpMap, i.uv.zw));

                float2 offset = bump.xy * _Distortion * _RefractionTex_TexelSize.xy;

                i.scrPos.xy = offset + i.scrPos.xy;

                fixed3 refrColor = tex2D(_RefractionTex, i.scrPos.xy / i.scrPos.w).rgb;



                fixed3 worldBump = normalize(half3(dot(i.TtoW0, bump), dot(i.TtoW1, bump), dot(i.TtoW2, bump)));

                fixed3 reflDir = reflect(-worldViewDir, worldBump);

                fixed3 reflColor = texCUBE(_CubeMap, reflDir).rgb * texColor;



                fixed3 finalColor = reflColor * (1 - _RefractAmount) + refrColor * _RefractAmount;



                return fixed4(finalColor, 1);

            }





            ENDCG

        }

    }

    FallBack "Diffuse"

}


猜你喜欢

转载自blog.csdn.net/qq_37352817/article/details/85233947