RenderTextureFactory.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using System.Collections.Generic;
  3. namespace UnityEngine.PostProcessing
  4. {
  5. public sealed class RenderTextureFactory : IDisposable
  6. {
  7. HashSet<RenderTexture> m_TemporaryRTs;
  8. public RenderTextureFactory()
  9. {
  10. m_TemporaryRTs = new HashSet<RenderTexture>();
  11. }
  12. public RenderTexture Get(RenderTexture baseRenderTexture)
  13. {
  14. return Get(
  15. baseRenderTexture.width,
  16. baseRenderTexture.height,
  17. baseRenderTexture.depth,
  18. baseRenderTexture.format,
  19. baseRenderTexture.sRGB ? RenderTextureReadWrite.sRGB : RenderTextureReadWrite.Linear,
  20. baseRenderTexture.filterMode,
  21. baseRenderTexture.wrapMode
  22. );
  23. }
  24. 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")
  25. {
  26. var rt = RenderTexture.GetTemporary(width, height, depthBuffer, format, rw); // add forgotten param rw
  27. rt.filterMode = filterMode;
  28. rt.wrapMode = wrapMode;
  29. rt.name = name;
  30. m_TemporaryRTs.Add(rt);
  31. return rt;
  32. }
  33. public void Release(RenderTexture rt)
  34. {
  35. if (rt == null)
  36. return;
  37. if (!m_TemporaryRTs.Contains(rt))
  38. throw new ArgumentException(string.Format("Attempting to remove a RenderTexture that was not allocated: {0}", rt));
  39. m_TemporaryRTs.Remove(rt);
  40. RenderTexture.ReleaseTemporary(rt);
  41. }
  42. public void ReleaseAll()
  43. {
  44. var enumerator = m_TemporaryRTs.GetEnumerator();
  45. while (enumerator.MoveNext())
  46. RenderTexture.ReleaseTemporary(enumerator.Current);
  47. m_TemporaryRTs.Clear();
  48. }
  49. public void Dispose()
  50. {
  51. ReleaseAll();
  52. }
  53. }
  54. }