300 lines
13 KiB
C#
300 lines
13 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEditor;
|
|
using UnityEngine.Serialization;
|
|
|
|
namespace UnityEngine.Rendering.Universal
|
|
{
|
|
/// <summary>
|
|
/// Universal Render Pipeline's Global Settings.
|
|
/// Global settings are unique per Render Pipeline type. In URP, Global Settings contain:
|
|
/// - light layer names
|
|
/// </summary>
|
|
[URPHelpURL("urp-global-settings")]
|
|
partial class UniversalRenderPipelineGlobalSettings : RenderPipelineGlobalSettings, ISerializationCallbackReceiver
|
|
{
|
|
#region Version system
|
|
|
|
private const int k_LastVersion = 3;
|
|
|
|
#pragma warning disable CS0414
|
|
[SerializeField][FormerlySerializedAs("k_AssetVersion")]
|
|
int m_AssetVersion = k_LastVersion;
|
|
#pragma warning restore CS0414
|
|
|
|
public void OnBeforeSerialize()
|
|
{
|
|
}
|
|
|
|
public void OnAfterDeserialize()
|
|
{
|
|
#if UNITY_EDITOR
|
|
if (m_AssetVersion != k_LastVersion)
|
|
{
|
|
EditorApplication.delayCall += () => UpgradeAsset(this.GetInstanceID());
|
|
}
|
|
#endif
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
static void UpgradeAsset(int assetInstanceID)
|
|
{
|
|
if (EditorUtility.InstanceIDToObject(assetInstanceID) is not UniversalRenderPipelineGlobalSettings asset)
|
|
return;
|
|
|
|
int assetVersionBeforeUpgrade = asset.m_AssetVersion;
|
|
|
|
if (asset.m_AssetVersion < 2)
|
|
{
|
|
#pragma warning disable 618 // Obsolete warning
|
|
// Renamed supportRuntimeDebugDisplay => stripDebugVariants, which results in inverted logic
|
|
asset.m_StripDebugVariants = !asset.supportRuntimeDebugDisplay;
|
|
asset.m_AssetVersion = 2;
|
|
#pragma warning restore 618 // Obsolete warning
|
|
|
|
// For old test projects lets keep post processing stripping enabled, as huge chance they did not used runtime profile creating
|
|
#if UNITY_INCLUDE_TESTS
|
|
asset.m_StripUnusedPostProcessingVariants = true;
|
|
#endif
|
|
}
|
|
|
|
if (asset.m_AssetVersion < 3)
|
|
{
|
|
int index = 0;
|
|
asset.m_RenderingLayerNames = new string[8];
|
|
#pragma warning disable 618 // Obsolete warning
|
|
asset.m_RenderingLayerNames[index++] = asset.lightLayerName0;
|
|
asset.m_RenderingLayerNames[index++] = asset.lightLayerName1;
|
|
asset.m_RenderingLayerNames[index++] = asset.lightLayerName2;
|
|
asset.m_RenderingLayerNames[index++] = asset.lightLayerName3;
|
|
asset.m_RenderingLayerNames[index++] = asset.lightLayerName4;
|
|
asset.m_RenderingLayerNames[index++] = asset.lightLayerName5;
|
|
asset.m_RenderingLayerNames[index++] = asset.lightLayerName6;
|
|
asset.m_RenderingLayerNames[index++] = asset.lightLayerName7;
|
|
#pragma warning restore 618 // Obsolete warning
|
|
asset.m_AssetVersion = 3;
|
|
asset.UpdateRenderingLayerNames();
|
|
}
|
|
|
|
// If the asset version has changed, means that a migration step has been executed
|
|
if (assetVersionBeforeUpgrade != asset.m_AssetVersion)
|
|
EditorUtility.SetDirty(asset);
|
|
}
|
|
|
|
#endif
|
|
#endregion
|
|
|
|
private static UniversalRenderPipelineGlobalSettings cachedInstance = null;
|
|
/// <summary>
|
|
/// Active URP Global Settings asset. If the value is null then no UniversalRenderPipelineGlobalSettings has been registered to the Graphics Settings with the UniversalRenderPipeline.
|
|
/// </summary>
|
|
public static UniversalRenderPipelineGlobalSettings instance
|
|
{
|
|
get
|
|
{
|
|
#if !UNITY_EDITOR
|
|
// The URP Global Settings could have been changed by script, undo/redo (case 1342987), or file update - file versioning, let us make sure we display the correct one
|
|
// In a Player, we do not need to worry about those changes as we only support loading one
|
|
if (cachedInstance == null)
|
|
#endif
|
|
cachedInstance = GraphicsSettings.GetSettingsForRenderPipeline<UniversalRenderPipeline>() as UniversalRenderPipelineGlobalSettings;
|
|
return cachedInstance;
|
|
}
|
|
}
|
|
|
|
static internal void UpdateGraphicsSettings(UniversalRenderPipelineGlobalSettings newSettings)
|
|
{
|
|
if (newSettings == cachedInstance)
|
|
return;
|
|
if (newSettings != null)
|
|
GraphicsSettings.RegisterRenderPipelineSettings<UniversalRenderPipeline>(newSettings as RenderPipelineGlobalSettings);
|
|
else
|
|
GraphicsSettings.UnregisterRenderPipelineSettings<UniversalRenderPipeline>();
|
|
cachedInstance = newSettings;
|
|
}
|
|
|
|
/// <summary>Default name when creating an URP Global Settings asset.</summary>
|
|
public static readonly string defaultAssetName = "UniversalRenderPipelineGlobalSettings";
|
|
|
|
#if UNITY_EDITOR
|
|
//Making sure there is at least one UniversalRenderPipelineGlobalSettings instance in the project
|
|
internal static UniversalRenderPipelineGlobalSettings Ensure(string folderPath = "", bool canCreateNewAsset = true)
|
|
{
|
|
if (instance == null)
|
|
{
|
|
UniversalRenderPipelineGlobalSettings assetCreated = null;
|
|
string path = $"Assets/{folderPath}/{defaultAssetName}.asset";
|
|
assetCreated = AssetDatabase.LoadAssetAtPath<UniversalRenderPipelineGlobalSettings>(path);
|
|
if (assetCreated == null)
|
|
{
|
|
var guidGlobalSettingsAssets = AssetDatabase.FindAssets("t:UniversalRenderPipelineGlobalSettings");
|
|
//If we could not find the asset at the default path, find the first one
|
|
if (guidGlobalSettingsAssets.Length > 0)
|
|
{
|
|
var curGUID = guidGlobalSettingsAssets[0];
|
|
path = AssetDatabase.GUIDToAssetPath(curGUID);
|
|
assetCreated = AssetDatabase.LoadAssetAtPath<UniversalRenderPipelineGlobalSettings>(path);
|
|
}
|
|
else if (canCreateNewAsset)// or create one altogether
|
|
{
|
|
if (!AssetDatabase.IsValidFolder("Assets/" + folderPath))
|
|
AssetDatabase.CreateFolder("Assets", folderPath);
|
|
assetCreated = Create(path);
|
|
|
|
// TODO: Reenable after next urp template is published
|
|
//Debug.LogWarning("No URP Global Settings Asset is assigned. One will be created for you. If you want to modify it, go to Project Settings > Graphics > URP Settings.");
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("If you are building a Player, make sure to save an URP Global Settings asset by opening the project in the Editor first.");
|
|
return null;
|
|
}
|
|
}
|
|
Debug.Assert(assetCreated, "Could not create URP's Global Settings - URP may not work correctly - Open Project Settings > Graphics > URP Settings for additional help.");
|
|
UpdateGraphicsSettings(assetCreated);
|
|
}
|
|
|
|
if (instance != null && instance.m_AssetVersion != k_LastVersion)
|
|
UpgradeAsset(instance.GetInstanceID());
|
|
|
|
return instance;
|
|
}
|
|
|
|
internal static UniversalRenderPipelineGlobalSettings Create(string path, UniversalRenderPipelineGlobalSettings src = null)
|
|
{
|
|
UniversalRenderPipelineGlobalSettings assetCreated = null;
|
|
|
|
// make sure the asset does not already exists
|
|
assetCreated = AssetDatabase.LoadAssetAtPath<UniversalRenderPipelineGlobalSettings>(path);
|
|
if (assetCreated == null)
|
|
{
|
|
assetCreated = ScriptableObject.CreateInstance<UniversalRenderPipelineGlobalSettings>();
|
|
if (assetCreated != null)
|
|
{
|
|
assetCreated.name = System.IO.Path.GetFileName(path);
|
|
}
|
|
AssetDatabase.CreateAsset(assetCreated, path);
|
|
Debug.Assert(assetCreated);
|
|
}
|
|
|
|
if (assetCreated)
|
|
{
|
|
if (src != null)
|
|
{
|
|
System.Array.Copy(src.m_RenderingLayerNames, assetCreated.m_RenderingLayerNames, src.m_RenderingLayerNames.Length);
|
|
}
|
|
|
|
AssetDatabase.SaveAssets();
|
|
AssetDatabase.Refresh();
|
|
}
|
|
|
|
return assetCreated;
|
|
}
|
|
|
|
#endif
|
|
|
|
void Reset()
|
|
{
|
|
UpdateRenderingLayerNames();
|
|
}
|
|
|
|
[SerializeField]
|
|
string[] m_RenderingLayerNames = new string[] { "Default" };
|
|
string[] renderingLayerNames
|
|
{
|
|
get
|
|
{
|
|
if (m_RenderingLayerNames == null)
|
|
UpdateRenderingLayerNames();
|
|
return m_RenderingLayerNames;
|
|
}
|
|
}
|
|
[System.NonSerialized]
|
|
string[] m_PrefixedRenderingLayerNames;
|
|
string[] prefixedRenderingLayerNames
|
|
{
|
|
get
|
|
{
|
|
if (m_PrefixedRenderingLayerNames == null)
|
|
UpdateRenderingLayerNames();
|
|
return m_PrefixedRenderingLayerNames;
|
|
}
|
|
}
|
|
/// <summary>Names used for display of rendering layer masks.</summary>
|
|
public string[] renderingLayerMaskNames => renderingLayerNames;
|
|
/// <summary>Names used for display of rendering layer masks with a prefix.</summary>
|
|
public string[] prefixedRenderingLayerMaskNames => prefixedRenderingLayerNames;
|
|
|
|
[SerializeField]
|
|
uint m_ValidRenderingLayers;
|
|
/// <summary>Valid rendering layers that can be used by graphics. </summary>
|
|
public uint validRenderingLayers => m_ValidRenderingLayers;
|
|
|
|
/// <summary>Regenerate Rendering Layer names and their prefixed versions.</summary>
|
|
internal void UpdateRenderingLayerNames()
|
|
{
|
|
// Update prefixed
|
|
if (m_PrefixedRenderingLayerNames == null)
|
|
m_PrefixedRenderingLayerNames = new string[32];
|
|
for (int i = 0; i < m_PrefixedRenderingLayerNames.Length; ++i)
|
|
{
|
|
uint renderingLayer = (uint)(1 << i);
|
|
|
|
m_ValidRenderingLayers = i < m_RenderingLayerNames.Length ? (m_ValidRenderingLayers | renderingLayer) : (m_ValidRenderingLayers & ~renderingLayer);
|
|
m_PrefixedRenderingLayerNames[i] = i < m_RenderingLayerNames.Length ? m_RenderingLayerNames[i] : $"Unused Layer {i}";
|
|
}
|
|
|
|
// Update decals
|
|
DecalProjector.UpdateAllDecalProperties();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Names used for display of light layers with Layer's index as prefix.
|
|
/// For example: "0: Light Layer Default"
|
|
/// </summary>
|
|
[Obsolete("This is obsolete, please use prefixedRenderingLayerMaskNames instead.", true)]
|
|
public string[] prefixedLightLayerNames => new string[0];
|
|
|
|
|
|
#region Light Layer Names [3D]
|
|
|
|
/// <summary>Name for light layer 0.</summary>
|
|
[Obsolete("This is obsolete, please use renderingLayerMaskNames instead.", false)]
|
|
public string lightLayerName0;
|
|
/// <summary>Name for light layer 1.</summary>
|
|
[Obsolete("This is obsolete, please use renderingLayerMaskNames instead.", false)]
|
|
public string lightLayerName1;
|
|
/// <summary>Name for light layer 2.</summary>
|
|
[Obsolete("This is obsolete, please use renderingLayerMaskNames instead.", false)]
|
|
public string lightLayerName2;
|
|
/// <summary>Name for light layer 3.</summary>
|
|
[Obsolete("This is obsolete, please use renderingLayerMaskNames instead.", false)]
|
|
public string lightLayerName3;
|
|
/// <summary>Name for light layer 4.</summary>
|
|
[Obsolete("This is obsolete, please use renderingLayerMaskNames instead.", false)]
|
|
public string lightLayerName4;
|
|
/// <summary>Name for light layer 5.</summary>
|
|
[Obsolete("This is obsolete, please use renderingLayerMaskNames instead.", false)]
|
|
public string lightLayerName5;
|
|
/// <summary>Name for light layer 6.</summary>
|
|
[Obsolete("This is obsolete, please use renderingLayerMaskNames instead.", false)]
|
|
public string lightLayerName6;
|
|
/// <summary>Name for light layer 7.</summary>
|
|
[Obsolete("This is obsolete, please use renderingLayerNames instead.", false)]
|
|
public string lightLayerName7;
|
|
|
|
/// <summary>
|
|
/// Names used for display of light layers.
|
|
/// </summary>
|
|
[Obsolete("This is obsolete, please use renderingLayerMaskNames instead.", false)]
|
|
public string[] lightLayerNames => new string[0];
|
|
|
|
internal void ResetRenderingLayerNames()
|
|
{
|
|
m_RenderingLayerNames = new string[] { "Default"};
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|