内置渲染管线

  1 Shader "Unity Shaders Book/Chapter 10/Glass Refraction" {
  2     Properties {
  3         _MainTex("Main Tex", 2D) = "white" {}
  4         _BumpMap("Normal Map", 2D) = "bump" {}
  5         _Cubemap("Env Cubemap", Cube) = "_Skybox" {}
  6         _Distortion("Distortion", Range(0, 100)) = 10
  7         _RefractAmount("Refract Amount", Range(0, 1)) = 0.5
  8     }
  9  
 10     SubShader {
 11         Tags {"Queue"="Transparent" "RenderType"="Opaque"}
 12  
 13         GrabPass {"_RefractionTex"}
 14  
 15         Pass {
 16             Tags {"LightMode"="ForwardBase"}
 17  
 18             CGPROGRAM
 19             #include "UnityCG.cginc"
 20  
 21             #pragma vertex vert
 22             #pragma fragment frag
 23  
 24             sampler2D _MainTex;
 25             float4 _MainTex_ST;
 26             sampler2D _BumpMap;
 27             float4 _BumpMap_ST;
 28             samplerCUBE _Cubemap;
 29             float _Distortion;
 30             fixed _RefractAmount;
 31             sampler2D _RefractionTex;
 32             float4 _RefractionTex_TexelSize;
 33  
 34             struct a2v {
 35                 float4 vertex : POSITION;
 36                 float3 normal : NORMAL;
 37                 float4 tangent : TANGENT;
 38                 float4 texcoord  : TEXCOORD0;
 39             };
 40  
 41             struct v2f {
 42                 float4 pos : SV_POSITION;
 43                 float4 uv : TEXCOORD0;
 44                 float4 scrPos : TEXCOORD1;
 45                 float4 TtoW0 : TEXCOORD2;
 46                 float4 TtoW1 : TEXCOORD3;
 47                 float4 TtoW2 : TEXCOORD4;
 48             };
 49  
 50             v2f vert(a2v v) {
 51                 v2f o;
 52                 o.pos = UnityObjectToClipPos(v.vertex);
 53  
 54                 o.scrPos = ComputeGrabScreenPos(o.pos);
 55  
 56                 o.uv.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
 57                 o.uv.zw = TRANSFORM_TEX(v.texcoord, _BumpMap);
 58                 // o.uv.xy = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
 59                 // o.uv.zw = v.texcoord.xy * _BumpMap_ST.xy + _BumpMap_ST.zw;
 60  
 61                 float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
 62                 fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
 63                 fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
 64                 fixed3 worldBinormal = cross(worldNormal, worldTangent) * v.tangent.w;
 65                 o.TtoW0 = float4(worldTangent.x, worldBinormal.x, worldNormal.x, worldPos.x);
 66                 o.TtoW1 = float4(worldTangent.y, worldBinormal.y, worldNormal.y, worldPos.y);
 67                 o.TtoW2 = float4(worldTangent.z, worldBinormal.z, worldNormal.z, worldPos.z);
 68  
 69                 return o;
 70             }
 71  
 72             fixed4 frag(v2f i) : SV_Target {
 73  
 74                 float3 worldPos = float3(i.TtoW0.w, i.TtoW1.w, i.TtoW2.w);
 75                 fixed3 worldViewDir = normalize(UnityWorldSpaceViewDir(worldPos));
 76  
 77                 fixed3 bump = UnpackNormal(tex2D(_BumpMap, i.uv.zw));
 78                 fixed3 worldNormal = half3(dot(i.TtoW0.xyz, bump), dot(i.TtoW1.xyz, bump), dot(i.TtoW2.xyz, bump));
 79                 worldNormal = normalize(worldNormal);
 80                 
 81                 // 折射
 82                 float2 offset = bump.xy * _Distortion * _RefractionTex_TexelSize.xy;
 83                 i.scrPos.xy = offset + i.scrPos.xy;
 84                 fixed3 refrCol = tex2D(_RefractionTex, i.scrPos.xy / i.scrPos.w).rgb;
 85  
 86                 // 反射
 87                 fixed3 reflDir = reflect(-worldViewDir, worldNormal);
 88                 fixed3 texColor = tex2D(_MainTex, i.uv.xy);
 89                 fixed3 reflCol = texCUBE(_Cubemap, reflDir).rgb * texColor.rgb;
 90  
 91                 fixed3 finalColor = reflCol * (1 - _RefractAmount) + refrCol * _RefractAmount;
 92                 return fixed4(finalColor, 1.0);
 93  
 94             }
 95  
 96             ENDCG
 97         }
 98     }
 99  
100     FallBack "Diffuse"
101 }

urp渲染管线

 

Shader "SunY/C10-U-GlassRefraction"
{
    Properties
    {
        _MainTex("Main Tex", 2D) = "white"{}
        _BumpMap("Bump Map", 2D) = "bump"{}
        _Cubemap("Env Cubemap", Cube) = "_Skybox"{}
        _Distortion("Distortion", Range(0, 100)) = 100
        _RefractAmount("Refraction Amount", Range(0, 1)) = 0.5
    }
    SubShader
    {
        Tags { "RenderPipeline" = "UniversalPipeline" "Queue" = "Transparent" "RenderType" = "Opaque" }

        Pass
        {
            Tags { "LightMode" = "UniversalForward" }

            HLSLPROGRAM

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareOpaqueTexture.hlsl"

            #pragma vertex vert
            #pragma fragment frag

            CBUFFER_START(UnityPerMaterial)
            float4 _MainTex_ST;
            float4 _BumpMap_ST;
            float _Distortion;
            half _RefractAmount;
            float4 _CameraOpaqueTexture_TexelSize;
            CBUFFER_END

            TEXTURE2D(_MainTex);
            SAMPLER(sampler_MainTex);

            TEXTURE2D(_BumpMap);
            SAMPLER(sampler_BumpMap);

            TEXTURECUBE(_Cubemap);
            SAMPLER(sampler_Cubemap);

            struct a2v
            {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
                float4 tangent : TANGENT;
                float4 texcoord : TEXCOORD0;
            };

            struct v2f
            {
                float4 pos : SV_POSITION;
                float4 uv : TEXCOORD0;
                float4 scrPos : TEXCOORD1;
                float4 TtoW0 : TEXCOORD2;
                float4 TtoW1 : TEXCOORD3;
                float4 TtoW2 : TEXCOORD4;
            };

            v2f vert(a2v v)
            {
                v2f o;
                VertexPositionInputs posInputs = GetVertexPositionInputs(v.vertex.xyz);
                o.pos = posInputs.positionCS;

                o.scrPos = ComputeScreenPos(posInputs.positionCS);

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

                float3 worldPos = mul(UNITY_MATRIX_M, v.vertex).xyz;

                half3 worldNormal = TransformObjectToWorldNormal(v.normal);
                half3 worldTangent = TransformObjectToWorldDir(v.tangent.xyz);
                half3 worldBinormal = cross(worldNormal, worldTangent) * v.tangent.w;

                o.TtoW0 = float4(worldTangent.x, worldBinormal.x, worldNormal.x, worldPos.x);
                o.TtoW1 = float4(worldTangent.y, worldBinormal.y, worldNormal.y, worldPos.y);
                o.TtoW2 = float4(worldTangent.z, worldBinormal.z, worldNormal.z, worldPos.z);

                return o;
            }

            half4 frag(v2f i) : SV_Target
            {
                float3 worldPos = float3(i.TtoW0.w, i.TtoW1.w, i.TtoW2.w);
                half3 worldViewDir = normalize(_WorldSpaceCameraPos.xyz - worldPos);

                half3 bump = UnpackNormal(SAMPLE_TEXTURE2D(_BumpMap, sampler_BumpMap, i.uv.zw));
                half3 worldNormal = half3(dot(i.TtoW0.xyz, bump), dot(i.TtoW1.xyz, bump), dot(i.TtoW2.xyz, bump));
                worldNormal = normalize(worldNormal);

                // 折射
                float2 offest = bump.xy * _Distortion * _CameraOpaqueTexture_TexelSize.xy;
                i.scrPos.xy = offest * i.scrPos.z + i.scrPos.xy;
                half3 refrCol = SampleSceneColor(i.scrPos.xy / i.scrPos.w);

                // 反射
                half3 reflDir = reflect(-worldViewDir, worldNormal);
                half3 texColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv.xy);
                half3 reflCol = SAMPLE_TEXTURECUBE(_Cubemap, sampler_Cubemap, reflDir).rgb * texColor.rgb;
 
                half3 finalColor = reflCol * (1 - _RefractAmount) + refrCol * _RefractAmount;

                return half4(finalColor, 1.0);
            }

            ENDHLSL
        }
    }
}