private void SetShadowCamera() { if (this.eyeCamera.renderingPath == RenderingPath.DeferredShading && this.enableFarShadows) { Shader.EnableKeyword("_USING_DEFERREDSHADING"); this.shadowCameraGO.transform.rotation = Quaternion.LookRotation(Sunshine.Instance.SunLight.transform.forward); Transform transform = this.shadowCameraGO.transform; SunshineMath.BoundingSphere boundingSphere = default(SunshineMath.BoundingSphere); boundingSphere = SunshineMath.FrustumBoundingSphereBinarySearch(this.eyeCamera, this.eyeCamera.nearClipPlane, this.FarCascadeDistance, true, 0f, 0.01f, 20); float num = SunshineMath.QuantizeValueWithoutFlicker(boundingSphere.radius, 100, this.last_boundingRadius); this.last_boundingRadius = num; float num2 = num * 2f; transform.position = boundingSphere.origin; transform.position = this.eyeCamera.transform.position - Sunshine.Instance.SunLight.transform.forward * this.FarCascadeDistance * 0.5f; Vector3 vector = transform.InverseTransformPoint(Vector3.zero); float step = num2 / (float)this.shadowMapSize; vector.x = SunshineMath.QuantizeValueWithoutFlicker(vector.x, step, this.last_lightWorldOrigin.x); vector.y = SunshineMath.QuantizeValueWithoutFlicker(vector.y, step, this.last_lightWorldOrigin.y); this.last_lightWorldOrigin = vector; transform.position -= transform.TransformPoint(vector); Vector3 vector2 = transform.InverseTransformPoint(boundingSphere.origin); transform.position += transform.forward * (vector2.z - (boundingSphere.radius + this.lightCamera.nearClipPlane)); this.lightCamera.orthographicSize = num2 * 0.5f; this.lightCamera.nearClipPlane = this.eyeCamera.nearClipPlane; this.lightCamera.farClipPlane = (boundingSphere.radius + this.lightCamera.nearClipPlane) * 2f; this.lightCamera.cullingMask = this.CullingMask; this.bb_quad = Vector3.Dot(-Sunshine.Instance.SunLight.transform.forward, Vector3.up); this.biasLerp = Mathf.Abs(this.bb_quad); this.bb_quad = Mathf.Clamp01(Mathf.Clamp01(this.bb_quad + 0.0001f) - 0.75f) * 4f; this.bb_billboard = 1f - this.bb_quad; Shader.SetGlobalVector(this.billboardShadeFadeFactorsProp, new Vector4(this.bb_billboard, this.bb_quad, 0f, 0f)); this.biasLerp = Mathf.Clamp01(Mathf.Abs(0.7071f - this.biasLerp) * 4f); this.FarCascadeDepthBias = Mathf.Lerp(this.MaxFarCascadeDepthBias, this.MinFarCascadeDepthBias, this.biasLerp); this.SetFarShadowMatrix(boundingSphere.radius, this.FarCascadeDistance); Component[] componentsInChildren = this.parent.GetComponentsInChildren(typeof(MeshFilter)); int num3 = componentsInChildren.Length; GL.PushMatrix(); Graphics.SetRenderTarget(this.m_shadowTexture); GL.LoadProjectionMatrix(this.lightCamera.projectionMatrix); GL.Clear(true, false, Color.black, 6000f); this.mat.SetPass(0); foreach (MeshFilter meshFilter in componentsInChildren) { Graphics.DrawMeshNow(meshFilter.mesh, this.matrix); } GL.PopMatrix(); } else { Shader.DisableKeyword("_USING_DEFERREDSHADING"); } float num4 = QualitySettings.shadowDistance * 0.125f; Shader.SetGlobalVector(this.farCascadeBlendValuesProps, new Vector4(QualitySettings.shadowDistance - num4 * 2f, num4, this.FarCascadeDistance - this.FarCascadeDistance * 0.275f)); }
private void SetShadowCamera() { if (this.eyeCamera.renderingPath == RenderingPath.DeferredShading && this.enableFarShadows) { Shader.EnableKeyword("_USING_DEFERREDSHADING"); if (this.shadowCameraGO == null || Sunshine.Instance == null) { return; } this.shadowCameraGO.transform.rotation = Quaternion.LookRotation(Sunshine.Instance.SunLight.transform.forward); Transform transform = this.shadowCameraGO.transform; SunshineMath.BoundingSphere boundingSphere = default(SunshineMath.BoundingSphere); boundingSphere = SunshineMath.FrustumBoundingSphereBinarySearch(this.eyeCamera, this.eyeCamera.nearClipPlane, this.FarCascadeDistance, true, 0f, 0.01f, 20); float num = SunshineMath.QuantizeValueWithoutFlicker(boundingSphere.radius, 100, this.last_boundingRadius); this.last_boundingRadius = num; float num2 = num * 2f; transform.position = boundingSphere.origin; transform.position = this.eyeCamera.transform.position - Sunshine.Instance.SunLight.transform.forward * this.FarCascadeDistance * 0.5f; Vector3 vector = transform.InverseTransformPoint(Vector3.zero); float step = num2 / (float)this.shadowMapSize; vector.x = SunshineMath.QuantizeValueWithoutFlicker(vector.x, step, this.last_lightWorldOrigin.x); vector.y = SunshineMath.QuantizeValueWithoutFlicker(vector.y, step, this.last_lightWorldOrigin.y); this.last_lightWorldOrigin = vector; transform.position -= transform.TransformPoint(vector); Vector3 vector2 = transform.InverseTransformPoint(boundingSphere.origin); transform.position += transform.forward * (vector2.z - (boundingSphere.radius + this.lightCamera.nearClipPlane)); this.lightCamera.orthographicSize = num2 * 0.5f; this.lightCamera.nearClipPlane = this.eyeCamera.nearClipPlane; this.lightCamera.farClipPlane = (boundingSphere.radius + this.lightCamera.nearClipPlane) * 2f; this.lightCamera.cullingMask = this.CullingMask; this.bb_quad = Vector3.Dot(-Sunshine.Instance.SunLight.transform.forward, Vector3.up); this.biasLerp = Mathf.Abs(this.bb_quad); this.bb_quad = Mathf.Clamp01(Mathf.Clamp01(this.bb_quad + 0.0001f) - 0.75f) * 4f; this.bb_billboard = 1f - this.bb_quad; Shader.SetGlobalVector(this.billboardShadeFadeFactorsProp, new Vector4(this.bb_billboard, this.bb_quad, 0f, 0f)); this.biasLerp = Mathf.Clamp01(Mathf.Abs(0.7071f - this.biasLerp) * 4f); this.FarCascadeDepthBias = Mathf.Lerp(this.MaxFarCascadeDepthBias, this.MinFarCascadeDepthBias, this.biasLerp); this.SetFarShadowMatrix(boundingSphere.radius, this.FarCascadeDistance); float shadowDistance = QualitySettings.shadowDistance; QualitySettings.shadowDistance = 0f; this.lightCamera.Render(); QualitySettings.shadowDistance = shadowDistance; Shader.SetGlobalTexture(this.farCascadeProp, this.m_shadowTexture); Shader.SetGlobalMatrix(this.farCascadeMatrixProp, this.m_shadowMatrix); } else { Shader.DisableKeyword("_USING_DEFERREDSHADING"); } float num3 = QualitySettings.shadowDistance * 0.125f; Shader.SetGlobalVector(this.farCascadeBlendValuesProps, new Vector4(QualitySettings.shadowDistance - num3 * 2f, num3, this.FarCascadeDistance - this.FarCascadeDistance * 0.275f)); }
public static void SetupShadowCamera(Light light, Camera lightCamera, Camera eyeCamera, float eyeNearClip, float eyeFarClip, float paddingZ, float paddingRadius, int snapResolution, ref SunshineMath.BoundingSphere totalShadowBounds, ref SunshineMath.ShadowCameraTemporalData temporalData) { Transform transform = lightCamera.transform; SunshineMath.BoundingSphere boundingSphere = default(SunshineMath.BoundingSphere); if (Sunshine.Instance.UsingCustomBounds) { boundingSphere = Sunshine.Instance.CustomBounds; } else { boundingSphere = SunshineMath.FrustumBoundingSphereBinarySearch(eyeCamera, eyeNearClip, eyeFarClip, true, paddingRadius, 0.01f, 20); } float num = SunshineMath.QuantizeValueWithoutFlicker(boundingSphere.radius, 100, temporalData.boundingRadius); temporalData.boundingRadius = num; float num2 = num * 2f; lightCamera.aspect = 1f; lightCamera.orthographic = true; lightCamera.nearClipPlane = eyeCamera.nearClipPlane; lightCamera.farClipPlane = (totalShadowBounds.radius + paddingZ + lightCamera.nearClipPlane) * 2f; lightCamera.orthographicSize = num2 * 0.5f; transform.rotation = Quaternion.LookRotation(light.transform.forward); transform.position = boundingSphere.origin; Vector3 vector = transform.InverseTransformPoint(Vector3.zero); float step = num2 / (float)snapResolution; vector.x = SunshineMath.QuantizeValueWithoutFlicker(vector.x, step, temporalData.lightWorldOrigin.x); vector.y = SunshineMath.QuantizeValueWithoutFlicker(vector.y, step, temporalData.lightWorldOrigin.y); temporalData.lightWorldOrigin = vector; transform.position -= transform.TransformPoint(vector); Vector3 vector2 = transform.InverseTransformPoint(totalShadowBounds.origin); transform.position += transform.forward * (vector2.z - (totalShadowBounds.radius + lightCamera.nearClipPlane + paddingZ)); }
public static float QuantizeValueWithoutFlicker(float number, int resolution, float lastResult) { return(SunshineMath.QuantizeValueWithoutFlicker(number, 1f / (float)resolution, lastResult)); }