using Microsoft.MixedReality.Toolkit; using Microsoft.MixedReality.Toolkit.Utilities; using System.Collections; using System.Collections.Generic; using UnityEngine; public class NormalFunctionTest : MonoBehaviour { Mesh mesh; int minX; int maxX; public enum Type { Spline, Ellipse, Hyperbola, Exponential, Logarithmic, Trigonometric, InverseTri, ThreeD, } public enum TriType { Sin, Cos, Tan } public Type type; public bool drawGizmos = false; // Start is called before the first frame update void Start() { List factors; minX = -10; maxX = 10; mesh = new Mesh { name = "NormalFunctionTest" }; factors = new List(); factors.Add(1); factors.Add(0); factors.Add(1); //DrawSpline(factors); //DrawEllipse(new Vector3(0, 0, 0), 3, 2); //DrawHyperbola(new Vector3(0, 0, 0), 3, 2); //DrawExponential(2, 3); //DrawLogarithmic(2, 3); DrawTrigonometric(TriType.Sin, 1, 0, 1); } // Update is called once per frame void Update() { } public void DrawSpline(List factors) { mesh.Clear(); type = Type.Spline; Vector3[] vectors = new Vector3[maxX - minX + 1]; int k = 0; for(int i = minX;i <= maxX; ++i) { 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[(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; drawGizmos = true; } public void DrawEllipse(Vector3 center, float a, float b) { mesh.Clear(); type = Type.Hyperbola; Vector3[] vectors = new Vector3[200]; int k = 0; float step = a / 50; for(float i = -a; i <= a; i += step) { float y = 1 - i * i / (a * a); y *= (b * b); y = Mathf.Sqrt(y); vectors[k] = new Vector3(i, y, 0); if(y != 0) { vectors[200 - k] = new Vector3(i, -y, 0); } k++; } mesh.vertices = vectors; int[] triangle = new int[400]; for (int i = 0; i < vectors.Length - 1; ++i) { triangle[2 * i] = i; triangle[2 * i + 1] = i + 1; } triangle[398] = 199; triangle[399] = 0; mesh.SetIndices(triangle, MeshTopology.Lines, 0); GetComponent().mesh = mesh; drawGizmos = true; } public void DrawHyperbola(Vector3 center, float a, float b) { mesh.Clear(); type = Type.Ellipse; Vector3[] vectors = new Vector3[202]; int k = 0; float step = a / 100; float x = -a; while(k < 51) { float y = x * x / (a * a) - 1; y *= (b * b); y = Mathf.Sqrt(y); vectors[50 + k] = new Vector3(x, y, 0); if(y != 0) { vectors[50 - k] = new Vector3(x, -y, 0); } x -= step; step *= 1.1f; k++; } x = a; step = a / 100; k = 0; while(k < 51) { float y = x * x / (a * a) - 1; y *= (b * b); y = Mathf.Sqrt(y); vectors[151 + k] = new Vector3(x, y, 0); if (y != 0) { vectors[151 - k] = new Vector3(x, -y, 0); } x += step; step *= 1.1f; k++; } mesh.vertices = vectors; int[] triangle = new int[400]; for (int i = 0; i < 100; ++i) { triangle[2 * i] = i; triangle[2 * i + 1] = i + 1; } for(int i = 100; i < 200; ++i) { triangle[2 * i] = i + 1; triangle[2 * i + 1] = i + 2; } mesh.SetIndices(triangle, MeshTopology.Lines, 0); GetComponent().mesh = mesh; drawGizmos = true; } public void DrawExponential(float factor, float c) { mesh.Clear(); type = Type.Spline; float step = 0.01f; Vector3[] vectors = new Vector3[100 * (maxX - minX) + 1]; int k = 0; for (float i = minX; i <= maxX; i = i + step) { float y = Mathf.Pow(c, i); y *= factor; vectors[k] = new Vector3(i, y, 0); k++; } vectors[100 * (maxX - minX)] = new Vector3(maxX, Mathf.Pow(c, maxX) * factor, 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; drawGizmos = true; } public void DrawLogarithmic(float factor, float a) { mesh.Clear(); type = Type.Spline; float step = 0.01f; Vector3[] vectors = new Vector3[100 * maxX]; int k = 0; float down = Mathf.Log(a); for (float i = step; i <= maxX; i = i + step) { float y = Mathf.Log(i) / down; y *= factor; vectors[k] = new Vector3(i, y, 0); k++; } vectors[100 * maxX - 1] = new Vector3(maxX, Mathf.Log(maxX) / down * factor, 0); mesh.vertices = vectors; int[] triangle = new int[100 * maxX * 2 - 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; drawGizmos = true; } public void DrawTrigonometric(TriType triType, float a, float b, float factor) { mesh.Clear(); type = Type.Spline; float step = 0.01f; Vector3[] vectors = new Vector3[100 * (maxX - minX) + 1]; int k = 0; for (float i = minX; i <= maxX; i = i + step) { float y = a * i + b; switch (triType) { case TriType.Sin: y = Mathf.Sin(y); break; case TriType.Cos: y = Mathf.Cos(y); break; } y *= factor; 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; } vectors[100 * (maxX - minX)] = new Vector3(maxX, Mathf.Sin(a * maxX + b) * factor, 0); mesh.SetIndices(triangle, MeshTopology.Lines, 0); GetComponent().mesh = mesh; drawGizmos = true; } private void OnDrawGizmos() { if (!drawGizmos) { return; } } }