2using System.Collections;
9 [pb_Gizmo(typeof(Light))]
13 private Light lightComponent;
14 private Mesh _lightMesh;
16 private readonly
Color yellow =
new Color(1f, 1f, 0f, .5f);
18 Matrix4x4 gizmoMatrix = Matrix4x4.identity;
20 private Mesh lightMesh
24 if(_lightMesh ==
null && lightComponent !=
null)
26 switch(lightComponent.type)
28 case LightType.Directional:
29 _lightMesh = DirectionalLightMesh();
30 lightRayMaterial = pb_BuiltinResource.GetMaterial(pb_BuiltinResource.mat_UnlitVertexColorWavy);
34 _lightMesh = SpotLightMesh();
35 lightRayMaterial = pb_BuiltinResource.GetMaterial(pb_BuiltinResource.mat_UnlitVertexColor);
39 _lightMesh = PointLightMesh();
40 lightRayMaterial = pb_BuiltinResource.GetMaterial(pb_BuiltinResource.mat_UnlitVertexColor);
44 _lightMesh = AreaLightMesh();
45 lightRayMaterial = pb_BuiltinResource.GetMaterial(pb_BuiltinResource.mat_UnlitVertexColorWavy);
58 private Mesh pointLightDisc;
60 private void RebuildGizmos()
62 if(_lightMesh !=
null)
64 pb_ObjectUtility.Destroy(lightMesh);
68 if(pointLightDisc ==
null)
70 pointLightDisc =
new Mesh();
71 pb_HandleMesh.CreateDiscMesh(ref pointLightDisc, 64, 1.1f);
72 pointLightDisc.colors = pb_CollectionUtil.Fill<
Color>(yellow, pointLightDisc.vertexCount);
78 icon = pb_BuiltinResource.GetMaterial(pb_BuiltinResource.mat_LightGizmo);
79 lightComponent = GetComponentInChildren<Light>();
90 if(lightMesh ==
null || lightComponent ==
null)
93 switch(lightComponent.type)
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);
103 gizmoMatrix.SetTRS(
trs.position,
trs.localRotation, Vector3.one);
107 for(
int i = 0; i < lightMesh.subMeshCount; i++)
108 Graphics.DrawMesh(lightMesh, gizmoMatrix,
lightRayMaterial, 0,
null, i,
null,
false,
false);
116 private Mesh PointLightMesh()
120 pb_HandleMesh.CreateRotateMesh(ref m, 64, 1f);
122 Color light_yellow = yellow;
123 light_yellow.a = .2f;
124 m.colors = pb_CollectionUtil.Fill<
Color>(light_yellow, m.vertexCount);
129 private Mesh DirectionalLightMesh()
133 const float EXTENTS = 4f;
134 const int segments = 32;
136 Vector3[] v =
new Vector3[segments * 3];
137 Vector2[] u =
new Vector2[segments * 3];
138 int[] t =
new int[segments * 3 * 2];
141 for(
int i = 0; i < segments; i++)
143 float dist = i/(float)segments;
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);
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);
170 m.colors = pb_CollectionUtil.Fill<
Color>(
Color.yellow, m.vertexCount);
171 m.normals = pb_CollectionUtil.Fill<
Vector3>(
Vector3.up, m.vertexCount);
174 m.SetIndices(t, MeshTopology.Lines, 0);
179 private Mesh SpotLightMesh()
183 float r = lightComponent.range * Mathf.Tan( Mathf.Deg2Rad * (lightComponent.spotAngle / 2f) );
185 const int RADIUS_INC = 32;
188 int[] tris =
new int[RADIUS_INC * 2 + 8];
192 for(
int i = 0; i < RADIUS_INC; i++)
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 );
197 tris[n++] = i < (RADIUS_INC - 1) ? i + 1 : 0;
202 tris[n++] = RADIUS_INC;
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;
212 m.normals = pb_CollectionUtil.Fill<
Vector3>(
Vector3.up, v.Length);
213 m.colors = pb_CollectionUtil.Fill<
Color>(yellow, v.Length);
216 m.SetIndices(tris, MeshTopology.Lines, 0);
221 private Mesh AreaLightMesh()
247 m.SetIndices(
new int[] {
260 m.colors = pb_CollectionUtil.Fill<
Color>(yellow, m.vertexCount);
override void OnComponentModified()
Material lightRayMaterial
Material icon
The icon to be rendered facing the camera at the position of this object.
Transform trs
A reference to this object's transform.
Transform cam
A reference to the main camera transform.