什么是屏幕后处理

屏幕后处理:指渲染整个场景得到屏幕图像后,再对这个图像进行一系列操作,实现各种屏幕特效。

unity提供抓取屏幕的接口OnRenderImage

Unity - Scripting API: MonoBehaviour.OnRenderImage(RenderTexture,RenderTexture) (unity3d.com)

利用Graphics.Blit函数完成对渲染纹理的处理

Unity - Scripting API: Graphics.Blit (unity3d.com)

屏幕后处理的基类

作用:检查条件是否满足屏幕后处理,比如当前平台是否支持等

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//PostEffectsBase.cs
using UnityEngine;
using System.Collections;
//编辑器状态下可以执行该脚本
[ExecuteInEditMode]

[RequireComponent (typeof(Camera))]
public class PostEffectsBase : MonoBehaviour {

// Called when start
protected void CheckResources() {
bool isSupported = CheckSupport();

if (isSupported == false) {
NotSupported();
}
}

// Called in CheckResources to check support on this platform
protected bool CheckSupport() {
if (SystemInfo.supportsImageEffects == false || SystemInfo.supportsRenderTextures == false) {
Debug.LogWarning("This platform does not support image effects or render textures.");
return false;
}

return true;
}

// Called when the platform doesn't support this effect
protected void NotSupported() {
enabled = false;
}

protected void Start() {
CheckResources();
}

// Called when need to create the material used by this effect
protected Material CheckShaderAndCreateMaterial(Shader shader, Material material) {
if (shader == null) {
return null;
}

if (shader.isSupported && material && material.shader == shader)
return material;

if (!shader.isSupported) {
return null;
}
else {
material = new Material(shader);
material.hideFlags = HideFlags.DontSave;
if (material)
return material;
else
return null;
}
}
}