예제 #1
0
        void RenderDebugOverlay(Camera camera, ScriptableRenderContext renderContext)
        {
            CommandBuffer debugCB = new CommandBuffer();

            debugCB.name = "Debug Overlay";

            float x            = 0;
            float overlayRatio = globalDebugParameters.debugOverlayRatio;
            float overlaySize  = Math.Min(camera.pixelHeight, camera.pixelWidth) * overlayRatio;
            float y            = camera.pixelHeight - overlaySize;

            MaterialPropertyBlock propertyBlock = new MaterialPropertyBlock();

            ShadowDebugParameters shadowDebug = globalDebugParameters.shadowDebugParameters;

            if (shadowDebug.visualizationMode != ShadowDebugMode.None)
            {
                if (shadowDebug.visualizationMode == ShadowDebugMode.VisualizeShadowMap)
                {
                    uint        visualizeShadowIndex = Math.Min(shadowDebug.visualizeShadowMapIndex, (uint)(GetCurrentShadowCount() - 1));
                    ShadowLight shadowLight          = m_ShadowsResult.shadowLights[visualizeShadowIndex];
                    for (int slice = 0; slice < shadowLight.shadowSliceCount; ++slice)
                    {
                        ShadowSliceData sliceData = m_ShadowsResult.shadowSlices[shadowLight.shadowSliceIndex + slice];

                        Vector4 texcoordScaleBias = new Vector4((float)sliceData.shadowResolution / m_Owner.shadowSettings.shadowAtlasWidth,
                                                                (float)sliceData.shadowResolution / m_Owner.shadowSettings.shadowAtlasHeight,
                                                                (float)sliceData.atlasX / m_Owner.shadowSettings.shadowAtlasWidth,
                                                                (float)sliceData.atlasY / m_Owner.shadowSettings.shadowAtlasHeight);

                        propertyBlock.SetVector("_TextureScaleBias", texcoordScaleBias);

                        debugCB.SetViewport(new Rect(x, y, overlaySize, overlaySize));
                        debugCB.DrawProcedural(Matrix4x4.identity, m_DebugDisplayShadowMap, 0, MeshTopology.Triangles, 6, 1, propertyBlock);

                        NextOverlayCoord(ref x, ref y, overlaySize, camera.pixelWidth);
                    }
                }
                else if (shadowDebug.visualizationMode == ShadowDebugMode.VisualizeAtlas)
                {
                    propertyBlock.SetVector("_TextureScaleBias", new Vector4(1.0f, 1.0f, 0.0f, 0.0f));

                    debugCB.SetViewport(new Rect(x, y, overlaySize, overlaySize));
                    debugCB.DrawProcedural(Matrix4x4.identity, m_DebugDisplayShadowMap, 0, MeshTopology.Triangles, 6, 1, propertyBlock);

                    NextOverlayCoord(ref x, ref y, overlaySize, camera.pixelWidth);
                }
            }

            renderContext.ExecuteCommandBuffer(debugCB);
        }
        void RenderAdditionalShadowmapAtlas(ref ScriptableRenderContext context, ref CullingResults cullResults, ref LightData lightData, ref ShadowData shadowData)
        {
            NativeArray <VisibleLight> visibleLights = lightData.visibleLights;

            bool additionalLightHasSoftShadows = false;
            // NOTE: Do NOT mix ProfilingScope with named CommandBuffers i.e. CommandBufferPool.Get("name").
            // Currently there's an issue which results in mismatched markers.
            CommandBuffer cmd = CommandBufferPool.Get();

            using (new ProfilingScope(cmd, m_ProfilingSampler))
            {
                bool anyShadowSliceRenderer = false;
                int  shadowSlicesCount      = m_AdditionalShadowCastingLightIndices.Count;
                for (int i = 0; i < shadowSlicesCount; ++i)
                {
                    // we do the shadow strength check here again here because when using
                    // the uniform array path we might have zero strength shadow lights.
                    // In that case we need the shadow data buffer but we can skip
                    // rendering them to shadowmap.
                    if (!m_UseStructuredBuffer && Mathf.Approximately(m_AdditionalLightsShadowParams[i].x, 0.0f))
                    {
                        continue;
                    }

                    // Index of the VisibleLight
                    int          shadowLightIndex = m_AdditionalShadowCastingLightIndices[i];
                    VisibleLight shadowLight      = visibleLights[shadowLightIndex];

                    ShadowSliceData shadowSliceData = m_AdditionalLightSlices[i];

                    var     settings   = new ShadowDrawingSettings(cullResults, shadowLightIndex);
                    Vector4 shadowBias = ShadowUtils.GetShadowBias(ref shadowLight, shadowLightIndex,
                                                                   ref shadowData, shadowSliceData.projectionMatrix, shadowSliceData.resolution);
                    ShadowUtils.SetupShadowCasterConstantBuffer(cmd, ref shadowLight, shadowBias);
                    ShadowUtils.RenderShadowSlice(cmd, ref context, ref shadowSliceData, ref settings);
                    additionalLightHasSoftShadows |= shadowLight.light.shadows == LightShadows.Soft;
                    anyShadowSliceRenderer         = true;
                }

                // We share soft shadow settings for main light and additional lights to save keywords.
                // So we check here if pipeline supports soft shadows and either main light or any additional light has soft shadows
                // to enable the keyword.
                // TODO: In PC and Consoles we can upload shadow data per light and branch on shader. That will be more likely way faster.
                bool mainLightHasSoftShadows = shadowData.supportsMainLightShadows &&
                                               lightData.mainLightIndex != -1 &&
                                               visibleLights[lightData.mainLightIndex].light.shadows ==
                                               LightShadows.Soft;

                bool softShadows = shadowData.supportsSoftShadows &&
                                   (mainLightHasSoftShadows || additionalLightHasSoftShadows);

                CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.AdditionalLightShadows, anyShadowSliceRenderer);
                CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.SoftShadows, softShadows);

                if (anyShadowSliceRenderer)
                {
                    SetupAdditionalLightsShadowReceiverConstants(cmd, ref shadowData, softShadows);
                }
            }

            context.ExecuteCommandBuffer(cmd);
            CommandBufferPool.Release(cmd);
        }