test.cs 1.88 KB
Newer Older
BlackAngle233's avatar
BlackAngle233 committed
1
2
3
4
5
6
7
8
9
10
11
12
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test : MonoBehaviour
{
    public CanvasManager3D CM;

    public List<Line> lines;
    public float width = 1.0f;

    Mesh mesh;
BlackAngle233's avatar
212    
BlackAngle233 committed
13
14

    bool tryGet = false;
BlackAngle233's avatar
BlackAngle233 committed
15
16
17
    // Start is called before the first frame update
    void Start()
    {
BlackAngle233's avatar
BlackAngle233 committed
18
        lines = new List<Line>();
BlackAngle233's avatar
212    
BlackAngle233 committed
19
        mesh = GetComponent<MeshFilter>().mesh;
BlackAngle233's avatar
BlackAngle233 committed
20
21
22
23
24
25
26
27
28
29
30
31
    }

    // Update is called once per frame
    void Update()
    {
        if(lines.Count != 0)
        {
            Vector3[] vertices = new Vector3[lines.Count * 4];

            int k = 0;
            foreach (Line l in lines)
            {
BlackAngle233's avatar
5.31    
BlackAngle233 committed
32
                Vector3 offset = Vector3.Cross(Camera.main.transform.forward, l.direction).normalized * width;
BlackAngle233's avatar
BlackAngle233 committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
                Vector3 tmp1 = new Vector3(l.point1.x, l.point1.y, l.point1.z);
                Vector3 tmp2 = new Vector3(l.point2.x, l.point2.y, l.point2.z);
                vertices[k] = tmp1 + offset;
                vertices[k + 1] = tmp1 - offset;
                vertices[k + 2] = tmp2 + offset;
                vertices[k + 3] = tmp2 - offset;
                k += 4;
            }
            mesh.vertices = vertices;

            int[] triangles = new int[lines.Count * 6];
            for (int i = 0; i < lines.Count; i++)
            {
                triangles[6 * i] = 4 * i;
                triangles[6 * i + 1] = 4 * i + 1;
                triangles[6 * i + 2] = 4 * i + 2;
                triangles[6 * i + 3] = 4 * i + 1;
                triangles[6 * i + 4] = 4 * i + 3;
                triangles[6 * i + 5] = 4 * i + 2;
            }
            mesh.SetIndices(triangles, MeshTopology.Triangles, 0);
        }
BlackAngle233's avatar
212    
BlackAngle233 committed
55
56
57
58
        if (tryGet)
        {
            getDig();
        }
BlackAngle233's avatar
BlackAngle233 committed
59
60
61
62
    }

    public void getDig()
    {
BlackAngle233's avatar
212    
BlackAngle233 committed
63
64
65
66
67
        if (CM.isFin)
        {
            lines = CM.GetLines();
            tryGet = false;
        }
BlackAngle233's avatar
BlackAngle233 committed
68
69
    }
}