Exemplo n.º 1
0
    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;
        }
    }
Exemplo n.º 2
0
    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;
        }
    }