void SetupSpotShadow(ref ScriptableRenderContext context, ref CullingResults cullingResults, int shadowMapSize, int index, ref VisibleLight visibleLight)
    {
        Bounds shadowBounds;

        if (visibleLight.light.shadows != LightShadows.None && cullingResults.GetShadowCasterBounds(index, out shadowBounds))
        {
            ShadowLight shadowLight = new ShadowLight();
            shadowLight.cascades       = new ShadowCascade[1];
            shadowLight.cullingSpheres = null;
            shadowLight.tileSize       = shadowMapSize;

            Matrix4x4       viewMatrix;
            Matrix4x4       projectionMatrix;
            ShadowSplitData splitData;

            if (cullingResults.ComputeSpotShadowMatricesAndCullingPrimitives(index, out viewMatrix, out projectionMatrix, out splitData))
            {
                ShadowCascade shadowCascade = new ShadowCascade();
                shadowCascade.viewMatrix          = viewMatrix;
                shadowCascade.projectionMatrix    = projectionMatrix;
                shadowCascade.worldToShadowMatrix = CreateWorldToShadowMatrix(viewMatrix, projectionMatrix);
                shadowCascade.tileOffset          = Vector2Int.zero;
                shadowCascade.splitData           = splitData;
                shadowLight.cascades[0]           = shadowCascade;
            }

            shadowData.lights[index] = shadowLight;
        }
    }
Exemple #2
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 SetupDirectionalShadow(ref ScriptableRenderContext context, ref CullingResults cullingResults, int shadowMapSize, int index, ref VisibleLight visibleLight)
    {
        Bounds shadowBounds;

        if (visibleLight.light.shadows != LightShadows.None && cullingResults.GetShadowCasterBounds(index, out shadowBounds))
        {
            ShadowLight shadowLight = new ShadowLight();
            shadowLight.cascades       = new ShadowCascade[4];
            shadowLight.cullingSpheres = new Vector4[4];
            shadowLight.tileSize       = shadowMapSize / 2;

            for (int j = 0; j < 4; j++)
            {
                Matrix4x4       viewMatrix;
                Matrix4x4       projectionMatrix;
                ShadowSplitData splitData;

                if (cullingResults.ComputeDirectionalShadowMatricesAndCullingPrimitives(index, j, 4, fourCascadesSplit, shadowLight.tileSize, visibleLight.light.shadowNearPlane, out viewMatrix, out projectionMatrix, out splitData))
                {
                    Vector2Int tileOffset = new Vector2Int(j % 2, j / 2);
                    Matrix4x4  tileMatrix = Matrix4x4.identity;
                    tileMatrix.m00 = tileMatrix.m11 = 0.5f;
                    tileMatrix.m03 = tileOffset.x * 0.5f;
                    tileMatrix.m13 = tileOffset.y * 0.5f;

                    ShadowCascade shadowCascade = new ShadowCascade();
                    shadowCascade.viewMatrix          = viewMatrix;
                    shadowCascade.projectionMatrix    = projectionMatrix;
                    shadowCascade.worldToShadowMatrix = tileMatrix * CreateWorldToShadowMatrix(viewMatrix, projectionMatrix);
                    shadowCascade.tileOffset          = tileOffset;
                    shadowCascade.splitData           = splitData;
                    shadowLight.cullingSpheres[j]     = splitData.cullingSphere;
                    shadowLight.cullingSpheres[j].w  *= shadowLight.cullingSpheres[j].w;
                    shadowLight.cascades[j]           = shadowCascade;
                }
            }

            shadowData.lights[index] = shadowLight;
        }
    }