当前位置:网站首页>[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 .
边栏推荐
- 【pytorch】nn.CrossEntropyLoss() 与 nn.NLLLoss()
- 韦东山板子编译内核问题解决
- Niuke monthly race 22- collect pieces of paper
- ESP8266 FreeRTOS开发环境搭建
- [interview brush 101] linked list
- [pytorch] 2.4 convolution function nn conv2d
- 奇怪,为什么ArrayList初始化容量大小为10?
- How to realize the usage of connecting multiple databases in idel
- Leetcode daily question brushing record --540 A single element in an ordered array
- Latex插入的eps图片模糊解决方法
猜你喜欢

SQL学习笔记(01)——数据库基本知识

2.2 【pytorch】torchvision.transforms

2.4 activation function

Microcomputer principle - bus and its formation

Principles of Microcomputer - Introduction

【检测技术课案】简易数显电子秤的设计与制作

Error org apache. catalina. core. StandardContext. FilterStart start filter exception

Principle and application of single chip microcomputer timer, serial communication and interrupt system

HMS Core音频编辑服务3D音频技术,助力打造沉浸式听觉盛宴

队列的实现和应用
随机推荐
2.2 【pytorch】torchvision. transforms
计网01-物理层
What is P in cap theory
Youqitong PE toolbox [vip] v3.7.2022.0106 official January 22 Edition
【pytorch】nn. AdaptiveMaxPool2d
【pytorch】transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
122. Thread class thread method summary; Why is the thread start method start () not run ()?
SQL学习笔记(02)——数据库表操作
Error org apache. catalina. core. StandardContext. FilterStart start filter exception
ESP8266 FreeRTOS开发环境搭建
Why is the Ltd independent station a Web3.0 website!
Principles of Microcomputer - Introduction
delete和delete[]引发的问题
Niuke monthly race 22- collect pieces of paper
nacos简易实现负载均衡
【检测技术课案】简易数显电子秤的设计与制作
LeetCode 344. Reverse string
Log4j 日志框架
Mikrotik Routeros Internet access settings
SQL学习笔记(01)——数据库基本知识