RenderTextureFactory.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using System.Collections.Generic;
  3. namespace UnityEngine.PostProcessing
  4. {
  5. public sealed class RenderTextureFactory : IDisposable
  6. {
  7. public RenderTextureFactory()
  8. {
  9. this.m_TemporaryRTs = new HashSet<RenderTexture>();
  10. }
  11. public RenderTexture Get(RenderTexture baseRenderTexture)
  12. {
  13. return this.Get(baseRenderTexture.width, baseRenderTexture.height, baseRenderTexture.depth, baseRenderTexture.format, (!baseRenderTexture.sRGB) ? RenderTextureReadWrite.Linear : RenderTextureReadWrite.sRGB, baseRenderTexture.filterMode, baseRenderTexture.wrapMode, "FactoryTempTexture");
  14. }
  15. public RenderTexture Get(int width, int height, int depthBuffer = 0, RenderTextureFormat format = RenderTextureFormat.ARGBHalf, RenderTextureReadWrite rw = RenderTextureReadWrite.Default, FilterMode filterMode = FilterMode.Bilinear, TextureWrapMode wrapMode = TextureWrapMode.Clamp, string name = "FactoryTempTexture")
  16. {
  17. RenderTexture temporary = RenderTexture.GetTemporary(width, height, depthBuffer, format, rw);
  18. temporary.filterMode = filterMode;
  19. temporary.wrapMode = wrapMode;
  20. temporary.name = name;
  21. this.m_TemporaryRTs.Add(temporary);
  22. return temporary;
  23. }
  24. public void Release(RenderTexture rt)
  25. {
  26. if (rt == null)
  27. {
  28. return;
  29. }
  30. if (!this.m_TemporaryRTs.Contains(rt))
  31. {
  32. throw new ArgumentException(string.Format("Attempting to remove a RenderTexture that was not allocated: {0}", rt));
  33. }
  34. this.m_TemporaryRTs.Remove(rt);
  35. RenderTexture.ReleaseTemporary(rt);
  36. }
  37. public void ReleaseAll()
  38. {
  39. foreach (RenderTexture temp in this.m_TemporaryRTs)
  40. {
  41. RenderTexture.ReleaseTemporary(temp);
  42. }
  43. this.m_TemporaryRTs.Clear();
  44. }
  45. public void Dispose()
  46. {
  47. this.ReleaseAll();
  48. }
  49. private HashSet<RenderTexture> m_TemporaryRTs;
  50. }
  51. }