暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CameraCaptureBridge.cs 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Collections.Generic;
  3. namespace UnityEngine.Rendering
  4. {
  5. /// <summary>
  6. /// Bridge class for camera captures.
  7. /// </summary>
  8. public static class CameraCaptureBridge
  9. {
  10. private class CameraEntry
  11. {
  12. internal HashSet<Action<RenderTargetIdentifier, CommandBuffer>> actions;
  13. internal IEnumerator<Action<RenderTargetIdentifier, CommandBuffer>> cachedEnumerator;
  14. }
  15. private static Dictionary<Camera, CameraEntry> actionDict = new();
  16. private static bool _enabled;
  17. /// <summary>
  18. /// Enable camera capture.
  19. /// </summary>
  20. public static bool enabled
  21. {
  22. get
  23. {
  24. return _enabled;
  25. }
  26. set
  27. {
  28. _enabled = value;
  29. }
  30. }
  31. /// <summary>
  32. /// Provides the set actions to the renderer to be triggered at the end of the render loop for camera capture
  33. /// </summary>
  34. /// <param name="camera">The camera to get actions for</param>
  35. /// <returns>Enumeration of actions</returns>
  36. public static IEnumerator<Action<RenderTargetIdentifier, CommandBuffer>> GetCaptureActions(Camera camera)
  37. {
  38. if (!actionDict.TryGetValue(camera, out var entry) || entry.actions.Count == 0)
  39. return null;
  40. return entry.actions.GetEnumerator();
  41. }
  42. internal static IEnumerator<Action<RenderTargetIdentifier, CommandBuffer>> GetCachedCaptureActionsEnumerator(Camera camera)
  43. {
  44. if (!actionDict.TryGetValue(camera, out var entry) || entry.actions.Count == 0)
  45. return null;
  46. // internal use only
  47. entry.cachedEnumerator.Reset();
  48. return entry.cachedEnumerator;
  49. }
  50. /// <summary>
  51. /// Adds actions for camera capture
  52. /// </summary>
  53. /// <param name="camera">The camera to add actions for</param>
  54. /// <param name="action">The action to add</param>
  55. public static void AddCaptureAction(Camera camera, Action<RenderTargetIdentifier, CommandBuffer> action)
  56. {
  57. actionDict.TryGetValue(camera, out var entry);
  58. if (entry == null)
  59. {
  60. entry = new CameraEntry {actions = new HashSet<Action<RenderTargetIdentifier, CommandBuffer>>()};
  61. actionDict.Add(camera, entry);
  62. }
  63. entry.actions.Add(action);
  64. entry.cachedEnumerator = entry.actions.GetEnumerator();
  65. }
  66. /// <summary>
  67. /// Removes actions for camera capture
  68. /// </summary>
  69. /// <param name="camera">The camera to remove actions for</param>
  70. /// <param name="action">The action to remove</param>
  71. public static void RemoveCaptureAction(Camera camera, Action<RenderTargetIdentifier, CommandBuffer> action)
  72. {
  73. if (camera == null)
  74. return;
  75. if (actionDict.TryGetValue(camera, out var entry))
  76. {
  77. entry.actions.Remove(action);
  78. entry.cachedEnumerator = entry.actions.GetEnumerator();
  79. }
  80. }
  81. }
  82. }