WuhuIslandTesting/Library/PackageCache/com.unity.render-pipelines.universal@8148.0.7-4/Runtime/Passes/DrawObjectsPass.cs
2025-01-07 02:06:59 +01:00

198 lines
9.5 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine.Profiling;
namespace UnityEngine.Rendering.Universal.Internal
{
/// <summary>
/// Draw objects into the given color and depth target
///
/// You can use this pass to render objects that have a material and/or shader
/// with the pass names UniversalForward or SRPDefaultUnlit.
/// </summary>
public class DrawObjectsPass : ScriptableRenderPass
{
#if DEBUG_NO_SHADERS
static Material s_defaultMat;
static Material defaultMat
{
get
{
if (s_defaultMat == null)
{
Shader s = Shader.Find("Hidden/DUMMY_SHADER");
s_defaultMat = new Material(s);
}
return s_defaultMat;
}
}
#endif
FilteringSettings m_FilteringSettings;
RenderStateBlock m_RenderStateBlock;
List<ShaderTagId> m_ShaderTagIdList = new List<ShaderTagId>();
string m_ProfilerTag;
ProfilingSampler m_ProfilingSampler;
bool m_IsOpaque;
public bool useMotionVectorData;
bool m_UseDepthPriming;
// Skybox Drawing Params ---------------------------------------------------
static GlobalKeyword s_DrawProcedural = GlobalKeyword.Create("DRAW_SKY_PROCEDURAL");
public bool drawSkybox;
static readonly int s_WorldSpaceLightPos0 = Shader.PropertyToID("_WorldSpaceLightPosSun");
static readonly int s_LightColor0 = Shader.PropertyToID("_LightColorSun");
//
static readonly int s_DrawObjectPassDataPropID = Shader.PropertyToID("_DrawObjectPassData");
public DrawObjectsPass(string profilerTag, ShaderTagId[] shaderTagIds, bool opaque, RenderPassEvent evt, RenderQueueRange renderQueueRange, LayerMask layerMask, StencilState stencilState, int stencilReference)
{
base.profilingSampler = new ProfilingSampler(nameof(DrawObjectsPass));
m_ProfilerTag = profilerTag;
m_ProfilingSampler = new ProfilingSampler(profilerTag);
foreach (ShaderTagId sid in shaderTagIds)
m_ShaderTagIdList.Add(sid);
renderPassEvent = evt;
m_FilteringSettings = new FilteringSettings(renderQueueRange, layerMask);
m_RenderStateBlock = new RenderStateBlock(RenderStateMask.Nothing);
m_IsOpaque = opaque;
if (stencilState.enabled)
{
m_RenderStateBlock.stencilReference = stencilReference;
m_RenderStateBlock.mask = RenderStateMask.Stencil;
m_RenderStateBlock.stencilState = stencilState;
}
}
public DrawObjectsPass(string profilerTag, bool opaque, RenderPassEvent evt, RenderQueueRange renderQueueRange, LayerMask layerMask, StencilState stencilState, int stencilReference)
: this(profilerTag,
new ShaderTagId[] { new ShaderTagId("SRPDefaultUnlit"), new ShaderTagId("UniversalForward"), new ShaderTagId("UniversalForwardOnly") },
opaque, evt, renderQueueRange, layerMask, stencilState, stencilReference)
{ }
internal DrawObjectsPass(URPProfileId profileId, bool opaque, RenderPassEvent evt, RenderQueueRange renderQueueRange, LayerMask layerMask, StencilState stencilState, int stencilReference)
: this(profileId.GetType().Name, opaque, evt, renderQueueRange, layerMask, stencilState, stencilReference)
{
m_ProfilingSampler = ProfilingSampler.Get(profileId);
}
public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
{
if (renderingData.cameraData.renderer.useDepthPriming && m_IsOpaque && (renderingData.cameraData.renderType == CameraRenderType.Base || renderingData.cameraData.clearDepth))
{
m_RenderStateBlock.depthState = new DepthState(false, CompareFunction.Equal);
m_RenderStateBlock.mask |= RenderStateMask.Depth;
}
else if (m_RenderStateBlock.depthState.compareFunction == CompareFunction.Equal)
{
m_RenderStateBlock.depthState = new DepthState(true, CompareFunction.LessEqual);
m_RenderStateBlock.mask |= RenderStateMask.Depth;
}
}
/// <inheritdoc/>
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
// NOTE: Do NOT mix ProfilingScope with named CommandBuffers i.e. CommandBufferPool.Get("name").
// Currently there's an issue which results in mismatched markers.
CommandBuffer cmd = CommandBufferPool.Get();
using (new ProfilingScope(cmd, m_ProfilingSampler))
{
// Global render pass data containing various settings.
// x,y,z are currently unused
// w is used for knowing whether the object is opaque(1) or alpha blended(0)
Vector4 drawObjectPassData = new Vector4(0.0f, 0.0f, 0.0f, (m_IsOpaque) ? 1.0f : 0.0f);
cmd.SetGlobalVector(s_DrawObjectPassDataPropID, drawObjectPassData);
// scaleBias.x = flipSign
// scaleBias.y = scale
// scaleBias.z = bias
// scaleBias.w = unused
float flipSign = (renderingData.cameraData.IsCameraProjectionMatrixFlipped()) ? -1.0f : 1.0f;
Vector4 scaleBias = (flipSign < 0.0f)
? new Vector4(flipSign, 1.0f, -1.0f, 1.0f)
: new Vector4(flipSign, 0.0f, 1.0f, 1.0f);
cmd.SetGlobalVector(ShaderPropertyId.scaleBiasRt, scaleBias);
context.ExecuteCommandBuffer(cmd);
cmd.Clear();
Camera camera = renderingData.cameraData.camera;
var sortFlags = (m_IsOpaque) ? renderingData.cameraData.defaultOpaqueSortFlags : SortingCriteria.CommonTransparent;
if (renderingData.cameraData.renderer.useDepthPriming && m_IsOpaque && (renderingData.cameraData.renderType == CameraRenderType.Base || renderingData.cameraData.clearDepth))
sortFlags = SortingCriteria.SortingLayer | SortingCriteria.RenderQueue | SortingCriteria.OptimizeStateChanges | SortingCriteria.CanvasOrder;
var filterSettings = m_FilteringSettings;
#if UNITY_EDITOR
// When rendering the preview camera, we want the layer mask to be forced to Everything
if (renderingData.cameraData.isPreviewCamera)
{
filterSettings.layerMask = -1;
}
#endif
DrawingSettings drawSettings = CreateDrawingSettings(m_ShaderTagIdList, ref renderingData, sortFlags);
if (useMotionVectorData)
{
drawSettings.perObjectData = drawSettings.perObjectData | PerObjectData.MotionVectors;
}
if (drawSkybox)
{
Material skybox = RenderSettings.skybox;
if (skybox)
{
Light sun = RenderSettings.sun;
Vector4 sunDir;
Vector4 lightColor;
if (sun && sun.isActiveAndEnabled)
{
sunDir = -sun.transform.forward;
lightColor = (Vector4)sun.color * sun.intensity;
}
else
{
sunDir = new Vector4(0,0,-1,0);
lightColor = Color.black;
}
skybox.SetVector(s_WorldSpaceLightPos0, sunDir);
skybox.SetVector(s_LightColor0, lightColor);
cmd.EnableKeyword(s_DrawProcedural);
cmd.DrawProcedural(Matrix4x4.identity, skybox, 0, MeshTopology.Triangles, 3, 1);
cmd.DisableKeyword(s_DrawProcedural);
context.ExecuteCommandBuffer(cmd);
cmd.Clear();
}
}
#if !DEBUG_NO_SHADERS
var activeDebugHandler = GetActiveDebugHandler(renderingData);
if (activeDebugHandler != null)
{
activeDebugHandler.DrawWithDebugRenderState(context, cmd, ref renderingData, ref drawSettings, ref filterSettings, ref m_RenderStateBlock,
(ScriptableRenderContext ctx, ref RenderingData data, ref DrawingSettings ds, ref FilteringSettings fs, ref RenderStateBlock rsb) =>
{
ctx.DrawRenderers(data.cullResults, ref ds, ref fs, ref rsb);
});
}
else
{
context.DrawRenderers(renderingData.cullResults, ref drawSettings, ref filterSettings, ref m_RenderStateBlock);
// Render objects that did not match any shader pass with error shader
RenderingUtils.RenderObjectsWithError(context, ref renderingData.cullResults, camera, filterSettings, SortingCriteria.None);
}
#else
drawSettings.overrideMaterial = defaultMat;
context.DrawRenderers(renderingData.cullResults, ref drawSettings, ref filterSettings);
#endif
}
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
}
}