using System.Collections; using System.Collections.Generic; using UnityEngine; public class Ellipse : MonoBehaviour { Mesh mesh; public Vector3 center; public float a; public float b; // Start is called before the first frame update void Start() { mesh = new Mesh { name = "Ellipse" }; } // Update is called once per frame void Update() { } public void DrawEllipse() { mesh.Clear(); 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; } public void DrawEllipse3D() { mesh.Clear(); Vector3[] vectors = new Vector3[400]; 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, -5); if (y != 0) { vectors[400 - k] = new Vector3(i, -y, -5); } k++; vectors[k] = new Vector3(i, y, 5); if (y != 0) { if(k % 2 == 0) vectors[400 - k] = new Vector3(i, -y, 5); else vectors[402 - k] = new Vector3(i, -y, 5); } k++; } 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 + 4) / 8); } mesh.uv = uvs; int[] triangle = new int[1200]; for (int i = 0; i < 199; ++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; } triangle[1194] = 398; triangle[1195] = 399; triangle[1196] = 0; triangle[1197] = 399; triangle[1198] = 1; triangle[1199] = 0; mesh.SetIndices(triangle, MeshTopology.Triangles, 0); GetComponent().mesh = mesh; } public void MeshClear() { mesh.Clear(); GetComponent().mesh = mesh; } }