forked from tddebart/LocalZoom
/
ViewSphere.cs
175 lines (150 loc) · 5.24 KB
/
ViewSphere.cs
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System.Collections.Generic;
using UnityEngine;
public class ViewSphere : MonoBehaviour
{
private LayerMask layerMask;
private Mesh mesh;
[Range(0, 360)]
public float fov = 90f;
public float viewDistance = 40f;
public Player player;
[Space]
public float meshResolution = 1;
public int edgeResolveIterations = 6;
public float edgeDstThreshold = 0.5f;
void Start()
{
mesh = new Mesh();
mesh.name = "ViewMesh";
GetComponent<MeshFilter>().mesh = mesh;
layerMask = ~(LayerMask.GetMask("PlayerObjectCollider") |
LayerMask.GetMask("Player") |
LayerMask.GetMask("Projectile") |
LayerMask.GetMask("BackgroundObject") |
LocalZoom.LocalZoom.extraLayerMask);
}
void LateUpdate()
{
var rotation = transform.eulerAngles;
rotation.z = player.data.weaponHandler.gun.transform.eulerAngles.z;
transform.eulerAngles = rotation;
DrawFOV();
}
void DrawFOV()
{
var stepCount = Mathf.RoundToInt(fov * meshResolution);
var stepAngelSize = fov / stepCount;
List<Vector3> viewPoints = new List<Vector3>();
ViewCastInfo oldViewCast = new ViewCastInfo();
for (int i = 0; i < stepCount; i++)
{
var angle = -transform.eulerAngles.z - fov / 2 + stepAngelSize * i;
ViewCastInfo newViewCast = ViewCast(angle);
if (i > 0)
{
bool edgeDstThresholdExceeded = Mathf.Abs(oldViewCast.dst - newViewCast.dst) > edgeDstThreshold;
if (oldViewCast.hit != newViewCast.hit || (oldViewCast.hit && newViewCast.hit && edgeDstThresholdExceeded))
{
EdgeInfo edge = FindEdge(oldViewCast, newViewCast);
if (edge.pointA != Vector3.zero)
{
viewPoints.Add(edge.pointA);
}
if (edge.pointB != Vector3.zero)
{
viewPoints.Add(edge.pointB);
}
}
}
viewPoints.Add(newViewCast.point);
oldViewCast = newViewCast;
}
int vertexCount = viewPoints.Count + 1;
Vector3[] vertices = new Vector3[vertexCount];
int[] triangles = new int[(vertexCount - 2) * 3];
vertices[0] = Vector3.zero;
for (int i = 0; i < vertexCount - 1; i++)
{
vertices[i + 1] = transform.InverseTransformPoint(viewPoints[i]);
if (i < vertexCount - 2)
{
triangles[i * 3 + 0] = 0;
triangles[i * 3 + 1] = i + 1;
triangles[i * 3 + 2] = i + 2;
}
}
mesh.Clear();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.RecalculateNormals();
}
EdgeInfo FindEdge(ViewCastInfo minView, ViewCastInfo maxView)
{
float minAngle = minView.angle;
float maxAngle = maxView.angle;
Vector3 minPoint = Vector3.zero;
Vector3 maxPoint = Vector3.zero;
for (int i = 0; i < edgeResolveIterations; i++)
{
float angle = (minAngle + maxAngle) / 2;
ViewCastInfo newViewCast = ViewCast(angle);
bool edgeDstThresholdExceeded = Mathf.Abs(minView.dst - newViewCast.dst) > edgeDstThreshold;
if (newViewCast.hit == minView.hit && !edgeDstThresholdExceeded)
{
minAngle = angle;
minPoint = newViewCast.point;
}
else
{
maxAngle = angle;
maxPoint = newViewCast.point;
}
}
return new EdgeInfo(minPoint, maxPoint);
}
ViewCastInfo ViewCast(float globalAngle)
{
Vector3 dir = DirFromAngle(globalAngle, true);
RaycastHit2D hit = Physics2D.Raycast(transform.position, dir, viewDistance, layerMask);
if (hit.collider != null)
{
return new ViewCastInfo(true, hit.point, hit.distance, globalAngle);
}
else
{
return new ViewCastInfo(false, transform.position + dir * viewDistance, viewDistance, globalAngle);
}
}
public Vector3 DirFromAngle(float angleInDegrees, bool angleIsGlobal)
{
if (!angleIsGlobal)
{
angleInDegrees += transform.eulerAngles.z;
}
return new Vector3(Mathf.Sin(angleInDegrees * Mathf.Deg2Rad), Mathf.Cos(angleInDegrees * Mathf.Deg2Rad));
}
public struct ViewCastInfo
{
public bool hit;
public Vector3 point;
public float dst;
public float angle;
public ViewCastInfo(bool _hit, Vector3 _point, float _dst, float _angle)
{
hit = _hit;
point = _point;
dst = _dst;
angle = _angle;
}
}
public struct EdgeInfo
{
public Vector3 pointA;
public Vector3 pointB;
public EdgeInfo(Vector3 _pointA, Vector3 _pointB)
{
pointA = _pointA;
pointB = _pointB;
}
}
}