经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 软件/图像 » unity » 查看文章
unity置灰处理的实现
来源:jb51  时间:2021/7/19 19:54:05  对本文有异议

由于人眼对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进行简单修改得到的,

  1. Shader "UI/UIGray"
  2. {
  3. Properties
  4. {
  5. _MainTex ("Texture", 2D) = "white" {}
  6. [Toggle]_ShowGray("show gray", Range(0,1)) = 0
  7. }
  8. SubShader
  9. {
  10. // No culling or depth
  11. Cull Off ZWrite Off ZTest Always
  12. //-----add code-------
  13. Blend SrcAlpha OneMinusSrcAlpha
  14. //----finish----
  15. Pass
  16. {
  17. CGPROGRAM
  18. #pragma vertex vert
  19. #pragma fragment frag
  20.  
  21. #include "UnityCG.cginc"
  22.  
  23. struct appdata
  24. {
  25. float4 vertex : POSITION;
  26. float2 uv : TEXCOORD0;
  27. };
  28.  
  29. struct v2f
  30. {
  31. float2 uv : TEXCOORD0;
  32. float4 vertex : SV_POSITION;
  33. };
  34.  
  35. v2f vert (appdata v)
  36. {
  37. v2f o;
  38. o.vertex = UnityObjectToClipPos(v.vertex);
  39. o.uv = v.uv;
  40. return o;
  41. }
  42.  
  43. sampler2D _MainTex;
  44. fixed _ShowGray;
  45.  
  46. fixed4 frag (v2f i) : SV_Target
  47. {
  48. fixed4 col = tex2D(_MainTex, i.uv);
  49. // just invert the colors
  50. //col.rgb = 1 - col.rgb;
  51. //----add code----
  52. fixed gray = dot(col.rgb, float3(0.298912, 0.586611, 0.114478));
  53. col.rgb = lerp(col.rgb, fixed3(gray, gray, gray), _ShowGray);
  54. //-----finish-----
  55. return col;
  56. }
  57. ENDCG
  58. }
  59. }
  60. }
  61.  

2 场景中所有对象置灰,比如战斗失败时候显示的置灰效果

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

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PostEffectGray : MonoBehaviour
  6. {
  7. public Material grayMaterial;
  8. void OnRenderImage(RenderTexture src, RenderTexture dest)
  9. {
  10. Graphics.Blit(src, dest, grayMaterial);
  11. }
  12. }
  13.  
  14.  

启用置灰脚本

在这里插入图片描述

禁用置灰脚本

在这里插入图片描述

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

  1. Shader "Unlit/Gray"
  2. {
  3. Properties
  4. {
  5. _MainTex ("Texture", 2D) = "white" {}
  6. }
  7. SubShader
  8. {
  9. Tags { "RenderType"="Opaque" }
  10. LOD 100
  11.  
  12. Pass
  13. {
  14. CGPROGRAM
  15. #pragma vertex vert
  16. #pragma fragment frag
  17. // make fog work
  18. #pragma multi_compile_fog
  19.  
  20. #include "UnityCG.cginc"
  21.  
  22. struct appdata
  23. {
  24. float4 vertex : POSITION;
  25. float2 uv : TEXCOORD0;
  26. };
  27.  
  28. struct v2f
  29. {
  30. float2 uv : TEXCOORD0;
  31. UNITY_FOG_COORDS(1)
  32. float4 vertex : SV_POSITION;
  33. };
  34.  
  35. sampler2D _MainTex;
  36. float4 _MainTex_ST;
  37.  
  38. v2f vert (appdata v)
  39. {
  40. v2f o;
  41. o.vertex = UnityObjectToClipPos(v.vertex);
  42. o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  43. UNITY_TRANSFER_FOG(o,o.vertex);
  44. return o;
  45. }
  46.  
  47. fixed4 frag (v2f i) : SV_Target
  48. {
  49. // sample the texture
  50. fixed4 col = tex2D(_MainTex, i.uv);
  51. half3 gray = dot(col.rgb, half3 (0.22, 0.707, 0.071));
  52. // apply fog
  53. UNITY_APPLY_FOG(i.fogCoord, col);
  54. return fixed4(gray.rgb, col.a);
  55. }
  56. ENDCG
  57. }
  58. }
  59. }
  60.  

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

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号