diff options
Diffstat (limited to 'JamHelper/Assets/JamUtils/Scripts/Smooth-Motion-in-Unity')
9 files changed, 0 insertions, 272 deletions
diff --git a/JamHelper/Assets/JamUtils/Scripts/Smooth-Motion-in-Unity/InterpolationFactorController.cs b/JamHelper/Assets/JamUtils/Scripts/Smooth-Motion-in-Unity/InterpolationFactorController.cs deleted file mode 100644 index efb96f3..0000000 --- a/JamHelper/Assets/JamUtils/Scripts/Smooth-Motion-in-Unity/InterpolationFactorController.cs +++ /dev/null @@ -1,58 +0,0 @@ -using UnityEngine; - -[DisallowMultipleComponent] -[DefaultExecutionOrder(ORDER_EXECUTION)] -public class InterpolationFactorController : MonoBehaviour -{ - public const int ORDER_EXECUTION = -1000; - - private static InterpolationFactorController Instance; - private float[] _lastFixedUpdates = new float[2]; - private int _lastIndex; - - public static float Factor { get; private set; } - - private void Awake() - { - if (Instance) - { - Destroy(this); - Debug.LogWarning($"The '{typeof(InterpolationFactorController).Name}' is a singleton!"); - return; - } - - Instance = this; - Factor = 1; - } - - private void Start() - { - _lastFixedUpdates = new float[2] { Time.fixedTime, Time.fixedTime }; - _lastIndex = 0; - } - - private void FixedUpdate() - { - _lastIndex = NextIndex(); - _lastFixedUpdates[_lastIndex] = Time.fixedTime; - } - - private void Update() - { - float lastTime = _lastFixedUpdates[_lastIndex]; - float prevTime = _lastFixedUpdates[NextIndex()]; - - if (lastTime == prevTime) - { - Factor = 1; - return; - } - - Factor = (Time.time - lastTime) / (lastTime - prevTime); - } - - private int NextIndex() - { - return (_lastIndex == 0) ? 1 : 0; - } -} diff --git a/JamHelper/Assets/JamUtils/Scripts/Smooth-Motion-in-Unity/InterpolationFactorController.cs.meta b/JamHelper/Assets/JamUtils/Scripts/Smooth-Motion-in-Unity/InterpolationFactorController.cs.meta deleted file mode 100644 index ca7364a..0000000 --- a/JamHelper/Assets/JamUtils/Scripts/Smooth-Motion-in-Unity/InterpolationFactorController.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 63b841e9c9589044db9f0b7edfca90bd -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: -95 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/JamHelper/Assets/JamUtils/Scripts/Smooth-Motion-in-Unity/InterpolationObjectController.cs b/JamHelper/Assets/JamUtils/Scripts/Smooth-Motion-in-Unity/InterpolationObjectController.cs deleted file mode 100644 index 3b6c0de..0000000 --- a/JamHelper/Assets/JamUtils/Scripts/Smooth-Motion-in-Unity/InterpolationObjectController.cs +++ /dev/null @@ -1,136 +0,0 @@ -using System;
-using System.Collections; -using UnityEngine; - -[DisallowMultipleComponent] -[DefaultExecutionOrder(ORDER_EXECUTION)] -public class InterpolationObjectController : MonoBehaviour -{
- [Flags]
- public enum InterpolateType
- {
- None,
- Position = 1,
- Rotation = 1 << 1,
- Scale = 1 << 2,
- } - - public InterpolateType type; - - private const int ORDER_EXECUTION = InterpolationFactorController.ORDER_EXECUTION - 1; - - private TransformData[] _transforms; - private int _index; - - private void Awake() - { - StartCoroutine(WaitForEndOfFrame()); - StartCoroutine(WaitForFixedUpdate()); - } - - private void OnEnable() - { - ResetTransforms(); - } - - private void BeforeFixedUpdate() - { - // Restoring actual transform for the FixedUpdate() cal where it could be change by the user. - RestoreActualTransform(); - } - - private void AfterFixedUpdate() - { - // Saving actual transform for being restored in the BeforeFixedUpdate() method. - SaveActualTransform(); - } - - private void Update() - { - // Set interpolated transform for being rendered. - SetInterpolatedTransform(); - } - - #region Helpers - - private void RestoreActualTransform() - { - var latest = _transforms[_index]; - - if((type & InterpolateType.Position) != 0)
- transform.localPosition = latest.position; - if ((type & InterpolateType.Scale) != 0)
- transform.localScale = latest.scale; - if ((type & InterpolateType.Rotation) != 0)
- transform.localRotation = latest.rotation; - } - - private void SaveActualTransform() - { - _index = NextIndex(); - _transforms[_index] = CurrentTransformData(); - } - - private void SetInterpolatedTransform() - { - var prev = _transforms[NextIndex()]; - float factor = InterpolationFactorController.Factor;
- if ((type & InterpolateType.Position) != 0)
- transform.localPosition = Vector3.Lerp(prev.position, transform.localPosition, factor);
- if ((type & InterpolateType.Rotation) != 0)
- transform.localRotation = Quaternion.Slerp(prev.rotation, transform.localRotation, factor);
- if ((type & InterpolateType.Scale) != 0)
- transform.localScale = Vector3.Lerp(prev.scale, transform.localScale, factor); - } - - public void ResetTransforms() - { - _index = 0; - var td = CurrentTransformData(); - _transforms = new TransformData[2] { td, td }; - } - - private TransformData CurrentTransformData() - { - return new TransformData(transform.localPosition, transform.localRotation, transform.localScale); - } - - private int NextIndex() - { - return (_index == 0) ? 1 : 0; - } - - private IEnumerator WaitForEndOfFrame() - { - while (true) - { - yield return new WaitForEndOfFrame(); - BeforeFixedUpdate(); - } - } - - private IEnumerator WaitForFixedUpdate() - { - while (true) - { - yield return new WaitForFixedUpdate(); - AfterFixedUpdate(); - } - } - - private struct TransformData - { - public Vector3 position; - public Quaternion rotation; - public Vector3 scale; - - public TransformData(Vector3 position, Quaternion rotation, Vector3 scale) - { - this.position = position; - this.rotation = rotation; - this.scale = scale; - } - } - - #endregion -} diff --git a/JamHelper/Assets/JamUtils/Scripts/Smooth-Motion-in-Unity/InterpolationObjectController.cs.meta b/JamHelper/Assets/JamUtils/Scripts/Smooth-Motion-in-Unity/InterpolationObjectController.cs.meta deleted file mode 100644 index 80c49da..0000000 --- a/JamHelper/Assets/JamUtils/Scripts/Smooth-Motion-in-Unity/InterpolationObjectController.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 60e373202cc879c4a8e781f92dbdcb48 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: -90 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/JamHelper/Assets/JamUtils/Scripts/Smooth-Motion-in-Unity/README.md b/JamHelper/Assets/JamUtils/Scripts/Smooth-Motion-in-Unity/README.md deleted file mode 100644 index 016072b..0000000 --- a/JamHelper/Assets/JamUtils/Scripts/Smooth-Motion-in-Unity/README.md +++ /dev/null @@ -1,12 +0,0 @@ -# Smooth Motion in Unity (refactored) - -This is an improved (refactored) version of the code from the ["Timesteps and Achieving Smooth Motion in Unity"](https://www.kinematicsoup.com/news/2016/8/9/rrypp5tkubynjwxhxjzd42s3o034o8?utm_source=youtube&utm_type=SMVideo) article. - -Now, there are only 2 components instead of original 3: - -1. [`InterpolationFactorController`](https://github.com/DevelAx/Smooth-Motion-in-Unity/blob/master/InterpolationFactorController.cs) a singleton scene component (the original name was `InterpolationController`) that calculates the current *interpolation factor*. -2. [`InterpolationObjectController`](https://github.com/DevelAx/Smooth-Motion-in-Unity/blob/master/InterpolationObjectController.cs) a component for a moving object (the original name was `InterpolatedTransform`) which calculates the current interpolation for the object in the `Update()` method before it's being rendered and then restores its original `transform` values for the next `FixedUpdate()` call. -3. The `InterpolatedTransformUpdater` component was removed as unnecessary. - -## Remarks -For these scripts to work correctly the user's script must change the object's `transform` only in the `FixedUpdate()` method. When teleporting the object the user should call the (`InterpolationObjectController`).`ResetTransforms()` method after it. diff --git a/JamHelper/Assets/JamUtils/Scripts/Smooth-Motion-in-Unity/README.md.meta b/JamHelper/Assets/JamUtils/Scripts/Smooth-Motion-in-Unity/README.md.meta deleted file mode 100644 index b5455ff..0000000 --- a/JamHelper/Assets/JamUtils/Scripts/Smooth-Motion-in-Unity/README.md.meta +++ /dev/null @@ -1,7 +0,0 @@ -fileFormatVersion: 2 -guid: 933a0ade2c59f904c9eed3fb284afb77 -TextScriptImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/JamHelper/Assets/JamUtils/Scripts/Smooth-Motion-in-Unity/Tests.meta b/JamHelper/Assets/JamUtils/Scripts/Smooth-Motion-in-Unity/Tests.meta deleted file mode 100644 index 74a5329..0000000 --- a/JamHelper/Assets/JamUtils/Scripts/Smooth-Motion-in-Unity/Tests.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 2255b40400c2ac8469bc94ee99676fb4 -folderAsset: yes -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/JamHelper/Assets/JamUtils/Scripts/Smooth-Motion-in-Unity/Tests/TestMotion.cs b/JamHelper/Assets/JamUtils/Scripts/Smooth-Motion-in-Unity/Tests/TestMotion.cs deleted file mode 100644 index 3bf706a..0000000 --- a/JamHelper/Assets/JamUtils/Scripts/Smooth-Motion-in-Unity/Tests/TestMotion.cs +++ /dev/null @@ -1,18 +0,0 @@ -using UnityEngine; - -/// <summary> -/// Use this component with a game object for demonstration purposes. -/// </summary> -[DisallowMultipleComponent] -[RequireComponent(typeof(InterpolationObjectController))] -public class TestMotion : MonoBehaviour -{ - [SerializeField] - private float _speed = 2f; - - private void FixedUpdate() - { - transform.position += Vector3.right * _speed * Time.fixedDeltaTime; - transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y + _speed * 30 * Time.fixedDeltaTime, transform.rotation.eulerAngles.z); - } -} diff --git a/JamHelper/Assets/JamUtils/Scripts/Smooth-Motion-in-Unity/Tests/TestMotion.cs.meta b/JamHelper/Assets/JamUtils/Scripts/Smooth-Motion-in-Unity/Tests/TestMotion.cs.meta deleted file mode 100644 index fe06ffe..0000000 --- a/JamHelper/Assets/JamUtils/Scripts/Smooth-Motion-in-Unity/Tests/TestMotion.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 80afb4adbb1d67247ac41a23d04b67d7 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: |