├── .gitattributes ├── .gitignore ├── Content ├── CubicCurve.cs ├── QuadraticCurve.cs └── SplineDrawer.cs ├── LICENSE ├── README.md ├── Splines.gif └── coffee.png /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /[Ll]ibrary/ 2 | /[Tt]emp/ 3 | /[Oo]bj/ 4 | /[Bb]uild/ 5 | /[Bb]uilds/ 6 | /Assets/AssetStoreTools* 7 | 8 | # Autogenerated VS/MD/Consulo solution and project files 9 | ExportedObj/ 10 | .consulo/ 11 | *.csproj 12 | *.unityproj 13 | *.sln 14 | *.suo 15 | *.tmp 16 | *.user 17 | *.userprefs 18 | *.pidb 19 | *.booproj 20 | *.svd 21 | 22 | 23 | # Unity3D generated meta files 24 | *.pidb.meta 25 | *.meta 26 | 27 | # Unity3D Generated File On Crash Reports 28 | sysinfo.txt 29 | 30 | # Builds 31 | *.apk 32 | *.unitypackage 33 | *.exe 34 | -------------------------------------------------------------------------------- /Content/CubicCurve.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public class CubicCurve 4 | { 5 | private Vector3 p1; 6 | private Vector3 p2; 7 | 8 | private Vector3 pivot1; 9 | private Vector3 pivot2; 10 | 11 | private int pointAmount; 12 | 13 | private Vector3[] points; //Generated spline points 14 | 15 | private int count; //Inner count, used for Next() 16 | 17 | public CubicCurve(Vector3 p1, Vector3 p2, Vector3 pivot1, Vector3 pivot2, int pointAmount) 18 | { 19 | this.p1 = p1; 20 | this.p2 = p2; 21 | this.pivot1 = pivot1; 22 | this.pivot2 = pivot2; 23 | this.pointAmount = pointAmount; 24 | 25 | RecalculatePoints(); 26 | } 27 | 28 | public Vector3 Next() 29 | { 30 | if (count == pointAmount - 1) 31 | { 32 | count = 0; 33 | return points[0]; 34 | } 35 | return points[count++]; 36 | } 37 | 38 | public void UpdatePointCount(int pointAmount) 39 | { 40 | this.pointAmount = pointAmount; 41 | 42 | RecalculatePoints(); 43 | } 44 | 45 | public void Update(Vector3 p1, Vector3 p2, Vector3 pivot1, Vector3 pivot2) 46 | { 47 | this.p1 = p1; 48 | this.p2 = p2; 49 | this.pivot1 = pivot1; 50 | this.pivot2 = pivot2; 51 | 52 | RecalculatePoints(); 53 | } 54 | 55 | public Vector3[] GetPoints () 56 | { 57 | return points; 58 | } 59 | 60 | public void Draw() 61 | { 62 | if (points != null) 63 | { 64 | for(int i = 0; i < points.Length; i++) 65 | { 66 | if (i != points.Length - 1) 67 | { 68 | Debug.DrawLine(points[i], points[i+1], Color.red); 69 | } 70 | } 71 | } 72 | 73 | Debug.DrawLine(p1, pivot1, Color.blue); 74 | Debug.DrawLine(p2, pivot1, Color.blue); 75 | 76 | Debug.DrawLine(p1, pivot2, Color.blue); 77 | Debug.DrawLine(p2, pivot2, Color.blue); 78 | } 79 | 80 | private void RecalculatePoints () 81 | { 82 | points = new Vector3[pointAmount]; 83 | points[0] = p1; 84 | 85 | float loopAmount = 1f/pointAmount; //Divide t into same-sized segments to evaluate 86 | 87 | for(int currentAmount = 0; currentAmount < pointAmount; currentAmount++) 88 | { 89 | if (currentAmount != 0) // Index 0 will be set to the origin 90 | { 91 | float t = loopAmount * (currentAmount+1); //+1 is because the loop starts at 0, 92 | points[currentAmount] = Evaluate(t); 93 | } 94 | } 95 | 96 | } 97 | 98 | public Vector3 Evaluate(float t) 99 | { 100 | t = Mathf.Clamp01(t); 101 | Vector3 quadratic1 = QuadraticCurve.Evaluate(p1, pivot1, p2, t); 102 | Vector3 quadratic2 = QuadraticCurve.Evaluate(pivot1, p2, pivot2, t); 103 | return Vector3.Lerp(quadratic1, quadratic2, t); 104 | } 105 | 106 | //For external use 107 | public static Vector3 Evaluate(Vector3 p1, Vector3 p2, Vector3 pivot1, Vector3 pivot2, float t) 108 | { 109 | t = Mathf.Clamp01(t); 110 | Vector2 quadratic1 = QuadraticCurve.Evaluate(p1, pivot1, p2, t); 111 | Vector2 quadratic2 = QuadraticCurve.Evaluate(pivot1, p2, pivot2, t); 112 | return Vector2.Lerp(quadratic1, quadratic2, t); 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /Content/QuadraticCurve.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public class QuadraticCurve 4 | { 5 | private Vector3 p1; 6 | private Vector3 p2; 7 | private Vector3 pivot; 8 | 9 | private int pointAmount; 10 | 11 | private Vector3[] points; 12 | 13 | private int count; 14 | 15 | public QuadraticCurve(Vector3 p1, Vector3 p2, Vector3 pivot, int pointAmount) 16 | { 17 | this.p1 = p1; 18 | this.p2 = p2; 19 | this.pivot = pivot; 20 | this.pointAmount = pointAmount; 21 | 22 | RecalculatePoints(); 23 | } 24 | 25 | public Vector3 Next() 26 | { 27 | if (count == pointAmount - 1) 28 | { 29 | count = 0; 30 | return points[0]; 31 | } 32 | return points[count++]; 33 | } 34 | 35 | public void UpdatePointCount(int pointAmount) 36 | { 37 | this.pointAmount = pointAmount; 38 | 39 | RecalculatePoints(); 40 | } 41 | 42 | public void Update(Vector3 p1, Vector3 p2, Vector3 pivot) 43 | { 44 | this.p1 = p1; 45 | this.p2 = p2; 46 | this.pivot = pivot; 47 | 48 | RecalculatePoints(); 49 | } 50 | 51 | public Vector3[] GetPoints () 52 | { 53 | return points; 54 | } 55 | 56 | public void Draw() 57 | { 58 | if (points != null) 59 | { 60 | for(int i = 0; i < points.Length; i++) 61 | { 62 | if (i != points.Length - 1) 63 | { 64 | Debug.DrawLine(points[i], points[i+1], Color.red); 65 | } 66 | } 67 | } 68 | 69 | Debug.DrawLine(p1, pivot, Color.blue); 70 | Debug.DrawLine(p2, pivot, Color.blue); 71 | } 72 | 73 | private void RecalculatePoints () 74 | { 75 | points = new Vector3[pointAmount]; 76 | points[0] = p1; //Origin is first point 77 | points[pointAmount-1] = p2; //End is second point 78 | 79 | float loopAmount = 1f/pointAmount; //Divide t into same-sized segments to evaluate 80 | 81 | for(int currentAmount = 0; currentAmount < pointAmount; currentAmount++) 82 | { 83 | float t; 84 | if (currentAmount != 0) // Index 0 will be set to the origin 85 | { 86 | t = loopAmount * (currentAmount+1); //+1 is because the loop starts at 0, 87 | points[currentAmount] = Evaluate(t); 88 | } 89 | else 90 | { 91 | points[currentAmount] = p1; //Set index 0 92 | } 93 | 94 | } 95 | 96 | } 97 | 98 | public Vector3 Evaluate(float t) 99 | { 100 | t = Mathf.Clamp01(t); 101 | Vector3 lerp1 = Vector3.Lerp(p1, pivot, t); 102 | Vector3 lerp2 = Vector3.Lerp(pivot, p2, t); 103 | 104 | return Vector3.Lerp(lerp1, lerp2, t); 105 | } 106 | 107 | //For external use 108 | public static Vector3 Evaluate(Vector3 p1, Vector3 p2, Vector3 pivot, float t) 109 | { 110 | t = Mathf.Clamp01(t); 111 | Vector3 lerp1 = Vector3.Lerp(p1, pivot, t); 112 | Vector3 lerp2 = Vector3.Lerp(pivot, p2, t); 113 | 114 | return Vector3.Lerp(lerp1, lerp2, t); 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /Content/SplineDrawer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | [ExecuteInEditMode] 6 | public class SplineDrawer : MonoBehaviour { 7 | 8 | public Transform p1; 9 | public Transform p2; 10 | public Transform pivot; 11 | 12 | [Range(2, 20)] 13 | public int splinePointCount; 14 | 15 | private QuadraticCurve curve; 16 | 17 | void Update () 18 | { 19 | if (p1 != null && p2 != null && pivot != null) 20 | { 21 | if (curve == null) 22 | { 23 | curve = new QuadraticCurve(p1.position, p2.position, pivot.position, splinePointCount); 24 | } 25 | 26 | UpdateCurve(); 27 | curve.Draw(); 28 | } 29 | } 30 | 31 | private void UpdateCurve() 32 | { 33 | curve.Update(p1.position, p2.position, pivot.position); 34 | curve.UpdatePointCount(splinePointCount); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 JPBotelho 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Splines 2 | 3 | Simple Quadratic/Cubic curve implementations in Unity. 4 | 5 | The curves have their own data structure, allowing you to create at start, sample whenever you want and update point locations and point count. 6 | 7 | 8 | ![Bubble](Splines.gif "Render.png") 9 | 10 | # Contains 11 | 12 | Quadratic Curve data structure; 13 | Cubic Curve data structure. 14 | 15 | Curves classes contain: 16 | 17 | Evaluate(float t) 18 | Draw() 19 | Update(p1, p2, pivot, [pivot2]) 20 | UpdatePointCount(int count) 21 | Next() 22 | 23 | Feel free to make pull requests and add more spline types! 24 | 25 | # Consider buying me a coffee if you like my work (click the image) 26 | [![Foo](coffee.png)](https://www.buymeacoffee.com/ZcRuWpUBf) 27 | -------------------------------------------------------------------------------- /Splines.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JPBotelho/Splines/58cd6888320b3e167cc0cf5ba68acbc14ffac8de/Splines.gif -------------------------------------------------------------------------------- /coffee.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JPBotelho/Splines/58cd6888320b3e167cc0cf5ba68acbc14ffac8de/coffee.png --------------------------------------------------------------------------------