【Shader】游戏屏幕黑白特效

来源:互联网 发布:景安代理平台源码 编辑:程序博客网 时间:2024/04/27 06:28

.cs脚本挂载到MainCamera

using System.Collections;using System.Collections.Generic;using UnityEngine;[ExecuteInEditMode]public class TestImage : MonoBehaviour {    #region    public Shader curShader;    private float grayScaleAmount = 1.0f;    private Material curMaterial;    #endregion    #region Properties    Material material    {        get{            if (curMaterial == null)            {                curMaterial = new Material(curShader);                curMaterial.hideFlags = HideFlags.HideAndDontSave;            }            return curMaterial;        }    }    #endregion    // Use this for initialization    void Start () {        if (!SystemInfo.supportsImageEffects)        {            enabled = false;            return;        }        if (!curShader && !curShader.isSupported)        {            enabled = false;        }    } //   // Update is called once per frame //   void Update () { //       grayScaleAmount = Mathf.Clamp(grayScaleAmount, 0.0f, 1.0f);    //}    private void OnRenderImage(RenderTexture source, RenderTexture destination)    {        if (curShader != null)        {            material.SetFloat("_LuminosityAmount",grayScaleAmount);            Graphics.Blit(source,destination,material);        } else        {            Graphics.Blit(source,destination);        }    }    private void OnDisable()    {        if (curMaterial)        {            DestroyImmediate(curMaterial);        }    }}

.shader脚本附加到上面的属性中

Shader "Hidden/NewImageEffectShader"{    Properties    {        _MainTex ("Texture", 2D) = "white" {}        _LuminosityAmount("GrayScale Amount",Range(0.0,1)) = 1.0    }    SubShader    {        Pass        {            CGPROGRAM            #pragma vertex vert_img            #pragma fragment frag            #pragma fragmentoption ARB_precision_hint_fastest            #include "UnityCG.cginc"            uniform sampler2D _MainTex;            fixed _LuminosityAmount;            fixed4 frag (v2f_img i) : COLOR            {                fixed4 renderTex = tex2D(_MainTex,i.uv);                float luminosity = 0.299 * renderTex.r + 0.587 * renderTex.g + 0.114 * renderTex.b;                fixed4 finalColor = lerp(renderTex,luminosity,_LuminosityAmount);                return finalColor;            }            ENDCG        }    }}
阅读全文
0 0
原创粉丝点击