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 | |
| parent | a71efa41f09e878b5cfa68072e896df1a85bc51f (diff) | |
| download | UnityDancingGraph-13bcae5f22a8034af1db830cce79c1f3ffd476cb.tar.gz UnityDancingGraph-13bcae5f22a8034af1db830cce79c1f3ffd476cb.zip | |
lerp and sliders
Diffstat (limited to 'Assets')
| -rw-r--r-- | Assets/Point URP.shadergraph | 4 | ||||
| -rw-r--r-- | Assets/Point.prefab | 4 | ||||
| -rw-r--r-- | Assets/Scripts/Graph.cs | 122 | ||||
| -rw-r--r-- | Assets/Scripts/MathFunctionLibrary.cs | 24 |
4 files changed, 127 insertions, 27 deletions
diff --git a/Assets/Point URP.shadergraph b/Assets/Point URP.shadergraph index c0fc845..0031fdc 100644 --- a/Assets/Point URP.shadergraph +++ b/Assets/Point URP.shadergraph @@ -1146,7 +1146,7 @@ "m_Value": { "e00": 0.5, "e01": 0.5, - "e02": 0.0, + "e02": 0.5, "e03": 2.0, "e10": 2.0, "e11": 2.0, @@ -1194,7 +1194,7 @@ "m_Value": { "x": 0.5, "y": 0.5, - "z": 0.0, + "z": 0.5, "w": 0.0 }, "m_DefaultValue": { diff --git a/Assets/Point.prefab b/Assets/Point.prefab index 05269fb..30bb1c7 100644 --- a/Assets/Point.prefab +++ b/Assets/Point.prefab @@ -26,8 +26,8 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 8840006879936015908} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0.98999995, y: 0.9702988, z: 0} - m_LocalScale: {x: 0.2, y: 0.2, z: 0.2} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 0} 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; } } diff --git a/Assets/Scripts/MathFunctionLibrary.cs b/Assets/Scripts/MathFunctionLibrary.cs index 1169eca..ba818ef 100644 --- a/Assets/Scripts/MathFunctionLibrary.cs +++ b/Assets/Scripts/MathFunctionLibrary.cs @@ -6,7 +6,13 @@ using static UnityEngine.Mathf; public static class MathFunctionLibrary { - public delegate float Function(float x, float t); + public delegate float Function(float x, float z, float t); + + public enum FunctionEnum { + Wave, + MultiWave, + Ripple + } static Function[] functions = { Wave, @@ -14,25 +20,25 @@ public static class MathFunctionLibrary Ripple }; - public static Function GetFunction(int index) + public static Function GetFunction(FunctionEnum name) { - return functions[index]; + return functions[(int)name]; } - public static float Wave(float x, float t) + public static float Wave(float x, float z, float t) { - return Sin(PI * (x + t)); + return Sin(PI * (x + z + t)); } - public static float MultiWave(float x, float t) + public static float MultiWave(float x, float z, float t) { - float y = Wave(x, t * 0.5f); + float y = Wave(x, z, t * 0.5f); y += Sin(2f * PI * (x + t)) * 0.5f; return y * (2f / 3f); } - public static float Ripple (float x, float t) { - float d = Abs(x); + public static float Ripple (float x, float z, float t) { + float d = Sqrt(x * x + z * z); float y = Sin(PI * (4f * d - (2f * t))); return y / (1f + 10f * d); } |
