2using System.Collections;
12 public bool directional {
get{
return m_LightType == LightType.Directional;}}
13 public bool spot {
get{
return m_LightType == LightType.Spot;}}
15 Bounds GetBoundsLocal()
18 return new Bounds(
new Vector3(0, 0,
m_Size.z*0.5f),
m_Size);
23 float baseSize = Mathf.Tan(l.spotAngle * Mathf.Deg2Rad * 0.5f) *
m_SpotFar * l.range * 2.0f;
24 return new Bounds(offset,
new Vector3(baseSize, baseSize, height));
27 Matrix4x4 GetBoundsMatrix()
29 Bounds bounds = GetBoundsLocal();
30 Transform t = transform;
31 return Matrix4x4.TRS(t.position + t.forward * bounds.center.z, t.rotation, bounds.size);
34 float GetFrustumApex()
40 void OnDrawGizmosSelected()
44 Gizmos.color =
Color.yellow;
47 Gizmos.matrix = GetBoundsMatrix();
52 Transform t = transform;
54 Gizmos.matrix = Matrix4x4.TRS(t.position, t.rotation,
Vector3.one);
59 void RenderQuadSections(Vector4 lightPos)
61 for (
int i = 0; i < 4; i++)
64 if (i == 0 && lightPos.y > 1 ||
65 i == 1 && lightPos.x > 1 ||
66 i == 2 && lightPos.y < -1 ||
67 i == 3 && lightPos.x < -1)
72 float top = i / 2.0f - 1.0f;
73 float bottom = top + 0.5f;
75 GL.Vertex3(-1, top, 0);
76 GL.Vertex3(1, top, 0);
77 GL.Vertex3(1, bottom, 0);
78 GL.Vertex3(-1, bottom, 0);
87 GL.Vertex3 (-1,-1, 0);
89 GL.Vertex3 (-1, 1, 0);
91 GL.Vertex3 ( 1, 1, 0);
93 GL.Vertex3 ( 1,-1, 0);
97 void RenderSpotFrustum()
99 Graphics.DrawMeshNow(m_SpotMesh, transform.position, transform.rotation);
104 Vector3 lightPos = transform.position;
109 return new Vector4(lightViewportPos3.x*2.0f - 1.0f, lightViewportPos3.y*2.0f - 1.0f, 0, 0);
116 return GeometryUtility.TestPlanesAABB(GeometryUtility.CalculateFrustumPlanes(lightToCameraProjection), GetBoundsLocal());
119 bool IntersectsNearPlane()
124 Vector3[] vertices = m_SpotMesh.vertices;
126 Transform t = transform;
127 for (
int i = 0; i < vertices.Length; i++)
129 float z =
m_CurrentCamera.WorldToViewportPoint(t.TransformPoint(vertices[i])).z;
130 if (z < nearPlaneFudged)
136 void SetKeyword(
bool firstOn,
string firstKeyword,
string secondKeyword)
138 Shader.EnableKeyword(firstOn ? firstKeyword : secondKeyword);
139 Shader.DisableKeyword(firstOn ? secondKeyword : firstKeyword);
144 m_ShadowmapDirty =
true;
147 void GetFrustumRays(out Matrix4x4 frustumRays, out Vector3 cameraPosLocal)
151 Matrix4x4 m = GetBoundsMatrix().inverse;
152 Vector2[] uvs =
new Vector2[] {
new Vector2(0, 0),
new Vector2(1, 0),
new Vector2(1, 1),
new Vector2(0, 1)};
153 frustumRays =
new Matrix4x4();
155 for (
int i = 0; i < 4; i++)
158 ray = m.MultiplyVector(ray);
159 frustumRays.SetRow(i, ray);
162 cameraPosLocal = m.MultiplyPoint3x4(cameraPos);
165 void SetFrustumRays(Material material)
167 Matrix4x4 frustumRays;
169 GetFrustumRays(out frustumRays, out cameraPosLocal);
170 material.SetVector(
"_CameraPosLocal", cameraPosLocal);
171 material.SetMatrix(
"_FrustumRays", frustumRays);
172 material.SetFloat(
"_FrustumApex", GetFrustumApex());
175 float GetDepthThresholdAdjusted()
199 cam.depthTextureMode |= DepthTextureMode.Depth;
void UpdateCameraDepthMode()