ScriptableRendererFeature.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System;
  2. using UnityEngine.Scripting.APIUpdating;
  3. namespace UnityEngine.Rendering.Universal
  4. {
  5. /// <summary>
  6. /// You can add a <c>ScriptableRendererFeature</c> to the <c>ScriptableRenderer</c>. Use this scriptable renderer feature to inject render passes into the renderer.
  7. /// </summary>
  8. /// <seealso cref="ScriptableRenderer"/>
  9. /// <seealso cref="ScriptableRenderPass"/>
  10. [ExcludeFromPreset]
  11. public abstract class ScriptableRendererFeature : ScriptableObject, IDisposable
  12. {
  13. [SerializeField, HideInInspector] private bool m_Active = true;
  14. /// <summary>
  15. /// Returns the state of the ScriptableRenderFeature (true: the feature is active, false: the feature is inactive). Use the method ScriptableRenderFeature.SetActive to change the value of this variable.
  16. /// </summary>
  17. public bool isActive => m_Active;
  18. /// <summary>
  19. /// Initializes this feature's resources. This is called every time serialization happens.
  20. /// </summary>
  21. public abstract void Create();
  22. /// <summary>
  23. /// Callback before cull happens in renderer.
  24. /// </summary>
  25. /// <param name="renderer">Renderer of callback.</param>
  26. /// <param name="cameraData">CameraData contains all relevant render target information for the camera.</param>
  27. public virtual void OnCameraPreCull(ScriptableRenderer renderer, in CameraData cameraData) { }
  28. /// <summary>
  29. /// Injects one or multiple <c>ScriptableRenderPass</c> in the renderer.
  30. /// </summary>
  31. /// <param name="renderer">Renderer used for adding render passes.</param>
  32. /// <param name="renderingData">Rendering state. Use this to setup render passes.</param>
  33. public abstract void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData);
  34. void OnEnable()
  35. {
  36. Create();
  37. }
  38. void OnValidate()
  39. {
  40. Create();
  41. }
  42. /// <summary>
  43. /// Override this method and return true if the feature should use the Native RenderPass API
  44. /// </summary>
  45. internal virtual bool SupportsNativeRenderPass()
  46. {
  47. return false;
  48. }
  49. /// <summary>
  50. /// Sets the state of ScriptableRenderFeature (true: the feature is active, false: the feature is inactive).
  51. /// If the feature is active, it is added to the renderer it is attached to, otherwise the feature is skipped while rendering.
  52. /// </summary>
  53. /// <param name="active">The true value activates the ScriptableRenderFeature and the false value deactivates it.</param>
  54. public void SetActive(bool active)
  55. {
  56. m_Active = active;
  57. }
  58. /// <summary>
  59. /// Disposable pattern implementation.
  60. /// </summary>
  61. public void Dispose()
  62. {
  63. Dispose(true);
  64. GC.SuppressFinalize(this);
  65. }
  66. protected virtual void Dispose(bool disposing)
  67. {
  68. }
  69. }
  70. }