/// <summary> /// Initialize Per frame shadowmap buffers for Shadowmap shader /// </summary> public static void UpdateShadowMapState(ref ShadowMapComponent comp, ref ShadowmapSettings settings) { Camera shadowCam = comp.shadowCam; Shader.SetGlobalFloat(ShaderIDs._ShadowCamFarClip, shadowCam.farClipPlane); Graphics.SetRenderTarget(comp.shadowmapTexture); GL.Clear(true, true, Color.white); Shader.SetGlobalVector(ShaderIDs._NormalBiases, settings.normalBias); Shader.SetGlobalVector(ShaderIDs._ShadowDisableDistance, new Vector4(settings.firstLevelDistance, settings.secondLevelDistance, settings.thirdLevelDistance, settings.farestDistance)); Shader.SetGlobalVector(ShaderIDs._LightDirection, -comp.light.transform.forward); Shader.SetGlobalVector(ShaderIDs._LightFinalColor, comp.light.color * comp.light.intensity); Shader.SetGlobalVector(ShaderIDs._SoftParam, settings.cascadeSoftValue / settings.resolution); }
/// <summary> /// Set Shadowcamera Position /// </summary> /// <param name="shadMap"></param> Shadowmap component /// <param name="settings"></param> Shadowmap Settings public static void SetShadowCameraPositionCloseFit(ref ShadowMapComponent shadMap, ref ShadowmapSettings settings) { Camera shadowCam = shadMap.shadowCam; NativeArray <AspectInfo> shadowFrustumPlanes = shadMap.shadowFrustumPlanes; AspectInfo info = shadowFrustumPlanes[0]; info.planeNormal = shadowCam.transform.right; shadowFrustumPlanes[0] = info; info = shadowFrustumPlanes[1]; info.planeNormal = shadowCam.transform.up; shadowFrustumPlanes[1] = info; info = shadowFrustumPlanes[2]; info.planeNormal = shadowCam.transform.forward; shadowFrustumPlanes[2] = info; for (int i = 0; i < 3; ++i) { info = shadowFrustumPlanes[i]; float least = float.MaxValue; float maximum = float.MinValue; Vector3 lessPoint = Vector3.zero; Vector3 morePoint = Vector3.zero; for (int x = 0; x < 8; ++x) { float dotValue = Vector3.Dot(info.planeNormal, shadMap.frustumCorners[x]); if (dotValue < least) { least = dotValue; lessPoint = shadMap.frustumCorners[x]; } if (dotValue > maximum) { maximum = dotValue; morePoint = shadMap.frustumCorners[x]; } } info.size = (maximum - least) / 2f; info.inPlanePoint = lessPoint + info.planeNormal * info.size; shadowFrustumPlanes[i] = info; } AspectInfo temp = shadowFrustumPlanes[2]; temp.size = settings.farestDistance; //Farest Cascade Distance shadowFrustumPlanes[2] = temp; Transform tr = shadowCam.transform; for (int i = 0; i < 3; ++i) { info = shadowFrustumPlanes[i]; float dist = Vector3.Dot(info.inPlanePoint, info.planeNormal) - Vector3.Dot(tr.position, info.planeNormal); tr.position += dist * info.planeNormal; } shadowCam.orthographicSize = shadowFrustumPlanes[1].size; shadowCam.aspect = shadowFrustumPlanes[0].size / shadowFrustumPlanes[1].size; shadowCam.nearClipPlane = 0; shadowCam.farClipPlane = shadowFrustumPlanes[2].size * 2; tr.position -= shadowFrustumPlanes[2].size * shadowFrustumPlanes[2].planeNormal; }