zoukankan      html  css  js  c++  java
  • unity屏幕灰色显示

    C#脚本如下:

    using UnityEngine;
    using System.Collections;
    
    [ExecuteInEditMode]
    public class TestRenderImage : MonoBehaviour
    {
        public Shader curShader;
        public float grayScaleAmount = 1.0f;
        private Material curMaterial;
    
        Material material
        {
            get
            { 
                if (curMaterial == null)
                {
                    curMaterial = new Material(curShader);
                    curMaterial.hideFlags = HideFlags.HideAndDontSave;
                }
                return curMaterial;
            }
        }
         
        void Start()
        {
            if (!SystemInfo.supportsImageEffects)
            {
                enabled = false;
                return;
            }
            if (!curShader && !curShader.isSupported)
            { 
                enabled = false;
            } 
        }
         
        void OnRenderImage(RenderTexture sourceTexture, RenderTexture destTexture)
        {
            if (curShader != null)
            {
                material.SetFloat("_LumionsityAmount", grayScaleAmount);
                Graphics.Blit(sourceTexture, destTexture, material);
            }
            else
            {
                Graphics.Blit(sourceTexture, destTexture);
            }
        }
    
        void Update()
        {
            grayScaleAmount = Mathf.Clamp(grayScaleAmount, 0, 1.0f);
        }
    
        void OnDisable()
        {
            if (curMaterial)
            {
                DestroyImmediate(curMaterial);
            }
        }
    }

    shader脚本如下:

    Shader "Custom/ImageEffect" {
        Properties {
            _MainTex("Base (RGB)",2D) = "white"{}
            _LuminosityAmount("GrayScale Amount",Range(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
            }
        }
    
        FallBack "Diffuse"
    }

     C#脚本拖到摄像机上面即可。

  • 相关阅读:
    设计模式读书笔记
    effective_c++(第三版)读书笔记
    CS-Notes 操作系统读书笔记
    数据库笔记
    后台开发核心技术与应用读书笔记
    python3.7安装numpy pandas失败的处理方案
    线段树模板
    KMP算法
    离散实验——欧拉图的判定和应用
    堆排序算法及其实现
  • 原文地址:https://www.cnblogs.com/Yellow0-0River/p/5670118.html
Copyright © 2011-2022 走看看