UniversalRenderPipelineAsset.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. using System;
  2. using UnityEngine.Scripting.APIUpdating;
  3. #if UNITY_EDITOR
  4. using UnityEditor;
  5. using UnityEditor.ProjectWindowCallback;
  6. using System.IO;
  7. using UnityEditorInternal;
  8. #endif
  9. using System.ComponentModel;
  10. using System.Linq;
  11. namespace UnityEngine.Rendering.LWRP
  12. {
  13. [Obsolete("LWRP -> Universal (UnityUpgradable) -> UnityEngine.Rendering.Universal.UniversalRenderPipelineAsset", true)]
  14. public class LightweightRenderPipelineAsset
  15. {
  16. }
  17. }
  18. namespace UnityEngine.Rendering.Universal
  19. {
  20. [MovedFrom("UnityEngine.Rendering.LWRP")] public enum ShadowQuality
  21. {
  22. Disabled,
  23. HardShadows,
  24. SoftShadows,
  25. }
  26. [MovedFrom("UnityEngine.Rendering.LWRP")] public enum ShadowResolution
  27. {
  28. _256 = 256,
  29. _512 = 512,
  30. _1024 = 1024,
  31. _2048 = 2048,
  32. _4096 = 4096
  33. }
  34. [MovedFrom("UnityEngine.Rendering.LWRP")] public enum MsaaQuality
  35. {
  36. Disabled = 1,
  37. _2x = 2,
  38. _4x = 4,
  39. _8x = 8
  40. }
  41. [MovedFrom("UnityEngine.Rendering.LWRP")] public enum Downsampling
  42. {
  43. None,
  44. _2xBilinear,
  45. _4xBox,
  46. _4xBilinear
  47. }
  48. internal enum DefaultMaterialType
  49. {
  50. Standard,
  51. Particle,
  52. Terrain,
  53. Sprite,
  54. UnityBuiltinDefault
  55. }
  56. [MovedFrom("UnityEngine.Rendering.LWRP")] public enum LightRenderingMode
  57. {
  58. Disabled = 0,
  59. PerVertex = 2,
  60. PerPixel = 1,
  61. }
  62. [MovedFrom("UnityEngine.Rendering.LWRP")] public enum ShaderVariantLogLevel
  63. {
  64. Disabled,
  65. OnlyUniversalRPShaders,
  66. AllShaders,
  67. }
  68. [Obsolete("PipelineDebugLevel is unused and has no effect.", false)]
  69. public enum PipelineDebugLevel
  70. {
  71. Disabled,
  72. Profiling,
  73. }
  74. [MovedFrom("UnityEngine.Rendering.LWRP")] public enum RendererType
  75. {
  76. Custom,
  77. ForwardRenderer,
  78. _2DRenderer,
  79. }
  80. public enum ColorGradingMode
  81. {
  82. LowDynamicRange,
  83. HighDynamicRange
  84. }
  85. /// <summary>
  86. /// Defines if Unity discards or stores the render targets of the DrawObjects Passes. Selecting the Store option significantly increases the memory bandwidth on mobile and tile-based GPUs.
  87. /// </summary>
  88. public enum StoreActionsOptimization
  89. {
  90. /// <summary>Unity uses the Discard option by default, and falls back to the Store option if it detects any injected Passes.</summary>
  91. Auto,
  92. /// <summary>Unity discards the render targets of render Passes that are not reused later (lower memory bandwidth).</summary>
  93. Discard,
  94. /// <summary>Unity stores all render targets of each Pass (higher memory bandwidth).</summary>
  95. Store
  96. }
  97. /// <summary>
  98. /// Defines the update frequency for the Volume Framework.
  99. /// </summary>
  100. public enum VolumeFrameworkUpdateMode
  101. {
  102. [InspectorName("Every Frame")]
  103. EveryFrame = 0,
  104. [InspectorName("Via Scripting")]
  105. ViaScripting = 1,
  106. [InspectorName("Use Pipeline Settings")]
  107. UsePipelineSettings = 2,
  108. }
  109. [ExcludeFromPreset]
  110. public partial class UniversalRenderPipelineAsset : RenderPipelineAsset, ISerializationCallbackReceiver
  111. {
  112. Shader m_DefaultShader;
  113. ScriptableRenderer[] m_Renderers = new ScriptableRenderer[1];
  114. // Default values set when a new UniversalRenderPipeline asset is created
  115. [SerializeField] int k_AssetVersion = 5;
  116. [SerializeField] int k_AssetPreviousVersion = 5;
  117. // Deprecated settings for upgrading sakes
  118. [SerializeField] RendererType m_RendererType = RendererType.ForwardRenderer;
  119. [EditorBrowsable(EditorBrowsableState.Never)]
  120. [SerializeField] internal ScriptableRendererData m_RendererData = null;
  121. // Renderer settings
  122. [SerializeField] internal ScriptableRendererData[] m_RendererDataList = new ScriptableRendererData[1];
  123. [SerializeField] internal int m_DefaultRendererIndex = 0;
  124. // General settings
  125. [SerializeField] bool m_RequireDepthTexture = false;
  126. [SerializeField] bool m_RequireOpaqueTexture = false;
  127. [SerializeField] Downsampling m_OpaqueDownsampling = Downsampling._2xBilinear;
  128. [SerializeField] bool m_SupportsTerrainHoles = true;
  129. [SerializeField] StoreActionsOptimization m_StoreActionsOptimization = StoreActionsOptimization.Auto;
  130. // Quality settings
  131. [SerializeField] bool m_SupportsHDR = true;
  132. [SerializeField] MsaaQuality m_MSAA = MsaaQuality.Disabled;
  133. [SerializeField] float m_RenderScale = 1.0f;
  134. // TODO: Shader Quality Tiers
  135. // Main directional light Settings
  136. [SerializeField] LightRenderingMode m_MainLightRenderingMode = LightRenderingMode.PerPixel;
  137. [SerializeField] bool m_MainLightShadowsSupported = true;
  138. [SerializeField] ShadowResolution m_MainLightShadowmapResolution = ShadowResolution._2048;
  139. // Additional lights settings
  140. [SerializeField] LightRenderingMode m_AdditionalLightsRenderingMode = LightRenderingMode.PerPixel;
  141. [SerializeField] int m_AdditionalLightsPerObjectLimit = 4;
  142. [SerializeField] bool m_AdditionalLightShadowsSupported = false;
  143. [SerializeField] ShadowResolution m_AdditionalLightsShadowmapResolution = ShadowResolution._512;
  144. // Shadows Settings
  145. [SerializeField] float m_ShadowDistance = 50.0f;
  146. [SerializeField] int m_ShadowCascadeCount = 1;
  147. [SerializeField] float m_Cascade2Split = 0.25f;
  148. [SerializeField] Vector2 m_Cascade3Split = new Vector2(0.1f, 0.3f);
  149. [SerializeField] Vector3 m_Cascade4Split = new Vector3(0.067f, 0.2f, 0.467f);
  150. [SerializeField] float m_ShadowDepthBias = 1.0f;
  151. [SerializeField] float m_ShadowNormalBias = 1.0f;
  152. [SerializeField] bool m_SoftShadowsSupported = false;
  153. // Advanced settings
  154. [SerializeField] bool m_UseSRPBatcher = true;
  155. [SerializeField] bool m_SupportsDynamicBatching = false;
  156. [SerializeField] bool m_MixedLightingSupported = true;
  157. [SerializeField][Obsolete] PipelineDebugLevel m_DebugLevel;
  158. // Adaptive performance settings
  159. [SerializeField] bool m_UseAdaptivePerformance = true;
  160. // Post-processing settings
  161. [SerializeField] ColorGradingMode m_ColorGradingMode = ColorGradingMode.LowDynamicRange;
  162. [SerializeField] int m_ColorGradingLutSize = 32;
  163. // Deprecated settings
  164. [SerializeField] ShadowQuality m_ShadowType = ShadowQuality.HardShadows;
  165. [SerializeField] bool m_LocalShadowsSupported = false;
  166. [SerializeField] ShadowResolution m_LocalShadowsAtlasResolution = ShadowResolution._256;
  167. [SerializeField] int m_MaxPixelLights = 0;
  168. [SerializeField] ShadowResolution m_ShadowAtlasResolution = ShadowResolution._256;
  169. [SerializeField] ShaderVariantLogLevel m_ShaderVariantLogLevel = ShaderVariantLogLevel.Disabled;
  170. [SerializeField] VolumeFrameworkUpdateMode m_VolumeFrameworkUpdateMode = VolumeFrameworkUpdateMode.EveryFrame;
  171. // Note: A lut size of 16^3 is barely usable with the HDR grading mode. 32 should be the
  172. // minimum, the lut being encoded in log. Lower sizes would work better with an additional
  173. // 1D shaper lut but for now we'll keep it simple.
  174. public const int k_MinLutSize = 16;
  175. public const int k_MaxLutSize = 65;
  176. internal const int k_ShadowCascadeMinCount = 1;
  177. internal const int k_ShadowCascadeMaxCount = 4;
  178. #if UNITY_EDITOR
  179. [NonSerialized]
  180. internal UniversalRenderPipelineEditorResources m_EditorResourcesAsset;
  181. public static readonly string packagePath = "Packages/com.unity.render-pipelines.universal";
  182. public static readonly string editorResourcesGUID = "a3d8d823eedde654bb4c11a1cfaf1abb";
  183. public static UniversalRenderPipelineAsset Create(ScriptableRendererData rendererData = null)
  184. {
  185. // Create Universal RP Asset
  186. var instance = CreateInstance<UniversalRenderPipelineAsset>();
  187. if (rendererData != null)
  188. instance.m_RendererDataList[0] = rendererData;
  189. else
  190. instance.m_RendererDataList[0] = CreateInstance<ForwardRendererData>();
  191. // Initialize default Renderer
  192. instance.m_EditorResourcesAsset = instance.editorResources;
  193. return instance;
  194. }
  195. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1812")]
  196. internal class CreateUniversalPipelineAsset : EndNameEditAction
  197. {
  198. public override void Action(int instanceId, string pathName, string resourceFile)
  199. {
  200. //Create asset
  201. AssetDatabase.CreateAsset(Create(CreateRendererAsset(pathName, RendererType.ForwardRenderer)), pathName);
  202. }
  203. }
  204. [MenuItem("Assets/Create/Rendering/Universal Render Pipeline/Pipeline Asset (Forward Renderer)", priority = CoreUtils.assetCreateMenuPriority1)]
  205. static void CreateUniversalPipeline()
  206. {
  207. ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, CreateInstance<CreateUniversalPipelineAsset>(),
  208. "UniversalRenderPipelineAsset.asset", null, null);
  209. }
  210. static ScriptableRendererData CreateRendererAsset(string path, RendererType type, bool relativePath = true)
  211. {
  212. ScriptableRendererData data = CreateRendererData(type);
  213. string dataPath;
  214. if (relativePath)
  215. dataPath =
  216. $"{Path.Combine(Path.GetDirectoryName(path), Path.GetFileNameWithoutExtension(path))}_Renderer{Path.GetExtension(path)}";
  217. else
  218. dataPath = path;
  219. AssetDatabase.CreateAsset(data, dataPath);
  220. return data;
  221. }
  222. static ScriptableRendererData CreateRendererData(RendererType type)
  223. {
  224. switch (type)
  225. {
  226. case RendererType.ForwardRenderer:
  227. return CreateInstance<ForwardRendererData>();
  228. // 2D renderer is experimental
  229. case RendererType._2DRenderer:
  230. return CreateInstance<Experimental.Rendering.Universal.Renderer2DData>();
  231. // Forward Renderer is the fallback renderer that works on all platforms
  232. default:
  233. return CreateInstance<ForwardRendererData>();
  234. }
  235. }
  236. //[MenuItem("Assets/Create/Rendering/Universal Pipeline Editor Resources", priority = CoreUtils.assetCreateMenuPriority1)]
  237. static void CreateUniversalPipelineEditorResources()
  238. {
  239. var instance = CreateInstance<UniversalRenderPipelineEditorResources>();
  240. ResourceReloader.ReloadAllNullIn(instance, packagePath);
  241. AssetDatabase.CreateAsset(instance, string.Format("Assets/{0}.asset", typeof(UniversalRenderPipelineEditorResources).Name));
  242. }
  243. UniversalRenderPipelineEditorResources editorResources
  244. {
  245. get
  246. {
  247. if (m_EditorResourcesAsset != null && !m_EditorResourcesAsset.Equals(null))
  248. return m_EditorResourcesAsset;
  249. string resourcePath = AssetDatabase.GUIDToAssetPath(editorResourcesGUID);
  250. var objs = InternalEditorUtility.LoadSerializedFileAndForget(resourcePath);
  251. m_EditorResourcesAsset = objs != null && objs.Length > 0 ? objs.First() as UniversalRenderPipelineEditorResources : null;
  252. return m_EditorResourcesAsset;
  253. }
  254. }
  255. #endif
  256. public ScriptableRendererData LoadBuiltinRendererData(RendererType type = RendererType.ForwardRenderer)
  257. {
  258. #if UNITY_EDITOR
  259. EditorUtility.SetDirty(this);
  260. return m_RendererDataList[0] =
  261. CreateRendererAsset("Assets/ForwardRenderer.asset", type, false);
  262. #else
  263. m_RendererDataList[0] = null;
  264. return m_RendererDataList[0];
  265. #endif
  266. }
  267. protected override RenderPipeline CreatePipeline()
  268. {
  269. if (m_RendererDataList == null)
  270. m_RendererDataList = new ScriptableRendererData[1];
  271. // If no default data we can't create pipeline instance
  272. if (m_RendererDataList[m_DefaultRendererIndex] == null)
  273. {
  274. // If previous version and current version are miss-matched then we are waiting for the upgrader to kick in
  275. if(k_AssetPreviousVersion != k_AssetVersion)
  276. return null;
  277. Debug.LogError(
  278. $"Default Renderer is missing, make sure there is a Renderer assigned as the default on the current Universal RP asset:{UniversalRenderPipeline.asset.name}",
  279. this);
  280. return null;
  281. }
  282. CreateRenderers();
  283. return new UniversalRenderPipeline(this);
  284. }
  285. void DestroyRenderers()
  286. {
  287. if (m_Renderers == null)
  288. return;
  289. for (int i = 0; i < m_Renderers.Length; i++)
  290. DestroyRenderer(ref m_Renderers[i]);
  291. }
  292. void DestroyRenderer(ref ScriptableRenderer renderer)
  293. {
  294. if (renderer != null)
  295. {
  296. renderer.Dispose();
  297. renderer = null;
  298. }
  299. }
  300. protected override void OnValidate()
  301. {
  302. DestroyRenderers();
  303. // This will call RenderPipelineManager.CleanupRenderPipeline that in turn disposes the render pipeline instance and
  304. // assign pipeline asset reference to null
  305. base.OnValidate();
  306. }
  307. protected override void OnDisable()
  308. {
  309. DestroyRenderers();
  310. // This will call RenderPipelineManager.CleanupRenderPipeline that in turn disposes the render pipeline instance and
  311. // assign pipeline asset reference to null
  312. base.OnDisable();
  313. }
  314. void CreateRenderers()
  315. {
  316. DestroyRenderers();
  317. if (m_Renderers == null || m_Renderers.Length != m_RendererDataList.Length)
  318. m_Renderers = new ScriptableRenderer[m_RendererDataList.Length];
  319. for (int i = 0; i < m_RendererDataList.Length; ++i)
  320. {
  321. if (m_RendererDataList[i] != null)
  322. m_Renderers[i] = m_RendererDataList[i].InternalCreateRenderer();
  323. }
  324. }
  325. Material GetMaterial(DefaultMaterialType materialType)
  326. {
  327. #if UNITY_EDITOR
  328. if (scriptableRendererData == null || editorResources == null)
  329. return null;
  330. var material = scriptableRendererData.GetDefaultMaterial(materialType);
  331. if (material != null)
  332. return material;
  333. switch (materialType)
  334. {
  335. case DefaultMaterialType.Standard:
  336. return editorResources.materials.lit;
  337. case DefaultMaterialType.Particle:
  338. return editorResources.materials.particleLit;
  339. case DefaultMaterialType.Terrain:
  340. return editorResources.materials.terrainLit;
  341. // Unity Builtin Default
  342. default:
  343. return null;
  344. }
  345. #else
  346. return null;
  347. #endif
  348. }
  349. /// <summary>
  350. /// Returns the default renderer being used by this pipeline.
  351. /// </summary>
  352. public ScriptableRenderer scriptableRenderer
  353. {
  354. get
  355. {
  356. if (m_RendererDataList?.Length > m_DefaultRendererIndex && m_RendererDataList[m_DefaultRendererIndex] == null)
  357. {
  358. Debug.LogError("Default renderer is missing from the current Pipeline Asset.", this);
  359. return null;
  360. }
  361. if (scriptableRendererData.isInvalidated || m_Renderers[m_DefaultRendererIndex] == null)
  362. {
  363. DestroyRenderer(ref m_Renderers[m_DefaultRendererIndex]);
  364. m_Renderers[m_DefaultRendererIndex] = scriptableRendererData.InternalCreateRenderer();
  365. }
  366. return m_Renderers[m_DefaultRendererIndex];
  367. }
  368. }
  369. /// <summary>
  370. /// Returns a renderer from the current pipeline asset
  371. /// </summary>
  372. /// <param name="index">Index to the renderer. If invalid index is passed, the default renderer is returned instead.</param>
  373. /// <returns></returns>
  374. public ScriptableRenderer GetRenderer(int index)
  375. {
  376. if (index == -1)
  377. index = m_DefaultRendererIndex;
  378. if (index >= m_RendererDataList.Length || index < 0 || m_RendererDataList[index] == null)
  379. {
  380. Debug.LogWarning(
  381. $"Renderer at index {index.ToString()} is missing, falling back to Default Renderer {m_RendererDataList[m_DefaultRendererIndex].name}",
  382. this);
  383. index = m_DefaultRendererIndex;
  384. }
  385. // RendererData list differs from RendererList. Create RendererList.
  386. if (m_Renderers == null || m_Renderers.Length < m_RendererDataList.Length)
  387. CreateRenderers();
  388. // This renderer data is outdated or invalid, we recreate the renderer
  389. // so we construct all render passes with the updated data
  390. if (m_RendererDataList[index].isInvalidated || m_Renderers[index] == null)
  391. {
  392. DestroyRenderer(ref m_Renderers[index]);
  393. m_Renderers[index] = m_RendererDataList[index].InternalCreateRenderer();
  394. }
  395. return m_Renderers[index];
  396. }
  397. internal ScriptableRendererData scriptableRendererData
  398. {
  399. get
  400. {
  401. if (m_RendererDataList[m_DefaultRendererIndex] == null)
  402. CreatePipeline();
  403. return m_RendererDataList[m_DefaultRendererIndex];
  404. }
  405. }
  406. #if UNITY_EDITOR
  407. internal GUIContent[] rendererDisplayList
  408. {
  409. get
  410. {
  411. GUIContent[] list = new GUIContent[m_RendererDataList.Length + 1];
  412. list[0] = new GUIContent($"Default Renderer ({RendererDataDisplayName(m_RendererDataList[m_DefaultRendererIndex])})");
  413. for (var i = 1; i < list.Length; i++)
  414. {
  415. list[i] = new GUIContent($"{(i - 1).ToString()}: {RendererDataDisplayName(m_RendererDataList[i-1])}");
  416. }
  417. return list;
  418. }
  419. }
  420. string RendererDataDisplayName(ScriptableRendererData data)
  421. {
  422. if (data != null)
  423. return data.name;
  424. return "NULL (Missing RendererData)";
  425. }
  426. #endif
  427. internal int[] rendererIndexList
  428. {
  429. get
  430. {
  431. int[] list = new int[m_RendererDataList.Length + 1];
  432. for (int i = 0; i < list.Length; i++)
  433. {
  434. list[i] = i - 1;
  435. }
  436. return list;
  437. }
  438. }
  439. public bool supportsCameraDepthTexture
  440. {
  441. get { return m_RequireDepthTexture; }
  442. set { m_RequireDepthTexture = value; }
  443. }
  444. public bool supportsCameraOpaqueTexture
  445. {
  446. get { return m_RequireOpaqueTexture; }
  447. set { m_RequireOpaqueTexture = value; }
  448. }
  449. public Downsampling opaqueDownsampling
  450. {
  451. get { return m_OpaqueDownsampling; }
  452. }
  453. public bool supportsTerrainHoles
  454. {
  455. get { return m_SupportsTerrainHoles; }
  456. }
  457. /// <summary>
  458. /// Returns the active store action optimization value.
  459. /// </summary>
  460. /// <returns>Returns the active store action optimization value.</returns>
  461. public StoreActionsOptimization storeActionsOptimization
  462. {
  463. get { return m_StoreActionsOptimization; }
  464. set { m_StoreActionsOptimization = value; }
  465. }
  466. public bool supportsHDR
  467. {
  468. get { return m_SupportsHDR; }
  469. set { m_SupportsHDR = value; }
  470. }
  471. public int msaaSampleCount
  472. {
  473. get { return (int)m_MSAA; }
  474. set { m_MSAA = (MsaaQuality)value; }
  475. }
  476. public float renderScale
  477. {
  478. get { return m_RenderScale; }
  479. set { m_RenderScale = ValidateRenderScale(value); }
  480. }
  481. public LightRenderingMode mainLightRenderingMode
  482. {
  483. get { return m_MainLightRenderingMode; }
  484. }
  485. public bool supportsMainLightShadows
  486. {
  487. get { return m_MainLightShadowsSupported; }
  488. }
  489. public int mainLightShadowmapResolution
  490. {
  491. get { return (int)m_MainLightShadowmapResolution; }
  492. }
  493. public LightRenderingMode additionalLightsRenderingMode
  494. {
  495. get { return m_AdditionalLightsRenderingMode; }
  496. }
  497. public int maxAdditionalLightsCount
  498. {
  499. get { return m_AdditionalLightsPerObjectLimit; }
  500. set { m_AdditionalLightsPerObjectLimit = ValidatePerObjectLights(value); }
  501. }
  502. public bool supportsAdditionalLightShadows
  503. {
  504. get { return m_AdditionalLightShadowsSupported; }
  505. }
  506. public int additionalLightsShadowmapResolution
  507. {
  508. get { return (int)m_AdditionalLightsShadowmapResolution; }
  509. }
  510. /// <summary>
  511. /// Controls the maximum distance at which shadows are visible.
  512. /// </summary>
  513. public float shadowDistance
  514. {
  515. get { return m_ShadowDistance; }
  516. set { m_ShadowDistance = Mathf.Max(0.0f, value); }
  517. }
  518. /// <summary>
  519. /// Returns the number of shadow cascades.
  520. /// </summary>
  521. public int shadowCascadeCount
  522. {
  523. get { return m_ShadowCascadeCount; }
  524. set
  525. {
  526. if (value < k_ShadowCascadeMinCount || value > k_ShadowCascadeMaxCount)
  527. {
  528. throw new ArgumentException($"Value ({value}) needs to be between {k_ShadowCascadeMinCount} and {k_ShadowCascadeMaxCount}.");
  529. }
  530. m_ShadowCascadeCount = value;
  531. }
  532. }
  533. /// <summary>
  534. /// Returns the split value.
  535. /// </summary>
  536. /// <returns>Returns a Float with the split value.</returns>
  537. public float cascade2Split
  538. {
  539. get { return m_Cascade2Split; }
  540. }
  541. /// <summary>
  542. /// Returns the split values.
  543. /// </summary>
  544. /// <returns>Returns a Vector2 with the split values.</returns>
  545. public Vector2 cascade3Split
  546. {
  547. get { return m_Cascade3Split; }
  548. }
  549. /// <summary>
  550. /// Returns the split values.
  551. /// </summary>
  552. /// <returns>Returns a Vector3 with the split values.</returns>
  553. public Vector3 cascade4Split
  554. {
  555. get { return m_Cascade4Split; }
  556. }
  557. /// <summary>
  558. /// The Shadow Depth Bias, controls the offset of the lit pixels.
  559. /// </summary>
  560. public float shadowDepthBias
  561. {
  562. get { return m_ShadowDepthBias; }
  563. set { m_ShadowDepthBias = ValidateShadowBias(value); }
  564. }
  565. /// <summary>
  566. /// Controls the distance at which the shadow casting surfaces are shrunk along the surface normal.
  567. /// </summary>
  568. public float shadowNormalBias
  569. {
  570. get { return m_ShadowNormalBias; }
  571. set { m_ShadowNormalBias = ValidateShadowBias(value); }
  572. }
  573. /// <summary>
  574. /// Returns true Soft Shadows are supported, false otherwise.
  575. /// </summary>
  576. public bool supportsSoftShadows
  577. {
  578. get { return m_SoftShadowsSupported; }
  579. }
  580. public bool supportsDynamicBatching
  581. {
  582. get { return m_SupportsDynamicBatching; }
  583. set { m_SupportsDynamicBatching = value; }
  584. }
  585. public bool supportsMixedLighting
  586. {
  587. get { return m_MixedLightingSupported; }
  588. }
  589. public ShaderVariantLogLevel shaderVariantLogLevel
  590. {
  591. get { return m_ShaderVariantLogLevel; }
  592. set { m_ShaderVariantLogLevel = value; }
  593. }
  594. /// <summary>
  595. /// Returns the selected update mode for volumes.
  596. /// </summary>
  597. public VolumeFrameworkUpdateMode volumeFrameworkUpdateMode => m_VolumeFrameworkUpdateMode;
  598. [Obsolete("PipelineDebugLevel is deprecated. Calling debugLevel is not necessary.", false)]
  599. public PipelineDebugLevel debugLevel
  600. {
  601. get => PipelineDebugLevel.Disabled ;
  602. }
  603. public bool useSRPBatcher
  604. {
  605. get { return m_UseSRPBatcher; }
  606. set { m_UseSRPBatcher = value; }
  607. }
  608. public ColorGradingMode colorGradingMode
  609. {
  610. get { return m_ColorGradingMode; }
  611. set { m_ColorGradingMode = value; }
  612. }
  613. public int colorGradingLutSize
  614. {
  615. get { return m_ColorGradingLutSize; }
  616. set { m_ColorGradingLutSize = Mathf.Clamp(value, k_MinLutSize, k_MaxLutSize); }
  617. }
  618. /// <summary>
  619. /// Set to true to allow Adaptive performance to modify graphics quality settings during runtime.
  620. /// Only applicable when Adaptive performance package is available.
  621. /// </summary>
  622. public bool useAdaptivePerformance
  623. {
  624. get { return m_UseAdaptivePerformance; }
  625. set { m_UseAdaptivePerformance = value; }
  626. }
  627. public override Material defaultMaterial
  628. {
  629. get { return GetMaterial(DefaultMaterialType.Standard); }
  630. }
  631. public override Material defaultParticleMaterial
  632. {
  633. get { return GetMaterial(DefaultMaterialType.Particle); }
  634. }
  635. public override Material defaultLineMaterial
  636. {
  637. get { return GetMaterial(DefaultMaterialType.Particle); }
  638. }
  639. public override Material defaultTerrainMaterial
  640. {
  641. get { return GetMaterial(DefaultMaterialType.Terrain); }
  642. }
  643. public override Material defaultUIMaterial
  644. {
  645. get { return GetMaterial(DefaultMaterialType.UnityBuiltinDefault); }
  646. }
  647. public override Material defaultUIOverdrawMaterial
  648. {
  649. get { return GetMaterial(DefaultMaterialType.UnityBuiltinDefault); }
  650. }
  651. public override Material defaultUIETC1SupportedMaterial
  652. {
  653. get { return GetMaterial(DefaultMaterialType.UnityBuiltinDefault); }
  654. }
  655. public override Material default2DMaterial
  656. {
  657. get { return GetMaterial(DefaultMaterialType.Sprite); }
  658. }
  659. public override Shader defaultShader
  660. {
  661. get
  662. {
  663. #if UNITY_EDITOR
  664. // TODO: When importing project, AssetPreviewUpdater:CreatePreviewForAsset will be called multiple time
  665. // which in turns calls this property to get the default shader.
  666. // The property should never return null as, when null, it loads the data using AssetDatabase.LoadAssetAtPath.
  667. // However it seems there's an issue that LoadAssetAtPath will not load the asset in some cases. so adding the null check
  668. // here to fix template tests.
  669. if (scriptableRendererData != null)
  670. {
  671. Shader defaultShader = scriptableRendererData.GetDefaultShader();
  672. if (defaultShader != null)
  673. return defaultShader;
  674. }
  675. if (m_DefaultShader == null)
  676. {
  677. string path = AssetDatabase.GUIDToAssetPath(ShaderUtils.GetShaderGUID(ShaderPathID.Lit));
  678. m_DefaultShader = AssetDatabase.LoadAssetAtPath<Shader>(path);
  679. }
  680. #endif
  681. if (m_DefaultShader == null)
  682. m_DefaultShader = Shader.Find(ShaderUtils.GetShaderPath(ShaderPathID.Lit));
  683. return m_DefaultShader;
  684. }
  685. }
  686. #if UNITY_EDITOR
  687. public override Shader autodeskInteractiveShader
  688. {
  689. get { return editorResources?.shaders.autodeskInteractivePS; }
  690. }
  691. public override Shader autodeskInteractiveTransparentShader
  692. {
  693. get { return editorResources?.shaders.autodeskInteractiveTransparentPS; }
  694. }
  695. public override Shader autodeskInteractiveMaskedShader
  696. {
  697. get { return editorResources?.shaders.autodeskInteractiveMaskedPS; }
  698. }
  699. public override Shader terrainDetailLitShader
  700. {
  701. get { return editorResources?.shaders.terrainDetailLitPS; }
  702. }
  703. public override Shader terrainDetailGrassShader
  704. {
  705. get { return editorResources?.shaders.terrainDetailGrassPS; }
  706. }
  707. public override Shader terrainDetailGrassBillboardShader
  708. {
  709. get { return editorResources?.shaders.terrainDetailGrassBillboardPS; }
  710. }
  711. public override Shader defaultSpeedTree7Shader
  712. {
  713. get { return editorResources?.shaders.defaultSpeedTree7PS; }
  714. }
  715. public override Shader defaultSpeedTree8Shader
  716. {
  717. get { return editorResources?.shaders.defaultSpeedTree8PS; }
  718. }
  719. #endif
  720. public void OnBeforeSerialize()
  721. {
  722. }
  723. public void OnAfterDeserialize()
  724. {
  725. if (k_AssetVersion < 3)
  726. {
  727. m_SoftShadowsSupported = (m_ShadowType == ShadowQuality.SoftShadows);
  728. k_AssetPreviousVersion = k_AssetVersion;
  729. k_AssetVersion = 3;
  730. }
  731. if (k_AssetVersion < 4)
  732. {
  733. m_AdditionalLightShadowsSupported = m_LocalShadowsSupported;
  734. m_AdditionalLightsShadowmapResolution = m_LocalShadowsAtlasResolution;
  735. m_AdditionalLightsPerObjectLimit = m_MaxPixelLights;
  736. m_MainLightShadowmapResolution = m_ShadowAtlasResolution;
  737. k_AssetPreviousVersion = k_AssetVersion;
  738. k_AssetVersion = 4;
  739. }
  740. if (k_AssetVersion < 5)
  741. {
  742. if (m_RendererType == RendererType.Custom)
  743. {
  744. m_RendererDataList[0] = m_RendererData;
  745. }
  746. k_AssetPreviousVersion = k_AssetVersion;
  747. k_AssetVersion = 5;
  748. }
  749. if (k_AssetVersion < 6)
  750. {
  751. #pragma warning disable 618 // Obsolete warning
  752. // Adding an upgrade here so that if it was previously set to 2 it meant 4 cascades.
  753. // So adding a 3rd cascade shifted this value up 1.
  754. int value = (int)m_ShadowCascades;
  755. if (value == 2)
  756. {
  757. m_ShadowCascadeCount = 4;
  758. }
  759. else
  760. {
  761. m_ShadowCascadeCount = value + 1;
  762. }
  763. k_AssetVersion = 6;
  764. #pragma warning restore 618 // Obsolete warning
  765. }
  766. #if UNITY_EDITOR
  767. if (k_AssetPreviousVersion != k_AssetVersion)
  768. {
  769. EditorApplication.delayCall += () => UpgradeAsset(this);
  770. }
  771. #endif
  772. }
  773. #if UNITY_EDITOR
  774. static void UpgradeAsset(UniversalRenderPipelineAsset asset)
  775. {
  776. if(asset.k_AssetPreviousVersion < 5)
  777. {
  778. if (asset.m_RendererType == RendererType.ForwardRenderer)
  779. {
  780. var data = AssetDatabase.LoadAssetAtPath<ForwardRendererData>("Assets/ForwardRenderer.asset");
  781. if (data)
  782. {
  783. asset.m_RendererDataList[0] = data;
  784. }
  785. else
  786. {
  787. asset.LoadBuiltinRendererData();
  788. }
  789. asset.m_RendererData = null; // Clears the old renderer
  790. }
  791. asset.k_AssetPreviousVersion = 5;
  792. }
  793. }
  794. #endif
  795. float ValidateShadowBias(float value)
  796. {
  797. return Mathf.Max(0.0f, Mathf.Min(value, UniversalRenderPipeline.maxShadowBias));
  798. }
  799. int ValidatePerObjectLights(int value)
  800. {
  801. return System.Math.Max(0, System.Math.Min(value, UniversalRenderPipeline.maxPerObjectLights));
  802. }
  803. float ValidateRenderScale(float value)
  804. {
  805. return Mathf.Max(UniversalRenderPipeline.minRenderScale, Mathf.Min(value, UniversalRenderPipeline.maxRenderScale));
  806. }
  807. /// <summary>
  808. /// Check to see if the RendererData list contains valid RendererData references.
  809. /// </summary>
  810. /// <param name="partial">This bool controls whether to test against all or any, if false then there has to be no invalid RendererData</param>
  811. /// <returns></returns>
  812. internal bool ValidateRendererDataList(bool partial = false)
  813. {
  814. var emptyEntries = 0;
  815. for (int i = 0; i < m_RendererDataList.Length; i++) emptyEntries += ValidateRendererData(i) ? 0 : 1;
  816. if (partial)
  817. return emptyEntries == 0;
  818. return emptyEntries != m_RendererDataList.Length;
  819. }
  820. internal bool ValidateRendererData(int index)
  821. {
  822. // Check to see if you are asking for the default renderer
  823. if (index == -1) index = m_DefaultRendererIndex;
  824. return index < m_RendererDataList.Length ? m_RendererDataList[index] != null : false;
  825. }
  826. }
  827. }