using System;
using System.Collections.Generic;
using UnityEngine.Rendering;
using UnityEngine.XR.ARSubsystems;
namespace UnityEngine.XR.ARFoundation
{
static class RenderingUtility
{
///
/// Name of the shader parameter for the camera forward scale.
///
internal const string k_CameraForwardScaleName = "_UnityCameraForwardScale";
///
/// Name of the shader parameter for the display transform matrix.
///
internal const string k_DisplayTransformName = "_UnityDisplayTransform";
internal static void SetShaderKeywords(Material material, XRShaderKeywords keywords)
{
if (keywords.enabledKeywords != null)
{
foreach (var shaderKeyword in keywords.enabledKeywords)
material.EnableKeyword(shaderKeyword);
}
if (keywords.disabledKeywords != null)
{
foreach (var shaderKeyword in keywords.disabledKeywords)
{
if (material.IsKeywordEnabled(shaderKeyword))
material.DisableKeyword(shaderKeyword);
}
}
}
internal static void SetShaderKeywordsGlobal(XRShaderKeywords keywords)
{
if (keywords.enabledKeywords != null)
{
foreach (var shaderKeyword in keywords.enabledKeywords)
Shader.EnableKeyword(shaderKeyword);
}
if (keywords.disabledKeywords != null)
{
foreach (var shaderKeyword in keywords.disabledKeywords)
{
if (Shader.IsKeywordEnabled(shaderKeyword))
Shader.DisableKeyword(shaderKeyword);
}
}
}
internal class BuiltInRendering
{
///
/// The list of [CameraEvent](https://docs.unity3d.com/ScriptReference/Rendering.CameraEvent.html)s
/// to add to the [CommandBuffer](https://docs.unity3d.com/ScriptReference/Rendering.CommandBuffer.html)
/// when rendering before opaques.
///
static readonly CameraEvent[] s_DefaultBeforeOpaqueCameraEvents = {
CameraEvent.BeforeForwardOpaque,
CameraEvent.BeforeGBuffer
};
///
/// The list of [CameraEvent](https://docs.unity3d.com/ScriptReference/Rendering.CameraEvent.html)s
/// to add to the [CommandBuffer](https://docs.unity3d.com/ScriptReference/Rendering.CommandBuffer.html)
/// when rendering after opaques.
///
static readonly CameraEvent[] s_DefaultAfterOpaqueCameraEvents = {
CameraEvent.AfterForwardOpaque,
CameraEvent.AfterGBuffer
};
///
/// The list of [CameraEvent](https://docs.unity3d.com/ScriptReference/Rendering.CameraEvent.html)s
/// to add to the [CommandBuffer](https://docs.unity3d.com/ScriptReference/Rendering.CommandBuffer.html).
/// By default, it will select either or
/// depending on the value of .
///
/// In the case where Before Opaques rendering has been selected it will return:
///
/// [BeforeForwardOpaque](https://docs.unity3d.com/ScriptReference/Rendering.CameraEvent.BeforeForwardOpaque.html)
/// and
/// [BeforeGBuffer](https://docs.unity3d.com/ScriptReference/Rendering.CameraEvent.BeforeGBuffer.html)}.
///
/// In the case where After Opaques rendering has been selected it will return:
///
/// [AfterForwardOpaque](https://docs.unity3d.com/ScriptReference/Rendering.CameraEvent.AfterForwardOpaque.html)
/// and
/// [AfterGBuffer](https://docs.unity3d.com/ScriptReference/Rendering.CameraEvent.AfterGBuffer.html)}.
///
/// Override to use different camera events.
///
internal static IEnumerable GetBuiltInRenderingCameraEvents(XRCameraBackgroundRenderingMode commandBufferRenderOrderState)
{
return commandBufferRenderOrderState switch
{
XRCameraBackgroundRenderingMode.BeforeOpaques => s_DefaultBeforeOpaqueCameraEvents,
XRCameraBackgroundRenderingMode.AfterOpaques => s_DefaultAfterOpaqueCameraEvents,
_ => default(IEnumerable)
};
}
///
/// Adds the AR Camera Background to the .
///
/// The camera to which command buffer should be added.
/// Camera events for which to add the command buffer.
/// The command buffer to add to the camera events.
internal static void AddCommandBufferToCameraEvent(Camera camera, IEnumerable cameraEvents, CommandBuffer commandBuffer)
{
foreach (var cameraEvent in cameraEvents)
{
camera.AddCommandBuffer(cameraEvent, commandBuffer);
}
}
///
/// Removes the AR Camera Background from the camera rendering events.
///
/// The camera from which command buffer should be removed.
/// Camera events for which to remove the command buffer.
/// The command buffer to remove from the camera events.
internal static void RemoveCommandBufferFromCameraEvents(Camera camera, IEnumerable cameraEvents, CommandBuffer commandBuffer)
{
foreach (var cameraEvent in cameraEvents)
{
camera.RemoveCommandBuffer(cameraEvent, commandBuffer);
}
}
}
}
}