Unity common gray processing

Since the human eye has different sensitivity to RGB, the sensitivity to green is the highest, the sensitivity to red is second, and the sensitivity to blue is the lowest. Therefore, it is necessary to set different weights for RGB to achieve the effect of grayscale display. The commonly used RGB weight values ​​are R: 0.298912, G: 0.586611, B: 0.114478
grayColor.rgb = float3(color.r 0.298912 , color.g 0.586611 ,color.b*0.114478)

1 When the UI object is unavailable, the graying effect is displayed
through the shader. Add the variable _ShowGray to the shader. In the code, you can dynamically assign a value to the variable to control whether to display the shader. The code is through Image
Effect The shader is obtained by simple modification,

Shader "UI/UIGray"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        [Toggle]_ShowGray("show gray", Range(0,1)) = 0
    }
    SubShader
    {
        // No culling or depth
        Cull Off ZWrite Off ZTest Always
        //-----add code-------
        Blend SrcAlpha OneMinusSrcAlpha
        //----finish----
        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;
            fixed _ShowGray;

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.uv);
                // just invert the colors
                //col.rgb = 1 - col.rgb;
            //----add code----
                fixed gray = dot(col.rgb, float3(0.298912, 0.586611, 0.114478));
                col.rgb = lerp(col.rgb, fixed3(gray, gray, gray), _ShowGray);
            //-----finish-----
                return col;
            }
            ENDCG
        }
    }
}

2 All objects in the scene are grayed out, such as the graying effect displayed when the battle fails. The
scene is grayed out. Generally, the camera rendering is set, and the script is added to the camera. In the OnRenderImage callback method, the rendering object is processed.
Script

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

public class PostEffectGray : MonoBehaviour
{
    public Material grayMaterial;
    void OnRenderImage(RenderTexture src, RenderTexture dest)
    {
        Graphics.Blit(src, dest, grayMaterial);
    }
}


Enable the graying script
insert image description here
Disable the graying script
insert image description here
The shader used by the Gray shader here is a simple graying effect shader, the code is as follows

Shader "Unlit/Gray"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #include "UnityCG.cginc"

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

            struct v2f
            {
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

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

            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                fixed4 col = tex2D(_MainTex, i.uv);
                half3 gray = dot(col.rgb, half3 (0.22, 0.707, 0.071));
            // apply fog
                UNITY_APPLY_FOG(i.fogCoord, col);
                return fixed4(gray.rgb, col.a);
            }
            ENDCG
        }
    }
}

Guess you like

Origin blog.csdn.net/u011484013/article/details/118880020