diff options
| author | realtradam <[email protected]> | 2022-12-07 04:28:20 -0500 |
|---|---|---|
| committer | realtradam <[email protected]> | 2022-12-07 04:28:20 -0500 |
| commit | 13bcae5f22a8034af1db830cce79c1f3ffd476cb (patch) | |
| tree | a5fe77976c6a387d30256d4c4a42127de0b281ac /Assets/Scripts/Graph.cs | |
| parent | a71efa41f09e878b5cfa68072e896df1a85bc51f (diff) | |
| download | UnityDancingGraph-13bcae5f22a8034af1db830cce79c1f3ffd476cb.tar.gz UnityDancingGraph-13bcae5f22a8034af1db830cce79c1f3ffd476cb.zip | |
lerp and sliders
Diffstat (limited to 'Assets/Scripts/Graph.cs')
| -rw-r--r-- | Assets/Scripts/Graph.cs | 122 |
1 files changed, 108 insertions, 14 deletions
diff --git a/Assets/Scripts/Graph.cs b/Assets/Scripts/Graph.cs index 1b4ad63..6c3b5de 100644 --- a/Assets/Scripts/Graph.cs +++ b/Assets/Scripts/Graph.cs @@ -8,28 +8,72 @@ public class Graph : MonoBehaviour [SerializeField] Transform pointPrefab; - [SerializeField, Range(10, 200)] - int resolution = 100; + const int max_resolution = 100; - [SerializeField, Range(0.1f, 2f)] - float size = 0.3f; + [SerializeField, Range(3, max_resolution)] + int resolution = 30; + int old_resolution = 30; - [SerializeField, Range(0, 2)] - int function = 1; + [SerializeField] + MathFunctionLibrary.FunctionEnum function; + MathFunctionLibrary.FunctionEnum old_function; + + [SerializeField, Range(0.1f, 2.0f)] + float function_scale = 1.0f; + + [SerializeField, Range(0f, 10f)] + float lerp_speed = 2.0f; + float lerp_timer = 0f; + bool lerping = false; Transform[] points; + void SetResolution() { + float step = 2f / resolution; + for(int i = 0, x = 0, z = 0; i < (max_resolution * max_resolution); i++, x++) { + Transform point = points[i]; + if(i >= (resolution * resolution)) + { + point.gameObject.SetActive(false); + } + else + { + point.gameObject.SetActive(true); + if(x == resolution) + { + x = 0; + z += 1; + } + Vector3 position = pointPrefab.localPosition; + position.x = (x + 0.5f) * step - 1f; + position.z = (z + 0.5f) * step - 1f; + point.localPosition = position; + point.localScale = new Vector3(step, step, step); + } + } + } + void Awake() { - points = new Transform[resolution]; - float step = 8f / points.Length; - for(int i = 0; i < points.Length; i++) { + points = new Transform[max_resolution * max_resolution]; + float step = 2f / resolution; + for(int i = 0, x = 0, z = 0; i < (max_resolution * max_resolution); i++, x++) { Transform point = Instantiate(pointPrefab); points[i] = point; + if(i >= (resolution * resolution)) + { + point.gameObject.SetActive(false); + } + if(x == resolution) + { + x = 0; + z += 1; + } Vector3 position = pointPrefab.localPosition; point.SetParent(transform, false); - position.x = (i + 0.5f) * step - 4f; + position.x = (x + 0.5f) * step - 1f; + position.z = (z + 0.5f) * step - 1f; point.localPosition = position; - point.localScale *= size; + point.localScale = new Vector3(step, step, step); } } @@ -42,12 +86,62 @@ public class Graph : MonoBehaviour // Update is called once per frame void Update() { - MathFunctionLibrary.Function visual_function = MathFunctionLibrary.GetFunction(function); float time = Time.time; - for(int i = 0; i < points.Length; i++) { + + if(old_resolution != resolution) { + old_resolution = resolution; + SetResolution(); + } + + if(function != old_function) + { + if(lerp_timer <= 0f) + { + if(lerping) + { + lerping = false; + old_function = function; + } + else + { + lerping = true; + lerp_timer = lerp_speed; + } + } + else + { + lerp_timer -= Time.deltaTime; + } + } + + MathFunctionLibrary.Function visual_function = MathFunctionLibrary.GetFunction(function); + MathFunctionLibrary.Function previous_function = MathFunctionLibrary.GetFunction(old_function); + + for(int i = 0; i < resolution * resolution; i++) { Transform point = points[i]; Vector3 position = point.localPosition; - position.y = visual_function(position.x, time); + if(function == old_function) + { + position.y = visual_function( + position.x * function_scale, + position.z * function_scale, + time + ); + } + else + { + var starting = previous_function( + position.x * function_scale, + position.z * function_scale, + time + ); + var target = visual_function( + position.x * function_scale, + position.z * function_scale, + time + ); + position.y = Mathf.Lerp(target, starting, lerp_timer / lerp_speed); + } point.localPosition = position; } } |
