Files
SampleRenderPasses/Assets/Scripts/RenderPasses/ScreenFadeFeature.cs
2025-05-18 22:39:39 +03:00

177 lines
5.8 KiB
C#

using Golems.RenderFeatures;
using UnityEngine;
using UnityEngine.Rendering;
using Golems.Attributes;
using RogueUtils.Data;
using UnityEngine.Rendering.Universal;
using static Golems.ScriptableRenderPassHelper;
namespace Golems
{
public class ScreenFadeFeature : SourceToDestinationBlitRenderFeature
{
[Header("Screen fade things")]
//
// This is the fade colour used to fade the screen.
// It is up to whatever is setting this to correctly set the alpha.
//
[SerializeField]
private ColorVariable m_TargetFadeColour = default;
public bool IsEnabled
{
get
{
return m_IsEnabled;;
}
set
{
m_IsEnabled = value;
}
}
[SerializeField]
private bool m_IsEnabled = false;
private ScreenFadePass m_Pass = default;
public override void Create()
{
m_Pass = new ScreenFadePass(m_TargetFadeColour);
DoCreate();
}
private void DebugSetEnabledState(bool state)
{
m_IsEnabled = state;
}
private bool DebugGetEnabledState()
{
return m_IsEnabled;
}
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
{
if (IsSourceAndDestinationSame() && !m_IsEnabled)
{
//
// Can skip this pass as the source and destination are the same and the fade
// is not enabled.
//
return;
}
m_Pass.Setup(m_IsEnabled, m_WhenToInsert, renderer.cameraColorTarget,
m_SourceTextureSourceType, m_SourceNamedTemporaryNameHash,
m_DestinationTextureSourceType, m_DestinationNamedTemporaryNameHash,
m_TempCopyNamedTemporaryNameHash);
renderer.EnqueuePass(m_Pass);
}
}
public class ScreenFadePass : SourceToDestinationBlitRenderPass
{
private const string k_ProfilerIdent = "ScreenFadePass";
private const string k_ScreenFaderCompositeShader = "Hidden/ScreenFaderComposite";
private const string k_FadeColour = "_FadeColour";
private static readonly int k_FadeColourId = Shader.PropertyToID(k_FadeColour);
private Material m_ScreenFaderCompositeMaterial = default;
private readonly Color m_FadeColour;
private bool m_IsEnabled;
public ScreenFadePass(ColorVariable colourVariable)
{
CreateMaterial(k_ScreenFaderCompositeShader, out m_ScreenFaderCompositeMaterial);
m_FadeColour = Color.white;
}
public void Setup(bool isEnabled, RenderPassEvent whenToRender, RenderTargetIdentifier cameraColourTargetIdentifier,
RenderFeatures.TextureSourceType sourceSourceType, int tempSourceTextureHash,
RenderFeatures.TextureSourceType destinationSourceType, int tempDestinationTextureHash,
int sameSourceDestTextureHash)
{
m_IsEnabled = isEnabled;
DoSetup(whenToRender, cameraColourTargetIdentifier,
sourceSourceType, tempSourceTextureHash,
destinationSourceType, tempDestinationTextureHash,
sameSourceDestTextureHash);
}
public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
{
DoOnCameraSetup(cmd, ref renderingData);
}
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
#if UNITY_EDITOR
if (m_ScreenFaderCompositeMaterial == null)
{
//
// In Editor the material will get destroyed on scene change
//
CreateMaterial(k_ScreenFaderCompositeShader, out m_ScreenFaderCompositeMaterial);
if (m_ScreenFaderCompositeMaterial == null)
{
return;
}
}
#endif
if (!m_GoodToExecute)
{
return;
}
//
// Set up all the shader parameters here!
//
var fadeColour = m_FadeColour;
bool doCopyOnly = fadeColour.a == 0.0f || !m_IsEnabled;
if (doCopyOnly)
{
//
// Nothing to blend
//
if (!m_SourceAndDestinationSame)
{
//
// Copy the source to the destination
//
var copyCmd = CommandBufferPool.Get(k_ProfilerIdent);
copyCmd.Blit(m_SourceTargetIdentifier, m_DestinationTargetIdentifier);
context.ExecuteCommandBuffer(copyCmd);
CommandBufferPool.Release(copyCmd);
}
return;
}
m_ScreenFaderCompositeMaterial.SetColor(k_FadeColourId, fadeColour);
var cmd = CommandBufferPool.Get(k_ProfilerIdent);
if (m_SourceAndDestinationSame)
{
// Blit the Camera into the screen copy texture using the the fader shader
cmd.Blit(m_SourceTargetIdentifier, m_TempCopyTargetIdentifier, m_ScreenFaderCompositeMaterial);
// blit back into the camera texture
cmd.Blit(m_TempCopyTargetIdentifier, m_DestinationTargetIdentifier);
}
else
{
cmd.Blit(m_SourceTargetIdentifier, m_DestinationTargetIdentifier, m_ScreenFaderCompositeMaterial);
}
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
}
}