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