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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
#define LightCone_OnDrawGizmos_Collide
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent( typeof(Light) )]
public class LightCone : MonoBehaviour {
public int wedges = 36;
public float dist = 20;
public float width = 2;
public float height = 2;
[SerializeField]
private Color _color = new Color(1, 1, 1, 0.25f);
public LayerMask collidesWith;
public int gizmoWedges = 4;
[Header("Spotlight Settings")]
public bool spotlightEnabled = true;
public float spotAnglePercent = 100;
public float rangePercent = 100;
MeshFilter mF;
Mesh m;
Vector3[] verts;
int[] tris;
Material mat;
RaycastHit[] hits;
private Light lt;
private float dist0, width0, sAP0, rP0;
public Color color
{
get
{
return _color;
}
set
{
_color = value;
UpdateLight();
}
}
// Use this for initialization
void Start () {
mF = GetComponent<MeshFilter>();
m = mF.mesh;
m.Clear();
MakeVerts();
tris = new int[wedges*3];
int triN=0;
int vertN=1;
for (int i=0; i<wedges; i++) {
tris[triN] = 0;
triN++;
if (vertN < verts.Length-1) {
tris[triN] = vertN+1;
} else {
tris[triN] = 1;
}
triN++;
tris[triN] = vertN;
vertN++;
triN++;
}
m.triangles = tris;
mat = GetComponent<Renderer>().material;
UpdateLight();
}
void MakeVerts() {
verts = new Vector3[wedges+1];
verts[0] = Vector3.zero;
Vector3 v, r;
hits = new RaycastHit[wedges];
for (int i=0; i<wedges; i++) {
v = Vector3.zero;
v.z = dist;
v.x = Mathf.Cos(i * Mathf.PI * 2 / wedges) * width;
v.y = Mathf.Sin(i * Mathf.PI * 2 / wedges) * height;
r = transform.rotation * v.normalized;
if ( Physics.Raycast(transform.position, r, out hits[i], v.magnitude, collidesWith) ) {
v = v.normalized * hits[i].distance;
}
verts[i+1] = v;
}
m.vertices = verts;
}
// Update is called once per frame
void Update () {
MakeVerts();
if (color != mat.color) {
mat.color = color;
}
}
// This draws the outline when not playing (and Selected)
void OnDrawGizmosSelected() {
if (Application.isPlaying) return;
Vector3[] pts = new Vector3[gizmoWedges];
Vector3 v, vDir;
Color litColor = color;
litColor.a = 1;
#if LightCone_OnDrawGizmos_Collide
Color grayColor = Color.Lerp(Color.clear, color, 0.5f);
grayColor.a = 0.5f;
#else
Gizmos.color = litColor;
#endif
for (int i=0; i<gizmoWedges; i++) {
v = Vector3.zero;
v.z = dist;
v.x = Mathf.Cos(i * Mathf.PI * 2 / gizmoWedges) * width;
v.y = Mathf.Sin(i * Mathf.PI * 2 / gizmoWedges) * height;
vDir = transform.rotation * v;
pts[i] = vDir;
pts[i] += transform.position;
#if LightCone_OnDrawGizmos_Collide
#else
Gizmos.DrawLine(transform.position, pts[i]);
if (i>0) {
Gizmos.DrawLine(pts[i-1], pts[i]);
if (i == gizmoWedges-1) {
Gizmos.DrawLine(pts[i], pts[0]);
}
}
#endif
}
#if LightCone_OnDrawGizmos_Collide
Vector3[] ptsColl = new Vector3[gizmoWedges];
RaycastHit hitInfo;
for (int i = 0; i < pts.Length; i++) {
ptsColl[i] = pts[i];
vDir = ptsColl[i] - transform.position;
if (Physics.Raycast(transform.position, vDir, out hitInfo, vDir.magnitude, collidesWith))
{
// The line collided with something, so make it shorter
ptsColl[i] = hitInfo.point;
}
// Draw the area past the collision
Gizmos.color = grayColor;
Gizmos.DrawLine(ptsColl[i], pts[i]);
if (i > 0)
{
Gizmos.DrawLine(pts[i - 1], pts[i]);
if (i == gizmoWedges - 1)
{
Gizmos.DrawLine(pts[i], pts[0]);
}
}
// Draw the collision
Gizmos.color = litColor;
Gizmos.DrawLine(transform.position, ptsColl[i]);
if (i > 0)
{
Gizmos.DrawLine(ptsColl[i - 1], ptsColl[i]);
if (i == gizmoWedges - 1)
{
Gizmos.DrawLine(ptsColl[i], ptsColl[0]);
}
}
}
#endif
}
private void OnDrawGizmos()
{
if (Application.isPlaying) return;
UpdateLight();
}
void UpdateLight() {
if (lt == null) {
lt = GetComponent<Light>();
lt.type = LightType.Spot;
}
if (dist0 != dist || width0 != width || sAP0 != spotAnglePercent || rangePercent != rP0) {
lt.spotAngle = Mathf.Atan2(width, dist)*360/Mathf.PI * (spotAnglePercent / 100f);
lt.range = dist * rangePercent / 100f;
dist0 = dist;
width0 = width;
sAP0 = spotAnglePercent;
rP0 = rangePercent;
}
lt.color = color;
lt.enabled = spotlightEnabled;
}
public RaycastHit[] GetRaycastHits() {
return hits;
}
}
|