testss
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
|
||||
namespace UnityEngine.Rendering.Universal
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>Prevents ScriptableRendererFeatures of same type to be added more than once to a Scriptable Renderer.</para>
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Class)]
|
||||
public class DisallowMultipleRendererFeature : Attribute
|
||||
{
|
||||
}
|
||||
}
|
@@ -0,0 +1,97 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine.Rendering.Universal;
|
||||
using UnityEngine.Rendering;
|
||||
using UnityEngine.Scripting.APIUpdating;
|
||||
|
||||
namespace UnityEngine.Experimental.Rendering.Universal
|
||||
{
|
||||
[MovedFrom("UnityEngine.Experimental.Rendering.LWRP")] public enum RenderQueueType
|
||||
{
|
||||
Opaque,
|
||||
Transparent,
|
||||
}
|
||||
|
||||
[ExcludeFromPreset]
|
||||
[MovedFrom("UnityEngine.Experimental.Rendering.LWRP")] public class RenderObjects : ScriptableRendererFeature
|
||||
{
|
||||
[System.Serializable]
|
||||
public class RenderObjectsSettings
|
||||
{
|
||||
public string passTag = "RenderObjectsFeature";
|
||||
public RenderPassEvent Event = RenderPassEvent.AfterRenderingOpaques;
|
||||
|
||||
public FilterSettings filterSettings = new FilterSettings();
|
||||
|
||||
public Material overrideMaterial = null;
|
||||
public int overrideMaterialPassIndex = 0;
|
||||
|
||||
public bool overrideDepthState = false;
|
||||
public CompareFunction depthCompareFunction = CompareFunction.LessEqual;
|
||||
public bool enableWrite = true;
|
||||
|
||||
public StencilStateData stencilSettings = new StencilStateData();
|
||||
|
||||
public CustomCameraSettings cameraSettings = new CustomCameraSettings();
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class FilterSettings
|
||||
{
|
||||
// TODO: expose opaque, transparent, all ranges as drop down
|
||||
public RenderQueueType RenderQueueType;
|
||||
public LayerMask LayerMask;
|
||||
public string[] PassNames;
|
||||
|
||||
public FilterSettings()
|
||||
{
|
||||
RenderQueueType = RenderQueueType.Opaque;
|
||||
LayerMask = 0;
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class CustomCameraSettings
|
||||
{
|
||||
public bool overrideCamera = false;
|
||||
public bool restoreCamera = true;
|
||||
public Vector4 offset;
|
||||
public float cameraFieldOfView = 60.0f;
|
||||
}
|
||||
|
||||
public RenderObjectsSettings settings = new RenderObjectsSettings();
|
||||
|
||||
RenderObjectsPass renderObjectsPass;
|
||||
|
||||
public override void Create()
|
||||
{
|
||||
FilterSettings filter = settings.filterSettings;
|
||||
|
||||
// Render Objects pass doesn't support events before rendering prepasses.
|
||||
// The camera is not setup before this point and all rendering is monoscopic.
|
||||
// Events before BeforeRenderingPrepasses should be used for input texture passes (shadow map, LUT, etc) that doesn't depend on the camera.
|
||||
// These events are filtering in the UI, but we still should prevent users from changing it from code or
|
||||
// by changing the serialized data.
|
||||
if (settings.Event < RenderPassEvent.BeforeRenderingPrePasses)
|
||||
settings.Event = RenderPassEvent.BeforeRenderingPrePasses;
|
||||
|
||||
renderObjectsPass = new RenderObjectsPass(settings.passTag, settings.Event, filter.PassNames,
|
||||
filter.RenderQueueType, filter.LayerMask, settings.cameraSettings);
|
||||
|
||||
renderObjectsPass.overrideMaterial = settings.overrideMaterial;
|
||||
renderObjectsPass.overrideMaterialPassIndex = settings.overrideMaterialPassIndex;
|
||||
|
||||
if (settings.overrideDepthState)
|
||||
renderObjectsPass.SetDetphState(settings.enableWrite, settings.depthCompareFunction);
|
||||
|
||||
if (settings.stencilSettings.overrideStencilState)
|
||||
renderObjectsPass.SetStencilState(settings.stencilSettings.stencilReference,
|
||||
settings.stencilSettings.stencilCompareFunction, settings.stencilSettings.passOperation,
|
||||
settings.stencilSettings.failOperation, settings.stencilSettings.zFailOperation);
|
||||
}
|
||||
|
||||
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
|
||||
{
|
||||
renderer.EnqueuePass(renderObjectsPass);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,313 @@
|
||||
using System;
|
||||
|
||||
namespace UnityEngine.Rendering.Universal
|
||||
{
|
||||
[Serializable]
|
||||
internal class ScreenSpaceAmbientOcclusionSettings
|
||||
{
|
||||
// Parameters
|
||||
[SerializeField] internal bool Downsample = false;
|
||||
[SerializeField] internal DepthSource Source = DepthSource.DepthNormals;
|
||||
[SerializeField] internal NormalQuality NormalSamples = NormalQuality.Medium;
|
||||
[SerializeField] internal float Intensity = 3.0f;
|
||||
[SerializeField] internal float DirectLightingStrength = 0.25f;
|
||||
[SerializeField] internal float Radius = 0.035f;
|
||||
[SerializeField] internal int SampleCount = 6;
|
||||
|
||||
// Enums
|
||||
internal enum DepthSource
|
||||
{
|
||||
Depth = 0,
|
||||
DepthNormals = 1,
|
||||
//GBuffer = 2
|
||||
}
|
||||
|
||||
internal enum NormalQuality
|
||||
{
|
||||
Low,
|
||||
Medium,
|
||||
High
|
||||
}
|
||||
}
|
||||
|
||||
[DisallowMultipleRendererFeature]
|
||||
internal class ScreenSpaceAmbientOcclusion : ScriptableRendererFeature
|
||||
{
|
||||
// Serialized Fields
|
||||
[SerializeField, HideInInspector] private Shader m_Shader = null;
|
||||
[SerializeField] private ScreenSpaceAmbientOcclusionSettings m_Settings = new ScreenSpaceAmbientOcclusionSettings();
|
||||
|
||||
// Private Fields
|
||||
private Material m_Material;
|
||||
private ScreenSpaceAmbientOcclusionPass m_SSAOPass = null;
|
||||
|
||||
// Constants
|
||||
private const string k_ShaderName = "Hidden/Universal Render Pipeline/ScreenSpaceAmbientOcclusion";
|
||||
private const string k_OrthographicCameraKeyword = "_ORTHOGRAPHIC";
|
||||
private const string k_NormalReconstructionLowKeyword = "_RECONSTRUCT_NORMAL_LOW";
|
||||
private const string k_NormalReconstructionMediumKeyword = "_RECONSTRUCT_NORMAL_MEDIUM";
|
||||
private const string k_NormalReconstructionHighKeyword = "_RECONSTRUCT_NORMAL_HIGH";
|
||||
private const string k_SourceDepthKeyword = "_SOURCE_DEPTH";
|
||||
private const string k_SourceDepthNormalsKeyword = "_SOURCE_DEPTH_NORMALS";
|
||||
private const string k_SourceGBufferKeyword = "_SOURCE_GBUFFER";
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void Create()
|
||||
{
|
||||
// Create the pass...
|
||||
if (m_SSAOPass == null)
|
||||
{
|
||||
m_SSAOPass = new ScreenSpaceAmbientOcclusionPass();
|
||||
}
|
||||
|
||||
GetMaterial();
|
||||
m_SSAOPass.profilerTag = name;
|
||||
m_SSAOPass.renderPassEvent = RenderPassEvent.BeforeRenderingOpaques;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
|
||||
{
|
||||
if (!GetMaterial())
|
||||
{
|
||||
Debug.LogErrorFormat(
|
||||
"{0}.AddRenderPasses(): Missing material. {1} render pass will not be added. Check for missing reference in the renderer resources.",
|
||||
GetType().Name, m_SSAOPass.profilerTag);
|
||||
return;
|
||||
}
|
||||
|
||||
bool shouldAdd = m_SSAOPass.Setup(m_Settings);
|
||||
if (shouldAdd)
|
||||
{
|
||||
renderer.EnqueuePass(m_SSAOPass);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
CoreUtils.Destroy(m_Material);
|
||||
}
|
||||
|
||||
private bool GetMaterial()
|
||||
{
|
||||
if (m_Material != null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (m_Shader == null)
|
||||
{
|
||||
m_Shader = Shader.Find(k_ShaderName);
|
||||
if (m_Shader == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
m_Material = CoreUtils.CreateEngineMaterial(m_Shader);
|
||||
m_SSAOPass.material = m_Material;
|
||||
return m_Material != null;
|
||||
}
|
||||
|
||||
// The SSAO Pass
|
||||
private class ScreenSpaceAmbientOcclusionPass : ScriptableRenderPass
|
||||
{
|
||||
// Public Variables
|
||||
internal string profilerTag;
|
||||
internal Material material;
|
||||
|
||||
// Private Variables
|
||||
private ScreenSpaceAmbientOcclusionSettings m_CurrentSettings;
|
||||
private ProfilingSampler m_ProfilingSampler = ProfilingSampler.Get(URPProfileId.SSAO);
|
||||
private RenderTargetIdentifier m_SSAOTexture1Target = new RenderTargetIdentifier(s_SSAOTexture1ID, 0, CubemapFace.Unknown, -1);
|
||||
private RenderTargetIdentifier m_SSAOTexture2Target = new RenderTargetIdentifier(s_SSAOTexture2ID, 0, CubemapFace.Unknown, -1);
|
||||
private RenderTargetIdentifier m_SSAOTexture3Target = new RenderTargetIdentifier(s_SSAOTexture3ID, 0, CubemapFace.Unknown, -1);
|
||||
private RenderTextureDescriptor m_Descriptor;
|
||||
|
||||
// Constants
|
||||
private const string k_SSAOAmbientOcclusionParamName = "_AmbientOcclusionParam";
|
||||
private const string k_SSAOTextureName = "_ScreenSpaceOcclusionTexture";
|
||||
|
||||
// Statics
|
||||
private static readonly int s_BaseMapID = Shader.PropertyToID("_BaseMap");
|
||||
private static readonly int s_SSAOParamsID = Shader.PropertyToID("_SSAOParams");
|
||||
private static readonly int s_SSAOTexture1ID = Shader.PropertyToID("_SSAO_OcclusionTexture1");
|
||||
private static readonly int s_SSAOTexture2ID = Shader.PropertyToID("_SSAO_OcclusionTexture2");
|
||||
private static readonly int s_SSAOTexture3ID = Shader.PropertyToID("_SSAO_OcclusionTexture3");
|
||||
|
||||
private enum ShaderPasses
|
||||
{
|
||||
AO = 0,
|
||||
BlurHorizontal = 1,
|
||||
BlurVertical = 2,
|
||||
BlurFinal = 3
|
||||
}
|
||||
|
||||
internal ScreenSpaceAmbientOcclusionPass()
|
||||
{
|
||||
m_CurrentSettings = new ScreenSpaceAmbientOcclusionSettings();
|
||||
}
|
||||
|
||||
internal bool Setup(ScreenSpaceAmbientOcclusionSettings featureSettings)
|
||||
{
|
||||
m_CurrentSettings = featureSettings;
|
||||
switch (m_CurrentSettings.Source)
|
||||
{
|
||||
case ScreenSpaceAmbientOcclusionSettings.DepthSource.Depth:
|
||||
ConfigureInput(ScriptableRenderPassInput.Depth);
|
||||
break;
|
||||
case ScreenSpaceAmbientOcclusionSettings.DepthSource.DepthNormals:
|
||||
ConfigureInput(ScriptableRenderPassInput.Normal);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
return material != null
|
||||
&& m_CurrentSettings.Intensity > 0.0f
|
||||
&& m_CurrentSettings.Radius > 0.0f
|
||||
&& m_CurrentSettings.SampleCount > 0;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
|
||||
{
|
||||
RenderTextureDescriptor cameraTargetDescriptor = renderingData.cameraData.cameraTargetDescriptor;
|
||||
int downsampleDivider = m_CurrentSettings.Downsample ? 2 : 1;
|
||||
|
||||
// Update SSAO parameters in the material
|
||||
Vector4 ssaoParams = new Vector4(
|
||||
m_CurrentSettings.Intensity, // Intensity
|
||||
m_CurrentSettings.Radius, // Radius
|
||||
1.0f / downsampleDivider, // Downsampling
|
||||
m_CurrentSettings.SampleCount // Sample count
|
||||
);
|
||||
material.SetVector(s_SSAOParamsID, ssaoParams);
|
||||
|
||||
// Update keywords
|
||||
CoreUtils.SetKeyword(material, k_OrthographicCameraKeyword, renderingData.cameraData.camera.orthographic);
|
||||
|
||||
if (m_CurrentSettings.Source == ScreenSpaceAmbientOcclusionSettings.DepthSource.Depth)
|
||||
{
|
||||
switch (m_CurrentSettings.NormalSamples)
|
||||
{
|
||||
case ScreenSpaceAmbientOcclusionSettings.NormalQuality.Low:
|
||||
CoreUtils.SetKeyword(material, k_NormalReconstructionLowKeyword, true);
|
||||
CoreUtils.SetKeyword(material, k_NormalReconstructionMediumKeyword, false);
|
||||
CoreUtils.SetKeyword(material, k_NormalReconstructionHighKeyword, false);
|
||||
break;
|
||||
case ScreenSpaceAmbientOcclusionSettings.NormalQuality.Medium:
|
||||
CoreUtils.SetKeyword(material, k_NormalReconstructionLowKeyword, false);
|
||||
CoreUtils.SetKeyword(material, k_NormalReconstructionMediumKeyword, true);
|
||||
CoreUtils.SetKeyword(material, k_NormalReconstructionHighKeyword, false);
|
||||
break;
|
||||
case ScreenSpaceAmbientOcclusionSettings.NormalQuality.High:
|
||||
CoreUtils.SetKeyword(material, k_NormalReconstructionLowKeyword, false);
|
||||
CoreUtils.SetKeyword(material, k_NormalReconstructionMediumKeyword, false);
|
||||
CoreUtils.SetKeyword(material, k_NormalReconstructionHighKeyword, true);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
|
||||
switch (m_CurrentSettings.Source)
|
||||
{
|
||||
case ScreenSpaceAmbientOcclusionSettings.DepthSource.DepthNormals:
|
||||
CoreUtils.SetKeyword(material, k_SourceDepthKeyword, false);
|
||||
CoreUtils.SetKeyword(material, k_SourceDepthNormalsKeyword, true);
|
||||
CoreUtils.SetKeyword(material, k_SourceGBufferKeyword, false);
|
||||
break;
|
||||
default:
|
||||
CoreUtils.SetKeyword(material, k_SourceDepthKeyword, true);
|
||||
CoreUtils.SetKeyword(material, k_SourceDepthNormalsKeyword, false);
|
||||
CoreUtils.SetKeyword(material, k_SourceGBufferKeyword, false);
|
||||
break;
|
||||
}
|
||||
|
||||
// Get temporary render textures
|
||||
m_Descriptor = cameraTargetDescriptor;
|
||||
m_Descriptor.msaaSamples = 1;
|
||||
m_Descriptor.depthBufferBits = 0;
|
||||
m_Descriptor.width /= downsampleDivider;
|
||||
m_Descriptor.height /= downsampleDivider;
|
||||
m_Descriptor.colorFormat = RenderTextureFormat.ARGB32;
|
||||
cmd.GetTemporaryRT(s_SSAOTexture1ID, m_Descriptor, FilterMode.Bilinear);
|
||||
|
||||
m_Descriptor.width *= downsampleDivider;
|
||||
m_Descriptor.height *= downsampleDivider;
|
||||
cmd.GetTemporaryRT(s_SSAOTexture2ID, m_Descriptor, FilterMode.Bilinear);
|
||||
cmd.GetTemporaryRT(s_SSAOTexture3ID, m_Descriptor, FilterMode.Bilinear);
|
||||
|
||||
// Configure targets and clear color
|
||||
ConfigureTarget(s_SSAOTexture2ID);
|
||||
ConfigureClear(ClearFlag.None, Color.white);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
|
||||
{
|
||||
if (material == null)
|
||||
{
|
||||
Debug.LogErrorFormat("{0}.Execute(): Missing material. {1} render pass will not execute. Check for missing reference in the renderer resources.", GetType().Name, profilerTag);
|
||||
return;
|
||||
}
|
||||
|
||||
CommandBuffer cmd = CommandBufferPool.Get();
|
||||
using (new ProfilingScope(cmd, m_ProfilingSampler))
|
||||
{
|
||||
CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.ScreenSpaceOcclusion, true);
|
||||
PostProcessUtils.SetSourceSize(cmd, m_Descriptor);
|
||||
|
||||
// Execute the SSAO
|
||||
Render(cmd, m_SSAOTexture1Target, ShaderPasses.AO);
|
||||
|
||||
// Execute the Blur Passes
|
||||
RenderAndSetBaseMap(cmd, m_SSAOTexture1Target, m_SSAOTexture2Target, ShaderPasses.BlurHorizontal);
|
||||
RenderAndSetBaseMap(cmd, m_SSAOTexture2Target, m_SSAOTexture3Target, ShaderPasses.BlurVertical);
|
||||
RenderAndSetBaseMap(cmd, m_SSAOTexture3Target, m_SSAOTexture2Target, ShaderPasses.BlurFinal);
|
||||
|
||||
// Set the global SSAO texture and AO Params
|
||||
cmd.SetGlobalTexture(k_SSAOTextureName, m_SSAOTexture2Target);
|
||||
cmd.SetGlobalVector(k_SSAOAmbientOcclusionParamName, new Vector4(0f, 0f, 0f, m_CurrentSettings.DirectLightingStrength));
|
||||
}
|
||||
|
||||
context.ExecuteCommandBuffer(cmd);
|
||||
CommandBufferPool.Release(cmd);
|
||||
}
|
||||
|
||||
private void Render(CommandBuffer cmd, RenderTargetIdentifier target, ShaderPasses pass)
|
||||
{
|
||||
cmd.SetRenderTarget(
|
||||
target,
|
||||
RenderBufferLoadAction.DontCare,
|
||||
RenderBufferStoreAction.Store,
|
||||
target,
|
||||
RenderBufferLoadAction.DontCare,
|
||||
RenderBufferStoreAction.DontCare
|
||||
);
|
||||
cmd.DrawMesh(RenderingUtils.fullscreenMesh, Matrix4x4.identity, material, 0, (int)pass);
|
||||
}
|
||||
|
||||
private void RenderAndSetBaseMap(CommandBuffer cmd, RenderTargetIdentifier baseMap, RenderTargetIdentifier target, ShaderPasses pass)
|
||||
{
|
||||
cmd.SetGlobalTexture(s_BaseMapID, baseMap);
|
||||
Render(cmd, target, pass);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void OnCameraCleanup(CommandBuffer cmd)
|
||||
{
|
||||
if (cmd == null)
|
||||
{
|
||||
throw new ArgumentNullException("cmd");
|
||||
}
|
||||
|
||||
CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.ScreenSpaceOcclusion, false);
|
||||
cmd.ReleaseTemporaryRT(s_SSAOTexture1ID);
|
||||
cmd.ReleaseTemporaryRT(s_SSAOTexture2ID);
|
||||
cmd.ReleaseTemporaryRT(s_SSAOTexture3ID);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,218 @@
|
||||
using System;
|
||||
using UnityEngine.Experimental.Rendering;
|
||||
|
||||
namespace UnityEngine.Rendering.Universal
|
||||
{
|
||||
[Serializable]
|
||||
internal class ScreenSpaceShadowsSettings
|
||||
{
|
||||
}
|
||||
|
||||
[DisallowMultipleRendererFeature]
|
||||
internal class ScreenSpaceShadows : ScriptableRendererFeature
|
||||
{
|
||||
// Serialized Fields
|
||||
[SerializeField, HideInInspector] private Shader m_Shader = null;
|
||||
[SerializeField] private ScreenSpaceShadowsSettings m_Settings = new ScreenSpaceShadowsSettings();
|
||||
|
||||
// Private Fields
|
||||
private Material m_Material;
|
||||
private ScreenSpaceShadowsPass m_SSShadowsPass = null;
|
||||
private RestoreShadowKeywordsPass m_RestoreShadowKeywordsPass = null;
|
||||
|
||||
// Constants
|
||||
private const string k_ShaderName = "Hidden/Universal Render Pipeline/ScreenSpaceShadows";
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void Create()
|
||||
{
|
||||
if (m_SSShadowsPass == null)
|
||||
m_SSShadowsPass = new ScreenSpaceShadowsPass();
|
||||
if (m_RestoreShadowKeywordsPass == null)
|
||||
m_RestoreShadowKeywordsPass = new RestoreShadowKeywordsPass();
|
||||
|
||||
LoadMaterial();
|
||||
|
||||
m_SSShadowsPass.profilerTag = name;
|
||||
m_SSShadowsPass.renderPassEvent = RenderPassEvent.BeforeRenderingOpaques;
|
||||
m_RestoreShadowKeywordsPass.renderPassEvent = RenderPassEvent.AfterRenderingOpaques;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
|
||||
{
|
||||
if (!LoadMaterial())
|
||||
{
|
||||
Debug.LogErrorFormat(
|
||||
"{0}.AddRenderPasses(): Missing material. {1} render pass will not be added. Check for missing reference in the renderer resources.",
|
||||
GetType().Name, m_SSShadowsPass.profilerTag);
|
||||
return;
|
||||
}
|
||||
|
||||
bool allowMainLightShadows = renderingData.shadowData.supportsMainLightShadows && renderingData.lightData.mainLightIndex != -1;
|
||||
bool shouldEnqueue = m_SSShadowsPass.Setup(m_Settings) && allowMainLightShadows;
|
||||
|
||||
if (shouldEnqueue)
|
||||
{
|
||||
renderer.EnqueuePass(m_SSShadowsPass);
|
||||
renderer.EnqueuePass(m_RestoreShadowKeywordsPass);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
CoreUtils.Destroy(m_Material);
|
||||
}
|
||||
|
||||
private bool LoadMaterial()
|
||||
{
|
||||
if (m_Material != null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (m_Shader == null)
|
||||
{
|
||||
m_Shader = Shader.Find(k_ShaderName);
|
||||
if (m_Shader == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
m_Material = CoreUtils.CreateEngineMaterial(m_Shader);
|
||||
m_SSShadowsPass.material = m_Material;
|
||||
|
||||
return m_Material != null;
|
||||
}
|
||||
|
||||
private class ScreenSpaceShadowsPass : ScriptableRenderPass
|
||||
{
|
||||
// Public Variables
|
||||
internal string profilerTag;
|
||||
internal Material material;
|
||||
|
||||
// Private Variables
|
||||
private ScreenSpaceShadowsSettings m_CurrentSettings;
|
||||
private RenderTextureDescriptor m_RenderTextureDescriptor;
|
||||
private RenderTargetHandle m_RenderTarget;
|
||||
|
||||
// Constants
|
||||
private const string k_SSShadowsTextureName = "_ScreenSpaceShadowmapTexture";
|
||||
|
||||
internal ScreenSpaceShadowsPass()
|
||||
{
|
||||
m_CurrentSettings = new ScreenSpaceShadowsSettings();
|
||||
m_RenderTarget.Init(k_SSShadowsTextureName);
|
||||
}
|
||||
|
||||
internal bool Setup(ScreenSpaceShadowsSettings featureSettings)
|
||||
{
|
||||
m_CurrentSettings = featureSettings;
|
||||
ConfigureInput(ScriptableRenderPassInput.Depth);
|
||||
|
||||
return material != null;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
|
||||
{
|
||||
m_RenderTextureDescriptor = renderingData.cameraData.cameraTargetDescriptor;
|
||||
m_RenderTextureDescriptor.depthBufferBits = 0;
|
||||
m_RenderTextureDescriptor.msaaSamples = 1;
|
||||
m_RenderTextureDescriptor.graphicsFormat = RenderingUtils.SupportsGraphicsFormat(GraphicsFormat.R8_UNorm, FormatUsage.Linear | FormatUsage.Render)
|
||||
? GraphicsFormat.R8_UNorm
|
||||
: GraphicsFormat.B8G8R8A8_UNorm;
|
||||
|
||||
cmd.GetTemporaryRT(m_RenderTarget.id, m_RenderTextureDescriptor, FilterMode.Point);
|
||||
|
||||
RenderTargetIdentifier renderTargetTexture = m_RenderTarget.Identifier();
|
||||
ConfigureTarget(renderTargetTexture);
|
||||
ConfigureClear(ClearFlag.None, Color.white);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
|
||||
{
|
||||
if (material == null)
|
||||
{
|
||||
Debug.LogErrorFormat("{0}.Execute(): Missing material. {1} render pass will not execute. Check for missing reference in the renderer resources.", GetType().Name, profilerTag);
|
||||
return;
|
||||
}
|
||||
|
||||
Camera camera = renderingData.cameraData.camera;
|
||||
|
||||
CommandBuffer cmd = CommandBufferPool.Get();
|
||||
using (new ProfilingScope(cmd, ProfilingSampler.Get(URPProfileId.ResolveShadows)))
|
||||
{
|
||||
if (!renderingData.cameraData.xr.enabled)
|
||||
{
|
||||
cmd.SetViewProjectionMatrices(Matrix4x4.identity, Matrix4x4.identity);
|
||||
cmd.DrawMesh(RenderingUtils.fullscreenMesh, Matrix4x4.identity, material);
|
||||
cmd.SetViewProjectionMatrices(camera.worldToCameraMatrix, camera.projectionMatrix);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Avoid setting and restoring camera view and projection matrices when in stereo.
|
||||
RenderTargetIdentifier screenSpaceShadowTexture = m_RenderTarget.Identifier();
|
||||
cmd.Blit(null, screenSpaceShadowTexture, material);
|
||||
}
|
||||
|
||||
CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.MainLightShadows, false);
|
||||
CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.MainLightShadowCascades, false);
|
||||
CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.MainLightShadowScreen, true);
|
||||
}
|
||||
|
||||
context.ExecuteCommandBuffer(cmd);
|
||||
CommandBufferPool.Release(cmd);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void OnCameraCleanup(CommandBuffer cmd)
|
||||
{
|
||||
if (cmd == null)
|
||||
{
|
||||
throw new ArgumentNullException("cmd");
|
||||
}
|
||||
|
||||
cmd.ReleaseTemporaryRT(m_RenderTarget.id);
|
||||
}
|
||||
}
|
||||
|
||||
private class RestoreShadowKeywordsPass : ScriptableRenderPass
|
||||
{
|
||||
const string m_ProfilerTag = "Restore Shadow Keywords Pass";
|
||||
private ProfilingSampler m_ProfilingSampler = new ProfilingSampler(m_ProfilerTag);
|
||||
|
||||
public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
|
||||
{
|
||||
ConfigureTarget(BuiltinRenderTextureType.CurrentActive);
|
||||
}
|
||||
|
||||
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
|
||||
{
|
||||
CommandBuffer cmd = CommandBufferPool.Get();
|
||||
using (new ProfilingScope(cmd, m_ProfilingSampler))
|
||||
{
|
||||
ShadowData shadowData = renderingData.shadowData;
|
||||
int cascadesCount = shadowData.mainLightShadowCascadesCount;
|
||||
|
||||
bool mainLightShadows = renderingData.shadowData.supportsMainLightShadows;
|
||||
bool receiveShadowsNoCascade = mainLightShadows && cascadesCount == 1;
|
||||
bool receiveShadowsCascades = mainLightShadows && cascadesCount > 1;
|
||||
|
||||
// Before transparent object pass, force screen space shadow for main light to disable
|
||||
CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.MainLightShadowScreen, false);
|
||||
|
||||
// then enable main light shadows with or without cascades
|
||||
CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.MainLightShadows, receiveShadowsNoCascade);
|
||||
CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.MainLightShadowCascades, receiveShadowsCascades);
|
||||
}
|
||||
|
||||
context.ExecuteCommandBuffer(cmd);
|
||||
CommandBufferPool.Release(cmd);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user