using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using UnityEngine.Assertions;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace UnityEngine.Rendering.PostProcessing
{
using SceneManagement;
using UnityObject = UnityEngine.Object;
using LoadAction = RenderBufferLoadAction;
using StoreAction = RenderBufferStoreAction;
///
/// A set of runtime utilities used by the post-processing stack.
///
public static class RuntimeUtilities
{
#region Textures
static Texture2D m_WhiteTexture;
///
/// A 1x1 white texture.
///
///
/// This texture is only created once and recycled afterward. You shouldn't modify it.
///
public static Texture2D whiteTexture
{
get
{
if (m_WhiteTexture == null)
{
m_WhiteTexture = new Texture2D(1, 1, TextureFormat.ARGB32, false) { name = "White Texture" };
m_WhiteTexture.SetPixel(0, 0, Color.white);
m_WhiteTexture.Apply();
}
return m_WhiteTexture;
}
}
static Texture3D m_WhiteTexture3D;
///
/// A 1x1x1 white texture.
///
///
/// This texture is only created once and recycled afterward. You shouldn't modify it.
///
public static Texture3D whiteTexture3D
{
get
{
if (m_WhiteTexture3D == null)
{
m_WhiteTexture3D = new Texture3D(1, 1, 1, TextureFormat.ARGB32, false) { name = "White Texture 3D" };
m_WhiteTexture3D.SetPixels(new Color[] { Color.white });
m_WhiteTexture3D.Apply();
}
return m_WhiteTexture3D;
}
}
static Texture2D m_BlackTexture;
///
/// A 1x1 black texture.
///
///
/// This texture is only created once and recycled afterward. You shouldn't modify it.
///
public static Texture2D blackTexture
{
get
{
if (m_BlackTexture == null)
{
m_BlackTexture = new Texture2D(1, 1, TextureFormat.ARGB32, false) { name = "Black Texture" };
m_BlackTexture.SetPixel(0, 0, Color.black);
m_BlackTexture.Apply();
}
return m_BlackTexture;
}
}
static Texture3D m_BlackTexture3D;
///
/// A 1x1x1 black texture.
///
///
/// This texture is only created once and recycled afterward. You shouldn't modify it.
///
public static Texture3D blackTexture3D
{
get
{
if (m_BlackTexture3D == null)
{
m_BlackTexture3D = new Texture3D(1, 1, 1, TextureFormat.ARGB32, false) { name = "Black Texture 3D" };
m_BlackTexture3D.SetPixels(new Color[] { Color.black });
m_BlackTexture3D.Apply();
}
return m_BlackTexture3D;
}
}
static Texture2D m_TransparentTexture;
///
/// A 1x1 transparent texture.
///
///
/// This texture is only created once and recycled afterward. You shouldn't modify it.
///
public static Texture2D transparentTexture
{
get
{
if (m_TransparentTexture == null)
{
m_TransparentTexture = new Texture2D(1, 1, TextureFormat.ARGB32, false) { name = "Transparent Texture" };
m_TransparentTexture.SetPixel(0, 0, Color.clear);
m_TransparentTexture.Apply();
}
return m_TransparentTexture;
}
}
static Texture3D m_TransparentTexture3D;
///
/// A 1x1x1 transparent texture.
///
///
/// This texture is only created once and recycled afterward. You shouldn't modify it.
///
public static Texture3D transparentTexture3D
{
get
{
if (m_TransparentTexture3D == null)
{
m_TransparentTexture3D = new Texture3D(1, 1, 1, TextureFormat.ARGB32, false) { name = "Transparent Texture 3D" };
m_TransparentTexture3D.SetPixels(new Color[] { Color.clear });
m_TransparentTexture3D.Apply();
}
return m_TransparentTexture3D;
}
}
static Dictionary m_LutStrips = new Dictionary();
///
/// Gets a 2D lookup table for color grading use. Its size will be width = height * height.
///
/// The height of the lookup table
/// A 2D lookup table
///
/// Lookup tables are recycled and only created once per size. You shouldn't modify them.
///
public static Texture2D GetLutStrip(int size)
{
Texture2D texture;
if (!m_LutStrips.TryGetValue(size, out texture))
{
int width = size * size;
int height = size;
var pixels = new Color[width * height];
float inv = 1f / (size - 1f);
for (int z = 0; z < size; z++)
{
var offset = z * size;
var b = z * inv;
for (int y = 0; y < size; y++)
{
var g = y * inv;
for (int x = 0; x < size; x++)
{
var r = x * inv;
pixels[y * width + offset + x] = new Color(r, g, b);
}
}
}
var format = TextureFormat.RGBAHalf;
if (!format.IsSupported())
format = TextureFormat.ARGB32;
texture = new Texture2D(size * size, size, format, false, true)
{
name = "Strip Lut" + size,
hideFlags = HideFlags.DontSave,
filterMode = FilterMode.Bilinear,
wrapMode = TextureWrapMode.Clamp,
anisoLevel = 0
};
texture.SetPixels(pixels);
texture.Apply();
m_LutStrips.Add(size, texture);
}
return texture;
}
#endregion
#region Rendering
static PostProcessResources s_Resources;
static Mesh s_FullscreenTriangle;
///
/// A fullscreen triangle mesh.
///
public static Mesh fullscreenTriangle
{
get
{
if (s_FullscreenTriangle != null)
return s_FullscreenTriangle;
s_FullscreenTriangle = new Mesh { name = "Fullscreen Triangle" };
// Because we have to support older platforms (GLES2/3, DX9 etc) we can't do all of
// this directly in the vertex shader using vertex ids :(
s_FullscreenTriangle.SetVertices(new List
{
new Vector3(-1f, -1f, 0f),
new Vector3(-1f, 3f, 0f),
new Vector3(3f, -1f, 0f)
});
s_FullscreenTriangle.SetIndices(new[] { 0, 1, 2 }, MeshTopology.Triangles, 0, false);
s_FullscreenTriangle.UploadMeshData(false);
return s_FullscreenTriangle;
}
}
static Material s_CopyStdMaterial;
///
/// A simple copy material to use with the builtin pipelines.
///
public static Material copyStdMaterial
{
get
{
if (s_CopyStdMaterial != null)
return s_CopyStdMaterial;
Assert.IsNotNull(s_Resources);
var shader = s_Resources.shaders.copyStd;
s_CopyStdMaterial = new Material(shader)
{
name = "PostProcess - CopyStd",
hideFlags = HideFlags.HideAndDontSave
};
return s_CopyStdMaterial;
}
}
static Material s_CopyStdFromDoubleWideMaterial;
///
/// A double-wide copy material to use with VR and the builtin pipelines.
///
public static Material copyStdFromDoubleWideMaterial
{
get
{
if (s_CopyStdFromDoubleWideMaterial != null)
return s_CopyStdFromDoubleWideMaterial;
Assert.IsNotNull(s_Resources);
var shader = s_Resources.shaders.copyStdFromDoubleWide;
s_CopyStdFromDoubleWideMaterial = new Material(shader)
{
name = "PostProcess - CopyStdFromDoubleWide",
hideFlags = HideFlags.HideAndDontSave
};
return s_CopyStdFromDoubleWideMaterial;
}
}
static Material s_CopyMaterial;
///
/// A simple copy material independent from the rendering pipeline.
///
public static Material copyMaterial
{
get
{
if (s_CopyMaterial != null)
return s_CopyMaterial;
Assert.IsNotNull(s_Resources);
var shader = s_Resources.shaders.copy;
s_CopyMaterial = new Material(shader)
{
name = "PostProcess - Copy",
hideFlags = HideFlags.HideAndDontSave
};
return s_CopyMaterial;
}
}
static Material s_CopyFromTexArrayMaterial;
///
/// A copy material with a texture array slice as a source for the builtin pipelines.
///
public static Material copyFromTexArrayMaterial
{
get
{
if (s_CopyFromTexArrayMaterial != null)
return s_CopyFromTexArrayMaterial;
Assert.IsNotNull(s_Resources);
var shader = s_Resources.shaders.copyStdFromTexArray;
s_CopyFromTexArrayMaterial = new Material(shader)
{
name = "PostProcess - CopyFromTexArray",
hideFlags = HideFlags.HideAndDontSave
};
return s_CopyFromTexArrayMaterial;
}
}
static PropertySheet s_CopySheet;
///
/// A pre-configured for .
///
public static PropertySheet copySheet
{
get
{
if (s_CopySheet == null)
s_CopySheet = new PropertySheet(copyMaterial);
return s_CopySheet;
}
}
static PropertySheet s_CopyFromTexArraySheet;
///
/// A pre-configured for .
///
public static PropertySheet copyFromTexArraySheet
{
get
{
if (s_CopyFromTexArraySheet == null)
s_CopyFromTexArraySheet = new PropertySheet(copyFromTexArrayMaterial);
return s_CopyFromTexArraySheet;
}
}
internal static bool isValidResources()
{
return s_Resources != null;
}
internal static void UpdateResources(PostProcessResources resources)
{
Destroy(s_CopyMaterial);
Destroy(s_CopyStdMaterial);
Destroy(s_CopyFromTexArrayMaterial);
Destroy(s_CopyStdFromDoubleWideMaterial);
s_CopyMaterial = null;
s_CopyStdMaterial = null;
s_CopyFromTexArrayMaterial = null;
s_CopyStdFromDoubleWideMaterial = null;
s_CopySheet = null;
s_CopyFromTexArraySheet = null;
s_Resources = resources;
}
///
/// Sets the current render target using specified .
///
/// The command buffer to set the render target on
/// The render target to set
/// The load action
/// The store action
///
/// are only used on Unity 2018.2 or newer.
///
public static void SetRenderTargetWithLoadStoreAction(this CommandBuffer cmd, RenderTargetIdentifier rt, RenderBufferLoadAction loadAction, RenderBufferStoreAction storeAction)
{
#if UNITY_2018_2_OR_NEWER
cmd.SetRenderTarget(rt, loadAction, storeAction);
#else
cmd.SetRenderTarget(rt);
#endif
}
///
/// Sets the current render target using specified .
///
/// The command buffer to set the render target on
/// The render target to set
/// The load action
/// The store action
/// The load action for the depth/stencil part of rt
/// The store action for the depth/stencil part of rt
///
/// are only used on Unity 2018.2 or newer.
///
public static void SetRenderTargetWithLoadStoreAction(this CommandBuffer cmd, RenderTargetIdentifier rt,
RenderBufferLoadAction loadAction, RenderBufferStoreAction storeAction,
RenderBufferLoadAction depthLoadAction, RenderBufferStoreAction depthStoreAction)
{
#if UNITY_2018_2_OR_NEWER
cmd.SetRenderTarget(rt, loadAction, storeAction, depthLoadAction, depthStoreAction);
#else
cmd.SetRenderTarget(rt);
#endif
}
///
/// Sets the current render target and its depth using specified .
///
/// The command buffer to set the render target on
/// The render target to set as color
/// The load action for the color render target
/// The store action for the color render target
/// The render target to set as depth
/// The load action for the depth render target
/// The store action for the depth render target
public static void SetRenderTargetWithLoadStoreAction(this CommandBuffer cmd,
RenderTargetIdentifier color, RenderBufferLoadAction colorLoadAction, RenderBufferStoreAction colorStoreAction,
RenderTargetIdentifier depth, RenderBufferLoadAction depthLoadAction, RenderBufferStoreAction depthStoreAction)
{
#if UNITY_2018_2_OR_NEWER
cmd.SetRenderTarget(color, colorLoadAction, colorStoreAction, depth, depthLoadAction, depthStoreAction);
#else
cmd.SetRenderTarget(color, depth);
#endif
}
///
/// Does a copy of source to destination using a fullscreen triangle.
///
/// The command buffer to use
/// The source render target
/// The destination render target
/// Should the destination target be cleared?
/// An optional viewport to consider for the blit
/// Should the depth buffer be preserved?
public static void BlitFullscreenTriangle(this CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier destination, bool clear = false, Rect? viewport = null, bool preserveDepth = false)
{
cmd.SetGlobalTexture(ShaderIDs.MainTex, source);
var colorLoad = viewport == null ? LoadAction.DontCare : LoadAction.Load;
cmd.SetRenderTargetWithLoadStoreAction(destination, colorLoad, StoreAction.Store, preserveDepth ? LoadAction.Load : colorLoad, StoreAction.Store);
if (viewport != null)
cmd.SetViewport(viewport.Value);
if (clear)
cmd.ClearRenderTarget(true, true, Color.clear);
cmd.DrawMesh(fullscreenTriangle, Matrix4x4.identity, copyMaterial, 0, 0);
}
///
/// Blits a fullscreen triangle using a given material.
///
/// The command buffer to use
/// The source render target
/// The destination render target
/// The property sheet to use
/// The pass from the material to use
/// The load action for this blit
/// An optional viewport to consider for the blit
/// Should the depth buffer be preserved?
public static void BlitFullscreenTriangle(this CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier destination, PropertySheet propertySheet, int pass, RenderBufferLoadAction loadAction, Rect? viewport = null, bool preserveDepth = false)
{
cmd.SetGlobalTexture(ShaderIDs.MainTex, source);
#if UNITY_2018_2_OR_NEWER
bool clear = (loadAction == LoadAction.Clear);
if (clear)
loadAction = LoadAction.DontCare;
#else
bool clear = false;
#endif
if (viewport != null)
loadAction = LoadAction.Load;
cmd.SetRenderTargetWithLoadStoreAction(destination, loadAction, StoreAction.Store, preserveDepth ? LoadAction.Load : loadAction, StoreAction.Store);
if (viewport != null)
cmd.SetViewport(viewport.Value);
if (clear)
cmd.ClearRenderTarget(true, true, Color.clear);
cmd.DrawMesh(fullscreenTriangle, Matrix4x4.identity, propertySheet.material, 0, pass, propertySheet.properties);
}
///
/// Blits a fullscreen triangle using a given material.
///
/// The command buffer to use
/// The source render target
/// The destination render target
/// The property sheet to use
/// The pass from the material to use
/// Should the destination target be cleared?
/// An optional viewport to consider for the blit
/// Should the depth buffer be preserved?
public static void BlitFullscreenTriangle(this CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier destination, PropertySheet propertySheet, int pass, bool clear = false, Rect? viewport = null, bool preserveDepth = false)
{
#if UNITY_2018_2_OR_NEWER
cmd.BlitFullscreenTriangle(source, destination, propertySheet, pass, clear ? LoadAction.Clear : LoadAction.DontCare, viewport, preserveDepth);
#else
cmd.SetGlobalTexture(ShaderIDs.MainTex, source);
var loadAction = viewport == null ? LoadAction.DontCare : LoadAction.Load;
cmd.SetRenderTargetWithLoadStoreAction(destination, loadAction, StoreAction.Store, preserveDepth ? LoadAction.Load : loadAction, StoreAction.Store);
if (viewport != null)
cmd.SetViewport(viewport.Value);
if (clear)
cmd.ClearRenderTarget(true, true, Color.clear);
cmd.DrawMesh(fullscreenTriangle, Matrix4x4.identity, propertySheet.material, 0, pass, propertySheet.properties);
#endif
}
///
/// Blits a fullscreen triangle from a double-wide source.
///
/// The command buffer to use
/// The source render target
/// The destination render target
/// The material to use for the blit
/// The pass from the material to use
/// The target eye
public static void BlitFullscreenTriangleFromDoubleWide(this CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier destination, Material material, int pass, int eye)
{
Vector4 uvScaleOffset = new Vector4(0.5f, 1.0f, 0, 0);
if (eye == 1)
uvScaleOffset.z = 0.5f;
cmd.SetGlobalVector(ShaderIDs.UVScaleOffset, uvScaleOffset);
cmd.BuiltinBlit(source, destination, material, pass);
}
///
/// Blits a fullscreen triangle to a double-wide destination.
///
/// The command buffer to use
/// The source render target
/// The destination render target
/// The property sheet to use
/// The pass from the material to use
/// The target eye
public static void BlitFullscreenTriangleToDoubleWide(this CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier destination, PropertySheet propertySheet, int pass, int eye)
{
Vector4 posScaleOffset = new Vector4(0.5f, 1.0f, -0.5f, 0);
if (eye == 1)
posScaleOffset.z = 0.5f;
propertySheet.EnableKeyword("STEREO_DOUBLEWIDE_TARGET");
propertySheet.properties.SetVector(ShaderIDs.PosScaleOffset, posScaleOffset);
cmd.BlitFullscreenTriangle(source, destination, propertySheet, 0);
}
///
/// Blits a fullscreen triangle using a given material.
///
/// The command buffer to use
/// The source texture array
/// The destination render target
/// The property sheet to use
/// The pass from the material to use
/// Should the destination target be cleared?
/// The slice to use for the texture array
public static void BlitFullscreenTriangleFromTexArray(this CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier destination, PropertySheet propertySheet, int pass, bool clear = false, int depthSlice = -1)
{
cmd.SetGlobalTexture(ShaderIDs.MainTex, source);
cmd.SetGlobalFloat(ShaderIDs.DepthSlice, depthSlice);
cmd.SetRenderTargetWithLoadStoreAction(destination, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
if (clear)
cmd.ClearRenderTarget(true, true, Color.clear);
cmd.DrawMesh(fullscreenTriangle, Matrix4x4.identity, propertySheet.material, 0, pass, propertySheet.properties);
}
///
/// Blits a fullscreen triangle using a given material.
///
/// The command buffer to use
/// The source render target
/// The destination render target
/// The property sheet to use
/// The pass from the material to use
/// Should the destination target be cleared?
/// The array slice to consider as a source
public static void BlitFullscreenTriangleToTexArray(this CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier destination, PropertySheet propertySheet, int pass, bool clear = false, int depthSlice = -1)
{
cmd.SetGlobalTexture(ShaderIDs.MainTex, source);
cmd.SetGlobalFloat(ShaderIDs.DepthSlice, depthSlice);
cmd.SetRenderTarget(destination, 0, CubemapFace.Unknown, -1);
if (clear)
cmd.ClearRenderTarget(true, true, Color.clear);
cmd.DrawMesh(fullscreenTriangle, Matrix4x4.identity, propertySheet.material, 0, pass, propertySheet.properties);
}
///
/// Blits a fullscreen triangle using a given material.
///
/// The command buffer to use
/// The source render target
/// The destination render target
/// The depth render target
/// The property sheet to use
/// The pass from the material to use
/// Should the destination target be cleared?
/// An optional viewport to consider for the blit
public static void BlitFullscreenTriangle(this CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier destination, RenderTargetIdentifier depth, PropertySheet propertySheet, int pass, bool clear = false, Rect? viewport = null)
{
cmd.SetGlobalTexture(ShaderIDs.MainTex, source);
LoadAction loadAction = viewport == null ? LoadAction.DontCare : LoadAction.Load;
if (clear)
{
cmd.SetRenderTargetWithLoadStoreAction(destination, loadAction, StoreAction.Store, depth, loadAction, StoreAction.Store);
cmd.ClearRenderTarget(true, true, Color.clear);
}
else
{
cmd.SetRenderTargetWithLoadStoreAction(destination, loadAction, StoreAction.Store, depth, LoadAction.Load, StoreAction.Store);
}
if (viewport != null)
cmd.SetViewport(viewport.Value);
cmd.DrawMesh(fullscreenTriangle, Matrix4x4.identity, propertySheet.material, 0, pass, propertySheet.properties);
}
///
/// Blits a fullscreen triangle using a given material.
///
/// The command buffer to use
/// The source render target
/// An array of destinations render targets
/// The depth render target
/// The property sheet to use
/// The pass from the material to use
/// Should the destination target be cleared?
/// An optional viewport to consider for the blit
public static void BlitFullscreenTriangle(this CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier[] destinations, RenderTargetIdentifier depth, PropertySheet propertySheet, int pass, bool clear = false, Rect? viewport = null)
{
cmd.SetGlobalTexture(ShaderIDs.MainTex, source);
cmd.SetRenderTarget(destinations, depth);
if (viewport != null)
cmd.SetViewport(viewport.Value);
if (clear)
cmd.ClearRenderTarget(true, true, Color.clear);
cmd.DrawMesh(fullscreenTriangle, Matrix4x4.identity, propertySheet.material, 0, pass, propertySheet.properties);
}
///
/// Does a copy of source to destination using the builtin blit command.
///
/// The command buffer to use
/// The source render target
/// The destination render target
public static void BuiltinBlit(this CommandBuffer cmd, Rendering.RenderTargetIdentifier source, RenderTargetIdentifier destination)
{
#if UNITY_2018_2_OR_NEWER
cmd.SetRenderTarget(destination, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
destination = BuiltinRenderTextureType.CurrentActive;
#endif
cmd.Blit(source, destination);
}
///
/// Blits a fullscreen quad using the builtin blit command and a given material.
///
/// The command buffer to use
/// The source render target
/// The destination render target
/// The material to use for the blit
/// The pass from the material to use
public static void BuiltinBlit(this CommandBuffer cmd, Rendering.RenderTargetIdentifier source, RenderTargetIdentifier destination, Material mat, int pass = 0)
{
#if UNITY_2018_2_OR_NEWER
cmd.SetRenderTarget(destination, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
destination = BuiltinRenderTextureType.CurrentActive;
#endif
cmd.Blit(source, destination, mat, pass);
}
// Fast basic copy texture if available, falls back to blit copy if not
// Assumes that both textures have the exact same type and format
///
/// Copies the content of a texture into the other. Both textures must have the same size
/// and format or this method will fail.
///
/// The command buffer to use
/// The source render target
/// The destination render target
///
/// If the CopyTexture command isn't supported on the target platform it will revert to a
/// fullscreen blit command instead.
///
public static void CopyTexture(CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier destination)
{
if (SystemInfo.copyTextureSupport > CopyTextureSupport.None)
{
cmd.CopyTexture(source, destination);
return;
}
cmd.BlitFullscreenTriangle(source, destination);
}
// TODO: Generalize the GetTemporaryRT and Blit commands in order to support
// RT Arrays for Stereo Instancing/MultiView
#endregion
#region Unity specifics & misc methods
///
/// Returns true if a scriptable render pipeline is currently in use, false
/// otherwise.
///
public static bool scriptableRenderPipelineActive
{
#if UNITY_2019_3_OR_NEWER
get { return GraphicsSettings.currentRenderPipeline != null; }
#else
get { return GraphicsSettings.renderPipelineAsset != null; }
#endif
}
///
/// Returns true if deferred shading is supported on the target platform,
/// false otherwise.
///
public static bool supportsDeferredShading
{
get { return scriptableRenderPipelineActive || GraphicsSettings.GetShaderMode(BuiltinShaderType.DeferredShading) != BuiltinShaderMode.Disabled; }
}
///
/// Returns true if is supported on the
/// target platform, false otherwise.
///
public static bool supportsDepthNormals
{
get { return scriptableRenderPipelineActive || GraphicsSettings.GetShaderMode(BuiltinShaderType.DepthNormals) != BuiltinShaderMode.Disabled; }
}
#if UNITY_EDITOR
///
/// Returns true if single-pass stereo rendering is selected, false otherwise.
///
///
/// This property only works in the editor.
///
public static bool isSinglePassStereoSelected
{
get
{
#if (ENABLE_VR_MODULE && ENABLE_VR) && !UNITY_2020_1_OR_NEWER
return UnityEditorInternal.VR.VREditor.GetVREnabledOnTargetGroup(BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget))
&& PlayerSettings.stereoRenderingPath == UnityEditor.StereoRenderingPath.SinglePass;
#else
return false;
#endif
}
}
#endif
///
/// Returns true if single-pass stereo rendering is active, false otherwise.
///
///
/// This property only works in the editor.
///
// TODO: Check for SPSR support at runtime
public static bool isSinglePassStereoEnabled
{
get
{
#if UNITY_EDITOR
return isSinglePassStereoSelected && Application.isPlaying;
#elif !(ENABLE_VR_MODULE && ENABLE_VR)
return false;
#else
return UnityEngine.XR.XRSettings.eyeTextureDesc.vrUsage == VRTextureUsage.TwoEyes;
#endif
}
}
///
/// Returns true if VR is enabled, false otherwise.
///
public static bool isVREnabled
{
get
{
#if (ENABLE_VR_MODULE && ENABLE_VR) && UNITY_EDITOR && !UNITY_2020_1_OR_NEWER
return UnityEditorInternal.VR.VREditor.GetVREnabledOnTargetGroup(BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget));
#elif UNITY_XBOXONE || !(ENABLE_VR_MODULE && ENABLE_VR)
return false;
#else
return UnityEngine.XR.XRSettings.enabled;
#endif
}
}
///
/// Returns true if the target platform is Android and the selected API is OpenGL,
/// false otherwise.
///
public static bool isAndroidOpenGL
{
get { return Application.platform == RuntimePlatform.Android && SystemInfo.graphicsDeviceType != GraphicsDeviceType.Vulkan; }
}
///
/// Gets the default HDR render texture format for the current target platform.
///
public static RenderTextureFormat defaultHDRRenderTextureFormat
{
get
{
#if !UNITY_2019_3_OR_NEWER && (UNITY_ANDROID || UNITY_IPHONE || UNITY_TVOS || UNITY_EDITOR)
RenderTextureFormat format = RenderTextureFormat.RGB111110Float;
#if UNITY_EDITOR
var target = EditorUserBuildSettings.activeBuildTarget;
if (target != BuildTarget.Android && target != BuildTarget.iOS && target != BuildTarget.tvOS)
return RenderTextureFormat.DefaultHDR;
#endif // UNITY_EDITOR
if (format.IsSupported())
return format;
#endif // #if !UNITY_2019_3_OR_NEWER && (UNITY_ANDROID || UNITY_IPHONE || UNITY_TVOS || UNITY_EDITOR)
return RenderTextureFormat.DefaultHDR;
}
}
///
/// Checks if a given render texture format is a floating-point format.
///
/// The format to test
/// true if the format is floating-point, false otherwise
public static bool isFloatingPointFormat(RenderTextureFormat format)
{
return format == RenderTextureFormat.DefaultHDR || format == RenderTextureFormat.ARGBHalf || format == RenderTextureFormat.ARGBFloat ||
format == RenderTextureFormat.RGFloat || format == RenderTextureFormat.RGHalf ||
format == RenderTextureFormat.RFloat || format == RenderTextureFormat.RHalf ||
format == RenderTextureFormat.RGB111110Float;
}
///
/// Checks if a given render texture format has an alpha channel.
///
/// The format to test
/// true if the format has an alpha channel, false otherwise
internal static bool hasAlpha(RenderTextureFormat format)
{
UnityEngine.Experimental.Rendering.GraphicsFormat gformat = UnityEngine.Experimental.Rendering.GraphicsFormatUtility.GetGraphicsFormat(format, RenderTextureReadWrite.Default);
return UnityEngine.Experimental.Rendering.GraphicsFormatUtility.HasAlphaChannel(gformat);
}
///
/// Properly destroys a given Unity object.
///
/// The object to destroy
public static void Destroy(UnityObject obj)
{
if (obj != null)
{
#if UNITY_EDITOR
if (Application.isPlaying)
UnityObject.Destroy(obj);
else
UnityObject.DestroyImmediate(obj);
#else
UnityObject.Destroy(obj);
#endif
}
}
///
/// Returns true if the current color space setting is set to Linear,
/// false otherwise.
///
public static bool isLinearColorSpace
{
get { return QualitySettings.activeColorSpace == ColorSpace.Linear; }
}
///
/// Checks if resolved depth is available on the current target platform.
///
/// A rendering camera
/// true if resolved depth is available, false otherwise
public static bool IsResolvedDepthAvailable(Camera camera)
{
// AFAIK resolved depth is only available on D3D11/12 via BuiltinRenderTextureType.ResolvedDepth
// TODO: Is there more proper way to determine this? What about SRPs?
var gtype = SystemInfo.graphicsDeviceType;
return camera.actualRenderingPath == RenderingPath.DeferredShading &&
(gtype == GraphicsDeviceType.Direct3D11
|| gtype == GraphicsDeviceType.Direct3D12
#if UNITY_GAMECORE
|| gtype == GraphicsDeviceType.GameCoreXboxSeries
|| gtype == GraphicsDeviceType.GameCoreXboxOne
#endif
|| gtype == GraphicsDeviceType.XboxOne
|| gtype == GraphicsDeviceType.XboxOneD3D12
);
}
///
/// Properly destroys a given profile.
///
/// The profile to destroy
/// Should we destroy all the embedded settings?
public static void DestroyProfile(PostProcessProfile profile, bool destroyEffects)
{
if (destroyEffects)
{
foreach (var effect in profile.settings)
Destroy(effect);
}
Destroy(profile);
}
///
/// Properly destroys a volume.
///
/// The volume to destroy
/// Should we destroy the attached profile?
/// Should we destroy the volume Game Object?
public static void DestroyVolume(PostProcessVolume volume, bool destroyProfile, bool destroyGameObject = false)
{
if (destroyProfile)
DestroyProfile(volume.profileRef, true);
var gameObject = volume.gameObject;
Destroy(volume);
if (destroyGameObject)
Destroy(gameObject);
}
///
/// Checks if a post-processing layer is active.
///
/// The layer to check; can be null
/// true if the layer is enabled, false otherwise
public static bool IsPostProcessingActive(PostProcessLayer layer)
{
return layer != null
&& layer.enabled;
}
///
/// Checks if temporal anti-aliasing is active on a given post-process layer.
///
/// The layer to check
/// true if temporal anti-aliasing is active, false otherwise
public static bool IsTemporalAntialiasingActive(PostProcessLayer layer)
{
return IsPostProcessingActive(layer)
&& layer.antialiasingMode == PostProcessLayer.Antialiasing.TemporalAntialiasing
&& layer.temporalAntialiasing.IsSupported();
}
///
/// Gets all scene objects in the hierarchy, including inactive objects. This method is slow
/// on large scenes and should be used with extreme caution.
///
/// The component to look for
/// A list of all components of type T in the scene
public static IEnumerable GetAllSceneObjects()
where T : Component
{
var queue = new Queue();
var roots = SceneManager.GetActiveScene().GetRootGameObjects();
foreach (var root in roots)
{
queue.Enqueue(root.transform);
var comp = root.GetComponent();
if (comp != null)
yield return comp;
}
while (queue.Count > 0)
{
foreach (Transform child in queue.Dequeue())
{
queue.Enqueue(child);
var comp = child.GetComponent();
if (comp != null)
yield return comp;
}
}
}
///
/// Creates an instance of a class if it's null.
///
/// The type to create
/// A reference to an instance to check and create if needed
public static void CreateIfNull(ref T obj)
where T : class, new()
{
if (obj == null)
obj = new T();
}
#endregion
#region Maths
///
/// Returns the base-2 exponential function of , which is 2
/// raised to the power .
///
/// Value of the exponent
/// The base-2 exponential function of
public static float Exp2(float x)
{
return Mathf.Exp(x * 0.69314718055994530941723212145818f);
}
///
/// Gets a jittered perspective projection matrix for a given camera.
///
/// The camera to build the projection matrix for
/// The jitter offset
/// A jittered projection matrix
public static Matrix4x4 GetJitteredPerspectiveProjectionMatrix(Camera camera, Vector2 offset)
{
float near = camera.nearClipPlane;
float far = camera.farClipPlane;
float vertical = Mathf.Tan(0.5f * Mathf.Deg2Rad * camera.fieldOfView) * near;
float horizontal = vertical * camera.aspect;
offset.x *= horizontal / (0.5f * camera.pixelWidth);
offset.y *= vertical / (0.5f * camera.pixelHeight);
var matrix = camera.projectionMatrix;
matrix[0, 2] += offset.x / horizontal;
matrix[1, 2] += offset.y / vertical;
return matrix;
}
///
/// Gets a jittered orthographic projection matrix for a given camera.
///
/// The camera to build the orthographic matrix for
/// The jitter offset
/// A jittered projection matrix
public static Matrix4x4 GetJitteredOrthographicProjectionMatrix(Camera camera, Vector2 offset)
{
float vertical = camera.orthographicSize;
float horizontal = vertical * camera.aspect;
offset.x *= horizontal / (0.5f * camera.pixelWidth);
offset.y *= vertical / (0.5f * camera.pixelHeight);
float left = offset.x - horizontal;
float right = offset.x + horizontal;
float top = offset.y + vertical;
float bottom = offset.y - vertical;
return Matrix4x4.Ortho(left, right, bottom, top, camera.nearClipPlane, camera.farClipPlane);
}
///
/// Gets a jittered perspective projection matrix from an original projection matrix.
///
/// The current render context
/// The original projection matrix
/// The jitter offset
/// A jittered projection matrix
public static Matrix4x4 GenerateJitteredProjectionMatrixFromOriginal(PostProcessRenderContext context, Matrix4x4 origProj, Vector2 jitter)
{
var planes = origProj.decomposeProjection;
float vertFov = Math.Abs(planes.top) + Math.Abs(planes.bottom);
float horizFov = Math.Abs(planes.left) + Math.Abs(planes.right);
var planeJitter = new Vector2(jitter.x * horizFov / context.screenWidth,
jitter.y * vertFov / context.screenHeight);
planes.left += planeJitter.x;
planes.right += planeJitter.x;
planes.top += planeJitter.y;
planes.bottom += planeJitter.y;
var jitteredMatrix = Matrix4x4.Frustum(planes);
return jitteredMatrix;
}
#endregion
#region Reflection
static IEnumerable m_AssemblyTypes;
///
/// Gets all currently available assembly types.
///
/// A list of all currently available assembly types
///
/// This method is slow and should be use with extreme caution. We recommend you use
/// instead if possible.
///
///
public static IEnumerable GetAllAssemblyTypes()
{
if (m_AssemblyTypes == null)
{
m_AssemblyTypes = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(t =>
{
// Ugly hack to handle mis-versioned dlls
var innerTypes = new Type[0];
try
{
innerTypes = t.GetTypes();
}
catch { }
return innerTypes;
});
}
return m_AssemblyTypes;
}
///
/// Gets all currently available assembly types derived from type .
///
/// The type to look for
/// A list of all currently available assembly types derived from type
public static IEnumerable GetAllTypesDerivedFrom()
{
#if UNITY_EDITOR && UNITY_2019_2_OR_NEWER
return UnityEditor.TypeCache.GetTypesDerivedFrom();
#else
return GetAllAssemblyTypes().Where(t => t.IsSubclassOf(typeof(T)));
#endif
}
///
/// Helper method to get the first attribute of type T on a given type.
///
/// The attribute type to look for
/// The type to explore
/// The attribute found
public static T GetAttribute(this Type type) where T : Attribute
{
Assert.IsTrue(type.IsDefined(typeof(T), false), "Attribute not found");
return (T)type.GetCustomAttributes(typeof(T), false)[0];
}
///
/// Returns all attributes set on a specific member.
///
/// The class type where the member is defined
/// The member type
/// An expression path to the member
/// An array of attributes
///
/// This method doesn't return inherited attributes, only explicit ones.
///
public static Attribute[] GetMemberAttributes(Expression> expr)
{
Expression body = expr;
if (body is LambdaExpression)
body = ((LambdaExpression)body).Body;
switch (body.NodeType)
{
case ExpressionType.MemberAccess:
var fi = (FieldInfo)((MemberExpression)body).Member;
return fi.GetCustomAttributes(false).Cast().ToArray();
default:
throw new InvalidOperationException();
}
}
///
/// Returns a string path from an expression. This is mostly used to retrieve serialized
/// properties without hardcoding the field path as a string and thus allowing proper
/// refactoring features.
///
/// The class type where the member is defined
/// The member type
/// An expression path fo the member
/// A string representation of the expression path
public static string GetFieldPath(Expression> expr)
{
MemberExpression me;
switch (expr.Body.NodeType)
{
case ExpressionType.MemberAccess:
me = expr.Body as MemberExpression;
break;
default:
throw new InvalidOperationException();
}
var members = new List();
while (me != null)
{
members.Add(me.Member.Name);
me = me.Expression as MemberExpression;
}
var sb = new StringBuilder();
for (int i = members.Count - 1; i >= 0; i--)
{
sb.Append(members[i]);
if (i > 0) sb.Append('.');
}
return sb.ToString();
}
#endregion
}
}