Unity Shader

Shaders 102 - Basics of Image Ef

2020-12-10  本文已影响0人  zitaoye

Shaders 102 - Basics of Image Effects

整体对屏幕效果进行处理
Screen = Mega Quad

image.png

OnRenderImage(src,dst);
制作脚本挂在Camera下,

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
public class CustomImageEffect : MonoBehaviour
{
    public Material EffectMat;
    private void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        if (EffectMat != null) {
            Graphics.Blit(source, destination, EffectMat);
        }
    }   
}

不要把texture想成一些一定是可视化的东西,实际上对于shader来说,texture就是数据,可以是任何噪声Noise贴图,混乱的等等。


image.png

使用一个Noise贴图去进行变化,需要把这个贴图设置为Repeat。

Shader "Hidden/BasicImageEffect"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _DisplaceTex("Displacement Texture",2D) = "white"{}
        _Magnitude("Magnitude",Range(0,0.1))=1
    }
    SubShader
    {
        // No culling or depth
        Cull Off ZWrite Off ZTest Always

        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;
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            sampler2D _MainTex;
            sampler2D _DisplaceTex;
            float _Magnitude;



            fixed4 frag(v2f i) : SV_Target
            {
                float2 disp = tex2D(_DisplaceTex, i.uv).xy;
                disp = ((disp * 2) - 1) * _Magnitude;

                fixed4 col = tex2D(_MainTex,i.uv+disp);
                
                // just invert the colors
                //col.rgb = 1 - col.rgb;
                return col;
            }
            ENDCG
        }
    }
}

Box Blur
通过多次blur自己的rendertexture
并且使用低分辨率的设置方法来提高效率

using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class BoxBlur : MonoBehaviour
{
    public Material blurMat;
    [Range(0,10)]
    public int Iterations;
    [Range(0, 4)]
    public int DownRes;

    private void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        int width = source.width >> DownRes;
        int height = source.height >> DownRes;

        RenderTexture rt = RenderTexture.GetTemporary(width, height);
        Graphics.Blit(source, rt);

        for (int i = 0; i < Iterations; i++)
        {
            RenderTexture rt2 = RenderTexture.GetTemporary(width, height);
            Graphics.Blit(rt, rt2, blurMat);
            RenderTexture.ReleaseTemporary(rt);
            rt = rt2;
        }
        Graphics.Blit(rt, destination);
        RenderTexture.ReleaseTemporary(rt);
    }
}


通过_Time.x的变量将波纹动起来
time指的是时间
Time (t/20, t, t2, t3), use to animate things inside the shaders.

Shader "Shader102/AnimatedDisplacement"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _DisplaceTex("Displacement Texture",2D) = "white"{}
        _Magnitude("Magnitude",Range(0,0.1)) = 1
    }
    SubShader
    {
        // No culling or depth
        Cull Off ZWrite Off ZTest Always

        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;
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            sampler2D _MainTex;
            sampler2D _DisplaceTex;
            float _Magnitude;

            fixed4 frag(v2f i) : SV_Target
            {
                // 用滚动的uv拿到滚动的noise的值,再用这个噪音颜色的值,去加上maintex的uv值...
                float2 distuv = float2(i.uv.x + _Time.x * 2,i.uv.y + _Time.x * 2);
                float2 disp = tex2D(_DisplaceTex,distuv).xy;
                disp = ((disp * 2) - 1)* _Magnitude;
                float4 col = tex2D(_MainTex, i.uv+disp);          
                return col;
            }
            ENDCG
        }
    }
}

上一篇下一篇

猜你喜欢

热点阅读