using System.Collections; using System.Collections.Generic; using UnityEngine; public class Spline : MonoBehaviour { Mesh mesh; public int minX; public int maxX; public List factors; // Start is called before the first frame update void Start() { mesh = new Mesh { name = "Spline" }; factors = new List(); factors.Add(1); factors.Add(0); factors.Add(1); } // Update is called once per frame void Update() { } public void DrawSpline() { mesh.Clear(); Vector3[] vectors = new Vector3[100 * (maxX - minX) + 1]; int k = 0; float step = 0.01f; for (float i = minX; i <= maxX; i+=step) { float y = 0; for (int j = factors.Count - 1; j > 0; --j) { y += factors[j]; y *= i; } y += factors[0]; vectors[k] = new Vector3(i, y, 0); k++; } mesh.vertices = vectors; int[] triangle = new int[100 * (maxX - minX) * 2]; for (int i = 0; i < vectors.Length - 1; ++i) { triangle[2 * i] = i; triangle[2 * i + 1] = i + 1; } mesh.SetIndices(triangle, MeshTopology.Lines, 0); GetComponent().mesh = mesh; } }