3 namespace UnityEngine
.Rendering
.PostProcessing
6 /// This class holds settings for the Lens Distortion effect.
9 [PostProcess(typeof(LensDistortionRenderer
), "Unity/Lens Distortion")]
10 public sealed class LensDistortion
: PostProcessEffectSettings
13 /// The total amount of distortion to apply.
15 [Range(-100f
, 100f
), Tooltip("Total distortion amount.")]
16 public FloatParameter intensity
= new FloatParameter { value = 0f }
;
19 /// Multiplies the intensity value on the x-axis. Setting this value to 0 will disable distortion on this axis.
21 [Range(0f
, 1f
), DisplayName("X Multiplier"), Tooltip("Intensity multiplier on the x-axis. Set it to 0 to disable distortion on this axis.")]
22 public FloatParameter intensityX
= new FloatParameter { value = 1f }
;
25 /// Multiplies the intensity value on the y-axis. Setting this value to 0 will disable distortion on this axis.
27 [Range(0f
, 1f
), DisplayName("Y Multiplier"), Tooltip("Intensity multiplier on the y-axis. Set it to 0 to disable distortion on this axis.")]
28 public FloatParameter intensityY
= new FloatParameter { value = 1f }
;
31 /// The center point for the distortion (x-axis).
34 [Range(-1f
, 1f
), Tooltip("Distortion center point (x-axis).")]
35 public FloatParameter centerX
= new FloatParameter { value = 0f }
;
38 /// The center point for the distortion (y-axis).
40 [Range(-1f
, 1f
), Tooltip("Distortion center point (y-axis).")]
41 public FloatParameter centerY
= new FloatParameter { value = 0f }
;
44 /// A global screen scaling factor.
47 [Range(0.01f
, 5f
), Tooltip("Global screen scaling.")]
48 public FloatParameter scale
= new FloatParameter { value = 1f }
;
51 public override bool IsEnabledAndSupported(PostProcessRenderContext context
)
54 && !Mathf
.Approximately(intensity
, 0f
)
55 && (intensityX
> 0f
|| intensityY
> 0f
)
56 && !RuntimeUtilities
.isVREnabled
;
60 #if UNITY_2017_1_OR_NEWER
61 [UnityEngine
.Scripting
.Preserve
]
63 internal sealed class LensDistortionRenderer
: PostProcessEffectRenderer
<LensDistortion
>
65 public override void Render(PostProcessRenderContext context
)
67 var sheet
= context
.uberSheet
;
69 float amount
= 1.6f
* Math
.Max(Mathf
.Abs(settings
.intensity
.value), 1f
);
70 float theta
= Mathf
.Deg2Rad
* Math
.Min(160f
, amount
);
71 float sigma
= 2f
* Mathf
.Tan(theta
* 0.5f
);
72 var p0
= new Vector4(settings
.centerX
.value, settings
.centerY
.value, Mathf
.Max(settings
.intensityX
.value, 1e-4f
), Mathf
.Max(settings
.intensityY
.value, 1e-4f
));
73 var p1
= new Vector4(settings
.intensity
.value >= 0f
? theta
: 1f
/ theta
, sigma
, 1f
/ settings
.scale
.value, settings
.intensity
.value);
75 sheet
.EnableKeyword("DISTORT");
76 sheet
.properties
.SetVector(ShaderIDs
.Distortion_CenterScale
, p0
);
77 sheet
.properties
.SetVector(ShaderIDs
.Distortion_Amount
, p1
);