SubpixelMorphologicalAntialiasing.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. namespace UnityEngine.Rendering.PostProcessing
  3. {
  4. /// <summary>
  5. /// This class holds settings for the Subpixel Morphological Anti-aliasing (SMAA) effect.
  6. /// </summary>
  7. [Serializable]
  8. public sealed class SubpixelMorphologicalAntialiasing
  9. {
  10. enum Pass
  11. {
  12. EdgeDetection = 0,
  13. BlendWeights = 3,
  14. NeighborhoodBlending = 6
  15. }
  16. /// <summary>
  17. /// Quality presets.
  18. /// </summary>
  19. public enum Quality
  20. {
  21. /// <summary>
  22. /// Low quality.
  23. /// </summary>
  24. Low = 0,
  25. /// <summary>
  26. /// Medium quality.
  27. /// </summary>
  28. Medium = 1,
  29. /// <summary>
  30. /// High quality.
  31. /// </summary>
  32. High = 2
  33. }
  34. /// <summary>
  35. /// The quality preset to use for the anti-aliasing filter.
  36. /// </summary>
  37. [Tooltip("Lower quality is faster at the expense of visual quality (Low = ~60%, Medium = ~80%).")]
  38. public Quality quality = Quality.High;
  39. /// <summary>
  40. /// Checks if the effect is supported on the target platform.
  41. /// </summary>
  42. /// <returns><c>true</c> if the anti-aliasing filter is supported, <c>false</c> otherwise</returns>
  43. public bool IsSupported()
  44. {
  45. return !RuntimeUtilities.isSinglePassStereoEnabled;
  46. }
  47. internal void Render(PostProcessRenderContext context)
  48. {
  49. var sheet = context.propertySheets.Get(context.resources.shaders.subpixelMorphologicalAntialiasing);
  50. sheet.properties.SetTexture("_AreaTex", context.resources.smaaLuts.area);
  51. sheet.properties.SetTexture("_SearchTex", context.resources.smaaLuts.search);
  52. var cmd = context.command;
  53. cmd.BeginSample("SubpixelMorphologicalAntialiasing");
  54. cmd.GetTemporaryRT(ShaderIDs.SMAA_Flip, context.width, context.height, 0, FilterMode.Bilinear, context.sourceFormat, RenderTextureReadWrite.Linear);
  55. cmd.GetTemporaryRT(ShaderIDs.SMAA_Flop, context.width, context.height, 0, FilterMode.Bilinear, context.sourceFormat, RenderTextureReadWrite.Linear);
  56. cmd.BlitFullscreenTriangle(context.source, ShaderIDs.SMAA_Flip, sheet, (int)Pass.EdgeDetection + (int)quality, true);
  57. cmd.BlitFullscreenTriangle(ShaderIDs.SMAA_Flip, ShaderIDs.SMAA_Flop, sheet, (int)Pass.BlendWeights + (int)quality);
  58. cmd.SetGlobalTexture("_BlendTex", ShaderIDs.SMAA_Flop);
  59. cmd.BlitFullscreenTriangle(context.source, context.destination, sheet, (int)Pass.NeighborhoodBlending);
  60. cmd.ReleaseTemporaryRT(ShaderIDs.SMAA_Flip);
  61. cmd.ReleaseTemporaryRT(ShaderIDs.SMAA_Flop);
  62. cmd.EndSample("SubpixelMorphologicalAntialiasing");
  63. }
  64. }
  65. }