using Microsoft.MixedReality.Toolkit.UI; using System.Collections; using System.Collections.Generic; using UnityEngine; public class Spline : MonoBehaviour { Mesh mesh; public int minX; public int maxX; bool is2D = false; bool is3D = false; public List factors; // Start is called before the first frame update void Start() { mesh = new Mesh { name = "Spline" }; } // 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++; } float ym = 0; for (int j = factors.Count - 1; j > 0; --j) { ym += factors[j]; ym *= maxX; } ym += factors[0]; vectors[100 * (maxX - minX)] = new Vector3(maxX, ym, 0); 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; is2D = true; } public void DrawSpline3D() { mesh.Clear(); Vector3[] vectors = new Vector3[200 * (maxX - minX) + 2]; 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, -5); k++; vectors[k] = new Vector3(i, y, 5); k++; } float ym = 0; for (int j = factors.Count - 1; j > 0; --j) { ym += factors[j]; ym *= maxX; } ym += factors[0]; vectors[200 * (maxX - minX)] = new Vector3(maxX, ym, -5); vectors[200 * (maxX - minX) + 1] = new Vector3(maxX, ym, 5); mesh.vertices = vectors; Vector2[] uvs = new Vector2[vectors.Length]; for (int i = 0; i < uvs.Length; i++) { uvs[i] = new Vector2(0, vectors[i].y / 100); } mesh.uv = uvs; int[] triangle = new int[100 * (maxX - minX) * 6]; for (int i = 0; i < 100 * (maxX - minX); ++i) { triangle[6 * i] = 2 * i; triangle[6 * i + 1] = 2 * i + 1; triangle[6 * i + 2] = 2 * i + 2; triangle[6 * i + 3] = 2 * i + 1; triangle[6 * i + 4] = 2 * i + 3; triangle[6 * i + 5] = 2 * i + 2; } mesh.SetIndices(triangle, MeshTopology.Triangles, 0); GetComponent().mesh = mesh; is3D = true; } public void MeshClear() { mesh.Clear(); GetComponent().mesh = mesh; is2D = false; is3D = false; } public void OnSliderUpdated(SliderEventData eventData) { factors[1] = Mathf.Tan((eventData.NewValue - 0.5f) * Mathf.PI); if (is2D) DrawSpline(); if (is3D) DrawSpline3D(); } public void OnSliderUpdated3(SliderEventData eventData) { factors[0] = eventData.NewValue * 3; if (is2D) DrawSpline(); if (is3D) DrawSpline3D(); } public void OnSliderUpdated2(SliderEventData eventData) { int n = (int)((eventData.NewValue ) * 5); factors = new List(); for(int i = 0; i < n; ++i) { factors.Add(0); } factors.Add(1); if (is2D) DrawSpline(); if (is3D) DrawSpline3D(); } }