RenderObjects.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System.Collections.Generic;
  2. using UnityEngine.Rendering.Universal;
  3. using UnityEngine.Rendering;
  4. namespace UnityEngine.Experimental.Rendering.Universal
  5. {
  6. public enum RenderQueueType
  7. {
  8. Opaque,
  9. Transparent,
  10. }
  11. [ExcludeFromPreset]
  12. [Tooltip("Render Objects simplifies the injection of additional render passes by exposing a selection of commonly used settings.")]
  13. public class RenderObjects : ScriptableRendererFeature
  14. {
  15. [System.Serializable]
  16. public class RenderObjectsSettings
  17. {
  18. public string passTag = "RenderObjectsFeature";
  19. public RenderPassEvent Event = RenderPassEvent.AfterRenderingOpaques;
  20. public FilterSettings filterSettings = new FilterSettings();
  21. public Material overrideMaterial = null;
  22. public int overrideMaterialPassIndex = 0;
  23. public bool overrideDepthState = false;
  24. public CompareFunction depthCompareFunction = CompareFunction.LessEqual;
  25. public bool enableWrite = true;
  26. public StencilStateData stencilSettings = new StencilStateData();
  27. public CustomCameraSettings cameraSettings = new CustomCameraSettings();
  28. }
  29. [System.Serializable]
  30. public class FilterSettings
  31. {
  32. // TODO: expose opaque, transparent, all ranges as drop down
  33. public RenderQueueType RenderQueueType;
  34. public LayerMask LayerMask;
  35. public string[] PassNames;
  36. public FilterSettings()
  37. {
  38. RenderQueueType = RenderQueueType.Opaque;
  39. LayerMask = 0;
  40. }
  41. }
  42. [System.Serializable]
  43. public class CustomCameraSettings
  44. {
  45. public bool overrideCamera = false;
  46. public bool restoreCamera = true;
  47. public Vector4 offset;
  48. public float cameraFieldOfView = 60.0f;
  49. }
  50. public RenderObjectsSettings settings = new RenderObjectsSettings();
  51. RenderObjectsPass renderObjectsPass;
  52. public override void Create()
  53. {
  54. FilterSettings filter = settings.filterSettings;
  55. // Render Objects pass doesn't support events before rendering prepasses.
  56. // The camera is not setup before this point and all rendering is monoscopic.
  57. // Events before BeforeRenderingPrepasses should be used for input texture passes (shadow map, LUT, etc) that doesn't depend on the camera.
  58. // These events are filtering in the UI, but we still should prevent users from changing it from code or
  59. // by changing the serialized data.
  60. if (settings.Event < RenderPassEvent.BeforeRenderingPrePasses)
  61. settings.Event = RenderPassEvent.BeforeRenderingPrePasses;
  62. renderObjectsPass = new RenderObjectsPass(settings.passTag, settings.Event, filter.PassNames,
  63. filter.RenderQueueType, filter.LayerMask, settings.cameraSettings);
  64. renderObjectsPass.overrideMaterial = settings.overrideMaterial;
  65. renderObjectsPass.overrideMaterialPassIndex = settings.overrideMaterialPassIndex;
  66. if (settings.overrideDepthState)
  67. renderObjectsPass.SetDetphState(settings.enableWrite, settings.depthCompareFunction);
  68. if (settings.stencilSettings.overrideStencilState)
  69. renderObjectsPass.SetStencilState(settings.stencilSettings.stencilReference,
  70. settings.stencilSettings.stencilCompareFunction, settings.stencilSettings.passOperation,
  71. settings.stencilSettings.failOperation, settings.stencilSettings.zFailOperation);
  72. }
  73. public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
  74. {
  75. renderer.EnqueuePass(renderObjectsPass);
  76. }
  77. internal override bool SupportsNativeRenderPass()
  78. {
  79. return true;
  80. }
  81. }
  82. }