Realization of unit ash disposal

Time:2021-9-24
catalog
  • 1. The graying effect is displayed when the UI object is unavailable
  • 2 all objects in the scene are grayed out, such as the graying effect displayed when the battle fails

Due to the different sensitivity of human eyes to RGB, the sensitivity to green is the highest, the sensitivity to red is the second, and the sensitivity to blue is the lowest. Therefore, different weights need to be set for RGB to achieve the effect of gray display. The commonly used RGB weight values are R: 0.298912, G: 0.586611, B: 0.114478
grayColor.rgb = float3(color.r0.298912 , color.g0.586611 ,color.b*0.114478)

1. The graying effect is displayed when the UI object is unavailable

Control and gray through the shader, and add variables to the shader_ Showgray, in the code, you can control whether to gray the display by dynamically assigning a value to the change quantity
The shader code is obtained by simply modifying the image effect shader,


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, the script is added on the camera, and the rendered object is processed in the onrenderimage callback method
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 graying script

在这里插入图片描述

Disable graying script

在这里插入图片描述

The shader used for 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
        }
    }
}

This is the end of this article about the implementation of unit graying. For more information about unit graying, please search for previous articles of developpaer or continue to browse the relevant articles below. I hope you will support developpaer in the future!

Recommended Today

Supervisor

Supervisor [note] Supervisor – H view supervisor command help Supervisorctl – H view supervisorctl command help Supervisorctl help view the action command of supervisorctl Supervisorctl help any action to view the use of this action 1. Introduction Supervisor is a process control system. Generally speaking, it can monitor your process. If the process exits abnormally, […]