forked from RemoteTechnologiesGroup/RemoteTech
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkLine.cs
More file actions
134 lines (113 loc) · 4.17 KB
/
Copy pathNetworkLine.cs
File metadata and controls
134 lines (113 loc) · 4.17 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using RemoteTech.SimpleTypes;
using System.Linq;
using UnityEngine;
namespace RemoteTech.UI
{
public class NetworkLine : MonoBehaviour
{
private static Material CommNetMaterial = null;
public BidirectionalEdge<ISatellite> Edge
{
set
{
UpdateMesh(value);
}
}
public float LineWidth { get; set; }
public Material Material
{
set
{
mRenderer.material = value;
}
}
public Color Color
{
set
{
mMeshFilter.mesh.colors = Enumerable.Repeat(value, 4).ToArray();
}
}
public bool Active
{
set
{
mRenderer.enabled = value;
gameObject.SetActive(value);
}
}
private MeshFilter mMeshFilter;
private MeshRenderer mRenderer;
private Vector3[] mPoints2D = new Vector3[4];
private Vector3[] mPoints3D = new Vector3[4];
public static NetworkLine Instantiate()
{
return new GameObject("NetworkLine", typeof(NetworkLine)).GetComponent<NetworkLine>();
}
public void Awake()
{
if (CommNetMaterial == null) { CommNetMaterial = Resources.Load<Material>("Telemetry/TelemetryMaterial"); }
SetupMesh();
gameObject.layer = 31;
LineWidth = 1.0f;
Color = Color.white;
Material = CommNetMaterial;
}
private void UpdateMesh(BidirectionalEdge<ISatellite> edge)
{
var camera = PlanetariumCamera.Camera;
var start = camera.WorldToScreenPoint(ScaledSpace.LocalToScaledSpace(edge.A.Position));
var end = camera.WorldToScreenPoint(ScaledSpace.LocalToScaledSpace(edge.B.Position));
var segment = new Vector3(end.y - start.y, start.x - end.x, 0).normalized * (LineWidth / 2);
if (!MapView.Draw3DLines)
{
//if position is behind camera
if (start.z < 0) { start = NetworkLine.FlipDirection(start, end); }
else if (end.z < 0) { end = NetworkLine.FlipDirection(end, start); }
var dist = Screen.height / 2 + 0.01f;
start.z = start.z >= 0.15f ? dist : -dist;
end.z = end.z >= 0.15f ? dist : -dist;
mPoints2D[0] = (start - segment);
mPoints2D[1] = (start + segment);
mPoints2D[2] = (end - segment);
mPoints2D[3] = (end + segment);
}
else
{
mPoints3D[0] = camera.ScreenToWorldPoint(start - segment);
mPoints3D[1] = camera.ScreenToWorldPoint(start + segment);
mPoints3D[2] = camera.ScreenToWorldPoint(end - segment);
mPoints3D[3] = camera.ScreenToWorldPoint(end + segment);
}
mMeshFilter.mesh.vertices = MapView.Draw3DLines ? mPoints3D : mPoints2D;
mMeshFilter.mesh.RecalculateBounds();
mMeshFilter.mesh.MarkDynamic();
}
private void SetupMesh()
{
mMeshFilter = gameObject.AddComponent<MeshFilter>();
mMeshFilter.mesh = new Mesh();
mRenderer = gameObject.AddComponent<MeshRenderer>();
mMeshFilter.mesh.name = "NetworkLine";
mMeshFilter.mesh.vertices = new Vector3[4];
mMeshFilter.mesh.uv = new Vector2[4] { new Vector2(0, 1), new Vector2(0, 0), new Vector2(1, 1), new Vector2(1, 0) };
mMeshFilter.mesh.SetIndices(new int[] { 0, 2, 1, 2, 3, 1 }, MeshTopology.Triangles, 0);
Active = false;
}
public void OnDestroy()
{
Active = false;
Destroy(mMeshFilter);
Destroy(mRenderer);
}
public static Vector3 FlipDirection(Vector3 point, Vector3 pivot)
{
point -= pivot; //translate to origin
point.x *= -1; //flip
point.y *= -1;
point.z *= -1;
point += pivot; //undo translate
return point;
}
}
}