Vignette.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. namespace UnityEngine.Rendering.PostProcessing
  3. {
  4. /// <summary>
  5. /// A list of available render modes for the Vignette effect.
  6. /// </summary>
  7. public enum VignetteMode
  8. {
  9. /// <summary>
  10. /// This mode offers parametric controls for the position, shape and intensity of the Vignette.
  11. /// </summary>
  12. Classic,
  13. /// <summary>
  14. /// This mode multiplies a custom texture mask over the screen to create a Vignette effect.
  15. /// </summary>
  16. Masked
  17. }
  18. /// <summary>
  19. /// A volume parameter holding a <see cref="VignetteMode"/> value.
  20. /// </summary>
  21. [Serializable]
  22. public sealed class VignetteModeParameter : ParameterOverride<VignetteMode> {}
  23. /// <summary>
  24. /// This class holds settings for the Vignette effect.
  25. /// </summary>
  26. [Serializable]
  27. [PostProcess(typeof(VignetteRenderer), "Unity/Vignette")]
  28. public sealed class Vignette : PostProcessEffectSettings
  29. {
  30. /// <summary>
  31. /// Use the \"Classic\" mode for parametric controls. Use the \"Masked\" mode to use your own texture mask.
  32. /// </summary>
  33. [Tooltip("Use the \"Classic\" mode for parametric controls. Use the \"Masked\" mode to use your own texture mask.")]
  34. public VignetteModeParameter mode = new VignetteModeParameter { value = VignetteMode.Classic };
  35. /// <summary>
  36. /// The color to use to tint the vignette.
  37. /// </summary>
  38. [Tooltip("Vignette color.")]
  39. public ColorParameter color = new ColorParameter { value = new Color(0f, 0f, 0f, 1f) };
  40. /// <summary>
  41. /// Sets the vignette center point (screen center is <c>[0.5,0.5]</c>).
  42. /// </summary>
  43. [Tooltip("Sets the vignette center point (screen center is [0.5, 0.5]).")]
  44. public Vector2Parameter center = new Vector2Parameter { value = new Vector2(0.5f, 0.5f) };
  45. /// <summary>
  46. /// The amount of vignetting on screen.
  47. /// </summary>
  48. [Range(0f, 1f), Tooltip("Amount of vignetting on screen.")]
  49. public FloatParameter intensity = new FloatParameter { value = 0f };
  50. /// <summary>
  51. /// The smoothness of the vignette borders.
  52. /// </summary>
  53. [Range(0.01f, 1f), Tooltip("Smoothness of the vignette borders.")]
  54. public FloatParameter smoothness = new FloatParameter { value = 0.2f };
  55. /// <summary>
  56. /// Lower values will make a square-ish vignette.
  57. /// </summary>
  58. [Range(0f, 1f), Tooltip("Lower values will make a square-ish vignette.")]
  59. public FloatParameter roundness = new FloatParameter { value = 1f };
  60. /// <summary>
  61. /// Should the vignette be perfectly round or be dependent on the current aspect ratio?
  62. /// </summary>
  63. [Tooltip("Set to true to mark the vignette to be perfectly round. False will make its shape dependent on the current aspect ratio.")]
  64. public BoolParameter rounded = new BoolParameter { value = false };
  65. /// <summary>
  66. /// A black and white mask to use as a vignette.
  67. /// </summary>
  68. [Tooltip("A black and white mask to use as a vignette.")]
  69. public TextureParameter mask = new TextureParameter { value = null };
  70. /// <summary>
  71. /// Mask opacity.
  72. /// </summary>
  73. [Range(0f, 1f), Tooltip("Mask opacity.")]
  74. public FloatParameter opacity = new FloatParameter { value = 1f };
  75. /// <inheritdoc />
  76. public override bool IsEnabledAndSupported(PostProcessRenderContext context)
  77. {
  78. return enabled.value
  79. && ((mode.value == VignetteMode.Classic && intensity.value > 0f)
  80. || (mode.value == VignetteMode.Masked && opacity.value > 0f && mask.value != null));
  81. }
  82. }
  83. internal sealed class VignetteRenderer : PostProcessEffectRenderer<Vignette>
  84. {
  85. public override void Render(PostProcessRenderContext context)
  86. {
  87. var sheet = context.uberSheet;
  88. sheet.EnableKeyword("VIGNETTE");
  89. sheet.properties.SetColor(ShaderIDs.Vignette_Color, settings.color.value);
  90. if (settings.mode == VignetteMode.Classic)
  91. {
  92. sheet.properties.SetFloat(ShaderIDs.Vignette_Mode, 0f);
  93. sheet.properties.SetVector(ShaderIDs.Vignette_Center, settings.center.value);
  94. float roundness = (1f - settings.roundness.value) * 6f + settings.roundness.value;
  95. sheet.properties.SetVector(ShaderIDs.Vignette_Settings, new Vector4(settings.intensity.value * 3f, settings.smoothness.value * 5f, roundness, settings.rounded.value ? 1f : 0f));
  96. }
  97. else // Masked
  98. {
  99. sheet.properties.SetFloat(ShaderIDs.Vignette_Mode, 1f);
  100. sheet.properties.SetTexture(ShaderIDs.Vignette_Mask, settings.mask.value);
  101. sheet.properties.SetFloat(ShaderIDs.Vignette_Opacity, Mathf.Clamp01(settings.opacity.value));
  102. }
  103. }
  104. }
  105. }