using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.AI;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
using static DrawRendersPass;
public class DrawRendersPass : ScriptableRendererFeature
{
static string rt_name = "_JZDrawRenders";
static int rt_id = Shader.PropertyToID(rt_name);
static int rt_Depth = Shader.PropertyToID("_JZCustomDepth");
[System.Serializable]
public class BaseSettings
{
public Material post_Material;
public LayerMask layerMask;
public List
{
"CustomMask"
};
public List
{
"Opaque"
};
public RenderPassEvent drawEvent;
}
[Header("基础设置")]
public BaseSettings baseSetting = new BaseSettings();
class CustomRenderPass : ScriptableRenderPass
{
private List
private FilteringSettings filtering;
public BaseSettings baseSettings;
public CustomRenderPass(BaseSettings baseSettings)
{
this.baseSettings = baseSettings;
RenderQueueRange renderQueue = new RenderQueueRange();
renderQueue.lowerBound = Mathf.Min(1000, 5000);
renderQueue.upperBound = Mathf.Max(1000, 5000);
for (int j = 0; j < baseSettings.LightTag.Count; j++)
{
shaderTagIds.Add(new ShaderTagId(baseSettings.LightTag[j]));
}
filtering = new FilteringSettings(renderQueue, baseSettings.layerMask);
}
public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
{
/* RenderTextureDescriptor descriptor = renderingData.cameraData.cameraTargetDescriptor;
cmd.GetTemporaryRT(rt_id, descriptor);
ConfigureTarget(rt_id);
ConfigureClear(ClearFlag.All, Color.black);*/
}
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
CommandBuffer cmd = CommandBufferPool.Get("JzRenderFeature");
DrawingSettings drawingSettings=CreateDrawingSettings(shaderTagIds,ref renderingData,SortingCriteria.CommonTransparent);
drawingSettings.overrideMaterial = baseSettings.post_Material;
drawingSettings.overrideMaterialPassIndex = 0;
context.DrawRenderers(renderingData.cullResults, ref drawingSettings, ref filtering);
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
public override void OnCameraCleanup(CommandBuffer cmd)
{
}
}
class JZCustmGrabScreen : ScriptableRenderPass
{
RenderTargetIdentifier source;
RenderTargetIdentifier RenderTarget;
public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
{
source = renderingData.cameraData.renderer.cameraColorTarget;
RenderTextureDescriptor descriptor = renderingData.cameraData.cameraTargetDescriptor;
cmd.GetTemporaryRT(rt_id, descriptor);
RenderTarget = new RenderTargetIdentifier(rt_id);
ConfigureTarget(rt_id);
ConfigureClear(ClearFlag.All, Color.black);
}
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
CommandBuffer cmd = CommandBufferPool.Get("GrabRenderFeature");
cmd.Blit(source, RenderTarget);
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
public override void OnCameraCleanup(CommandBuffer cmd)
{
cmd.ReleaseTemporaryRT(rt_id);
}
}
class DrawTransparentRenderDepth : ScriptableRenderPass
{
private BaseSettings settings;
private List
public DrawTransparentRenderDepth(BaseSettings baseSettings)
{
this.settings = baseSettings;
for (int j = 0; j < baseSettings.LightDepth.Count; j++)
{
ListShader.Add(new ShaderTagId(baseSettings.LightDepth[j]));
}
}
public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
{
}
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
CommandBuffer cmd = CommandBufferPool.Get("DrawDepth");
RenderTextureDescriptor descriptor = renderingData.cameraData.cameraTargetDescriptor;
descriptor.colorFormat = RenderTextureFormat.Depth;
descriptor.depthBufferBits = 32;//深度图采样精度已经做高了
descriptor.msaaSamples = 1;//抗锯齿级别
cmd.GetTemporaryRT(rt_Depth, descriptor, FilterMode.Point);
cmd.SetRenderTarget(rt_Depth, renderingData.cameraData.renderer.cameraDepthTarget);
context.ExecuteCommandBuffer(cmd);
//cmd.SetRenderTarget(renderingData.cameraData.renderer.cameraDepthTarget);
//context.ExecuteCommandBuffer(cmd);
DrawingSettings draw = CreateDrawingSettings(ListShader, ref renderingData, SortingCriteria.CommonOpaque);
FilteringSettings filteringSettings = new FilteringSettings(RenderQueueRange.transparent, renderingData.cameraData.camera.cullingMask);
context.DrawRenderers(renderingData.cullResults, ref draw, ref filteringSettings);
cmd.Clear();
cmd.ReleaseTemporaryRT(rt_Depth);
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
}
CustomRenderPass m_ScriptablePass;
JZCustmGrabScreen jZCustmGrabScreen;
DrawTransparentRenderDepth drawTransparentRenderDepth;
///
public override void Create()
{
m_ScriptablePass = new CustomRenderPass(baseSetting);
jZCustmGrabScreen = new JZCustmGrabScreen();
drawTransparentRenderDepth = new DrawTransparentRenderDepth(baseSetting);
jZCustmGrabScreen.renderPassEvent = RenderPassEvent.AfterRenderingTransparents;
m_ScriptablePass.renderPassEvent = RenderPassEvent.BeforeRenderingPostProcessing;
drawTransparentRenderDepth.renderPassEvent= baseSetting.drawEvent;
}
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
{
renderer.EnqueuePass(m_ScriptablePass);
renderer.EnqueuePass(jZCustmGrabScreen);
renderer.EnqueuePass(drawTransparentRenderDepth);
}
}