Tanoda
pb_Gizmo_Light.cs
Go to the documentation of this file.
1using UnityEngine;
2using System.Collections;
3
4namespace GILES
5{
9 [pb_Gizmo(typeof(Light))]
10 public class pb_Gizmo_Light : pb_Gizmo
11 {
12 public Material lightRayMaterial;
13 private Light lightComponent;
14 private Mesh _lightMesh;
15
16 private readonly Color yellow = new Color(1f, 1f, 0f, .5f);
17
18 Matrix4x4 gizmoMatrix = Matrix4x4.identity;
19
20 private Mesh lightMesh
21 {
22 get
23 {
24 if(_lightMesh == null && lightComponent != null)
25 {
26 switch(lightComponent.type)
27 {
28 case LightType.Directional:
29 _lightMesh = DirectionalLightMesh();
30 lightRayMaterial = pb_BuiltinResource.GetMaterial(pb_BuiltinResource.mat_UnlitVertexColorWavy);
31 break;
32
33 case LightType.Spot:
34 _lightMesh = SpotLightMesh();
35 lightRayMaterial = pb_BuiltinResource.GetMaterial(pb_BuiltinResource.mat_UnlitVertexColor);
36 break;
37
38 case LightType.Point:
39 _lightMesh = PointLightMesh();
40 lightRayMaterial = pb_BuiltinResource.GetMaterial(pb_BuiltinResource.mat_UnlitVertexColor);
41 break;
42
43 case LightType.Area:
44 _lightMesh = AreaLightMesh();
45 lightRayMaterial = pb_BuiltinResource.GetMaterial(pb_BuiltinResource.mat_UnlitVertexColorWavy);
46 break;
47
48 default:
49 _lightMesh = null;
50 break;
51 }
52 }
53
54 return _lightMesh;
55 }
56 }
57
58 private Mesh pointLightDisc;
59
60 private void RebuildGizmos()
61 {
62 if(_lightMesh != null)
63 {
64 pb_ObjectUtility.Destroy(lightMesh);
65 _lightMesh = null;
66 }
67
68 if(pointLightDisc == null)
69 {
70 pointLightDisc = new Mesh();
71 pb_HandleMesh.CreateDiscMesh(ref pointLightDisc, 64, 1.1f);
72 pointLightDisc.colors = pb_CollectionUtil.Fill<Color>(yellow, pointLightDisc.vertexCount);
73 }
74 }
75
76 private void Start()
77 {
78 icon = pb_BuiltinResource.GetMaterial(pb_BuiltinResource.mat_LightGizmo);
79 lightComponent = GetComponentInChildren<Light>();
80 RebuildGizmos();
81 }
82
83 public override void Update()
84 {
85 base.Update();
86
87 if(!isSelected)
88 return;
89
90 if(lightMesh == null || lightComponent == null)
91 return;
92
93 switch(lightComponent.type)
94 {
95 case LightType.Point:
96
97 gizmoMatrix.SetTRS(trs.position, cam.localRotation, Vector3.one * lightComponent.range);
98 Graphics.DrawMesh(pointLightDisc, gizmoMatrix, lightRayMaterial, 0, null, 0, null, false, false);
99 gizmoMatrix.SetTRS(trs.position, Quaternion.identity, Vector3.one * lightComponent.range);
100 break;
101
102 default:
103 gizmoMatrix.SetTRS(trs.position, trs.localRotation, Vector3.one);
104 break;
105 }
106
107 for(int i = 0; i < lightMesh.subMeshCount; i++)
108 Graphics.DrawMesh(lightMesh, gizmoMatrix, lightRayMaterial, 0, null, i, null, false, false);
109 }
110
111 public override void OnComponentModified()
112 {
113 RebuildGizmos();
114 }
115
116 private Mesh PointLightMesh()
117 {
118 Mesh m = new Mesh();
119
120 pb_HandleMesh.CreateRotateMesh(ref m, 64, 1f);
121
122 Color light_yellow = yellow;
123 light_yellow.a = .2f;
124 m.colors = pb_CollectionUtil.Fill<Color>(light_yellow, m.vertexCount);
125
126 return m;
127 }
128
129 private Mesh DirectionalLightMesh()
130 {
131 Mesh m = new Mesh();
132
133 const float EXTENTS = 4f;
134 const int segments = 32;
135
136 Vector3[] v = new Vector3[segments * 3];
137 Vector2[] u = new Vector2[segments * 3];
138 int[] t = new int[segments * 3 * 2];
139
140 int n = 0, c = 0;
141 for(int i = 0; i < segments; i++)
142 {
143 float dist = i/(float)segments;
144
145 v[n+0] = new Vector3(-.6f, -.5f, dist * EXTENTS);
146 v[n+1] = new Vector3( .6f, -.5f, dist * EXTENTS);
147 v[n+2] = new Vector3( 0f, .2f, dist * EXTENTS);
148
149 u[n+0] = new Vector2(dist, (1-dist) + .0f);
150 u[n+1] = new Vector2(dist, (1-dist) + .23f);
151 u[n+2] = new Vector2(dist, (1-dist) + .34f);
152
153 if(i < segments - 1)
154 {
155 t[c++] = n+0;
156 t[c++] = n+3;
157
158 t[c++] = n+1;
159 t[c++] = n+4;
160
161 t[c++] = n+2;
162 t[c++] = n+5;
163 }
164
165 n += 3;
166 }
167
168 m.vertices = v;
169 m.uv = u;
170 m.colors = pb_CollectionUtil.Fill<Color>(Color.yellow, m.vertexCount);
171 m.normals = pb_CollectionUtil.Fill<Vector3>(Vector3.up, m.vertexCount);
172
173 m.subMeshCount = 1;
174 m.SetIndices(t, MeshTopology.Lines, 0);
175
176 return m;
177 }
178
179 private Mesh SpotLightMesh()
180 {
181 Mesh m = new Mesh();
182
183 float r = lightComponent.range * Mathf.Tan( Mathf.Deg2Rad * (lightComponent.spotAngle / 2f) );
184
185 const int RADIUS_INC = 32;
186
187 Vector3[] v = new Vector3[RADIUS_INC + 1];
188 int[] tris = new int[RADIUS_INC * 2 + 8];
189
190 int n = 0;
191
192 for(int i = 0; i < RADIUS_INC; i++)
193 {
194 float p = (i/(float)RADIUS_INC) * 360f * Mathf.Deg2Rad;
195 v[i] = new Vector3( Mathf.Cos(p) * r, Mathf.Sin(p) * r, lightComponent.range );
196 tris[n++] = i;
197 tris[n++] = i < (RADIUS_INC - 1) ? i + 1 : 0;
198 }
199
200 v[RADIUS_INC] = Vector3.zero;
201
202 tris[n++] = RADIUS_INC;
203 tris[n++] = 0;
204 tris[n++] = RADIUS_INC;
205 tris[n++] = RADIUS_INC / 4;
206 tris[n++] = RADIUS_INC;
207 tris[n++] = RADIUS_INC / 2;
208 tris[n++] = RADIUS_INC;
209 tris[n++] = (RADIUS_INC / 4) * 3;
210
211 m.vertices = v;
212 m.normals = pb_CollectionUtil.Fill<Vector3>(Vector3.up, v.Length);
213 m.colors = pb_CollectionUtil.Fill<Color>(yellow, v.Length);
214
215 m.subMeshCount = 1;
216 m.SetIndices(tris, MeshTopology.Lines, 0);
217
218 return m;
219 }
220
221 private Mesh AreaLightMesh()
222 {
223 Mesh m = new Mesh();
224
225 // Vector3 areaSize = new Vector3(light.areaSize.x, light.areaSize.y, 0f);
226 Vector3 areaSize = Vector3.one;
227
228 m.vertices = new Vector3[]
229 {
230 Vector3.Scale(new Vector3(-.5f, -.5f, 0f), areaSize),
231 Vector3.Scale(new Vector3( .5f, -.5f, 0f), areaSize),
232 Vector3.Scale(new Vector3( .5f, .5f, 0f), areaSize),
233 Vector3.Scale(new Vector3(-.5f, .5f, 0f), areaSize),
234
235 Vector3.Scale(new Vector3(-.4f, -.4f, 0f), areaSize),
236 Vector3.Scale(new Vector3( .4f, -.4f, 0f), areaSize),
237 Vector3.Scale(new Vector3( .4f, .4f, 0f), areaSize),
238 Vector3.Scale(new Vector3(-.4f, .4f, 0f), areaSize),
239
240 Vector3.Scale(new Vector3(-.4f, -.4f, 0f), areaSize) + Vector3.forward,
241 Vector3.Scale(new Vector3( .4f, -.4f, 0f), areaSize) + Vector3.forward,
242 Vector3.Scale(new Vector3( .4f, .4f, 0f), areaSize) + Vector3.forward,
243 Vector3.Scale(new Vector3(-.4f, .4f, 0f), areaSize) + Vector3.forward
244 };
245
246 m.subMeshCount = 1;
247 m.SetIndices( new int[] {
248 0, 1,
249 1, 2,
250 2, 3,
251 3, 0,
252 4, 8,
253 5, 9,
254 6, 10,
255 7, 11
256 },
257 MeshTopology.Lines,
258 0 );
259
260 m.colors = pb_CollectionUtil.Fill<Color>(yellow, m.vertexCount);
261
262 return m;
263 }
264 }
265}
UnityEngine.Color Color
Definition: TestScript.cs:32
override void OnComponentModified()
override void Update()
Material icon
The icon to be rendered facing the camera at the position of this object.
Definition: pb_Gizmo.cs:17
Transform trs
A reference to this object's transform.
Definition: pb_Gizmo.cs:23
Transform cam
A reference to the main camera transform.
Definition: pb_Gizmo.cs:20
bool isSelected
Definition: pb_Gizmo.cs:29