Example #1
0
        private void PrepareSpotlights()
        {
            MyLights.SpotlightsBvh.OverlapAllFrustum(ref MyEnvironment.ViewFrustumClippedD, MyLightRendering.VisibleSpotlights);

            if (MyLightRendering.VisibleSpotlights.Count == 0)
            {
                OtherShadowsTriangleCounter = 0;
            }

            MyLightRendering.VisibleSpotlights.Sort(m_spotlightCastersComparer);
            MyArrayHelpers.Reserve(ref MyLightRendering.Spotlights, MyLightRendering.VisibleSpotlights.Count);

            int index       = 0;
            int casterIndex = 0;

            foreach (var id in MyLightRendering.VisibleSpotlights)
            {
                var     nearPlaneDistance   = 0.5f;
                var     worldMatrix         = MatrixD.CreateTranslation(MyEnvironment.CameraPosition);
                var     viewMatrix          = MatrixD.CreateLookAt(id.Position, id.Position + MyLights.Spotlights[id.Index].Direction, MyLights.Spotlights[id.Index].Up);
                var     projectionMatrix    = MatrixD.CreatePerspectiveFieldOfView((float)(Math.Acos(MyLights.Spotlights[id.Index].ApertureCos) * 2), 1.0f, nearPlaneDistance, Math.Max(id.ShadowDistance, nearPlaneDistance));
                var     viewProjection      = viewMatrix * projectionMatrix;
                MatrixD worldViewProjection = worldMatrix * viewProjection;
                MyLightRendering.Spotlights[index].ShadowMatrix = Matrix.Transpose(worldViewProjection * MyMatrixHelpers.ClipspaceToTexture);

                if (id.CastsShadows && casterIndex < MAX_SPOTLIGHT_SHADOWCASTERS)
                {
                    if (ShadowmapsPool.Count <= casterIndex)
                    {
                        ShadowmapsPool.Add(MyRwTextures.CreateShadowmap(SpotlightShadowmapSize, SpotlightShadowmapSize));
                    }

                    MyLights.Lights.Data[id.Index].CastsShadowsThisFrame = true;

                    var query = new MyShadowmapQuery
                    {
                        DepthBuffer    = ShadowmapsPool[casterIndex].Dsv,
                        Viewport       = new MyViewport(SpotlightShadowmapSize, SpotlightShadowmapSize),
                        QueryType      = MyFrustumEnum.ShadowProjection,
                        ProjectionInfo = new MyProjectionInfo
                        {
                            WorldCameraOffsetPosition = MyEnvironment.CameraPosition,
                            WorldToProjection         = viewProjection,
                            LocalToProjection         = worldViewProjection
                        },
                        IgnoredEntities = MyLights.IgnoredEntitites.ContainsKey(id) ? MyLights.IgnoredEntitites[id] : null,
                    };
                    m_shadowmapQueries.Add(query);
                    ++casterIndex;
                }
                else
                {
                    MyLights.Lights.Data[id.Index].CastsShadowsThisFrame = false;
                }
                MyLights.WriteSpotlightConstants(id, ref MyLightRendering.Spotlights[index]);

                index++;
            }
        }
Example #2
0
        static void PrepareSpotlights()
        {
            MyLights.SpotlightsBvh.OverlapAllFrustum(ref MyEnvironment.ViewFrustum, MyLightRendering.VisibleSpotlights);

            MyLightRendering.VisibleSpotlights.Sort(m_spotlightCastersComparer);
            while (MyLightRendering.VisibleSpotlights.Count > MAX_SPOTLIGHT_SHADOWCASTERS)
            {
                MyLightRendering.VisibleSpotlights.RemoveAtFast(MyLightRendering.VisibleSpotlights.Count - 1);
            }

            MyArrayHelpers.Reserve(ref MyLightRendering.Spotlights, MyLightRendering.VisibleSpotlights.Count);

            int index       = 0;
            int casterIndex = 0;

            foreach (var id in MyLightRendering.VisibleSpotlights)
            {
                MyLights.WriteSpotlightConstants(id, ref MyLightRendering.Spotlights[index]);

                if (id.CastsShadows)
                {
                    var query = new MyShadowmapQuery();

                    if (MyLights.IgnoredEntitites.ContainsKey(id))
                    {
                        query.IgnoredEntities = MyLights.IgnoredEntitites[id];
                    }

                    var shadowMatrix = Matrix.CreateLookAt(id.Position, id.Position + MyLights.Spotlights[id.Index].Direction, MyLights.Spotlights[id.Index].Up) *
                                       Matrix.CreatePerspectiveFieldOfView((float)(Math.Acos(MyLights.Spotlights[id.Index].ApertureCos) * 2), 1.0f, 0.5f, id.ShadowDistance);

                    if (ShadowmapsPool.Count <= casterIndex)
                    {
                        ShadowmapsPool.Add(MyRwTextures.CreateShadowmap(512, 512));
                    }

                    query.DepthBuffer    = ShadowmapsPool[casterIndex].Dsv;
                    query.Viewport       = new MyViewport(512, 512);
                    query.QueryType      = MyFrustumEnum.ShadowProjection;
                    query.ProjectionInfo = new MyProjectionInfo
                    {
                        WorldCameraOffsetPosition = MyEnvironment.CameraPosition,
                        WorldToProjection         = shadowMatrix,
                        LocalToProjection         = Matrix.CreateTranslation(MyEnvironment.CameraPosition) * shadowMatrix
                    };

                    MyLightRendering.Spotlights[index].ShadowMatrix = Matrix.Transpose(query.ProjectionInfo.CurrentLocalToProjection * MyMatrixHelpers.ClipspaceToTexture);

                    m_shadowmapQueries.Add(query);
                    casterIndex++;
                }
                index++;
            }
        }
Example #3
0
        private void PrepareSpotlights()
        {
            MyLights.Update();

            MyLights.SpotlightsBvh.OverlapAllFrustum(ref MyRender11.Environment.ViewFrustumClippedD, MyLightRendering.VisibleSpotlights);

            if (MyLightRendering.VisibleSpotlights.Count == 0)
            {
                OtherShadowsTriangleCounter = 0;
            }

            MyLightRendering.VisibleSpotlights.Sort(m_spotlightCastersComparer);

            int index       = 0;
            int casterIndex = 0;
            var worldMatrix = MatrixD.CreateTranslation(MyRender11.Environment.CameraPosition);

            foreach (var id in MyLightRendering.VisibleSpotlights)
            {
                if (id.CastsShadows && casterIndex < MAX_SPOTLIGHT_SHADOWCASTERS)
                {
                    if (ShadowmapsPool.Count <= casterIndex)
                    {
                        ShadowmapsPool.Add(MyRwTextures.CreateShadowmap(SpotlightShadowmapSize, SpotlightShadowmapSize));
                    }

                    MyLights.Lights.Data[id.Index].CastsShadowsThisFrame = true;

                    MatrixD viewProjection = MyLights.GetSpotlightViewProjection(id);
                    var     query          = new MyShadowmapQuery
                    {
                        DepthBuffer    = ShadowmapsPool[casterIndex].Dsv,
                        Viewport       = new MyViewport(SpotlightShadowmapSize, SpotlightShadowmapSize),
                        QueryType      = MyFrustumEnum.ShadowProjection,
                        ProjectionInfo = new MyProjectionInfo
                        {
                            WorldCameraOffsetPosition = MyRender11.Environment.CameraPosition,
                            WorldToProjection         = viewProjection,
                            LocalToProjection         = worldMatrix * viewProjection
                        },
                        IgnoredEntities = MyLights.IgnoredEntitites.ContainsKey(id) ? MyLights.IgnoredEntitites[id] : null,
                    };
                    m_shadowmapQueries.Add(query);
                    ++casterIndex;
                }
                else
                {
                    MyLights.Lights.Data[id.Index].CastsShadowsThisFrame = false;
                }

                index++;
            }
        }