using System.Collections; using System.Collections.Generic; using UnityEngine; public class ThreeD : MonoBehaviour { Mesh mesh; Calculator3D calculator3D; public string formula; public int minX; public int maxX; public int minY; public int maxY; // Start is called before the first frame update void Start() { mesh = new Mesh { name = "ThreeD" }; calculator3D = new Calculator3D(); } // Update is called once per frame public void DrawThreeD() { mesh.Clear(); Vector3[] vectors = new Vector3[(10 * (maxX - minX) + 1) * (10 * (maxY - minY) + 1)]; int[] triangle = new int[100 * (maxX - minX) * (maxY - minY) * 2 * 3]; int k = 0; float step = 0.1f; for(float i = minX; i <= maxX + 0.01f; i += step) { for(float j = minY; j <= maxY + 0.01f; j += step) { float z = 0; if(calculator3D.calculate3D(formula, i, j, out z)) { vectors[k] = new Vector3(i, j, z); } else { vectors[k] = vectors[k - 1]; } ++k; } } int lineCount = 10 * (maxY - minY) + 1; int line = 0; for(int i = 0; i < lineCount - 1; ++i) { i *= lineCount; for(int j = 0; j < lineCount - 1; ++j) { triangle[6 * (i + j - line)] = (i + j); triangle[6 * (i + j - line) + 1] = (i + j) + 1; triangle[6 * (i + j - line) + 2] = (i + j) + lineCount; triangle[6 * (i + j - line) + 3] = (i + j) + 1; ; triangle[6 * (i + j - line) + 4] = (i + j) + lineCount + 1; triangle[6 * (i + j - line) + 5] = (i + j) + lineCount; } i /= lineCount; line++; } 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; mesh.SetIndices(triangle, MeshTopology.Triangles, 0); GetComponent().mesh = mesh; } public void MeshClear() { mesh.Clear(); GetComponent().mesh = mesh; } }