// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) VRMADA, All rights reserved. // // -------------------------------------------------------------------------------------------------------------------- using System; using UltimateXR.Animation.Interpolation; using UltimateXR.Extensions.Unity; using UnityEngine; namespace UltimateXR.Animation.Lights { /// /// Component that allows to animate a light's intensity. /// public class UxrAnimatedLightIntensity : UxrAnimatedComponent { #region Inspector Properties/Serialized Fields [SerializeField] private Light _light; #endregion #region Public Types & Data /// public new float Speed { get => base.Speed.x; set => base.Speed = ToVector4(value); } /// public new float InterpolatedValueStart { get => base.InterpolatedValueStart.x; set => base.InterpolatedValueStart = ToVector4(value); } /// public new float InterpolatedValueEnd { get => base.InterpolatedValueEnd.x; set => base.InterpolatedValueEnd = ToVector4(value); } /// public new float InterpolatedValueWhenDisabled { get => base.InterpolatedValueWhenDisabled.x; set => base.InterpolatedValueWhenDisabled = ToVector4(value); } /// public new float NoiseValueMin { get => base.NoiseValueMin.x; set => base.NoiseValueMin = ToVector4(value); } /// public new float NoiseValueMax { get => base.NoiseValueMax.x; set => base.NoiseValueMax = ToVector4(value); } /// public new float NoiseValueStart { get => base.NoiseValueStart.x; set => base.NoiseValueStart = ToVector4(value); } /// public new float NoiseValueEnd { get => base.NoiseValueEnd.x; set => base.NoiseValueEnd = ToVector4(value); } /// public new float NoiseFrequency { get => base.NoiseFrequency.x; set => base.NoiseFrequency = ToVector4(value); } /// public new float NoiseOffset { get => base.NoiseOffset.x; set => base.NoiseOffset = ToVector4(value); } #endregion #region Public Methods /// /// Starts an animation at a constant speed /// /// The light component to apply the animation to /// /// The animation speed. For int/float values use .x, for Vector2 use x and y. For /// Vector3 use x, y, z. etc. /// /// /// If it is true then Time.unscaledTime will be used /// to count seconds. By default it is false meaning Time.time will be used instead. /// Time.time is affected by Time.timeScale which in many cases is used for application pauses /// or bullet-time effects, while Time.unscaledTime is not. /// /// Animation component public static UxrAnimatedLightIntensity Animate(Light light, float speed, bool useUnscaledTime = false) { UxrAnimatedLightIntensity component = light.gameObject.GetOrAddComponent(); if (component) { component._light = light; component.AnimationMode = UxrAnimationMode.Speed; component.Speed = speed; component.UseUnscaledTime = useUnscaledTime; component.StartTimer(); } return component; } /// /// Starts an animation using an interpolation curve /// /// The light component to apply the animation to /// The start intensity value /// The end intensity value /// The interpolation settings with the curve parameters /// Optional callback when the animation finished /// Animation component public static UxrAnimatedLightIntensity AnimateInterpolation(Light light, float startValue, float endValue, UxrInterpolationSettings settings, Action finishedCallback = null) { UxrAnimatedLightIntensity component = light.gameObject.GetOrAddComponent(); if (component) { component._light = light; component.AnimationMode = UxrAnimationMode.Interpolate; component.InterpolatedValueStart = startValue; component.InterpolatedValueEnd = endValue; component.InterpolationSettings = settings; component._finishedCallback = finishedCallback; component.StartTimer(); } return component; } /// /// Starts an animation using noise. /// /// The light component to apply the animation to /// The time in seconds the noise will start (Time.time or Time.unscaledTime value) /// The duration in seconds of the noise animation /// The start intensity value /// The end intensity value /// The minimum intensity value for the noise /// The maximum intensity value for the noise /// The noise frequency /// If true it will use Time.unscaledTime, if false it will use Time.time /// Optional callback when the animation finished /// Animation component public static UxrAnimatedLightIntensity AnimateNoise(Light light, float noiseTimeStart, float noiseTimeDuration, float noiseValueStart, float noiseValueEnd, float noiseValueMin, float noiseValueMax, float noiseValueFrequency, bool useUnscaledTime = false, Action finishedCallback = null) { UxrAnimatedLightIntensity component = light.gameObject.GetOrAddComponent(); if (component) { component._light = light; component.AnimationMode = UxrAnimationMode.Noise; component.NoiseTimeStart = noiseTimeStart; component.NoiseDurationSeconds = noiseTimeDuration; component.NoiseValueStart = noiseValueStart; component.NoiseValueEnd = noiseValueEnd; component.NoiseValueMin = noiseValueMin; component.NoiseValueMax = noiseValueMax; component.NoiseFrequency = noiseValueFrequency; component.UseUnscaledTime = useUnscaledTime; component._finishedCallback = finishedCallback; component.StartTimer(); } return component; } #endregion #region Unity /// /// Stores the initial light intensity /// protected override void Awake() { base.Awake(); _initialIntensity = _light != null ? _light.intensity : 0.0f; } #endregion #region Event Trigger Methods /// protected override void OnFinished(UxrAnimatedLightIntensity anim) { base.OnFinished(anim); _finishedCallback?.Invoke(); } #endregion #region Protected Overrides UxrAnimatedComponent /// protected override void RestoreOriginalValue() { if (_light != null) { _light.intensity = _initialIntensity; } } /// protected override Vector4 GetParameterValue() { return ToVector4(_light != null ? _light.intensity : 0.0f); } /// protected override void SetParameterValue(Vector4 value) { if (_light != null) { _light.intensity = value.x; } } #endregion #region Private Types & Data private float _initialIntensity; private Action _finishedCallback; #endregion } }