TaaComponent.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System;
  2. namespace UnityEngine.PostProcessing
  3. {
  4. public sealed class TaaComponent : PostProcessingComponentRenderTexture<AntialiasingModel>
  5. {
  6. public override bool active
  7. {
  8. get
  9. {
  10. return base.model.enabled && base.model.settings.method == AntialiasingModel.Method.Taa && SystemInfo.supportsMotionVectors && SystemInfo.supportedRenderTargetCount >= 2 && !this.context.interrupted;
  11. }
  12. }
  13. public override DepthTextureMode GetCameraFlags()
  14. {
  15. return DepthTextureMode.Depth | DepthTextureMode.MotionVectors;
  16. }
  17. public Vector2 jitterVector { get; private set; }
  18. public void ResetHistory()
  19. {
  20. this.m_ResetHistory = true;
  21. }
  22. public void SetProjectionMatrix(Func<Vector2, Matrix4x4> jitteredFunc)
  23. {
  24. AntialiasingModel.TaaSettings taaSettings = base.model.settings.taaSettings;
  25. Vector2 vector = this.GenerateRandomOffset();
  26. vector *= taaSettings.jitterSpread;
  27. this.context.camera.nonJitteredProjectionMatrix = this.context.camera.projectionMatrix;
  28. if (jitteredFunc != null)
  29. {
  30. this.context.camera.projectionMatrix = jitteredFunc(vector);
  31. }
  32. else
  33. {
  34. this.context.camera.projectionMatrix = ((!this.context.camera.orthographic) ? this.GetPerspectiveProjectionMatrix(vector) : this.GetOrthographicProjectionMatrix(vector));
  35. }
  36. this.context.camera.useJitteredProjectionMatrixForTransparentRendering = false;
  37. vector.x /= (float)this.context.width;
  38. vector.y /= (float)this.context.height;
  39. Material material = this.context.materialFactory.Get("Hidden/Post FX/Temporal Anti-aliasing");
  40. material.SetVector(TaaComponent.Uniforms._Jitter, vector);
  41. this.jitterVector = vector;
  42. }
  43. public void Render(RenderTexture source, RenderTexture destination)
  44. {
  45. Material material = this.context.materialFactory.Get("Hidden/Post FX/Temporal Anti-aliasing");
  46. material.shaderKeywords = null;
  47. AntialiasingModel.TaaSettings taaSettings = base.model.settings.taaSettings;
  48. if (this.m_ResetHistory || this.m_HistoryTexture == null || this.m_HistoryTexture.width != source.width || this.m_HistoryTexture.height != source.height)
  49. {
  50. if (this.m_HistoryTexture)
  51. {
  52. RenderTexture.ReleaseTemporary(this.m_HistoryTexture);
  53. }
  54. this.m_HistoryTexture = RenderTexture.GetTemporary(source.width, source.height, 0, source.format);
  55. this.m_HistoryTexture.name = "TAA History";
  56. Graphics.Blit(source, this.m_HistoryTexture, material, 2);
  57. }
  58. material.SetVector(TaaComponent.Uniforms._SharpenParameters, new Vector4(taaSettings.sharpen, 0f, 0f, 0f));
  59. material.SetVector(TaaComponent.Uniforms._FinalBlendParameters, new Vector4(taaSettings.stationaryBlending, taaSettings.motionBlending, 6000f, 0f));
  60. material.SetTexture(TaaComponent.Uniforms._MainTex, source);
  61. material.SetTexture(TaaComponent.Uniforms._HistoryTex, this.m_HistoryTexture);
  62. RenderTexture temporary = RenderTexture.GetTemporary(source.width, source.height, 0, source.format);
  63. temporary.name = "TAA History";
  64. this.m_MRT[0] = destination.colorBuffer;
  65. this.m_MRT[1] = temporary.colorBuffer;
  66. Graphics.SetRenderTarget(this.m_MRT, source.depthBuffer);
  67. GraphicsUtils.Blit(material, (!this.context.camera.orthographic) ? 0 : 1);
  68. RenderTexture.ReleaseTemporary(this.m_HistoryTexture);
  69. this.m_HistoryTexture = temporary;
  70. this.m_ResetHistory = false;
  71. }
  72. private float GetHaltonValue(int index, int radix)
  73. {
  74. float num = 0f;
  75. float num2 = 1f / (float)radix;
  76. while (index > 0)
  77. {
  78. num += (float)(index % radix) * num2;
  79. index /= radix;
  80. num2 /= (float)radix;
  81. }
  82. return num;
  83. }
  84. private Vector2 GenerateRandomOffset()
  85. {
  86. Vector2 result = new Vector2(this.GetHaltonValue(this.m_SampleIndex & 1023, 2), this.GetHaltonValue(this.m_SampleIndex & 1023, 3));
  87. if (++this.m_SampleIndex >= 8)
  88. {
  89. this.m_SampleIndex = 0;
  90. }
  91. return result;
  92. }
  93. private Matrix4x4 GetPerspectiveProjectionMatrix(Vector2 offset)
  94. {
  95. float num = Mathf.Tan(0.008726646f * this.context.camera.fieldOfView);
  96. float num2 = num * this.context.camera.aspect;
  97. offset.x *= num2 / (0.5f * (float)this.context.width);
  98. offset.y *= num / (0.5f * (float)this.context.height);
  99. float num3 = (offset.x - num2) * this.context.camera.nearClipPlane;
  100. float num4 = (offset.x + num2) * this.context.camera.nearClipPlane;
  101. float num5 = (offset.y + num) * this.context.camera.nearClipPlane;
  102. float num6 = (offset.y - num) * this.context.camera.nearClipPlane;
  103. Matrix4x4 result = default(Matrix4x4);
  104. result[0, 0] = 2f * this.context.camera.nearClipPlane / (num4 - num3);
  105. result[0, 1] = 0f;
  106. result[0, 2] = (num4 + num3) / (num4 - num3);
  107. result[0, 3] = 0f;
  108. result[1, 0] = 0f;
  109. result[1, 1] = 2f * this.context.camera.nearClipPlane / (num5 - num6);
  110. result[1, 2] = (num5 + num6) / (num5 - num6);
  111. result[1, 3] = 0f;
  112. result[2, 0] = 0f;
  113. result[2, 1] = 0f;
  114. result[2, 2] = -(this.context.camera.farClipPlane + this.context.camera.nearClipPlane) / (this.context.camera.farClipPlane - this.context.camera.nearClipPlane);
  115. result[2, 3] = -(2f * this.context.camera.farClipPlane * this.context.camera.nearClipPlane) / (this.context.camera.farClipPlane - this.context.camera.nearClipPlane);
  116. result[3, 0] = 0f;
  117. result[3, 1] = 0f;
  118. result[3, 2] = -1f;
  119. result[3, 3] = 0f;
  120. return result;
  121. }
  122. private Matrix4x4 GetOrthographicProjectionMatrix(Vector2 offset)
  123. {
  124. float orthographicSize = this.context.camera.orthographicSize;
  125. float num = orthographicSize * this.context.camera.aspect;
  126. offset.x *= num / (0.5f * (float)this.context.width);
  127. offset.y *= orthographicSize / (0.5f * (float)this.context.height);
  128. float left = offset.x - num;
  129. float right = offset.x + num;
  130. float top = offset.y + orthographicSize;
  131. float bottom = offset.y - orthographicSize;
  132. return Matrix4x4.Ortho(left, right, bottom, top, this.context.camera.nearClipPlane, this.context.camera.farClipPlane);
  133. }
  134. public override void OnDisable()
  135. {
  136. if (this.m_HistoryTexture != null)
  137. {
  138. RenderTexture.ReleaseTemporary(this.m_HistoryTexture);
  139. }
  140. this.m_HistoryTexture = null;
  141. this.m_SampleIndex = 0;
  142. this.ResetHistory();
  143. }
  144. private const string k_ShaderString = "Hidden/Post FX/Temporal Anti-aliasing";
  145. private const int k_SampleCount = 8;
  146. private readonly RenderBuffer[] m_MRT = new RenderBuffer[2];
  147. private int m_SampleIndex;
  148. private bool m_ResetHistory = true;
  149. private RenderTexture m_HistoryTexture;
  150. private static class Uniforms
  151. {
  152. internal static int _Jitter = Shader.PropertyToID("_Jitter");
  153. internal static int _SharpenParameters = Shader.PropertyToID("_SharpenParameters");
  154. internal static int _FinalBlendParameters = Shader.PropertyToID("_FinalBlendParameters");
  155. internal static int _HistoryTex = Shader.PropertyToID("_HistoryTex");
  156. internal static int _MainTex = Shader.PropertyToID("_MainTex");
  157. }
  158. }
  159. }