欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

unity置灰处理的实现

程序员文章站 2022-03-30 13:40:36
目录1 ui对象不可用的时候显示置灰效果由于人眼对rgb敏刚程度不同,对绿色的敏感度最高,对红色的敏感度次之,对蓝色的敏感度最低,因此需要对rgb设置不同的权重,来达到灰度显示的效果,比较常用的rgb...

由于人眼对rgb敏刚程度不同,对绿色的敏感度最高,对红色的敏感度次之,对蓝色的敏感度最低,因此需要对rgb设置不同的权重,来达到灰度显示的效果,比较常用的rgb权重值为 r:0.298912, g:0.586611,b: 0.114478
graycolor.rgb = float3(color.r0.298912 , color.g0.586611 ,color.b*0.114478)

1 ui对象不可用的时候显示置灰效果

通过shader进行控制置灰,shader中添加变量 _showgray,在代码中可以通过动态给改变量赋值的方式,控制是否进行置灰显示
shader 代码是通过 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 场景中所有对象置灰,比如战斗失败时候显示的置灰效果

场景置灰,一般采用的是对相机渲染进行设置,在相机上面添加脚本,在onrenderimage回调方法里面,对渲染对象进行处理
脚本

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);
    }
}


启用置灰脚本

unity置灰处理的实现

禁用置灰脚本

unity置灰处理的实现

这里的gray材质球用的的shader是一个简单的置灰效果shader,代码如下

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
        }
    }
}

到此这篇关于unity置灰处理的实现的文章就介绍到这了,更多相关unity置灰处理内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: unity 置灰