/// <summary> /// Creates an animation curve of the specified type and resolution, and adds it to the specified asset /// </summary> /// <param name="asset"></param> /// <param name="curveType"></param> /// <param name="curveResolution"></param> /// <param name="anti"></param> protected virtual void CreateAnimationCurve(ScriptableObject asset, MMTween.MMTweenCurve curveType, int curveResolution, bool anti) { // generates an animation curve AnimationCurve animationCurve = new AnimationCurve(); for (int i = 0; i < curveResolution; i++) { _keyframe.time = i / (curveResolution - 1f); if (anti) { _keyframe.value = MMTween.Tween(_keyframe.time, 0f, 1f, 1f, 0f, curveType); } else { _keyframe.value = MMTween.Tween(_keyframe.time, 0f, 1f, 0f, 1f, curveType); } animationCurve.AddKey(_keyframe); } // smoothes the curve's tangents for (int j = 0; j < curveResolution; j++) { animationCurve.SmoothTangents(j, 0f); } // we add the curve to the scriptable object _parameters = new object[] { animationCurve, curveType.ToString() }; _addMethodInfo.Invoke(asset, _parameters); }
protected static IEnumerator MoveTransformCo(Transform targetTransform, Transform origin, Transform destination, WaitForSeconds delay, float delayDuration, float duration, MMTween.MMTweenCurve curve, bool updatePosition = true, bool updateRotation = true) { if (delayDuration > 0f) { yield return(delay); } float timeLeft = duration; while (timeLeft > 0f) { if (updatePosition) { targetTransform.transform.position = MMTween.Tween(duration - timeLeft, 0f, duration, origin.position, destination.position, curve); } if (updateRotation) { targetTransform.transform.rotation = MMTween.Tween(duration - timeLeft, 0f, duration, origin.rotation, destination.rotation, curve); } timeLeft -= Time.deltaTime; yield return(null); } if (updatePosition) { targetTransform.transform.position = destination.position; } if (updateRotation) { targetTransform.transform.localEulerAngles = destination.localEulerAngles; } }
/// <summary> /// Fades the canvasgroup towards its target alpha /// </summary> protected virtual void Fade() { float currentTime = IgnoreTimescale ? Time.unscaledTime : Time.time; if (_frameCountOne) { if (Time.frameCount <= 2) { _canvasGroup.alpha = _initialAlpha; return; } _fadeStartedAt = IgnoreTimescale ? Time.unscaledTime : Time.time; currentTime = _fadeStartedAt; _frameCountOne = false; } float endTime = _fadeStartedAt + _currentDuration; if (currentTime - _fadeStartedAt < _currentDuration) { float result = MMTween.Tween(currentTime, _fadeStartedAt, endTime, _initialAlpha, _currentTargetAlpha, _currentCurve); _canvasGroup.alpha = result; } else { StopFading(); } }
protected static IEnumerator RotateTransformAroundCo(Transform targetTransform, Transform center, Transform destination, float angle, WaitForSeconds delay, float delayDuration, float duration, MMTween.MMTweenCurve curve) { if (delayDuration > 0f) { yield return(delay); } Vector3 initialRotationPosition = targetTransform.transform.position; Quaternion initialRotationRotation = targetTransform.transform.rotation; float rate = 1f / duration; float timeSpent = 0f; while (timeSpent < duration) { float newAngle = MMTween.Tween(timeSpent, 0f, duration, 0f, angle, curve); targetTransform.transform.position = initialRotationPosition; initialRotationRotation = targetTransform.transform.rotation; targetTransform.RotateAround(center.transform.position, center.transform.up, newAngle); targetTransform.transform.rotation = initialRotationRotation; timeSpent += Time.deltaTime; yield return(null); } targetTransform.transform.position = destination.position; }
/// <summary> /// A coroutine that expands the mask to cover both its current position and its destination area, then resizes itself to match the destination size /// </summary> /// <param name="newPosition"></param> /// <param name="newSize"></param> /// <param name="duration"></param> /// <param name="curve"></param> /// <returns></returns> protected virtual IEnumerator ExpandAndMoveMaskToCoroutine(Vector2 newPosition, Vector2 newSize, float duration, MMTween.MMTweenCurve curve) { if (duration > 0) { _initialPosition = this.transform.position; _initialScale = this.transform.localScale; float startedAt = MaskTime; // first we move to the total size and position _targetScale.x = this.transform.localScale.x / 2f + Mathf.Abs((this.transform.position - (Vector3)newPosition).x) * ScaleMultiplier + ComputeTargetScale(newSize).x / 2f; _targetScale.y = this.transform.localScale.y / 2f + Mathf.Abs((this.transform.position - (Vector3)newPosition).y) * ScaleMultiplier + ComputeTargetScale(newSize).y / 2f; _targetScale.z = 1f; _targetPosition = ( (this.transform.position + (Vector3.up * this.transform.localScale.y / ScaleMultiplier / 2f) + (Vector3.left * this.transform.localScale.x / ScaleMultiplier / 2f)) + ((Vector3)newPosition + (Vector3.down * newSize.y / 2f) + (Vector3.right * newSize.x / 2f)) ) / 2f; while (MaskTime - startedAt <= (duration / 2f)) { float currentTime = MaskTime - startedAt; _newPosition = MMTween.Tween(currentTime, 0f, (duration / 2f), _initialPosition, _targetPosition, curve); _newScale = MMTween.Tween(currentTime, 0f, (duration / 2f), _initialScale, _targetScale, curve); this.transform.position = _newPosition; this.transform.localScale = _newScale; yield return(null); } // then we move to the final position startedAt = MaskTime; _initialPosition = this.transform.position; _initialScale = this.transform.localScale; _targetPosition = ComputeTargetPosition(newPosition); _targetScale = ComputeTargetScale(newSize); while (MaskTime - startedAt <= duration / 2f) { float currentTime = MaskTime - startedAt; _newPosition = MMTween.Tween(currentTime, 0f, (duration / 2f), _initialPosition, _targetPosition, curve); _newScale = MMTween.Tween(currentTime, 0f, (duration / 2f), _initialScale, _targetScale, curve); this.transform.position = _newPosition; this.transform.localScale = _newScale; yield return(null); } } this.transform.position = ComputeTargetPosition(newPosition); this.transform.localScale = ComputeTargetScale(newSize); }
/// <summary> /// Fades the canvasgroup towards its target alpha /// </summary> protected virtual void Fade() { float currentTime = IgnoreTimescale ? Time.unscaledTime : Time.time; float endTime = _fadeStartedAt + _currentDuration; if (currentTime - _fadeStartedAt < _currentDuration) { float result = MMTween.Tween(currentTime, _fadeStartedAt, endTime, _initialAlpha, _currentTargetAlpha, _currentCurve); _canvasGroup.alpha = result; } else { StopFading(); } }
/// <summary> /// Fades the canvasgroup towards its target alpha /// </summary> protected virtual void Fade() { float currentTime = IgnoreTimescale ? Time.unscaledTime : Time.time; float endTime = _fadeStartedAt + _currentDuration; if (currentTime - _fadeStartedAt < _currentDuration) { float newScale = MMTween.Tween(currentTime, _fadeStartedAt, endTime, _initialScale, _currentTargetScale, _currentCurve); FaderMask.transform.localScale = newScale * Vector3.one; } else { StopFading(); } }
/// <summary> /// Fades the canvasgroup towards its target alpha /// </summary> protected virtual void Fade() { float currentTime = IgnoreTimescale ? Time.unscaledTime : Time.time; float endTime = _fadeStartedAt + _currentDuration; if (currentTime - _fadeStartedAt < _currentDuration) { _newPosition = MMTween.Tween(currentTime, _fadeStartedAt, endTime, _fromPosition, _toPosition, _currentCurve); _rectTransform.anchoredPosition = _newPosition; } else { StopFading(); } }
protected static IEnumerator MoveTransformCo(Transform targetTransform, Vector3 origin, Vector3 destination, WaitForSeconds delay, float delayDuration, float duration, MMTween.MMTweenCurve curve, bool ignoreTimescale = false) { if (delayDuration > 0f) { yield return(delay); } float timeLeft = duration; while (timeLeft > 0f) { targetTransform.transform.position = MMTween.Tween(duration - timeLeft, 0f, duration, origin, destination, curve); timeLeft -= ignoreTimescale ? Time.unscaledDeltaTime : Time.deltaTime; yield return(null); } targetTransform.transform.position = destination; }
/// <summary> /// Fades an entire track over time /// </summary> /// <param name="track"></param> /// <param name="duration"></param> /// <param name="initialVolume"></param> /// <param name="finalVolume"></param> /// <param name="tweenType"></param> /// <returns></returns> protected virtual IEnumerator FadeTrackCoroutine(MMSoundManagerTracks track, float duration, float initialVolume, float finalVolume, MMTweenType tweenType) { float startedAt = Time.unscaledTime; if (tweenType == null) { tweenType = new MMTweenType(MMTween.MMTweenCurve.EaseInOutQuartic); } while (Time.unscaledTime - startedAt <= duration) { float elapsedTime = Time.unscaledTime - startedAt; float newVolume = MMTween.Tween(elapsedTime, 0f, duration, initialVolume, finalVolume, tweenType); settingsSo.SetTrackVolume(track, newVolume); yield return(null); } settingsSo.SetTrackVolume(track, finalVolume); }
/// <summary> /// Fades an audiosource's volume over time /// </summary> /// <param name="source"></param> /// <param name="duration"></param> /// <param name="initialVolume"></param> /// <param name="finalVolume"></param> /// <param name="tweenType"></param> /// <returns></returns> protected virtual IEnumerator FadeCoroutine(AudioSource source, float duration, float initialVolume, float finalVolume, MMTweenType tweenType) { float startedAt = Time.unscaledTime; if (tweenType == null) { tweenType = new MMTweenType(MMTween.MMTweenCurve.EaseInOutQuartic); } while (Time.unscaledTime - startedAt <= duration) { float elapsedTime = Time.unscaledTime - startedAt; float newVolume = MMTween.Tween(elapsedTime, 0f, duration, initialVolume, finalVolume, tweenType); source.volume = newVolume; yield return(null); } source.volume = finalVolume; }
protected static IEnumerator MoveRectTransformCo(RectTransform targetTransform, Vector3 origin, Vector3 destination, WaitForSeconds delay, float delayDuration, float duration, MMTween.MMTweenCurve curve) { if (delayDuration > 0f) { yield return(delay); } float timeLeft = duration; while (timeLeft > 0f) { targetTransform.localPosition = MMTween.Tween(duration - timeLeft, 0f, duration, origin, destination, curve); timeLeft -= Time.deltaTime; yield return(null); } targetTransform.localPosition = destination; }
/// <summary> /// Moves the filter towards its current target position /// </summary> protected virtual void MoveTowardsCurrentTarget() { if (_newPosition != this.transform.localPosition) { this.transform.localPosition = _newPosition; } float originY = Active ? _initialPosition.y + FilterOffset.y : _initialPosition.y + FilterOffset.x; float targetY = Active ? _initialPosition.y + FilterOffset.x : _initialPosition.y + FilterOffset.y; float currentTime = (TimeScale == TimeScales.Unscaled) ? Time.unscaledTime : Time.time; _newPosition = this.transform.localPosition; _newPosition.y = MMTween.Tween(currentTime - _lastMovementStartedAt, 0f, _duration, originY, targetY, Curve); if (currentTime - _lastMovementStartedAt > _duration) { _newPosition.y = targetY; this.transform.localPosition = _newPosition; _lastReachedState = Active; } }
protected virtual void StartFading(float initialAlpha, float endAlpha, float duration, MMTweenType curve, int id, bool ignoreTimeScale, Vector3 worldPosition) { if (id != ID) { return; } if (TargetCamera == null) { Debug.LogWarning(this.name + " : You're using a fader round but its TargetCamera hasn't been setup in its inspector. It can't fade."); return; } FaderMask.anchoredPosition = Vector3.zero; Vector3 viewportPosition = TargetCamera.WorldToViewportPoint(worldPosition); viewportPosition.x = Mathf.Clamp01(viewportPosition.x); viewportPosition.y = Mathf.Clamp01(viewportPosition.y); viewportPosition.z = Mathf.Clamp01(viewportPosition.z); FaderMask.anchorMin = viewportPosition; FaderMask.anchorMax = viewportPosition; IgnoreTimescale = ignoreTimeScale; EnableFader(); _fading = true; _initialScale = initialAlpha; _currentTargetScale = endAlpha; _fadeStartedAt = IgnoreTimescale ? Time.unscaledTime : Time.time; _currentCurve = curve; _currentDuration = duration; float newScale = MMTween.Tween(0f, 0f, duration, _initialScale, _currentTargetScale, _currentCurve); FaderMask.transform.localScale = newScale * Vector3.one; }
/// <summary> /// A coroutine used to toggle the menu /// </summary> /// <param name="active"></param> /// <returns></returns> protected virtual IEnumerator ToggleCo(bool active) { if (_toggling) { yield break; } if (!active) { _containerRect.gameObject.SetActive(true); } _toggling = true; Active = active; _newPosition = active ? _offPosition : _initialContainerPosition; MMTween.MoveRectTransform(this, _containerRect, _containerRect.localPosition, _newPosition, null, 0f, Data.ToggleDuration, Data.ToggleCurve); yield return(MMCoroutine.WaitFor(Data.ToggleDuration)); if (active) { _containerRect.gameObject.SetActive(false); } Active = !active; _toggling = false; }
/// <summary> /// Starts a fade /// </summary> /// <param name="fadingIn"></param> /// <param name="duration"></param> /// <param name="curve"></param> /// <param name="id"></param> /// <param name="ignoreTimeScale"></param> /// <param name="worldPosition"></param> protected virtual IEnumerator StartFading(bool fadingIn, float duration, MMTweenType curve, int id, bool ignoreTimeScale, Vector3 worldPosition) { if (id != ID) { yield break; } if (InitialDelay > 0f) { yield return(MMCoroutine.WaitFor(InitialDelay)); } if (!_initialized) { Initialization(); } if (curve == null) { curve = DefaultTween; } IgnoreTimescale = ignoreTimeScale; EnableFader(); _fading = true; _fadeStartedAt = IgnoreTimescale ? Time.unscaledTime : Time.time; _currentCurve = curve; _currentDuration = duration; _fromPosition = _rectTransform.anchoredPosition; _toPosition = fadingIn ? _initialPosition : ExitPosition(); _newPosition = MMTween.Tween(0f, 0f, duration, _fromPosition, _toPosition, _currentCurve); _rectTransform.anchoredPosition = _newPosition; }
/// <summary> /// On Update, we move our value based on the defined settings /// </summary> protected virtual void Update() { if ((TargetObject == null) || (TargetAttribute == null) || (!Active) || (!_attributeFound)) { return; } switch (ControlMode) { case ControlModes.PingPong: if (GetTime() - _lastPingPongPauseAt < PingPongPauseDuration) { return; } PingPong += GetDeltaTime() * _pingPongDirection; if (PingPong < 0f) { PingPong = 0f; _pingPongDirection = -_pingPongDirection; _lastPingPongPauseAt = GetTime(); } if (PingPong > Duration) { PingPong = Duration; _pingPongDirection = -_pingPongDirection; _lastPingPongPauseAt = GetTime(); } CurrentValue = MMTween.Tween(PingPong, 0f, Duration, MinValue, MaxValue, Curve); break; case ControlModes.Random: _elapsedTime += GetDeltaTime(); CurrentValue = (Mathf.PerlinNoise(_randomFrequency * _elapsedTime, _randomShift) * 2.0f - 1.0f) * _randomAmplitude; break; case ControlModes.OneTime: if (!_oneTimeShaking) { return; } _remappedTimeSinceStart = MMMaths.Remap(Time.time - _oneTimeStartedTimestamp, 0f, OneTimeDuration, 0f, 1f); CurrentValue = OneTimeCurve.Evaluate(_remappedTimeSinceStart) * OneTimeAmplitude; if (AddToInitialValue) { CurrentValue += InitialValue; } break; case ControlModes.AudioAnalyzer: CurrentValue = AudioAnalyzer.Beats[BeatID].CurrentValue * AudioAnalyzerMultiplier; break; } if (AddToInitialValue) { CurrentValue += InitialValue; } if (_oneTimeShaking && (Time.time - _oneTimeStartedTimestamp > OneTimeDuration)) { _oneTimeShaking = false; CurrentValue = InitialValue; } TargetAttribute.SetValue(CurrentValue); }
protected virtual void Update() { _curvePointNewMovement = Vector3Zero; _curvePointNewMovement.x = _currentMovement; _parameter[0] = _currentMovement; _newValue = InvokeTween(TweenMethodIndex, _parameter); _curvePointNewMovement.y = _newValue; _curvePointNewMovement *= GraphSize; _axis.PlotterCurvePoint.transform.localPosition = _curvePointNewMovement; _curvePointNewMovement = _positionPointInitialPosition; _curvePointNewMovement.x = _newValue; _axis.PositionPoint.transform.localPosition = _curvePointNewMovement; _curvePointNewMovement = _positionPointVerticalInitialPosition; _curvePointNewMovement.y = _newValue; _axis.PositionPointVertical.transform.localPosition = _curvePointNewMovement; _curvePointNewMovement = _rotationPointInitialRotation; _curvePointNewMovement.z = _newValue * 360f; _axis.RotationPoint.transform.localEulerAngles = _curvePointNewMovement; _curvePointNewMovement = _scalePointInitialScale; _curvePointNewMovement *= _newValue; _axis.ScalePoint.transform.localScale = _curvePointNewMovement; if (Time.unscaledTime - _lastMovementEndedAt < MovementPauseDuration) { if (Time.unscaledTime - _lastMovementEndedAt < MovementPauseDuration / 2f) { _currentMovement = 1f; _newScaleUnit = MMTween.Tween(Time.unscaledTime - _lastMovementEndedAt, 0f, (MovementPauseDuration / 2f), 1f, 0f, MMTween.MMTweenCurve.EaseInCubic); _newScale = Vector3.one * _newScaleUnit; _axis.PlotterCurvePoint.localScale = _newScale * _plotterCurvePointScale; _axis.PositionPoint.localScale = _newScale; _axis.PositionPointVertical.localScale = _newScale; _axis.RotationPoint.localScale = _newScale; _axis.ScalePoint.localScale = _newScale; } else { _currentMovement = 0f; _newScaleUnit = MMTween.Tween(Time.unscaledTime - _lastMovementEndedAt, (MovementPauseDuration / 2f), MovementPauseDuration / 2f, 0f, 1f, MMTween.MMTweenCurve.EaseOutCubic); _newScale = Vector3.one * _newScaleUnit; _axis.PlotterCurvePoint.localScale = _newScale * _plotterCurvePointScale; _axis.PositionPointVertical.localScale = _newScale; _axis.PositionPoint.localScale = _newScale; _axis.RotationPoint.localScale = _newScale; _axis.ScalePoint.localScale = Vector3.zero; } } else { _axis.PlotterCurvePoint.localScale = Vector3.one * _plotterCurvePointScale; _currentMovement += Time.unscaledDeltaTime; } if (_currentMovement > 1f) { _lastMovementEndedAt = Time.unscaledTime; _currentMovement = 1f; } //_timeString = String.Format("t = {0}s", _currentMovement.ToString("0.000")); _axis.TimeLabel.text = _timeString; }
/// <summary> /// On Update, we move our value based on the defined settings /// </summary> protected virtual void Update() { _targetObjectLastFrame = TargetObject; _targetAttributeLastFrame = TargetAttribute; if ((TargetObject == null) || (TargetAttribute == null) || (!_attributeFound)) { return; } switch (ControlMode) { case ControlModes.PingPong: if (GetTime() - _lastPingPongPauseAt < PingPongPauseDuration) { return; } PingPong += GetDeltaTime() * _pingPongDirection; if (PingPong < 0f) { PingPong = 0f; _pingPongDirection = -_pingPongDirection; _lastPingPongPauseAt = GetTime(); } if (PingPong > Duration) { PingPong = Duration; _pingPongDirection = -_pingPongDirection; _lastPingPongPauseAt = GetTime(); } CurrentValue = MMTween.Tween(PingPong, 0f, Duration, MinValue, MaxValue, Curve); CurrentValueNormalized = MMMaths.Remap(CurrentValue, MinValue, MaxValue, 0f, 1f); break; case ControlModes.Random: _elapsedTime += GetDeltaTime(); CurrentValueNormalized = Mathf.PerlinNoise(_randomFrequency * _elapsedTime, _randomShift); if (RemapNoiseValues) { CurrentValue = CurrentValueNormalized; CurrentValue = MMMaths.Remap(CurrentValue, 0f, 1f, RemapNoiseZero, RemapNoiseOne); } else { CurrentValue = (CurrentValueNormalized * 2.0f - 1.0f) * _randomAmplitude; } break; case ControlModes.OneTime: if (!_shaking) { return; } _remappedTimeSinceStart = MMMaths.Remap(Time.time - _shakeStartTimestamp, 0f, OneTimeDuration, 0f, 1f); CurrentValueNormalized = OneTimeCurve.Evaluate(_remappedTimeSinceStart); CurrentValue = MMMaths.Remap(CurrentValueNormalized, 0f, 1f, OneTimeRemapMin, OneTimeRemapMax); CurrentValue *= OneTimeAmplitude; break; case ControlModes.AudioAnalyzer: if (AudioAnalyzerMode == AudioAnalyzerModes.Beat) { CurrentValue = AudioAnalyzer.Beats[BeatID].CurrentValue * AudioAnalyzerMultiplier; } else { CurrentValue = AudioAnalyzer.NormalizedBufferedBandLevels[NormalizedLevelID] * AudioAnalyzerMultiplier; } CurrentValueNormalized = Mathf.Clamp(AudioAnalyzer.Beats[BeatID].CurrentValue, 0f, 1f); break; case ControlModes.Driven: CurrentValue = DrivenLevel; CurrentValueNormalized = Mathf.Clamp(CurrentValue, 0f, 1f); break; case ControlModes.ToDestination: if (!_shaking) { return; } _remappedTimeSinceStart = MMMaths.Remap(Time.time - _shakeStartTimestamp, 0f, ToDestinationDuration, 0f, 1f); float time = ToDestinationCurve.Evaluate(_remappedTimeSinceStart); CurrentValue = Mathf.LerpUnclamped(_initialValue, ToDestinationValue, time); CurrentValueNormalized = MMMaths.Remap(CurrentValue, _initialValue, ToDestinationValue, 0f, 1f); break; } if (AddToInitialValue) { CurrentValue += InitialValue; } if (ControlMode == ControlModes.OneTime) { if (_shaking && (Time.time - _shakeStartTimestamp > OneTimeDuration)) { _shaking = false; if (RevertToInitialValueAfterEnd) { CurrentValue = InitialValue; TargetAttribute.SetValue(CurrentValue); } else { CurrentValue = OneTimeCurve.Evaluate(1f); CurrentValue = MMMaths.Remap(CurrentValue, 0f, 1f, OneTimeRemapMin, OneTimeRemapMax); CurrentValue *= OneTimeAmplitude; TargetAttribute.SetValue(CurrentValue); } if (DisableAfterOneTime) { this.enabled = false; } if (DisableGameObjectAfterOneTime) { this.gameObject.SetActive(false); } return; } } if (ControlMode == ControlModes.ToDestination) { if (_shaking && (Time.time - _shakeStartTimestamp > ToDestinationDuration)) { _shaking = false; if (RevertToInitialValueAfterEnd) { CurrentValue = InitialValue; } else { CurrentValue = ToDestinationValue; } TargetAttribute.SetValue(CurrentValue); if (DisableAfterOneTime) { this.enabled = false; } if (DisableGameObjectAfterOneTime) { this.gameObject.SetActive(false); } return; } } TargetAttribute.SetValue(CurrentValue); }
/// <summary> /// Returns the corresponding value based on the selected SignalType for a given time value /// </summary> /// <param name="time"></param> /// <param name="signalType"></param> /// <param name="phase"></param> /// <param name="amplitude"></param> /// <param name="frequency"></param> /// <param name="offset"></param> /// <param name="Invert"></param> /// <returns></returns> public static float GetValue(float time, SignalType signalType, float phase, float amplitude, float frequency, float offset, bool Invert = false, AnimationCurve curve = null, MMTween.MMTweenCurve tweenCurve = MMTween.MMTweenCurve.LinearTween) { float value = 0f; float invert = Invert ? -1 : 1; float t = frequency * time + phase; switch (signalType) { case SignalType.Sine: value = (float)Mathf.Sin(2f * Mathf.PI * t); break; case SignalType.Square: value = Mathf.Sign(Mathf.Sin(2f * Mathf.PI * t)); break; case SignalType.Triangle: value = 1f - 4f * (float)Mathf.Abs(Mathf.Round(t - 0.25f) - (t - 0.25f)); break; case SignalType.Sawtooth: value = 2f * (t - (float)Mathf.Floor(t + 0.5f)); break; case SignalType.Pulse: value = (Mathf.Abs(Mathf.Sin(2 * Mathf.PI * t)) < 1.0 - 10E-3) ? (0) : (1); break; case SignalType.WhiteNoise: value = 2f * Random.Range(0, int.MaxValue) / int.MaxValue - 1f; break; case SignalType.DigitalNoise: value = Random.Range(0, 2); break; case SignalType.PerlinNoise: value = Mathf.PerlinNoise(time * frequency, time * amplitude); break; case SignalType.ValueNoise: value = ValueNoise(time, frequency) * amplitude; break; case SignalType.AnimationCurve: if (curve == null) { return(0f); } t = (t != 1f) ? t - Mathf.Floor(t) : 1f; value = curve.Evaluate(t); break; case SignalType.MMTween: t = (t != 1f) ? t - Mathf.Floor(t) : 1f; value = MMTween.Tween(t, 0f, 1f, 0f, 1f, tweenCurve); break; } return(invert * amplitude * value + offset); }
public static float GetValueNormalized(float time, SignalType signalType, float phase, float amplitude, float frequency, float offset, bool Invert = false, AnimationCurve curve = null, MMTween.MMTweenCurve tweenCurve = MMTween.MMTweenCurve.LinearTween, bool clamp = true, float clampMin = 0f, float clampMax = 1f, bool backAndForth = false, float backAndForthTippingPoint = 0.5f) { float value = 0f; float invert = Invert ? -1 : 1; if (backAndForth) { if (time < backAndForthTippingPoint) { time = MMMaths.Remap(time, 0f, backAndForthTippingPoint, 0f, 1f); } else if (time == backAndForthTippingPoint) { time = 1f; } else if (time > backAndForthTippingPoint) { time = MMMaths.Remap(time, backAndForthTippingPoint, 1f, 1f, 0f); } } float t = frequency * time + phase; switch (signalType) { case SignalType.Sine: value = (float)Mathf.Sin(2f * Mathf.PI * t); value = MMMaths.Remap(value, -1f, 1f, 0f, 1f); break; case SignalType.Square: value = Mathf.Sign(Mathf.Sin(2f * Mathf.PI * t)); value = MMMaths.Remap(value, -1f, 1f, 0f, 1f); break; case SignalType.Triangle: value = 1f - 4f * (float)Mathf.Abs(Mathf.Round(t - 0.25f) - (t - 0.25f)); value = MMMaths.Remap(value, -1f, 1f, 0f, 1f); break; case SignalType.Sawtooth: value = 2f * (t - (float)Mathf.Floor(t + 0.5f)); value = MMMaths.Remap(value, -1f, 1f, 0f, 1f); break; case SignalType.Pulse: value = (Mathf.Abs(Mathf.Sin(2 * Mathf.PI * t)) < 1.0 - 10E-3) ? (0) : (1); break; case SignalType.WhiteNoise: value = 2f * Random.Range(0, int.MaxValue) / int.MaxValue - 1f; value = MMMaths.Remap(value, -1f, 1f, 0f, 1f); break; case SignalType.DigitalNoise: value = Random.Range(0, 2); break; case SignalType.PerlinNoise: value = Mathf.PerlinNoise(t, t * amplitude); break; case SignalType.ValueNoise: value = ValueNoise(time, frequency) * amplitude; break; case SignalType.AnimationCurve: if (curve == null) { return(0f); } t = (t != 1f) ? t - Mathf.Floor(t) : 1f; value = curve.Evaluate(t); break; case SignalType.MMTween: t = (t != 1f) ? t - Mathf.Floor(t) : 1f; value = MMTween.Tween(t, 0f, 1f, 0f, 1f, tweenCurve); break; } if (Invert) { value = MMMaths.Remap(value, 0f, 1f, 1f, 0f); } float returnValue = amplitude * value + offset; // we clamp the value if (clamp) { returnValue = Mathf.Clamp(returnValue, clampMin, clampMax); } return(returnValue); }
/// <summary> /// Updates the value over time based on the selected options /// </summary> protected virtual void UpdateValue() { if (RendererIsNull() || (!PropertyFound)) { return; } switch (ControlMode) { case ControlModes.PingPong: if (GetTime() - _lastPingPongPauseAt < PingPongPauseDuration) { return; } PingPong += GetDeltaTime() * _pingPongDirection; if (PingPong < 0f) { PingPong = 0f; _pingPongDirection = -_pingPongDirection; _lastPingPongPauseAt = GetTime(); } if (PingPong > Duration) { PingPong = Duration; _pingPongDirection = -_pingPongDirection; _lastPingPongPauseAt = GetTime(); } CurrentValue = MMTween.Tween(PingPong, 0f, Duration, MinValue, MaxValue, Curve); break; case ControlModes.Random: _elapsedTime += GetDeltaTime(); CurrentValue = (Mathf.PerlinNoise(_randomFrequency * _elapsedTime, _randomShift) * 2.0f - 1.0f) * _randomAmplitude; break; case ControlModes.OneTime: if (!_shaking) { return; } _remappedTimeSinceStart = MMMaths.Remap(Time.time - _startedTimestamp, 0f, OneTimeDuration, 0f, 1f); CurrentValue = OneTimeCurve.Evaluate(_remappedTimeSinceStart); CurrentValue = MMMaths.Remap(CurrentValue, 0f, 1f, OneTimeRemapMin, OneTimeRemapMax); CurrentValue *= OneTimeAmplitude; break; case ControlModes.AudioAnalyzer: CurrentValue = Mathf.Lerp(CurrentValue, AudioAnalyzer.Beats[BeatID].CurrentValue * AudioAnalyzerMultiplier + AudioAnalyzerOffset, AudioAnalyzerLerp * Time.deltaTime); break; case ControlModes.ToDestination: if (!_shaking) { return; } _remappedTimeSinceStart = MMMaths.Remap(Time.time - _startedTimestamp, 0f, ToDestinationDuration, 0f, 1f); float time = ToDestinationCurve.Evaluate(_remappedTimeSinceStart); CurrentValue = Mathf.LerpUnclamped(_initialValue, ToDestinationValue, time); break; } if (PropertyType == PropertyTypes.Color) { _currentColor = Color.Lerp(FromColor, ToColor, CurrentValue); } if (AddToInitialValue) { CurrentValue += InitialValue; } if ((ControlMode == ControlModes.OneTime) && _shaking && (Time.time - _startedTimestamp > OneTimeDuration)) { _shaking = false; if (RevertToInitialValueAfterEnd) { CurrentValue = InitialValue; if (PropertyType == PropertyTypes.Color) { _currentColor = InitialColor; } } else { CurrentValue = OneTimeCurve.Evaluate(1f); CurrentValue = MMMaths.Remap(CurrentValue, 0f, 1f, OneTimeRemapMin, OneTimeRemapMax); CurrentValue *= OneTimeAmplitude; } SetValue(CurrentValue); if (DisableAfterOneTime) { this.enabled = false; } return; } if ((ControlMode == ControlModes.ToDestination) && _shaking && (Time.time - _startedTimestamp > ToDestinationDuration)) { _shaking = false; FromColor = _fromColorStorage; if (RevertToInitialValueAfterEnd) { CurrentValue = InitialValue; if (PropertyType == PropertyTypes.Color) { _currentColor = InitialColor; } } else { CurrentValue = ToDestinationValue; } SetValue(CurrentValue); if (DisableAfterToDestination) { this.enabled = false; } return; } SetValue(CurrentValue); }
/// <summary> /// On Update, we move our value based on the defined settings /// </summary> protected virtual void Update() { if (RendererIsNull() || (!Active) || (!PropertyFound)) { return; } switch (ControlMode) { case ControlModes.PingPong: if (GetTime() - _lastPingPongPauseAt < PingPongPauseDuration) { return; } PingPong += GetDeltaTime() * _pingPongDirection; if (PingPong < 0f) { PingPong = 0f; _pingPongDirection = -_pingPongDirection; _lastPingPongPauseAt = GetTime(); } if (PingPong > Duration) { PingPong = Duration; _pingPongDirection = -_pingPongDirection; _lastPingPongPauseAt = GetTime(); } CurrentValue = MMTween.Tween(PingPong, 0f, Duration, MinValue, MaxValue, Curve); break; case ControlModes.Random: _elapsedTime += GetDeltaTime(); CurrentValue = (Mathf.PerlinNoise(_randomFrequency * _elapsedTime, _randomShift) * 2.0f - 1.0f) * _randomAmplitude; break; case ControlModes.OneTime: if (!_oneTimeShaking) { return; } _remappedTimeSinceStart = MMMaths.Remap(Time.time - _oneTimeStartedTimestamp, 0f, OneTimeDuration, 0f, 1f); CurrentValue = OneTimeCurve.Evaluate(_remappedTimeSinceStart) * OneTimeAmplitude; if (AddToInitialValue) { CurrentValue += InitialValue; } break; case ControlModes.AudioAnalyzer: CurrentValue = Mathf.Lerp(CurrentValue, AudioAnalyzer.Beats[BeatID].CurrentValue * AudioAnalyzerMultiplier + AudioAnalyzerOffset, AudioAnalyzerLerp * Time.deltaTime); break; } if (PropertyType == PropertyTypes.Color) { _currentColor = Color.Lerp(FromColor, ToColor, CurrentValue); } if (AddToInitialValue) { CurrentValue += InitialValue; } if (_oneTimeShaking && (Time.time - _oneTimeStartedTimestamp > OneTimeDuration)) { _oneTimeShaking = false; if (ResetValueAfterOneTime) { CurrentValue = InitialValue; } } SetValue(CurrentValue); }