当前位置:网站首页>[unity rendering] customized screen post-processing
[unity rendering] customized screen post-processing
2022-07-01 09:34:00 【True ghost 123】
Custom screen post-processing is not difficult , The steps are divided into two parts ,1. Set shaders and... For picture processing Shader;2. stay C# The code uses OnrenderImage Function to process the camera picture .
1. establish Shader And shaders
Use ImageEffect( Image effects ) Type of Shader.
Shader It should be named _MainTex Main texture of , For reception C# Method to pass in the screen image . Otherwise, it is impossible to perform post-processing .
What will be written shader Assign to the shader .
Here is a simple blur shader.
Shader "Hidden/Image_Fuzzy"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Offset("Offset", Range(0, 0.05)) = 0.02
}
SubShader
{
// No culling or depth
//Cull Off ZWrite Off ZTest Always
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;
float _Offset;
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
// just invert the colors
//col.rgb = 1 - col.rgb;
fixed4 c1 = tex2D(_MainTex, i.uv - (_Offset, 0));
fixed4 c2 = tex2D(_MainTex, i.uv + (_Offset, 0));
fixed4 c3 = tex2D(_MainTex, i.uv + (0, _Offset));
fixed4 c4 = tex2D(_MainTex, i.uv - (0, _Offset));
return( col + c1 + c2 + c3 + c4) / 5;;
}
ENDCG
}
}
}
2. call C# in OnRenderImage Method
OnRenderImage Post processing effect . Called after all the rendering operations of the image have been completed .
This function allows you to process the final image using shader based filters , To modify the final image . source Render textures for incoming images .destination Texture the output result .
When the camera is attached with multiple image filters , They process the images in sequence according to the following methods : Pass the target of the first filter as the source to the next filter , And so on .
Material material;
void OnRenderImage(RenderTexture source, RenderTexture destination)
{
// Copy the source Render Texture to the destination,
// applying the material along the way.
Graphics.Blit(source, destination, material);
}
Graphics.Blit (Texture source, RenderTexture dest, Material mat, int pass= -1) function
Use shaders to copy the source texture to the target render texture .
source For the input image , Corresponding shader Medium _MainTex attribute , Fill in OnRenderImage Medium source that will do .
dest For the output image , Fill in OnRenderImage Medium destination that will do .
mat Materials used for post-treatment .
function Unity You can see the post-processing effect .
Be careful :1. with OnRenderImage Method , Need to hang to Camera Only on components can .
2.Shader It should be named _MainTex Main texture of , For reception C# Method to pass in the screen image . Otherwise, it is impossible to perform post-processing .
边栏推荐
- [interview brush 101] linked list
- js valueOf 与 toString 区别
- Construction of esp8266 FreeRTOS development environment
- tensorrt yolov5_ trt. Py comments
- 富文本实现插值
- [pytorch] 2.4 convolution function nn conv2d
- Daily practice of C language - day 80: currency change
- Clickhouse: Test on query speed of A-share minute data [Part 2]
- Youqitong PE toolbox [vip] v3.7.2022.0106 official January 22 Edition
- Summary of reptile knowledge points
猜你喜欢
随机推荐
JS functionarguments object
[untitled]
【pytorch】nn. AdaptiveMaxPool2d
【pytorch】nn.CrossEntropyLoss() 与 nn.NLLLoss()
【电赛训练】红外光通信装置 2013年电赛真题
Error org apache. catalina. core. StandardContext. FilterStart start filter exception
【pytorch】2.4 卷积函数 nn.conv2d
电脑USB、HDMI、DP各种接口及速度
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云和Arduino的化学环境系统检测,支持钉钉机器人告警
Summary of reptile knowledge points
2.2 【pytorch】torchvision.transforms
Log4j log framework
【pytorch】transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
Rich text interpolation
js变量提升(hoisting)
富文本实现插值
LeetCode 344. Reverse string
JS prototype chain
闭包实现迭代器效果
Simple load balancing with Nacos




![Problems caused by delete and delete[]](/img/d9/a1c3e5ce51ef1be366a973aa42d1f0.png)




