当前位置:网站首页>Simple refraction effect

Simple refraction effect

2022-06-24 08:04:00 Kenight_

adopt GrabPass Realization
Shader "Unlit/Glass"
{
    Properties
    {
        _BumpAmt ("Distortion", Range (0,100)) = 10
        _TintAmt ("Tint Amount", Range(0,1)) = 0.1
        _BlurAmt ("Blur Amount", Range (0,1)) = 0.1
        _MainTex ("Texture", 2D) = "white" {}
        _BumpMap ("Normal Map", 2D) = "bump" {}
    }
    SubShader
    {
        Tags { "Queue" = "Transparent" } //  important : Use transparent queues to ensure that other objects are drawn on the screen 

        GrabPass { "_GrabBlurTexture" }

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float4 uv : TEXCOORD0;
                float4 grabPos : TEXCOORD1;
                float4 vertex : SV_POSITION;
            };

            float _BumpAmt;
            float _TintAmt;
            float _BlurAmt;
            sampler2D _MainTex, _BumpMap;
            float4 _MainTex_ST, _BumpMap_ST;
            sampler2D _GrabBlurTexture;
            float4 _GrabBlurTexture_TexelSize; // texture size, x:1.0/width y:1.0/height z:width w:height

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv.xy = TRANSFORM_TEX(v.uv, _MainTex);
                o.uv.zw = TRANSFORM_TEX(v.uv, _BumpMap);
                o.grabPos = ComputeGrabScreenPos(o.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                //  from  normalmap  The bump value is sampled from 
                float2 bump = UnpackNormal(tex2D(_BumpMap, i.uv.zw)).xy;

                //  Offset (*_TexelSize.xy  The offset value is scaled according to the unit pixel of the captured texture )
                float2 offset = bump * _BumpAmt * _GrabBlurTexture_TexelSize.xy;

                i.grabPos.xy = offset + i.grabPos.xy;

                fixed4 col = tex2D(_GrabBlurTexture, i.grabPos.xy / i.grabPos.w); //  Divide  w  The screen space sampling coordinates are obtained 

                // blur ( This is a more garbage blur effect )
                float blur = _BlurAmt / 100;
                col += tex2D(_GrabBlurTexture, grabuv + float2(-blur, 0));
                col += tex2D(_GrabBlurTexture, grabuv + float2(blur, 0));
                col += tex2D(_GrabBlurTexture, grabuv + float2(0, blur));
                col += tex2D(_GrabBlurTexture, grabuv + float2(0, -blur));
                col = col / 5;

                fixed4 tint = tex2D(_MainTex, i.uv.xy);
                col = lerp (col, tint, _TintAmt);
                return col;
            }
            ENDCG
        }
    }
}
adopt Command Buffers Realization
//  Use  command buffer  To generate  RT
public class ComdBuf : MonoBehaviour
{
    //  Execute... Before camera rendering 
    void OnWillRenderObject()
    {
        var cam = Camera.current;
        CommandBuffer buff = new CommandBuffer();

        //  apply  RT
        int rtID = Shader.PropertyToID("_GrabBlurTexture");
        buff.GetTemporaryRT(rtID, -1, -1, 0, FilterMode.Bilinear); // -1 for camera pixel width or height

        // blit  Method acceptance  RT  Identifier  RenderTargetIdentifier ( namely  rtID)
        buff.Blit(BuiltinRenderTextureType.CurrentActive, rtID); //  Render the current  RT  Copy to  rtID

        //  Use this command to make the  shader  Pass through  sampler2D _GrabBlurTexture  Get the right  RT
        buff.SetGlobalTexture(rtID, rtID); //  The first parameter is  shader  Variable name in , The second is what the name identifies  RT

        //  Execution opportunity 
        cam.AddCommandBuffer(CameraEvent.AfterSkybox, buff);
    }
}

You can then use the... Above Shader And get rid of it. GrabPass { “_GrabBlurTexture” } You can achieve the same effect

原网站

版权声明
本文为[Kenight_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240219094242.html