暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

UniversalRendererData.cs 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. #if UNITY_EDITOR
  2. using UnityEditor;
  3. using UnityEditor.ProjectWindowCallback;
  4. using ShaderKeywordFilter = UnityEditor.ShaderKeywordFilter;
  5. #endif
  6. using System;
  7. using UnityEngine.Scripting.APIUpdating;
  8. using UnityEngine.Assertions;
  9. using UnityEngine.Experimental.Rendering;
  10. namespace UnityEngine.Rendering.Universal
  11. {
  12. /// <summary>
  13. /// Defines if Unity will copy the depth that can be bound in shaders as _CameraDepthTexture after the opaques pass or after the transparents pass.
  14. /// </summary>
  15. public enum CopyDepthMode
  16. {
  17. /// <summary>Depth will be copied after the opaques pass</summary>
  18. AfterOpaques,
  19. /// <summary>Depth will be copied after the transparents pass</summary>
  20. AfterTransparents,
  21. /// <summary>Depth will be written by a depth prepass</summary>
  22. ForcePrepass
  23. }
  24. /// <summary>
  25. /// Render path exposed as flags to allow compatibility to be expressed with multiple options.
  26. /// </summary>
  27. [Flags]
  28. public enum RenderPathCompatibility
  29. {
  30. /// <summary>Forward Rendering Path</summary>
  31. Forward = 1 << 0,
  32. /// <summary>Deferred Rendering Path</summary>
  33. Deferred = 1 << 1,
  34. /// <summary>Forward+ Rendering Path</summary>
  35. ForwardPlus = 1 << 2,
  36. /// <summary>All Rendering Paths</summary>
  37. All = Forward | Deferred | ForwardPlus
  38. }
  39. [AttributeUsage(AttributeTargets.Field)]
  40. sealed class RenderPathCompatibleAttribute : Attribute
  41. {
  42. public RenderPathCompatibility renderPath;
  43. public RenderPathCompatibleAttribute(RenderPathCompatibility renderPath)
  44. {
  45. this.renderPath = renderPath;
  46. }
  47. }
  48. /// <summary>
  49. /// Dept format options for the depth texture and depth attachment.
  50. /// Each option is marked with all the render paths its compatible with.
  51. /// </summary>
  52. public enum DepthFormat
  53. {
  54. /// <summary>
  55. /// Default format for Android and Switch platforms is <see cref="GraphicsFormat.D24_UNorm_S8_UInt"/> and <see cref="GraphicsFormat.D32_SFloat_S8_UInt"/> for other platforms
  56. /// </summary>
  57. [RenderPathCompatible(RenderPathCompatibility.All)]
  58. Default,
  59. /// <summary>
  60. /// Format containing 16 unsigned normalized bits in depth component. Corresponds to <see cref="GraphicsFormat.D16_UNorm"/>.
  61. /// </summary>
  62. [RenderPathCompatible(RenderPathCompatibility.Forward | RenderPathCompatibility.ForwardPlus)]
  63. Depth_16 = GraphicsFormat.D16_UNorm,
  64. /// <summary>
  65. /// Format containing 24 unsigned normalized bits in depth component. Corresponds to <see cref="GraphicsFormat.D24_UNorm"/>.
  66. /// </summary>
  67. [RenderPathCompatible(RenderPathCompatibility.Forward | RenderPathCompatibility.ForwardPlus)]
  68. Depth_24 = GraphicsFormat.D24_UNorm,
  69. /// <summary>
  70. /// Format containing 32 signed float bits in depth component. Corresponds to <see cref="GraphicsFormat.D32_SFloat"/>.
  71. /// </summary>
  72. [RenderPathCompatible(RenderPathCompatibility.Forward | RenderPathCompatibility.ForwardPlus)]
  73. Depth_32 = GraphicsFormat.D32_SFloat,
  74. /// <summary>
  75. /// Format containing 16 unsigned normalized bits in depth component and 8 unsigned integer bits in stencil. Corresponds to <see cref="GraphicsFormat.D16_UNorm_S8_UInt"/>.
  76. /// </summary>
  77. [RenderPathCompatible(RenderPathCompatibility.All)]
  78. Depth_16_Stencil_8 = GraphicsFormat.D16_UNorm_S8_UInt,
  79. /// <summary>
  80. /// Format containing 24 unsigned normalized bits in depth component and 8 unsigned integer bits in stencil. Corresponds to <see cref="GraphicsFormat.D24_UNorm_S8_UInt"/>.
  81. /// </summary>
  82. [RenderPathCompatible(RenderPathCompatibility.All)]
  83. Depth_24_Stencil_8 = GraphicsFormat.D24_UNorm_S8_UInt,
  84. /// <summary>
  85. /// Format containing 32 signed float bits in depth component and 8 unsigned integer bits in stencil. Corresponds to <see cref="GraphicsFormat.D32_SFloat_S8_UInt"/>.
  86. /// </summary>
  87. [RenderPathCompatible(RenderPathCompatibility.All)]
  88. Depth_32_Stencil_8 = GraphicsFormat.D32_SFloat_S8_UInt,
  89. }
  90. /// <summary>
  91. /// Class containing resources needed for the <c>UniversalRenderer</c>.
  92. /// </summary>
  93. [Serializable, ReloadGroup, ExcludeFromPreset]
  94. [URPHelpURL("urp-universal-renderer")]
  95. public partial class UniversalRendererData : ScriptableRendererData, ISerializationCallbackReceiver
  96. {
  97. #if UNITY_EDITOR
  98. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1812")]
  99. internal class CreateUniversalRendererAsset : EndNameEditAction
  100. {
  101. public override void Action(int instanceId, string pathName, string resourceFile)
  102. {
  103. var instance = UniversalRenderPipelineAsset.CreateRendererAsset(pathName, RendererType.UniversalRenderer, false) as UniversalRendererData;
  104. Selection.activeObject = instance;
  105. }
  106. }
  107. [MenuItem("Assets/Create/Rendering/URP Universal Renderer", priority = CoreUtils.Sections.section3 + CoreUtils.Priorities.assetsCreateRenderingMenuPriority + 2)]
  108. static void CreateUniversalRendererData()
  109. {
  110. ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, CreateInstance<CreateUniversalRendererAsset>(), "New Custom Universal Renderer Data.asset", null, null);
  111. }
  112. #endif
  113. /// <summary>
  114. /// Resources needed for Post Processing.
  115. /// </summary>
  116. public PostProcessData postProcessData = null;
  117. const int k_LatestAssetVersion = 2;
  118. [SerializeField] int m_AssetVersion = 0;
  119. [SerializeField] LayerMask m_OpaqueLayerMask = -1;
  120. [SerializeField] LayerMask m_TransparentLayerMask = -1;
  121. [SerializeField] StencilStateData m_DefaultStencilState = new StencilStateData() { passOperation = StencilOp.Replace }; // This default state is compatible with deferred renderer.
  122. [SerializeField] bool m_ShadowTransparentReceive = true;
  123. [SerializeField] RenderingMode m_RenderingMode = RenderingMode.Forward;
  124. [SerializeField] DepthPrimingMode m_DepthPrimingMode = DepthPrimingMode.Disabled; // Default disabled because there are some outstanding issues with Text Mesh rendering.
  125. [SerializeField] CopyDepthMode m_CopyDepthMode = CopyDepthMode.AfterTransparents;
  126. [SerializeField] DepthFormat m_DepthAttachmentFormat = DepthFormat.Default;
  127. [SerializeField] DepthFormat m_DepthTextureFormat = DepthFormat.Default;
  128. #if UNITY_EDITOR
  129. // Do not strip accurateGbufferNormals on Mobile Vulkan as some GPUs do not support R8G8B8A8_SNorm, which then force us to use accurateGbufferNormals
  130. [ShaderKeywordFilter.ApplyRulesIfNotGraphicsAPI(GraphicsDeviceType.Vulkan)]
  131. [ShaderKeywordFilter.RemoveIf(false, keywordNames: ShaderKeywordStrings._GBUFFER_NORMALS_OCT)]
  132. #endif
  133. [SerializeField] bool m_AccurateGbufferNormals = false;
  134. [SerializeField] IntermediateTextureMode m_IntermediateTextureMode = IntermediateTextureMode.Always;
  135. /// <inheritdoc/>
  136. protected override ScriptableRenderer Create()
  137. {
  138. if (!Application.isPlaying)
  139. {
  140. ReloadAllNullProperties();
  141. }
  142. return new UniversalRenderer(this);
  143. }
  144. /// <summary>
  145. /// Use this to configure how to filter opaque objects.
  146. /// </summary>
  147. public LayerMask opaqueLayerMask
  148. {
  149. get => m_OpaqueLayerMask;
  150. set
  151. {
  152. SetDirty();
  153. m_OpaqueLayerMask = value;
  154. }
  155. }
  156. /// <summary>
  157. /// Use this to configure how to filter transparent objects.
  158. /// </summary>
  159. public LayerMask transparentLayerMask
  160. {
  161. get => m_TransparentLayerMask;
  162. set
  163. {
  164. SetDirty();
  165. m_TransparentLayerMask = value;
  166. }
  167. }
  168. /// <summary>
  169. /// The default stencil state settings.
  170. /// </summary>
  171. public StencilStateData defaultStencilState
  172. {
  173. get => m_DefaultStencilState;
  174. set
  175. {
  176. SetDirty();
  177. m_DefaultStencilState = value;
  178. }
  179. }
  180. /// <summary>
  181. /// True if transparent objects receive shadows.
  182. /// </summary>
  183. public bool shadowTransparentReceive
  184. {
  185. get => m_ShadowTransparentReceive;
  186. set
  187. {
  188. SetDirty();
  189. m_ShadowTransparentReceive = value;
  190. }
  191. }
  192. /// <summary>
  193. /// Rendering mode.
  194. /// </summary>
  195. public RenderingMode renderingMode
  196. {
  197. get => m_RenderingMode;
  198. set
  199. {
  200. SetDirty();
  201. m_RenderingMode = value;
  202. }
  203. }
  204. /// <summary>
  205. /// Depth priming mode.
  206. /// </summary>
  207. public DepthPrimingMode depthPrimingMode
  208. {
  209. get => m_DepthPrimingMode;
  210. set
  211. {
  212. SetDirty();
  213. m_DepthPrimingMode = value;
  214. }
  215. }
  216. /// <summary>
  217. /// Copy depth mode.
  218. /// </summary>
  219. public CopyDepthMode copyDepthMode
  220. {
  221. get => m_CopyDepthMode;
  222. set
  223. {
  224. SetDirty();
  225. m_CopyDepthMode = value;
  226. }
  227. }
  228. /// <summary>
  229. /// Depth format used for CameraDepthAttachment
  230. /// </summary>
  231. public DepthFormat depthAttachmentFormat
  232. {
  233. get
  234. {
  235. if (m_DepthAttachmentFormat != DepthFormat.Default && !SystemInfo.IsFormatSupported((GraphicsFormat)m_DepthAttachmentFormat, GraphicsFormatUsage.Render))
  236. {
  237. Debug.LogWarning("Selected Depth Attachment Format is not supported on this platform, falling back to Default");
  238. return DepthFormat.Default;
  239. }
  240. return m_DepthAttachmentFormat;
  241. }
  242. set
  243. {
  244. SetDirty();
  245. if (renderingMode == RenderingMode.Deferred && !GraphicsFormatUtility.IsStencilFormat((GraphicsFormat)value))
  246. {
  247. Debug.LogWarning("Depth format without stencil is not supported on Deferred renderer, falling back to Default");
  248. m_DepthAttachmentFormat = DepthFormat.Default;
  249. }
  250. else
  251. {
  252. m_DepthAttachmentFormat = value;
  253. }
  254. }
  255. }
  256. /// <summary>
  257. /// Depth format used for CameraDepthTexture
  258. /// </summary>
  259. public DepthFormat depthTextureFormat
  260. {
  261. get
  262. {
  263. if (m_DepthTextureFormat != DepthFormat.Default && !SystemInfo.IsFormatSupported((GraphicsFormat) m_DepthTextureFormat, GraphicsFormatUsage.Render))
  264. {
  265. Debug.LogWarning($"Selected Depth Texture Format {m_DepthTextureFormat.ToString()} is not supported on this platform, falling back to Default");
  266. return DepthFormat.Default;
  267. }
  268. return m_DepthTextureFormat;
  269. }
  270. set
  271. {
  272. SetDirty();
  273. m_DepthTextureFormat = value;
  274. }
  275. }
  276. /// <summary>
  277. /// Use Octahedron normal vector encoding for gbuffer normals.
  278. /// The overhead is negligible from desktop GPUs, while it should be avoided for mobile GPUs.
  279. /// </summary>
  280. public bool accurateGbufferNormals
  281. {
  282. get => m_AccurateGbufferNormals;
  283. set
  284. {
  285. SetDirty();
  286. m_AccurateGbufferNormals = value;
  287. }
  288. }
  289. /// <summary>
  290. /// Controls when URP renders via an intermediate texture.
  291. /// </summary>
  292. public IntermediateTextureMode intermediateTextureMode
  293. {
  294. get => m_IntermediateTextureMode;
  295. set
  296. {
  297. SetDirty();
  298. m_IntermediateTextureMode = value;
  299. }
  300. }
  301. /// <inheritdoc/>
  302. protected override void OnEnable()
  303. {
  304. base.OnEnable();
  305. ReloadAllNullProperties();
  306. }
  307. private void ReloadAllNullProperties()
  308. {
  309. // Upon asset creation, OnEnable is called and `shaders` reference is not yet initialized
  310. // We need to call the OnEnable for data migration when updating from old versions of UniversalRP that
  311. // serialized resources in a different format. Early returning here when OnEnable is called
  312. // upon asset creation is fine because we guarantee new assets get created with all resources initialized.
  313. #if UNITY_EDITOR
  314. ResourceReloader.TryReloadAllNullIn(this, UniversalRenderPipelineAsset.packagePath);
  315. if (postProcessData != null)
  316. ResourceReloader.TryReloadAllNullIn(postProcessData, UniversalRenderPipelineAsset.packagePath);
  317. #endif
  318. }
  319. /// <inheritdoc/>
  320. void ISerializationCallbackReceiver.OnBeforeSerialize()
  321. {
  322. m_AssetVersion = k_LatestAssetVersion;
  323. }
  324. /// <inheritdoc/>
  325. void ISerializationCallbackReceiver.OnAfterDeserialize()
  326. {
  327. if (m_AssetVersion <= 1)
  328. {
  329. // To avoid breaking existing projects, keep the old AfterOpaques behaviour. The new AfterTransparents default will only apply to new projects.
  330. m_CopyDepthMode = CopyDepthMode.AfterOpaques;
  331. }
  332. m_AssetVersion = k_LatestAssetVersion;
  333. }
  334. }
  335. }