TextureFlipper.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. using UnityEngine;
  3. namespace UnityEditor.Recorder
  4. {
  5. class TextureFlipper : IDisposable
  6. {
  7. internal RenderTexture workTexture { private set; get; }
  8. internal bool inPlace { set; get; }
  9. internal TextureFlipper(bool flipInPlace = true)
  10. {
  11. inPlace = flipInPlace;
  12. }
  13. internal void Init(RenderTexture template)
  14. {
  15. UnityHelpers.Destroy(workTexture);
  16. workTexture = new RenderTexture(template);
  17. }
  18. internal void Flip(RenderTexture target)
  19. {
  20. if (workTexture == null || workTexture.width != target.width || workTexture.height != target.height)
  21. Init(target);
  22. var sRGBWrite = GL.sRGBWrite;
  23. GL.sRGBWrite = PlayerSettings.colorSpace == ColorSpace.Linear;
  24. Graphics.Blit(target, workTexture, new Vector2(1.0f, -1.0f), new Vector2(0.0f, 1.0f));
  25. if (inPlace)
  26. Graphics.Blit(workTexture, target);
  27. GL.sRGBWrite = sRGBWrite;
  28. }
  29. public void Dispose()
  30. {
  31. UnityHelpers.Destroy(workTexture);
  32. workTexture = null;
  33. }
  34. }
  35. }