当前位置:网站首页>[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 .
边栏推荐
- SQL学习笔记(02)——数据库表操作
- [ESP nanny level tutorial] crazy completion chapter - Case: temperature and humidity monitoring system based on Alibaba cloud, applet and Arduino
- JS functionarguments object
- SQL learning notes (02) - database table operation
- js作用域链与闭包
- Differences between JS valueof and toString
- [ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + DHT11 +nodejs local service + MySQL database
- 2.4 激活函数
- Meituan machine test in 2022
- 122. Thread class thread method summary; Why is the thread start method start () not run ()?
猜你喜欢

计网01-物理层

dotnet 控制台 使用 Microsoft.Maui.Graphics 配合 Skia 进行绘图入门

樹結構---二叉樹2非遞歸遍曆

SQL学习笔记(01)——数据库基本知识
![[pytorch] 2.4 convolution function nn conv2d](/img/eb/382a00af5f88d5954f10ea76343d6e.png)
[pytorch] 2.4 convolution function nn conv2d

Spark's action operator

2.4 activation function

Tree structure -- binary tree 2 non recursive traversal
![[video game training] real topic of 2013 video game of infrared optical communication device](/img/ef/c2c45c1c6c24aed0a4e93101047372.png)
[video game training] real topic of 2013 video game of infrared optical communication device
![[untitled]](/img/1a/e18918cc09db9b072759409a5f39a1.png)
[untitled]
随机推荐
[interview brush 101] linked list
js重写自己的函数
js this丢失问题分析 及 解决方案
手指点击屏幕就模拟进入F11进入全屏
奇怪,为什么ArrayList初始化容量大小为10?
The jar package embedded with SQLite database is deployed by changing directories on the same machine, and the newly added database records are gone
【pytorch学习】torch.device
Differences between JS valueof and toString
SDN_简单总结
es6-顶层对象与window的脱钩
Log4j log framework
ESP8266 FreeRTOS开发环境搭建
delete和delete[]引发的问题
Why is the Ltd independent station a Web3.0 website!
韦东山板子编译内核问题解决
Spark's action operator
【无标题】
Class loading
122. Thread class thread method summary; Why is the thread start method start () not run ()?
[ESP nanny level tutorial] crazy completion chapter - Case: chemical environment system detection based on Alibaba cloud and Arduino, supporting nail robot alarm