Renderer2DData.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using UnityEngine.Rendering;
  3. using UnityEngine.Rendering.Universal;
  4. using UnityEngine.Scripting.APIUpdating;
  5. using UnityEngine.Serialization;
  6. #if UNITY_EDITOR
  7. using UnityEditor;
  8. using UnityEditor.ProjectWindowCallback;
  9. #endif
  10. namespace UnityEngine.Experimental.Rendering.Universal
  11. {
  12. [Serializable, ReloadGroup, ExcludeFromPreset]
  13. [MovedFrom("UnityEngine.Experimental.Rendering.LWRP")]
  14. public class Renderer2DData : ScriptableRendererData
  15. {
  16. public enum Renderer2DDefaultMaterialType
  17. {
  18. Lit,
  19. Unlit,
  20. Custom
  21. }
  22. [SerializeField]
  23. TransparencySortMode m_TransparencySortMode = TransparencySortMode.Default;
  24. [SerializeField]
  25. Vector3 m_TransparencySortAxis = Vector3.up;
  26. [SerializeField]
  27. float m_HDREmulationScale = 1;
  28. [SerializeField, FormerlySerializedAs("m_LightOperations")]
  29. Light2DBlendStyle[] m_LightBlendStyles = null;
  30. [SerializeField]
  31. bool m_UseDepthStencilBuffer = true;
  32. #if UNITY_EDITOR
  33. [SerializeField]
  34. Renderer2DDefaultMaterialType m_DefaultMaterialType = Renderer2DDefaultMaterialType.Lit;
  35. [SerializeField, Reload("Runtime/Materials/Sprite-Lit-Default.mat")]
  36. Material m_DefaultCustomMaterial = null;
  37. [SerializeField, Reload("Runtime/Materials/Sprite-Lit-Default.mat")]
  38. Material m_DefaultLitMaterial = null;
  39. [SerializeField, Reload("Runtime/Materials/Sprite-Unlit-Default.mat")]
  40. Material m_DefaultUnlitMaterial = null;
  41. #endif
  42. [SerializeField, Reload("Shaders/2D/Light2D-Shape.shader")]
  43. Shader m_ShapeLightShader = null;
  44. [SerializeField, Reload("Shaders/2D/Light2D-Shape-Volumetric.shader")]
  45. Shader m_ShapeLightVolumeShader = null;
  46. [SerializeField, Reload("Shaders/2D/Light2D-Point.shader")]
  47. Shader m_PointLightShader = null;
  48. [SerializeField, Reload("Shaders/2D/Light2D-Point-Volumetric.shader")]
  49. Shader m_PointLightVolumeShader = null;
  50. [SerializeField, Reload("Shaders/Utils/Blit.shader")]
  51. Shader m_BlitShader = null;
  52. [SerializeField, Reload("Shaders/2D/ShadowGroup2D.shader")]
  53. Shader m_ShadowGroupShader = null;
  54. [SerializeField, Reload("Shaders/2D/Shadow2DRemoveSelf.shader")]
  55. Shader m_RemoveSelfShadowShader = null;
  56. [SerializeField, Reload("Runtime/Data/PostProcessData.asset")]
  57. PostProcessData m_PostProcessData = null;
  58. public float hdrEmulationScale => m_HDREmulationScale;
  59. public Light2DBlendStyle[] lightBlendStyles => m_LightBlendStyles;
  60. internal bool useDepthStencilBuffer => m_UseDepthStencilBuffer;
  61. internal Shader shapeLightShader => m_ShapeLightShader;
  62. internal Shader shapeLightVolumeShader => m_ShapeLightVolumeShader;
  63. internal Shader pointLightShader => m_PointLightShader;
  64. internal Shader pointLightVolumeShader => m_PointLightVolumeShader;
  65. internal Shader blitShader => m_BlitShader;
  66. internal Shader shadowGroupShader => m_ShadowGroupShader;
  67. internal Shader removeSelfShadowShader => m_RemoveSelfShadowShader;
  68. internal PostProcessData postProcessData => m_PostProcessData;
  69. internal TransparencySortMode transparencySortMode => m_TransparencySortMode;
  70. internal Vector3 transparencySortAxis => m_TransparencySortAxis;
  71. protected override ScriptableRenderer Create()
  72. {
  73. #if UNITY_EDITOR
  74. if (!Application.isPlaying)
  75. {
  76. ResourceReloader.TryReloadAllNullIn(this, UniversalRenderPipelineAsset.packagePath);
  77. ResourceReloader.TryReloadAllNullIn(m_PostProcessData, UniversalRenderPipelineAsset.packagePath);
  78. }
  79. #endif
  80. return new Renderer2D(this);
  81. }
  82. #if UNITY_EDITOR
  83. internal static void Create2DRendererData(Action<Renderer2DData> onCreatedCallback)
  84. {
  85. var instance = CreateInstance<Create2DRendererDataAsset>();
  86. instance.onCreated += onCreatedCallback;
  87. ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, instance, "New 2D Renderer Data.asset", null, null);
  88. }
  89. class Create2DRendererDataAsset : EndNameEditAction
  90. {
  91. public event Action<Renderer2DData> onCreated;
  92. public override void Action(int instanceId, string pathName, string resourceFile)
  93. {
  94. var instance = CreateInstance<Renderer2DData>();
  95. instance.OnCreate();
  96. AssetDatabase.CreateAsset(instance, pathName);
  97. Selection.activeObject = instance;
  98. onCreated(instance);
  99. }
  100. }
  101. void OnCreate()
  102. {
  103. m_LightBlendStyles = new Light2DBlendStyle[4];
  104. m_LightBlendStyles[0].name = "Default";
  105. m_LightBlendStyles[0].blendMode = Light2DBlendStyle.BlendMode.Multiply;
  106. m_LightBlendStyles[0].renderTextureScale = 1.0f;
  107. for (int i = 1; i < m_LightBlendStyles.Length; ++i)
  108. {
  109. m_LightBlendStyles[i].name = "Blend Style " + i;
  110. m_LightBlendStyles[i].blendMode = Light2DBlendStyle.BlendMode.Multiply;
  111. m_LightBlendStyles[i].renderTextureScale = 1.0f;
  112. }
  113. }
  114. protected override void OnEnable()
  115. {
  116. base.OnEnable();
  117. // Provide a list of suggested texture property names to Sprite Editor via EditorPrefs.
  118. const string suggestedNamesKey = "SecondarySpriteTexturePropertyNames";
  119. const string maskTex = "_MaskTex";
  120. const string normalMap = "_NormalMap";
  121. string suggestedNamesPrefs = EditorPrefs.GetString(suggestedNamesKey);
  122. if (string.IsNullOrEmpty(suggestedNamesPrefs))
  123. EditorPrefs.SetString(suggestedNamesKey, maskTex + "," + normalMap);
  124. else
  125. {
  126. if (!suggestedNamesPrefs.Contains(maskTex))
  127. suggestedNamesPrefs += ("," + maskTex);
  128. if (!suggestedNamesPrefs.Contains(normalMap))
  129. suggestedNamesPrefs += ("," + normalMap);
  130. EditorPrefs.SetString(suggestedNamesKey, suggestedNamesPrefs);
  131. }
  132. ResourceReloader.TryReloadAllNullIn(this, UniversalRenderPipelineAsset.packagePath);
  133. ResourceReloader.TryReloadAllNullIn(m_PostProcessData, UniversalRenderPipelineAsset.packagePath);
  134. }
  135. internal override Material GetDefaultMaterial(DefaultMaterialType materialType)
  136. {
  137. if (materialType == DefaultMaterialType.Sprite || materialType == DefaultMaterialType.Particle)
  138. {
  139. if (m_DefaultMaterialType == Renderer2DDefaultMaterialType.Lit)
  140. return m_DefaultLitMaterial;
  141. else if (m_DefaultMaterialType == Renderer2DDefaultMaterialType.Unlit)
  142. return m_DefaultUnlitMaterial;
  143. else
  144. return m_DefaultCustomMaterial;
  145. }
  146. return null;
  147. }
  148. internal override Shader GetDefaultShader()
  149. {
  150. return Shader.Find("Universal Render Pipeline/2D/Sprite-Lit-Default");
  151. }
  152. #endif
  153. }
  154. }