BlueRoseNote/07-Other/Unity/Unity通用渲染管线(URP)系列(一)——自定义渲染管线.md
2023-06-29 11:55:02 +08:00

71 lines
3.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Unity通用渲染管线URP系列——自定义渲染管线
## 逻辑分布
- CustomRenderPipelineAsset.cs定义渲染管线Asset最后创建并且返回定义的管线实例。
- CustomRenderPipeline.cs整个渲染管线逻辑。考虑到多视图渲染的关系还需要为摄像机类实现对应的逻辑。
- CameraRenderer.cs摄像机对应的渲染逻辑。
## CustomRenderPipeline
重写`void Render(ScriptableRenderContext context,Camera[] cameras)`,使用自定义的`CameraRenderer`类型的变量renderer,调用`renderer.Render(context,camera)`为每个摄像机(视图)进行渲染。
## CameraRenderer
大部分逻辑都集中在这个类中。
重写`void Render(ScriptableRenderContext context,Camera camera)`
裁剪之前会进行
- `PrepareBuffer()`根据摄像机来设置缓存名称
- `PrepareForSceneWindow()`绘制UI准备
1. [裁剪](#Cull)
2. [设置初始化参数](#Setup)
3. [绘制可见多边形](#DrawVisibleGeometry)
4. [绘制Gizmos]()
5. [提交](#Submit)
全代码
```
```
###
#### Command Buffers
自定义的渲染功能需要设置Command Buffers以存储这个功能所有的渲染命令创建新的Command Buffers需要设置名称案例中使用“Render Camera”。
使用`buffer.BeginSample(bufferName)``buffer.EndSample(bufferName)`给Profiler与帧调试器识别到该渲染功能。
执行`Command Buffers`需要在`context`上调用ExecuteCommandBuffer。这会从缓冲区复制命令但并不会清除它如果要重用它的话就必须在之后明确地执行该操作。因为执行和清除总是一起完成的所以添加同时执行这两种方法的方法很方便。
#### 编辑器相关功能
使用`partial`关键字来建立局部类将Editor相关的代码转移到另一个文件中。
### Cull()
尝试通过Camera获取剪裁变量之后存储裁剪后的结果。
### lighting.Setup(context,cullingResults)
使用`CullingResults`向Shader传递`DirectionalLight`信息,支持多个方向光。
### Setup()
设置初始变量以及做一些初始化操作:
1. `SetupCameraProperties`设置摄像相关属性比如投影矩阵
2. `buffer.ClearRenderTarget`清屏操作。根据清屏标志进行对应的处理。为Depth只会清空深度缓存颜色的话会清空深度与颜色缓存如果项目设置为线性颜色则使用线性颜色进行清屏。
### DrawVisibleGeometry()
根据`SortingSettings``DrawingSettings``FilteringSettings`,之后调用
` context.DrawRenderers(cullingResults,ref drawingSettings,ref filteringSettings)`绘制模型。
`DrawinSettings`的构造函数中,`SortingSettings`起到确定基于正焦还是基于透视的应用排序,还可以用来设置绘制顺序,`criteria = SortingCriteria.CommonOpaque`设置绘制顺序为从近到远。
绘制还需要需要对应着色器的Id案例中使用`static ShaderTagId unlitShaderTagId = new ShaderTagId("SRPDefaultUnlit")`获取。
`FilteringSettings`用于确定哪一些渲染队列是被允许渲染。
以及`context.DrawSkybox(camera)`来绘制天空盒。
#### 绘制顺序
### lighting.Clearup()
渲染完阴影后,清理阴影图集。
### Submit()
提交给渲染队列。