using System.Collections; using System.Collections.Generic; using UnityEngine; public class MathExamples : MonoBehaviour { // REFERENCES public Transform steppedTrans; public Transform badSmoothTrans; public Transform smoothedTrans; // PARAMETERS public Color clrA; public Color clrB; public Vector3 a; public Vector3 b; [Range(0f, 1f)] public float t; public bool useTime = false; public AnimationCurve easingCurve; public float smoothingRate; public float smoothingFactor; public float smoothingHalflife; // INTERNALS private Vector3 _offsetA = Vector3.zero; private Vector3 _offsetB = Vector3.zero; private float _lastSmoothingTime = 0f; private float _badSmooth = 0f; private float _smoothed = 0f; // LIFECYCLE private void Awake() { this._offsetA = Random.insideUnitSphere * 100f; this._offsetB = Random.insideUnitSphere * 100f; } private void Update() { //this.ShowLerpSmoothing(); //this.ShowLerp(a, b); this.ShowNoise(); } // BEHAVIORS private void ShowLerpSmoothing() { float s = Mathf.Round(Mathf.Sin(Time.time * 0.75f) * 2f); if ((Time.time - this._lastSmoothingTime) > this.smoothingRate) { this._lastSmoothingTime = Time.time; this._badSmooth = Mathf.Lerp(this._badSmooth, s, this.smoothingFactor); this._smoothed = MathExtras.LerpSmooth(this._smoothed, s, smoothingRate, this.smoothingHalflife); } this.steppedTrans.position = new Vector3(Time.time, s, 0.5f); this.badSmoothTrans.position = new Vector3(Time.time, this._badSmooth, 0f); this.smoothedTrans.position = new Vector3(Time.time, this._smoothed, 0f); } private void ShowNoise() { //Vector3 newA = this.a + Random.insideUnitSphere; //Vector3 newB = this.b + Random.insideUnitSphere; //Vector3 newA = this.a + (Vector3.up * Mathf.PerlinNoise1D((Time.time * 2f) + 1234f)); //Vector3 newB = this.b + (Vector3.up * Mathf.PerlinNoise1D((Time.time * 2f) + 1234f)); Vector3 newA = this.a + MathExtras.CheapPerlin3d((Time.time * Vector3.one) + this._offsetA); Vector3 newB = this.b + MathExtras.CheapPerlin3d((Time.time * Vector3.one) + this._offsetB); this.ShowLerp(newA, newB); } private void ShowLerp(Vector3 a, Vector3 b) { Debug.DrawLine(a, b, Color.white); float tmp = (this.useTime ? Mathf.PingPong(Time.time * 0.2f, 1f) : this.t); Vector3 pt = Vector3.Lerp(a, b, tmp); this.DrawGizmo(pt, Color.green); pt = Vector3.Lerp(a, b, this.easingCurve.Evaluate(tmp)); this.DrawGizmo(pt, Color.blue); } private void DrawGizmo(Vector3 pt, Color clr) { Debug.DrawLine(pt + Vector3.up, pt + Vector3.down, clr); Debug.DrawLine(pt + Vector3.right, pt + Vector3.left, clr); Debug.DrawLine(pt + Vector3.forward, pt + Vector3.back, clr); } }