最近一直在思考如何能更好的做优化渲染,本篇文章只是另一种实现的思路,其实我也没完全想好怎么应用到实际游戏中来统计,希望各位看官多多提宝贵意见。
1.本例Unity的版本是Unity2019.3.1.4
2.采用URP渲染管线,老的渲染管线没有试过,大家可以试试看。
3.FrameDebugger会将每一这数据存入RT中,名字对应如下。
实际代码中就可以这样取到它的Texture了
1
2
3
4
5
Texture texture = Shader.GetGlobalTexture("_CameraColorTexture");
if (!texture)
{
texture = Shader.GetGlobalTexture("_CameraOpaqueTexture");
}
并非所有都能取,比如shadowmap的RT,这名字是没有意义的,如果真想取,那就用C#反射吧。 但其实有上面两个基本已经够用了。
4.为了让代码更快的比较两帧图片的颜色,我采用了Burst编译比暴力的for循环快的可不是一点点。
5.为了让工具更加方便,不得不在代码中做了很多反射FrameDebugger的代码。
工具使用之前,大家可以先用FrameDebugger看一下自己需要截那些帧的数据。接着运行Unity,填入开始和结束帧的索引后,点击开始截取按钮即可。
截取完毕后,左边会保存每帧截取的图片,最后还会生成一张第1张和最后一张的中像素变化的图片(红色的区域就表示变化)还会输出最终像素数,重复像素数,总渲染顶点数。
在回到优化上来
1.重复像素数越多,其实就是半透明overdraw比较多。
2.最终像素数表示,光栅化后mesh最终呈现的颜色数。
3.总渲染顶点数,这个数值就很重要的。比如模型参与渲染了好几万个顶点,然而结果最终只贡献给屏幕20个像素,那这显然就不太合理了。
一些问题
1.如果摄像机会移动,那么就会造成有时候离模型近,有时候离的远,这样统计就不准确的。
2.如果使用RenderDoc能看到的信息会更多,我也比较推荐用Renderdoc,这篇文章只是开放一下思路。
上代码
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
using System;
using System.Collections;
using System.IO;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using Unity.EditorCoroutines.Editor;
using Unity.Collections;
using Unity.Burst;
using Unity.Jobs;
using Unity.Mathematics;
public class FrameDebugExamplle : EditorWindow
{
static Type s_frameDebugType = Type.GetType("UnityEditorInternal.FrameDebuggerUtility,UnityEditor");
static Type s_frameDebugWindowsType = Type.GetType("UnityEditor.FrameDebuggerWindow,UnityEditor");
static bool s_HasLatSample;
static NativeArray s_LastSample2DColor;
static NativeArray s_FirstSample2DColor;
static int s_FinalPixel = 0;
static int s_ProcessPixel = 0;
static int s_VertexCount = 0;
static int s_StartDC;
static int s_EndDC;
static string s_Result;
static string DirectoryPath = "Assets/采样";
static EditorWindow s_FrameDebugWindows;
[MenuItem("Example/开始")]
public static void ShowWindow()
{
OpenAndEnableFrameDebugger();
}
void OnGUI()
{
int count = (int)s_frameDebugType.GetProperty("count", BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static).GetValue(null);
if (count == 0)
{
//强制打开framedebugger窗口
OpenAndEnableFrameDebugger();
}
GUILayout.Label(string.Format($"DC区间 0 - {count}"));
s_StartDC = Mathf.Clamp(EditorGUILayout.IntField("开始", s_StartDC),0,count - 1);
s_EndDC = Mathf.Max(Mathf.Clamp(EditorGUILayout.IntField("结束", s_EndDC),0,count), s_StartDC);
if (GUILayout.Button($"开始截取: {s_StartDC}DC-{s_EndDC}DC", GUILayout.Width(200), GUILayout.Height(50)))
{
OpenAndEnableFrameDebugger();
EditorCoroutineUtility.StartCoroutineOwnerless(StartGetData());
}
GUILayout.Label(s_Result);
}
//等N帧
IEnumerator WaitFive(int count)
{
for (int i = 0; i < count; i++)
{
yield return null;
}
}
//开始获取数据
IEnumerator StartGetData()
{
s_VertexCount = 0;
s_ProcessPixel = 0;
s_FinalPixel = 0;
s_Result = string.Empty;
s_HasLatSample = false;
FileUtil.DeleteFileOrDirectory(DirectoryPath);
Directory.CreateDirectory(DirectoryPath);
for (int i = s_StartDC; i
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?