Unity 简单的屏幕特效样例

作者: 2b75747cf703 | 来源:发表于2016-02-25 21:53 被阅读1474次

脚本挂在相机组件上即可。

Paste_Image.png Paste_Image.png Paste_Image.png
using UnityEngine;

[ExecuteInEditMode]
public class ImageEffectExample : MonoBehaviour
{
    public Shader shader;

    private Material _material;
    private Material material
    {
        get
        {
            if (shader == null)
                return null;

            if (_material == null)
            {
                _material = new Material(shader);
                _material.hideFlags = HideFlags.HideAndDontSave;
            }

            return _material;
        }
    }

    void Start()
    {
        if (!SystemInfo.supportsImageEffects)
        {
            enabled = false;
            return;
        }

        if (shader == null || !shader.isSupported)
        {
            enabled = false;
        }
    }

    void OnRenderImage(RenderTexture sourceTexture, RenderTexture destTexture)
    {
        if (material != null)
            Graphics.Blit(sourceTexture, destTexture, material);
        else
            Graphics.Blit(sourceTexture, destTexture);
    }

    void OnDisable()
    {
        if (_material)
        {
            DestroyImmediate(_material);
        }
    }
}
Shader "Hidden/ImageEffectShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    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 = mul(UNITY_MATRIX_MVP, v.vertex);
                o.uv = v.uv;
                return o;
            }

            sampler2D _MainTex;

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.uv);
                col.rgb = (col.r + col.g + col.b) / 3;
                return col;
            }
            ENDCG
        }
    }
}

相关文章

网友评论

  • Alphazhu:您好,基于屏幕的,为什么第二张图的两个ui没有被影响???
    2b75747cf703:@Alphazhu 不同相机
    Alphazhu:@千里伏骥 额,就是第二张图的左上角的按钮为什么还是彩色的
    2b75747cf703:哪里没被影响?
  • 蓬莱仙羽:不错!

本文标题:Unity 简单的屏幕特效样例

本文链接:https://www.haomeiwen.com/subject/mwzikttx.html