当前位置:网站首页>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 .
边栏推荐
- Picking up the camera is the best artistic healing
- Wasserstein slim gain with clipping penalty (wsgain-cp) introduction and code implementation -- missing data filling based on generating countermeasure network
- A4988 and 42 stepper motors
- Wechat applet video sharing platform system graduation design completion (4) opening report
- NVIDIA graphics card failed to initialize nvml driver/library version mismatch error solution
- Steamos 3.3 beta release, steam deck Chinese keyboard finally came
- Wechat nucleic acid detection appointment applet system graduation design completion (4) opening report
- 巴比特 | 元宇宙每日必读:一千块就能买一个虚拟主播?这是小企业的直播福音还是在“割韭菜”?...
- matplotlib的安装教程以及简单调用
- 微信小程序视频分享平台系统毕业设计毕设(6)开题答辩PPT
猜你喜欢

微信核酸检测预约小程序系统毕业设计毕设(1)开发概要
![[golang | grpc] use grpc to realize simple remote call](/img/93/ae6eba2ba65d6e23bd00f45d04a2ed.png)
[golang | grpc] use grpc to realize simple remote call

切换变换的时候记得使用三元表达式

Aloam code reading and summary

Ora-19838 -- restore control files to the standby database

微信核酸检测预约小程序系统毕业设计毕设(5)任务书

Another double non reform exam 408, will it be cold? Software College of Nanchang Aviation University

好玩的免费GM游戏整理汇总

Remember to use ternary expressions when switching transformations

Babbitt | metauniverse daily must read: can you buy a virtual anchor for 1000 yuan? Is this the live gospel of small businesses or "cutting leeks"
随机推荐
No such file or directory: ‘/tmp/tmpxxx/tmpxxx. py‘
Aloam code reading and summary
2020互联网行业术语
【Oracle 期末复习】表空间、表、约束、索引、视图的增删改
Babbitt | metauniverse daily must read: can you buy a virtual anchor for 1000 yuan? Is this the live gospel of small businesses or "cutting leeks"
Aptos教程-参与官方激励测试网(AIT2 激励测试网)
毕业总结
Iframe nesting details
RDK仿真实验
实施阴影介绍
MySQL安装与配置
paddlepaddle 28 搭建基于卷积的自动编码机
铁塔安全监测系统 无人值守倾角振动监测系统
A4988 and 42 stepper motors
Chrome officially supports MathML, which is enabled in chromium dev 105 by default
再放宽!这些应届生,可直接落户上海
How can you omit a large number of switch statements
Qt官方示例:Qt Quick Controls - Gallery
Unity学习shader笔记[八十一]简单的颜色调整后处理(亮度,饱和度,对比度)
C # detect whether the picture is rotated and modified to the true rotation