当前位置:网站首页>[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 .
边栏推荐
- JS prototype chain
- UE small knowledge point controller possess pawn process
- JS use toString to distinguish between object and array
- JS prototype inheritance can only inherit instances, not constructors
- Unity tips for reducing the amount of code -- empty protection extension
- dsPIC30F6014a LCD 方块显示
- Closure implementation iterator effect
- nacos簡易實現負載均衡
- Wechat applet WebView prohibits page scrolling without affecting the implementation of overflow scrolling in the business
- node. How to implement the SQL statement after JS connects to the database?
猜你喜欢
Design and manufacture of simple digital display electronic scale
The market is relatively weak recently
【检测技术课案】简易数显电子秤的设计与制作
Simple load balancing with Nacos
奇怪,为什么ArrayList初始化容量大小为10?
2.3 【kaggle数据集 - dog breed 举例】数据预处理、重写Dataset、DataLoader读取数据
delete和delete[]引发的问题
Construction of esp8266 FreeRTOS development environment
Principles of Microcomputer - internal and external structure of microprocessor
nacos简易实现负载均衡
随机推荐
JS rewrite their own functions
JS variable lifting
js变量提升(hoisting)
[ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + DHT11 +nodejs local service + MySQL database
SQL学习笔记(02)——数据库表操作
队列的实现和应用
[pytorch] softmax function
类加载
Structure de l'arbre - - - arbre binaire 2 traversée non récursive
SQL learning notes (02) - database table operation
delete和delete[]引发的问题
SQL learning notes (01) - basic knowledge of database
Meituan machine test in 2022
PHP array functions (merge, split, append, find, delete, etc.)
[ESP nanny level tutorial] crazy completion chapter - Case: chemical environment system detection based on Alibaba cloud and Arduino, supporting nail robot alarm
Record a redis timeout
nacos簡易實現負載均衡
Preparing for the Blue Bridge Cup -- bit operation
JS scope chain and closure
2.3 【pytorch】数据预处理 torchvision.datasets.ImageFolder