原文地址:https://blog.csdn.net/SnoopyNa2Co3/article/details/114090245
深度图基础
深度图里存放了[0,1]范围的非线性分布的深度值,这些深度值来自NDC坐标。
在延迟渲染中,深度值默认已经渲染到G-buffer;
https://www.jianshu.com/p/80a932d1f11e这里已经有详细说明
没优化前深度图获取
在C#中设置Camera.main.depthTextureMode = DepthTextureMode.Depth;
在Shader中声明_CameraDepthTexture
原理:Unity在背后利用Shader Replacement将RenderType为Opaque、渲染队列小于等于2500并且有ShadowCaster Pass的物体的深度值渲染到深度图中。所以drawcall翻倍
深度图获取优化
使用接口m_Camera.SetTargetBuffers设置depthbuffer
然后depthbuffer输出导一张rendertexture,这张图就是我们要的深度图,shader里面声明全局图片,我定义为_DepthTex
然后shader直接声明_DepthTex就可以使用
通过Blit处理rendertexture避免多次渲染
下面是代码
using UnityEngine;
using UnityEngine.Rendering;
/// <summary>
/// Pieken 2020
/// </summary>
public class RenderMgr : MonoBehaviour
{
/// <summary>
/// 当前摄像机
/// </summary>
private Camera m_Camera;
/// <summary>
/// 抗锯齿材质球(后处理用)
/// </summary>
public Material ssaa;
/// <summary>
/// 在天空盒渲染之后保存的图片id(就是特效渲染之前)
/// </summary>
private static int m_AfterSkyboxTexId = Shader.PropertyToID("_AfterSkyboxTex");
/// <summary>
/// 深度图id
/// </summary>
private static int m_DepthTexId = Shader.PropertyToID("_DepthTex");
/// <summary>
/// 在天空盒渲染之后保存的图片(就是特效渲染之前)
/// </summary>
public RenderTexture m_AfterSkyboxTex;
/// <summary>
/// 用于存储深度
/// </summary>
public RenderTexture m_depthBufferTex;
/// <summary>
/// 深度图
/// </summary>
public RenderTexture m_DepthTex;
/// <summary>
/// 当前摄像机渲染最终的图片
/// </summary>
private RenderTexture m_CameraRenderTex;
/// <summary>
/// 在渲染天空盒之后的commandbuff指令
/// </summary>
private CommandBuffer m_AfterSkyboxCommandBuffer;
/// <summary>
/// 处理深度图的commandbuff指令
/// </summary>
private CommandBuffer m_DepthBuffer;
private void Start()
{
m_Camera = GetComponent<Camera>();
Init();
}
private void Init()
{
//屏幕渲染图
m_CameraRenderTex = RenderTexture.GetTemporary(Screen.width, Screen.height, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Default);
//存储深度
m_depthBufferTex = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.Depth);
m_depthBufferTex.name = "DepthBuffer";
//深度图
m_DepthTex = new RenderTexture(Screen.width, Screen.height, 0, RenderTextureFormat.RHalf);
m_DepthTex.name = "DepthTex";
//添加处理深度图commandbuffer
m_DepthBuffer = new CommandBuffer();
m_DepthBuffer.name = "CommandBuffer_DepthBuffer";
//把depthbuffer写入m_DepthTex的colorbuffer
//把depthbuffer合成一张rt和自带的是重新渲染一张rt效果一样
//我这里定义rt全局id为_DepthTex,shader直接获取这个就可以使用自定义深度图
m_DepthBuffer.Blit(m_depthBufferTex.depthBuffer, m_DepthTex.colorBuffer);
m_Camera.AddCommandBuffer(CameraEvent.AfterForwardOpaque, m_DepthBuffer);
//设置shader全局深度图
Shader.SetGlobalTexture(m_DepthTexId, m_DepthTex);
//半透渲染前的commandbuffer
m_AfterSkyboxTex = new RenderTexture(Screen.width, Screen.height, 0, RenderTextureFormat.Default);
m_AfterSkyboxTex.name = "AfterSkyboxTex";
m_AfterSkyboxCommandBuffer = new CommandBuffer();
m_AfterSkyboxCommandBuffer.name = "AfterSkyBox_CommandBuffer";
//buffer之类把当前渲染出来的图片保存到m_AfterSkyboxTex
m_AfterSkyboxCommandBuffer.Blit(BuiltinRenderTextureType.CurrentActive, m_AfterSkyboxTex);
//设置摄像机触发commandbuffer时机
m_Camera.AddCommandBuffer(CameraEvent.AfterSkybox, m_AfterSkyboxCommandBuffer);
//设置shader全局图片,方便给扭曲效果用
Shader.SetGlobalTexture(m_AfterSkyboxTexId, m_AfterSkyboxTex);
}
private void OnPreRender()
{
m_Camera.SetTargetBuffers(m_CameraRenderTex.colorBuffer, m_depthBufferTex.depthBuffer);
}
private void OnPostRender()
{
//开始处理后处理,ssaa是抗锯齿后处理
Graphics.Blit(m_CameraRenderTex, null as RenderTexture, ssaa);
}
}
注意:m_Camera.SetTargetBuffers用的rendertexture必须抗锯齿一样,由于depthbuff不能用抗锯齿,所以colorbuffer也不能用抗锯齿
,只能通过后处理来处理抗锯齿(上面代码有用后处理抗锯齿)
使用版本unity 2018.4.23 ,这个优化用于旧项目优化,新项目建议使用更新版本使用urp
链接:https://pan.baidu.com/s/1jXROoypDy4BooeXDIunqFQ
提取码:9c0b