Shader 高斯模糊, 鏡頭模糊

 

對物體圖像 做一個  上下左右 偏移渲染

 

Shader 部分:

 

Shader "Custom/Gaosi"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _Speed("SetSpeed",float) = 1
        _offestPos("offestPos" ,float) = 0.1
    }
    SubShader
    {
        // No culling or depth
      //  Cull Off ZWrite Off //ZTest Always
      Cull Off
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

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

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };
            float _WaveA;
            float _WaveX;
            v2f vert (appdata v)
            {
                v2f o;
                float xx =_WaveA* sin(v.vertex.x* _WaveX+_Time.z);
                v.vertex.y += xx;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            sampler2D _MainTex;
            float _Speed;
            float _offestPos;
            fixed4 frag (v2f i) : SV_Target
            {
                float2 tmpUV = i.uv;
                //tmpUV.x += _Time.y*_Speed;
                fixed4 col = tex2D(_MainTex, tmpUV);
                fixed4 col1 = tex2D(_MainTex,i.uv+float2(0,_offestPos));
                fixed4 col2 = tex2D(_MainTex,i.uv-float2(0,_offestPos));
                fixed4 col3 = tex2D(_MainTex,i.uv+float2(_offestPos,0));
                fixed4 col4 = tex2D(_MainTex,i.uv-float2(_offestPos,0));

                col = (col+ col1+ col2+ col3+ col4) / 5.0;
                // just invert the colors
               // col.rgb = 1 - col.rgb;
                return col;
            }
            ENDCG
        }
    }
}

 

 

對鏡頭 進行模糊處理:

C#  部分代碼:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Gaosi : MonoBehaviour
{
    public Material material;

    // Start is called before the first frame update
    void Start()
    {
        
    }

  //說明:此函數在當完成所有渲染圖片後被調用,用來渲染圖片後期效果
    private void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        //用 material 去對source 在渲染一次, 渲染完成存入destination
        Graphics.Blit(source, destination, material);
    }
    // Update is called once per frame
    void Update()
    {
      
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章