ThreeD.cs 2.11 KB
Newer Older
BlackAngle233's avatar
BlackAngle233 committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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();
        DrawCustomized();
    }

    // Update is called once per frame
    public void DrawCustomized()
    {
        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;
        mesh.SetIndices(triangle, MeshTopology.Triangles, 0);

        GetComponent<MeshFilter>().mesh = mesh;
    }
}