You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
7.8 KiB

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