| using System; |
| using System.Collections.Generic; |
| using UnityEngine.Experimental.Rendering; |
| using UnityEngine.Rendering.RenderGraphModule; |
|
|
| namespace UnityEngine.Rendering.Universal |
| { |
| |
| |
| |
| public struct ShadowSliceData |
| { |
| |
| |
| |
| public Matrix4x4 viewMatrix; |
|
|
| |
| |
| |
| public Matrix4x4 projectionMatrix; |
|
|
| |
| |
| |
| public Matrix4x4 shadowTransform; |
|
|
| |
| |
| |
| public int offsetX; |
|
|
| |
| |
| |
| public int offsetY; |
|
|
| |
| |
| |
| public int resolution; |
|
|
| |
| |
| |
| public ShadowSplitData splitData; |
|
|
| |
| |
| |
| |
| public float depthScale; |
| |
|
|
| |
| |
| |
| |
| public float shadowDistance; |
| |
|
|
| |
| |
| |
| public void Clear() |
| { |
| viewMatrix = Matrix4x4.identity; |
| projectionMatrix = Matrix4x4.identity; |
| shadowTransform = Matrix4x4.identity; |
| offsetX = offsetY = 0; |
| resolution = 1024; |
| |
| depthScale = 0.0f; |
| shadowDistance = 0.0f; |
| |
| } |
| } |
|
|
| |
| |
| |
| public static class ShadowUtils |
| { |
| internal static readonly bool m_ForceShadowPointSampling; |
|
|
| |
| |
| public delegate void ShadowAdjustmentDelegate(ref UniversalCameraData cameraData, ref ShadowSliceData sliceData, ref float shadowDistance); |
| public static ShadowAdjustmentDelegate ShadowAdjustment; |
| |
| internal static UniversalCameraData CurrentCameraData; |
| |
|
|
| static ShadowUtils() |
| { |
| m_ForceShadowPointSampling = SystemInfo.graphicsDeviceType == GraphicsDeviceType.Metal && |
| GraphicsSettings.HasShaderDefine(Graphics.activeTier, BuiltinShaderDefine.UNITY_METAL_SHADOWS_USE_POINT_FILTERING); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static bool ExtractDirectionalLightMatrix(ref CullingResults cullResults, ref ShadowData shadowData, int shadowLightIndex, int cascadeIndex, int shadowmapWidth, int shadowmapHeight, int shadowResolution, float shadowNearPlane, out Vector4 cascadeSplitDistance, out ShadowSliceData shadowSliceData, out Matrix4x4 viewMatrix, out Matrix4x4 projMatrix) |
| { |
| bool result = ExtractDirectionalLightMatrix(ref cullResults, ref shadowData, shadowLightIndex, cascadeIndex, shadowmapWidth, shadowmapHeight, shadowResolution, shadowNearPlane, out cascadeSplitDistance, out shadowSliceData); |
| viewMatrix = shadowSliceData.viewMatrix; |
| projMatrix = shadowSliceData.projectionMatrix; |
| return result; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static bool ExtractDirectionalLightMatrix(ref CullingResults cullResults, ref ShadowData shadowData, int shadowLightIndex, int cascadeIndex, int shadowmapWidth, int shadowmapHeight, int shadowResolution, float shadowNearPlane, out Vector4 cascadeSplitDistance, out ShadowSliceData shadowSliceData) |
| { |
| return ExtractDirectionalLightMatrix(ref cullResults, shadowData.universalShadowData, |
| shadowLightIndex, cascadeIndex, shadowmapWidth, shadowmapHeight, shadowResolution, |
| shadowNearPlane, out cascadeSplitDistance, out shadowSliceData); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static bool ExtractDirectionalLightMatrix(ref CullingResults cullResults, UniversalShadowData shadowData, int shadowLightIndex, int cascadeIndex, int shadowmapWidth, int shadowmapHeight, int shadowResolution, float shadowNearPlane, out Vector4 cascadeSplitDistance, out ShadowSliceData shadowSliceData) |
| { |
| bool success = cullResults.ComputeDirectionalShadowMatricesAndCullingPrimitives(shadowLightIndex, |
| cascadeIndex, shadowData.mainLightShadowCascadesCount, shadowData.mainLightShadowCascadesSplit, shadowResolution, shadowNearPlane, out shadowSliceData.viewMatrix, out shadowSliceData.projectionMatrix, |
| out shadowSliceData.splitData); |
|
|
| cascadeSplitDistance = shadowSliceData.splitData.cullingSphere; |
| shadowSliceData.offsetX = (cascadeIndex % 2) * shadowResolution; |
| shadowSliceData.offsetY = (cascadeIndex / 2) * shadowResolution; |
| shadowSliceData.resolution = shadowResolution; |
|
|
| |
| |
| float shadowDistance = CurrentCameraData != null ? CurrentCameraData.maxShadowDistance : float.MaxValue; |
| if (ShadowAdjustment != null && CurrentCameraData != null) |
| { |
| shadowSliceData.shadowTransform = default; |
| shadowSliceData.depthScale = default; |
| shadowSliceData.shadowDistance = default; |
| ShadowAdjustment(ref CurrentCameraData, ref shadowSliceData, ref shadowDistance); |
| } |
| shadowSliceData.shadowDistance = shadowDistance; |
| |
|
|
| shadowSliceData.shadowTransform = GetShadowTransform(shadowSliceData.projectionMatrix, shadowSliceData.viewMatrix); |
|
|
| |
| shadowSliceData.depthScale = Mathf.Abs(2.0f / shadowSliceData.projectionMatrix[2, 2]); |
| |
|
|
| |
| |
| shadowSliceData.splitData.shadowCascadeBlendCullingFactor = 1.0f; |
|
|
| |
| |
| if (shadowData.mainLightShadowCascadesCount > 1) |
| ApplySliceTransform(ref shadowSliceData, shadowmapWidth, shadowmapHeight); |
|
|
| return success; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static bool ExtractSpotLightMatrix(ref CullingResults cullResults, ref ShadowData shadowData, int shadowLightIndex, out Matrix4x4 shadowMatrix, out Matrix4x4 viewMatrix, out Matrix4x4 projMatrix, out ShadowSplitData splitData) |
| { |
| return ExtractSpotLightMatrix(ref cullResults, shadowData.universalShadowData, shadowLightIndex, out shadowMatrix, out viewMatrix, out projMatrix, out splitData); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static bool ExtractSpotLightMatrix(ref CullingResults cullResults, UniversalShadowData shadowData, int shadowLightIndex, out Matrix4x4 shadowMatrix, out Matrix4x4 viewMatrix, out Matrix4x4 projMatrix, out ShadowSplitData splitData) |
| { |
| bool success = cullResults.ComputeSpotShadowMatricesAndCullingPrimitives(shadowLightIndex, out viewMatrix, out projMatrix, out splitData); |
| shadowMatrix = GetShadowTransform(projMatrix, viewMatrix); |
| return success; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static bool ExtractPointLightMatrix(ref CullingResults cullResults, ref ShadowData shadowData, int shadowLightIndex, CubemapFace cubemapFace, float fovBias, out Matrix4x4 shadowMatrix, out Matrix4x4 viewMatrix, out Matrix4x4 projMatrix, out ShadowSplitData splitData) |
| { |
| return ExtractPointLightMatrix(ref cullResults, shadowData.universalShadowData, shadowLightIndex, cubemapFace, fovBias, out shadowMatrix, out viewMatrix, out projMatrix, out splitData); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static bool ExtractPointLightMatrix(ref CullingResults cullResults, UniversalShadowData shadowData, int shadowLightIndex, CubemapFace cubemapFace, float fovBias, out Matrix4x4 shadowMatrix, out Matrix4x4 viewMatrix, out Matrix4x4 projMatrix, out ShadowSplitData splitData) |
| { |
| bool success = cullResults.ComputePointShadowMatricesAndCullingPrimitives(shadowLightIndex, cubemapFace, fovBias, out viewMatrix, out projMatrix, out splitData); |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| { |
| viewMatrix.m10 = -viewMatrix.m10; |
| viewMatrix.m11 = -viewMatrix.m11; |
| viewMatrix.m12 = -viewMatrix.m12; |
| viewMatrix.m13 = -viewMatrix.m13; |
| } |
|
|
| shadowMatrix = GetShadowTransform(projMatrix, viewMatrix); |
| return success; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static void RenderShadowSlice(CommandBuffer cmd, ref ScriptableRenderContext context, |
| ref ShadowSliceData shadowSliceData, ref ShadowDrawingSettings settings, |
| Matrix4x4 proj, Matrix4x4 view) |
| { |
| cmd.SetGlobalDepthBias(1.0f, 2.5f); |
|
|
| cmd.SetViewport(new Rect(shadowSliceData.offsetX, shadowSliceData.offsetY, shadowSliceData.resolution, shadowSliceData.resolution)); |
| cmd.SetViewProjectionMatrices(view, proj); |
| var rl = context.CreateShadowRendererList(ref settings); |
| cmd.DrawRendererList(rl); |
| cmd.DisableScissorRect(); |
| context.ExecuteCommandBuffer(cmd); |
| cmd.Clear(); |
|
|
| cmd.SetGlobalDepthBias(0.0f, 0.0f); |
| } |
|
|
| internal static void RenderShadowSlice(RasterCommandBuffer cmd, |
| ref ShadowSliceData shadowSliceData, ref RendererList shadowRendererList, |
| Matrix4x4 proj, Matrix4x4 view) |
| { |
| cmd.SetGlobalDepthBias(1.0f, 2.5f); |
|
|
| cmd.SetViewport(new Rect(shadowSliceData.offsetX, shadowSliceData.offsetY, shadowSliceData.resolution, shadowSliceData.resolution)); |
| cmd.SetViewProjectionMatrices(view, proj); |
| if(shadowRendererList.isValid) |
| cmd.DrawRendererList(shadowRendererList); |
|
|
| cmd.DisableScissorRect(); |
| cmd.SetGlobalDepthBias(0.0f, 0.0f); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| public static void RenderShadowSlice(CommandBuffer cmd, ref ScriptableRenderContext context, |
| ref ShadowSliceData shadowSliceData, ref ShadowDrawingSettings settings) |
| { |
| RenderShadowSlice(cmd, ref context, ref shadowSliceData, ref settings, |
| shadowSliceData.projectionMatrix, shadowSliceData.viewMatrix); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| public static int GetMaxTileResolutionInAtlas(int atlasWidth, int atlasHeight, int tileCount) |
| { |
| int resolution = Mathf.Min(atlasWidth, atlasHeight); |
| int currentTileCount = atlasWidth / resolution * atlasHeight / resolution; |
| while (currentTileCount < tileCount) |
| { |
| resolution = resolution >> 1; |
| currentTileCount = atlasWidth / resolution * atlasHeight / resolution; |
| } |
| return resolution; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| public static void ApplySliceTransform(ref ShadowSliceData shadowSliceData, int atlasWidth, int atlasHeight) |
| { |
| Matrix4x4 sliceTransform = Matrix4x4.identity; |
| float oneOverAtlasWidth = 1.0f / atlasWidth; |
| float oneOverAtlasHeight = 1.0f / atlasHeight; |
| sliceTransform.m00 = shadowSliceData.resolution * oneOverAtlasWidth; |
| sliceTransform.m11 = shadowSliceData.resolution * oneOverAtlasHeight; |
| sliceTransform.m03 = shadowSliceData.offsetX * oneOverAtlasWidth; |
| sliceTransform.m13 = shadowSliceData.offsetY * oneOverAtlasHeight; |
|
|
| |
| shadowSliceData.shadowTransform = sliceTransform * shadowSliceData.shadowTransform; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static Vector4 GetShadowBias(ref VisibleLight shadowLight, int shadowLightIndex, ref ShadowData shadowData, Matrix4x4 lightProjectionMatrix, float shadowResolution) |
| { |
| return GetShadowBias(ref shadowLight, shadowLightIndex, shadowData.bias, shadowData.supportsSoftShadows, lightProjectionMatrix, shadowResolution); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static Vector4 GetShadowBias(ref VisibleLight shadowLight, int shadowLightIndex, UniversalShadowData shadowData, Matrix4x4 lightProjectionMatrix, float shadowResolution) |
| { |
| return GetShadowBias(ref shadowLight, shadowLightIndex, shadowData.bias, shadowData.supportsSoftShadows, lightProjectionMatrix, shadowResolution); |
| } |
|
|
| static Vector4 GetShadowBias(ref VisibleLight shadowLight, int shadowLightIndex, List<Vector4> bias, bool supportsSoftShadows, Matrix4x4 lightProjectionMatrix, float shadowResolution) |
| { |
| if (shadowLightIndex < 0 || shadowLightIndex >= bias.Count) |
| { |
| Debug.LogWarning(string.Format("{0} is not a valid light index.", shadowLightIndex)); |
| return Vector4.zero; |
| } |
|
|
| float frustumSize; |
| if (shadowLight.lightType == LightType.Directional) |
| { |
| |
| frustumSize = 2.0f / lightProjectionMatrix.m00; |
| } |
| else if (shadowLight.lightType == LightType.Spot) |
| { |
| |
| |
| |
| |
| |
| |
| frustumSize = Mathf.Tan(shadowLight.spotAngle * 0.5f * Mathf.Deg2Rad) * shadowLight.range; |
| } |
| else if (shadowLight.lightType == LightType.Point) |
| { |
| |
| |
| |
| |
| |
| |
| |
| |
| float fovBias = Internal.AdditionalLightsShadowCasterPass.GetPointLightShadowFrustumFovBiasInDegrees((int)shadowResolution, (shadowLight.light.shadows == LightShadows.Soft)); |
| |
| float cubeFaceAngle = 90 + fovBias; |
| frustumSize = Mathf.Tan(cubeFaceAngle * 0.5f * Mathf.Deg2Rad) * shadowLight.range; |
| } |
| else |
| { |
| Debug.LogWarning("Only point, spot and directional shadow casters are supported in universal pipeline"); |
| frustumSize = 0.0f; |
| } |
|
|
| |
| float texelSize = frustumSize / shadowResolution; |
| float depthBias = -bias[shadowLightIndex].x * texelSize; |
| float normalBias = -bias[shadowLightIndex].y * texelSize; |
|
|
| |
| |
| |
| if (shadowLight.lightType == LightType.Point) |
| normalBias = 0.0f; |
|
|
| if (supportsSoftShadows && shadowLight.light.shadows == LightShadows.Soft) |
| { |
| SoftShadowQuality softShadowQuality = SoftShadowQuality.Medium; |
| if (shadowLight.light.TryGetComponent(out UniversalAdditionalLightData additionalLightData)) |
| softShadowQuality = additionalLightData.softShadowQuality; |
|
|
| |
| |
| |
| |
| |
| float kernelRadius = 2.5f; |
|
|
| switch (softShadowQuality) |
| { |
| case SoftShadowQuality.High: kernelRadius = 3.5f; break; |
| case SoftShadowQuality.Medium: kernelRadius = 2.5f; break; |
| case SoftShadowQuality.Low: kernelRadius = 1.5f; break; |
| default: break; |
| } |
|
|
| depthBias *= kernelRadius; |
| normalBias *= kernelRadius; |
| } |
|
|
| return new Vector4(depthBias, normalBias, (float)shadowLight.lightType, 0.0f); |
| } |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| internal static void GetScaleAndBiasForLinearDistanceFade(float fadeDistance, float border, out float scale, out float bias) |
| { |
| |
| |
| if (border < 0.0001f) |
| { |
| float multiplier = 1000f; |
| scale = multiplier; |
| bias = -fadeDistance * multiplier; |
| return; |
| } |
|
|
| border = 1 - border; |
| border *= border; |
|
|
| |
| float distanceFadeNear = border * fadeDistance; |
| scale = 1.0f / (fadeDistance - distanceFadeNear); |
| bias = -distanceFadeNear / (fadeDistance - distanceFadeNear); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| public static void SetupShadowCasterConstantBuffer(CommandBuffer cmd, ref VisibleLight shadowLight, Vector4 shadowBias) |
| { |
| SetupShadowCasterConstantBuffer(CommandBufferHelpers.GetRasterCommandBuffer(cmd), ref shadowLight, shadowBias); |
| } |
|
|
| internal static void SetupShadowCasterConstantBuffer(RasterCommandBuffer cmd, ref VisibleLight shadowLight, Vector4 shadowBias) |
| { |
| SetShadowBias(cmd, shadowBias); |
|
|
| |
| Vector3 lightDirection = -shadowLight.localToWorldMatrix.GetColumn(2); |
| SetLightDirection(cmd, lightDirection); |
|
|
| |
| Vector3 lightPosition = shadowLight.localToWorldMatrix.GetColumn(3); |
| SetLightPosition(cmd, lightPosition); |
| } |
|
|
| internal static void SetShadowBias(RasterCommandBuffer cmd, Vector4 shadowBias) |
| { |
| cmd.SetGlobalVector(ShaderPropertyId.shadowBias, shadowBias); |
| } |
|
|
| internal static void SetLightDirection(RasterCommandBuffer cmd, Vector3 lightDirection) |
| { |
| cmd.SetGlobalVector(ShaderPropertyId.lightDirection, new Vector4(lightDirection.x, lightDirection.y, lightDirection.z, 0.0f)); |
| } |
|
|
| internal static void SetLightPosition(RasterCommandBuffer cmd, Vector3 lightPosition) |
| { |
| cmd.SetGlobalVector(ShaderPropertyId.lightPosition, new Vector4(lightPosition.x, lightPosition.y, lightPosition.z, 1.0f)); |
| } |
|
|
| internal static void SetCameraPosition(RasterCommandBuffer cmd, Vector3 worldSpaceCameraPos) |
| { |
| cmd.SetGlobalVector(ShaderPropertyId.worldSpaceCameraPos, worldSpaceCameraPos); |
| } |
|
|
| internal static void SetWorldToCameraAndCameraToWorldMatrices(RasterCommandBuffer cmd, Matrix4x4 viewMatrix) |
| { |
| |
| |
| |
| Matrix4x4 worldToCameraMatrix = Matrix4x4.Scale(new Vector3(1.0f, 1.0f, -1.0f)) * viewMatrix; |
| Matrix4x4 cameraToWorldMatrix = worldToCameraMatrix.inverse; |
| cmd.SetGlobalMatrix(ShaderPropertyId.worldToCameraMatrix, worldToCameraMatrix); |
| cmd.SetGlobalMatrix(ShaderPropertyId.cameraToWorldMatrix, cameraToWorldMatrix); |
| } |
|
|
| private static RenderTextureDescriptor GetTemporaryShadowTextureDescriptor(int width, int height, int bits) |
| { |
| var format = Experimental.Rendering.GraphicsFormatUtility.GetDepthStencilFormat(bits, 0); |
| RenderTextureDescriptor rtd = new RenderTextureDescriptor(width, height, Experimental.Rendering.GraphicsFormat.None, format); |
| rtd.shadowSamplingMode = RenderingUtils.SupportsRenderTextureFormat(RenderTextureFormat.Shadowmap) ? ShadowSamplingMode.CompareDepths : ShadowSamplingMode.None; |
| return rtd; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| [Obsolete("Use AllocShadowRT or ShadowRTReAllocateIfNeeded", true)] |
| public static RenderTexture GetTemporaryShadowTexture(int width, int height, int bits) |
| { |
| var rtd = GetTemporaryShadowTextureDescriptor(width, height, bits); |
| var shadowTexture = RenderTexture.GetTemporary(rtd); |
| shadowTexture.filterMode = m_ForceShadowPointSampling ? FilterMode.Point : FilterMode.Bilinear; |
| shadowTexture.wrapMode = TextureWrapMode.Clamp; |
| return shadowTexture; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static bool ShadowRTNeedsReAlloc(RTHandle handle, int width, int height, int bits, int anisoLevel, float mipMapBias, string name) |
| { |
| if (handle == null || handle.rt == null) |
| return true; |
| var descriptor = GetTemporaryShadowTextureDescriptor(width, height, bits); |
| if (m_ForceShadowPointSampling) |
| { |
| if (handle.rt.filterMode != FilterMode.Point) |
| return true; |
| } |
| else |
| { |
| if (handle.rt.filterMode != FilterMode.Bilinear) |
| return true; |
| } |
|
|
| TextureDesc shadowDesc = RTHandleResourcePool.CreateTextureDesc(descriptor, TextureSizeMode.Explicit, anisoLevel, mipMapBias, m_ForceShadowPointSampling ? FilterMode.Point : FilterMode.Bilinear, TextureWrapMode.Clamp, name); |
| return RenderingUtils.RTHandleNeedsReAlloc(handle, shadowDesc, false); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static RTHandle AllocShadowRT(int width, int height, int bits, int anisoLevel, float mipMapBias, string name) |
| { |
| var rtd = GetTemporaryShadowTextureDescriptor(width, height, bits); |
| return RTHandles.Alloc(rtd, m_ForceShadowPointSampling ? FilterMode.Point : FilterMode.Bilinear, TextureWrapMode.Clamp, isShadowMap: true, name: name); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static bool ShadowRTReAllocateIfNeeded(ref RTHandle handle, int width, int height, int bits, int anisoLevel = 1, float mipMapBias = 0, string name = "") |
| { |
| if (ShadowRTNeedsReAlloc(handle, width, height, bits, anisoLevel, mipMapBias, name)) |
| { |
| handle?.Release(); |
| handle = AllocShadowRT(width, height, bits, anisoLevel, mipMapBias, name); |
| return true; |
| } |
| return false; |
| } |
|
|
| static Matrix4x4 GetShadowTransform(Matrix4x4 proj, Matrix4x4 view) |
| { |
| |
| |
| if (SystemInfo.usesReversedZBuffer) |
| { |
| proj.m20 = -proj.m20; |
| proj.m21 = -proj.m21; |
| proj.m22 = -proj.m22; |
| proj.m23 = -proj.m23; |
| } |
|
|
| Matrix4x4 worldToShadow = proj * view; |
|
|
| var textureScaleAndBias = Matrix4x4.identity; |
| textureScaleAndBias.m00 = 0.5f; |
| textureScaleAndBias.m11 = 0.5f; |
| textureScaleAndBias.m22 = 0.5f; |
| textureScaleAndBias.m03 = 0.5f; |
| textureScaleAndBias.m23 = 0.5f; |
| textureScaleAndBias.m13 = 0.5f; |
| |
|
|
| |
| return textureScaleAndBias * worldToShadow; |
| } |
|
|
| internal static float SoftShadowQualityToShaderProperty(Light light, bool softShadowsEnabled) |
| { |
| float softShadows = softShadowsEnabled ? 1.0f : 0.0f; |
| if (light.TryGetComponent(out UniversalAdditionalLightData additionalLightData)) |
| { |
| var softShadowQuality = (additionalLightData.softShadowQuality == SoftShadowQuality.UsePipelineSettings) |
| ? UniversalRenderPipeline.asset?.softShadowQuality |
| : additionalLightData.softShadowQuality; |
| softShadows *= Math.Max((int)softShadowQuality, (int)SoftShadowQuality.Low); |
| } |
|
|
| return softShadows; |
| } |
|
|
| internal static bool SupportsPerLightSoftShadowQuality() |
| { |
| bool supportsPerLightSoftShadowQuality = true; |
| #if ENABLE_VR && ENABLE_VR_MODULE |
| #if PLATFORM_WINRT || PLATFORM_ANDROID |
| |
| supportsPerLightSoftShadowQuality = !PlatformAutoDetect.isXRMobile; |
| #endif |
| #endif |
| return supportsPerLightSoftShadowQuality; |
| } |
|
|
| internal static void SetPerLightSoftShadowKeyword(RasterCommandBuffer cmd, bool hasSoftShadows) |
| { |
| if (SupportsPerLightSoftShadowQuality()) |
| cmd.SetKeyword(ShaderGlobalKeywords.SoftShadows, hasSoftShadows); |
| } |
|
|
| internal static void SetSoftShadowQualityShaderKeywords(RasterCommandBuffer cmd, UniversalShadowData shadowData) |
| { |
| cmd.SetKeyword(ShaderGlobalKeywords.SoftShadows, shadowData.isKeywordSoftShadowsEnabled); |
| if (SupportsPerLightSoftShadowQuality()) |
| { |
| cmd.SetKeyword(ShaderGlobalKeywords.SoftShadowsLow, false); |
| cmd.SetKeyword(ShaderGlobalKeywords.SoftShadowsMedium, false); |
| cmd.SetKeyword(ShaderGlobalKeywords.SoftShadowsHigh, false); |
| } |
| else |
| { |
| if (shadowData.isKeywordSoftShadowsEnabled && UniversalRenderPipeline.asset?.softShadowQuality == SoftShadowQuality.Low) |
| { |
| cmd.SetKeyword(ShaderGlobalKeywords.SoftShadowsLow, true); |
| cmd.SetKeyword(ShaderGlobalKeywords.SoftShadowsMedium, false); |
| cmd.SetKeyword(ShaderGlobalKeywords.SoftShadowsHigh, false); |
| cmd.SetKeyword(ShaderGlobalKeywords.SoftShadows, false); |
| } |
| else if (shadowData.isKeywordSoftShadowsEnabled && UniversalRenderPipeline.asset?.softShadowQuality == SoftShadowQuality.Medium) |
| { |
| cmd.SetKeyword(ShaderGlobalKeywords.SoftShadowsLow, false); |
| cmd.SetKeyword(ShaderGlobalKeywords.SoftShadowsMedium, true); |
| cmd.SetKeyword(ShaderGlobalKeywords.SoftShadowsHigh, false); |
| cmd.SetKeyword(ShaderGlobalKeywords.SoftShadows, false); |
| } |
| else if (shadowData.isKeywordSoftShadowsEnabled && UniversalRenderPipeline.asset?.softShadowQuality == SoftShadowQuality.High) |
| { |
| cmd.SetKeyword(ShaderGlobalKeywords.SoftShadowsLow, false); |
| cmd.SetKeyword(ShaderGlobalKeywords.SoftShadowsMedium, false); |
| cmd.SetKeyword(ShaderGlobalKeywords.SoftShadowsHigh, true); |
| cmd.SetKeyword(ShaderGlobalKeywords.SoftShadows, false); |
| } |
| } |
| } |
|
|
| internal static bool IsValidShadowCastingLight(UniversalLightData lightData, int i) |
| { |
| ref VisibleLight shadowLight = ref lightData.visibleLights.UnsafeElementAt(i); |
| Light light = shadowLight.light; |
|
|
| if (light == null) |
| return false; |
|
|
| return IsValidShadowCastingLight(lightData, i, shadowLight.lightType, light.shadows, light.shadowStrength); |
| } |
|
|
| internal static bool IsValidShadowCastingLight(UniversalLightData lightData, int i, LightType lightType, LightShadows lightShadows, float shadowStrength) |
| { |
| if (i == lightData.mainLightIndex) |
| return false; |
|
|
| |
| if (lightType == LightType.Directional) |
| return false; |
|
|
| return lightShadows != LightShadows.None && shadowStrength > 0.0f; |
| } |
|
|
| internal static int GetPunctualLightShadowSlicesCount(in LightType lightType) |
| { |
| switch (lightType) |
| { |
| case LightType.Spot: |
| return 1; |
| case LightType.Point: |
| return 6; |
| default: |
| return 0; |
| } |
| } |
|
|
| internal static bool FastApproximately(float a, float b) |
| { |
| return Mathf.Abs(a - b) < 0.000001f; |
| } |
|
|
| internal static bool FastApproximately(Vector4 a, Vector4 b) |
| { |
| return FastApproximately(a.x, b.x) |
| && FastApproximately(a.y, b.y) |
| && FastApproximately(a.z, b.z) |
| && FastApproximately(a.w, b.w); |
| } |
|
|
| internal const int kMinimumPunctualLightHardShadowResolution = 8; |
| internal const int kMinimumPunctualLightSoftShadowResolution = 16; |
| |
| internal static int MinimalPunctualLightShadowResolution(bool softShadow) |
| { |
| return softShadow ? kMinimumPunctualLightSoftShadowResolution : kMinimumPunctualLightHardShadowResolution; |
| } |
| } |
| } |
|
|