LensDistortion.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. namespace UnityEngine.Rendering.PostProcessing
  3. {
  4. /// <summary>
  5. /// This class holds settings for the Lens Distortion effect.
  6. /// </summary>
  7. [Serializable]
  8. [PostProcess(typeof(LensDistortionRenderer), "Unity/Lens Distortion")]
  9. public sealed class LensDistortion : PostProcessEffectSettings
  10. {
  11. /// <summary>
  12. /// The total amount of distortion to apply.
  13. /// </summary>
  14. [Range(-100f, 100f), Tooltip("Total distortion amount.")]
  15. public FloatParameter intensity = new FloatParameter { value = 0f };
  16. /// <summary>
  17. /// Multiplies the intensity value on the x-axis. Setting this value to 0 will disable distortion on this axis.
  18. /// </summary>
  19. [Range(0f, 1f), DisplayName("X Multiplier"), Tooltip("Intensity multiplier on the x-axis. Set it to 0 to disable distortion on this axis.")]
  20. public FloatParameter intensityX = new FloatParameter { value = 1f };
  21. /// <summary>
  22. /// Multiplies the intensity value on the y-axis. Setting this value to 0 will disable distortion on this axis.
  23. /// </summary>
  24. [Range(0f, 1f), DisplayName("Y Multiplier"), Tooltip("Intensity multiplier on the y-axis. Set it to 0 to disable distortion on this axis.")]
  25. public FloatParameter intensityY = new FloatParameter { value = 1f };
  26. /// <summary>
  27. /// The center point for the distortion (x-axis).
  28. /// </summary>
  29. [Space]
  30. [Range(-1f, 1f), Tooltip("Distortion center point (x-axis).")]
  31. public FloatParameter centerX = new FloatParameter { value = 0f };
  32. /// <summary>
  33. /// The center point for the distortion (y-axis).
  34. /// </summary>
  35. [Range(-1f, 1f), Tooltip("Distortion center point (y-axis).")]
  36. public FloatParameter centerY = new FloatParameter { value = 0f };
  37. /// <summary>
  38. /// A global screen scaling factor.
  39. /// </summary>
  40. [Space]
  41. [Range(0.01f, 5f), Tooltip("Global screen scaling.")]
  42. public FloatParameter scale = new FloatParameter { value = 1f };
  43. /// <inheritdoc />
  44. public override bool IsEnabledAndSupported(PostProcessRenderContext context)
  45. {
  46. return enabled.value
  47. && !Mathf.Approximately(intensity, 0f)
  48. && (intensityX > 0f || intensityY > 0f)
  49. && !RuntimeUtilities.isVREnabled;
  50. }
  51. }
  52. internal sealed class LensDistortionRenderer : PostProcessEffectRenderer<LensDistortion>
  53. {
  54. public override void Render(PostProcessRenderContext context)
  55. {
  56. var sheet = context.uberSheet;
  57. float amount = 1.6f * Math.Max(Mathf.Abs(settings.intensity.value), 1f);
  58. float theta = Mathf.Deg2Rad * Math.Min(160f, amount);
  59. float sigma = 2f * Mathf.Tan(theta * 0.5f);
  60. var p0 = new Vector4(settings.centerX.value, settings.centerY.value, Mathf.Max(settings.intensityX.value, 1e-4f), Mathf.Max(settings.intensityY.value, 1e-4f));
  61. var p1 = new Vector4(settings.intensity.value >= 0f ? theta : 1f / theta, sigma, 1f / settings.scale.value, settings.intensity.value);
  62. sheet.EnableKeyword("DISTORT");
  63. sheet.properties.SetVector(ShaderIDs.Distortion_CenterScale, p0);
  64. sheet.properties.SetVector(ShaderIDs.Distortion_Amount, p1);
  65. }
  66. }
  67. }