diff options
Diffstat (limited to 'Assets/Scripts/Rendering/CustomRenderingPipeline.cs')
-rw-r--r-- | Assets/Scripts/Rendering/CustomRenderingPipeline.cs | 66 |
1 files changed, 57 insertions, 9 deletions
diff --git a/Assets/Scripts/Rendering/CustomRenderingPipeline.cs b/Assets/Scripts/Rendering/CustomRenderingPipeline.cs index af70e760..9387cf52 100644 --- a/Assets/Scripts/Rendering/CustomRenderingPipeline.cs +++ b/Assets/Scripts/Rendering/CustomRenderingPipeline.cs @@ -4,43 +4,91 @@ using System.Collections.Generic; using UnityEngine; using UnityEngine.Rendering; +// 相机的自定义管线 [RequireComponent(typeof(MainCamera))] public class CustomRenderingPipeline : MonoBehaviour {
+ [Tooltip("开启自定义延迟渲染")]
+ public bool enableDeferredRender;
public delegate void RenderEventHandler();
+ // UnitPreprocessing, UnitLensEffect, etc
public event RenderEventHandler onPreCull;
public event RenderEventHandler onPreRender;
public event RenderEventHandler onPostRender;
- #region 公共贴图
+ Camera m_Camera;
- #endregion
+ // command buffers
+ CommandBuffer m_CommandBufferAfterDepth;
- Camera m_Camera;
+ RenderTargetIdentifier[] m_GBuffer = new RenderTargetIdentifier[4];
+ RenderTargetIdentifier m_DepthBuffer;
+ RenderTexture m_GBufferTextureDiffuse;
+ RenderTexture m_GBufferTextureNormal;
+ RenderTexture m_GBufferTexturePosition;
+ RenderTexture m_GBufferTextureTexCoord;
- private void OnEable()
+ void OnEnable()
{
m_Camera = GetComponent<Camera>();
+
+ // command buffers
+ m_CommandBufferAfterDepth = new CommandBuffer();
+ m_Camera.AddCommandBuffer(CameraEvent.AfterDepthTexture, m_CommandBufferAfterDepth);
+
+ // render targets
+ int width = m_Camera.pixelWidth, height = m_Camera.pixelHeight;
+ m_GBufferTextureDiffuse = RenderTexture.GetTemporary(width, height, 24, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
+ m_GBufferTextureNormal = RenderTexture.GetTemporary(width, height, 24, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
+ m_GBufferTexturePosition = RenderTexture.GetTemporary(width, height, 24, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear);
+ m_GBufferTextureTexCoord = RenderTexture.GetTemporary(width, height, 24, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
+ m_GBuffer[0] = m_GBufferTextureDiffuse.colorBuffer;
+ m_GBuffer[1] = m_GBufferTextureNormal.colorBuffer;
+ m_GBuffer[2] = m_GBufferTexturePosition.colorBuffer;
+ m_GBuffer[3] = m_GBufferTextureTexCoord.colorBuffer;
+ m_DepthBuffer = m_GBufferTextureDiffuse.depthBuffer;
+
} - private void Start()
+ void OnDisable()
{
- }
+ m_Camera.RemoveAllCommandBuffers();
+ } - private void OnPreCull()
+ void OnPreCull()
{
+ m_CommandBufferAfterDepth.Clear();
+
+ RenderGBuffer();
+
onPreCull?.Invoke();
}
- private void OnPreRender()
+ void OnPreRender()
{
onPreRender?.Invoke();
}
- private void OnPostRender()
+ void OnPostRender()
{
onPostRender?.Invoke();
}
+ void RenderGBuffer()
+ {
+ CommandBuffer cb = m_CommandBufferAfterDepth;
+ cb.SetRenderTarget(m_GBuffer, m_DepthBuffer);
+ cb.ClearRenderTarget(true, true, new Color(0, 0, 0, 0));
+ List<CustomRenderer> renderers = CustomRendererRegistry.Instance.renderers;
+ Material mat = new Material(Shader.Find(StaticDefine.shaders[EShader.GBuffer].name));
+ for(int i = 0; i < renderers.Count; ++i)
+ {
+ CustomRenderer renderer = renderers[i];
+ if (renderer == null)
+ continue;
+ cb.DrawRenderer(renderer.renderer, mat);
+ }
+ } +
}
\ No newline at end of file |