当前位置:网站首页>Unity learning shader notes [82] black and white processing of enhanced single channel color rendering
Unity learning shader notes [82] black and white processing of enhanced single channel color rendering
2022-07-02 18:22:00 【Touch fleeting time】
Think of the original form of torture 2 The picture of being beaten with residual blood inside is only reserved for red , Everything else is grey and black . It's similar to this


Got an original picture , As UI Of ScreenSapce-Camera Imaging as a camera , Be careful ScreenSapce-Overlay Of UI Will not participate in screen post-processing .
Because the red part will not turn black , So the idea is that the red part should be the original color , Instead of lack of saturation , The code is as follows
// The stronger the red, the more invariant the gray , The weaker the area, the more use _Saturation To adjust
float grayDegree = lerp(_Saturation, 1, renderTex.r);
finalColor = lerp(grayColor, finalColor, grayDegree );
Set the saturation to 0,
The effect is as follows , Red does obviously remain , But there are other places that retain color .
Because many other colors have r component , We need to be below a certain level r Discard the source of the component r The influence of component 
So I made a little change to the code , The idea is to treat the red part below a certain threshold as red 0,
Then treat red as 0 Part of , Use the original red value to adjust the saturation .
float redLerpValue = renderTex.r - _LeastRed < 0 ? 0 : renderTex.r;
// The stronger the red, the more invariant the gray , The weaker the area, the more use _Saturation To adjust
float grayDegree = lerp(_Saturation, 1, redLerpValue);
finalColor = lerp(grayColor, finalColor, grayDegree );
The parameters and effects are as follows :


It's only red , Just use the original saturation value directly after it is less than a certain value, so that the transition of the film source with the red value above and below the threshold will be very contrary . When I was stuck in difficulties, I suddenly wondered whether these models with special effects were painted with vertex colors when they left the factory , Then the film source makes color reservation according to the value of vertex color , In this way, the parts that should not retain color are strictly limited , But post-processing shader Vertex colors belonging to a single model cannot be read , At most, it is to reconstruct the world coordinates according to the depth map or do some other operations .
Now is to keep the red , If red is lower than a certain value, it turns gray , And the transition is not inconsistent , Think of an effect carefully analyzed before , Unity Grid Programming Notes [ Four ] Press the sphere to compress It's used in pow, Under the same coefficient , He can make the distance between his relatively small decimal and his relatively large decimal , And the larger the coefficient, the larger the drive , But be pow After treatment, the effect is continuous and will not be disconnected, which can simulate the transition


So I made some changes .
float redLerpValue = pow(renderTex.r, _RedPowerValue) ;
// The stronger the red, the more invariant the gray , The weaker the area, the more use _Saturation To adjust
float grayDegree = lerp(_Saturation, 1, redLerpValue);
finalColor = lerp(grayColor, finalColor, grayDegree );




I feel better , But the white part of the sky doesn't turn black and white , The sky is white , White is generally rgb The three channels are relatively high , Then the values of the three channels are equal .
To achieve the effect in the game , It feels like more than just reading r passageway , Yes. rgb Channel comprehensive judgment , Because when it is untreated, the red part will be kept and will not be grayed out , This part must be r The passage is much larger than gb The tunnel , Only considered r Channels are more inclined to single channel color retention , image unity Of inspector When viewing pictures on the panel, you only see the single channel . We use it r The difference between the mean value of the channel and that of other channels is taken as pow The basic value of , This itself is not the red part , The base value will be small and considered to be used only saturation control , Pay attention because the purpose is to highlight the red area , The delimited area of the red area is R The channel is much larger than gb The average of the channels , It's much bigger than , Instead of simply judging the difference between the two , Because of existence (0,1,1) This color , If such a color is also defined as red, it is incorrect .
So I made some changes .
float redChannelValue = renderTex.r;
float blueChannelValue = renderTex.b;
float greenChannelValue = renderTex.g;
float blueGreenAverage = (blueChannelValue + greenChannelValue) / 2;
float redDeltaBlueGreen = abs(redChannelValue - blueGreenAverage) ;
redDeltaBlueGreen = redChannelValue > blueGreenAverage ? redDeltaBlueGreen : 0;
float redGrayLerpValue = pow(redDeltaBlueGreen, _RedPowerValue) ;
float fullSaturateValue = 1;
// The stronger the red, the more invariant the gray , The weaker the area, the more use _Saturation To adjust
float grayDegree = lerp(_Saturation, fullSaturateValue, redGrayLerpValue);
finalColor = lerp(grayColor, finalColor, grayDegree );


The effect is OK , Because it's the difference , Difference as pow It's better than itself r The channel value should be small , As pow When the cardinality of , The degree of attenuation is greater than r The passage should be larger , You can imagine by comparing any left and right points of the function graph , therefore pow The coefficient range of can be reduced . Because before this post-processing, I did some HDR Color +bloom Or special effects , Then I did the black-and-white post-processing , According to the principle of both , There's no interference with each other , The glowing part still glows .
In the game, in order to highlight the desire for blood and survival when being beaten with residual blood, and the contempt for everything else and unclear vision , Red is highlighted , Darken the black and white colors , These changes actually make the dark darker , The light becomes brighter , That is, contrast enhancement , In addition, put Bloom The coefficient of adjustment is larger , Let the red glow more .

Ordinary contrast enhancement is the overall enhancement rather than the obvious enhancement of red contrast , The contrast of the red part here should be specially treated . So I made some changes :
//contrast Contrast : First calculate the value with the lowest contrast
fixed3 avgColor = fixed3(0.5, 0.5, 0.5);
float redContrastLerpValue = pow(redDeltaBlueGreen, 3) ;
float contrastDegree = lerp(_Contrast, _Contrast * 10, redContrastLerpValue);
// according to Contrast The difference between the image with the lowest contrast and the original image
finalColor = lerp(avgColor, finalColor, contrastDegree);

There may be others when there is residual blood LUT,ToneMapping What's the post-processing of color mapping .
边栏推荐
- 【Golang | gRPC】使用openssl生成证书
- Wechat applet video sharing platform system graduation design completion (1) development outline
- Architecture design - ID generator "suggestions collection"
- Develop a controller that prohibits deleting namespaces
- 求求你们,别再刷 Star 了!这跟“爱国”没关系!
- vimium映射鍵
- QQmlApplicationEngine
- 微信核酸检测预约小程序系统毕业设计毕设(5)任务书
- MySQL --- 数据库的基本概念
- Aloam code reading and summary
猜你喜欢

Intelligent hydropower meter energy consumption monitoring cloud platform

SteamOS 3.3 Beta 发布,Steam Deck 中文键盘终于来了

微信小程序视频分享平台系统毕业设计毕设(1)开发概要

微信核酸检测预约小程序系统毕业设计毕设(4)开题报告

Aloam code reading and summary

RDK仿真实验

再放寬!這些應届生,可直接落戶上海

wait_ for_ Gap -- restore archive from primary archive to secondary Archive

再放宽!这些应届生,可直接落户上海

Wechat nucleic acid detection appointment applet system graduation design (2) applet function
随机推荐
Unity学习shader笔记[八十二]增强单通道颜色渲染的黑白处理
How to download wechat payment certificate (API certificate)
初夏,开源魔改一个带击杀音效的电蚊拍!
人人工势场法
Freemaker+poi realizes dynamic generation and parsing of Excel files
Wechat applet video sharing platform system graduation design completion (8) graduation design thesis template
Embedded development board ~ description
Microsoft LDAP 配置页中输入有效的用户名及密码,microsoft ldap 配置页中输入有效的用户名
Hbuilderx runs to the mobile phone or simulator and prompts that the device is not found
王者荣耀商城异地多活架构设计
如何开启IDEA的Run Dashboard功能
win10 卸载cuda
ESP32-C3入门教程 问题篇⑩——error: implicit declaration of function ‘esp_blufi_close‘;
又一所双非改考408,会爆冷么?南昌航空大学软件学院
Leetcode 面试题 17.01. 不用加号的加法
D构造函数问题
微信核酸检测预约小程序系统毕业设计毕设(2)小程序功能
再放宽!这些应届生,可直接落户上海
D constructor problem
ESP32-C3入门教程 问题篇⑪——esp-tls: create_ssl_handle failed, tls_io_instance->options.trusted_certs null