namespace UnityEngine.Rendering.HighDefinition
{
///
/// Base class for sky rendering.
///
public abstract class SkyRenderer
{
int m_LastFrameUpdate = -1;
/// Determines if the sky should be rendered when the sun light changes.
public bool SupportDynamicSunLight = true;
///
/// Called on startup. Create resources used by the renderer (shaders, materials, etc).
///
public abstract void Build();
///
/// Called on cleanup. Release resources used by the renderer.
///
public abstract void Cleanup();
///
/// HDRP calls this function once every frame. Implement it if your SkyRenderer needs to iterate independently of the user defined update frequency (see SkySettings UpdateMode).
///
/// Engine parameters that you can use to update the sky.
/// True if the update determines that sky lighting needs to be re-rendered. False otherwise.
protected virtual bool Update(BuiltinSkyParameters builtinParams) { return false; }
///
/// Preprocess for rendering the sky. Called before the DepthPrePass operations
///
/// Engine parameters that you can use to render the sky.
/// Pass in true if you want to render the sky into a cubemap for lighting. This is useful when the sky renderer needs a different implementation in this case.
/// If the sky renderer supports the rendering of a sun disk, it must not render it if this is set to false.
[System.Obsolete("Please override PreRenderSky(BuiltinSkyParameters) instead. #from(2021.2)")]
public virtual void PreRenderSky(BuiltinSkyParameters builtinParams, bool renderForCubemap, bool renderSunDisk)
{
PreRenderSky(builtinParams);
}
///
/// Preprocess for rendering the sky. Called before the DepthPrePass operations
///
/// Engine parameters that you can use to render the sky.
public virtual void PreRenderSky(BuiltinSkyParameters builtinParams) { }
///
/// Whether the PreRenderSky step is required.
///
/// Engine parameters that you can use to render the sky.
/// True if the PreRenderSky step is required.
[System.Obsolete("Please implement RequiresPreRender instead. #from(2022.2)")]
public virtual bool RequiresPreRenderSky(BuiltinSkyParameters builtinParams) { return false; }
///
/// Whether the PreRenderSky step is required or not.
///
/// Sky setting for the current sky.
/// True if the sky needs a pre-render pass.
public virtual bool RequiresPreRender(SkySettings skySettings) { return false; }
///
/// Implements actual rendering of the sky. HDRP calls this when rendering the sky into a cubemap (for lighting) and also during main frame rendering.
///
/// Engine parameters that you can use to render the sky.
/// Pass in true if you want to render the sky into a cubemap for lighting. This is useful when the sky renderer needs a different implementation in this case.
/// If the sky renderer supports the rendering of a sun disk, it must not render it if this is set to false.
public abstract void RenderSky(BuiltinSkyParameters builtinParams, bool renderForCubemap, bool renderSunDisk);
///
/// Returns exposure setting for the provided SkySettings.
///
/// SkySettings for which exposure is required.
/// Current debug display settings
/// Returns SkySetting exposure.
protected static float GetSkyIntensity(SkySettings skySettings, DebugDisplaySettings debugSettings)
{
return skySettings.GetIntensityFromSettings();
}
///
/// Setup global parameters for the sky renderer.
///
/// Command buffer provided to setup shader constants.
/// Sky system builtin parameters.
public virtual void SetGlobalSkyData(CommandBuffer cmd, BuiltinSkyParameters builtinParams)
{
// bind empty resources for non-PBR sky.
if (ShaderConfig.s_PrecomputedAtmosphericAttenuation == 0)
{
cmd.SetGlobalTexture(HDShaderIDs._AirSingleScatteringTexture, (RenderTargetIdentifier)CoreUtils.blackVolumeTexture);
cmd.SetGlobalTexture(HDShaderIDs._AerosolSingleScatteringTexture, (RenderTargetIdentifier)CoreUtils.blackVolumeTexture);
cmd.SetGlobalTexture(HDShaderIDs._MultipleScatteringTexture, (RenderTargetIdentifier)CoreUtils.blackVolumeTexture);
}
else
cmd.SetGlobalTexture(HDShaderIDs._AtmosphericScatteringLUT, (RenderTargetIdentifier)CoreUtils.blackVolumeTexture);
cmd.SetGlobalBuffer(HDShaderIDs._CelestialBodyDatas, builtinParams.emptyCelestialBodyBuffer);
}
internal bool DoUpdate(BuiltinSkyParameters parameters)
{
if (m_LastFrameUpdate < parameters.frameIndex)
{
// Here we need a temporary command buffer to be executed because this is called during render graph construction.
// This means that we don't have a proper command buffer to provide unless in a render graph pass.
// Besides, we need this function to be executed immediately to retrieve the return value so it cannot be executed later as a proper render graph pass.
var previousCommandBuffer = parameters.commandBuffer;
var commandBuffer = CommandBufferPool.Get("SkyUpdate");
parameters.commandBuffer = commandBuffer;
m_LastFrameUpdate = parameters.frameIndex;
var result = Update(parameters);
Graphics.ExecuteCommandBuffer(commandBuffer);
CommandBufferPool.Release(commandBuffer);
parameters.commandBuffer = previousCommandBuffer;
return result;
}
return false;
}
internal void Reset()
{
m_LastFrameUpdate = -1;
}
}
}