示例#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++;
            }
        }
示例#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++;
            }
        }
示例#3
0
        internal unsafe static void RenderSpotlights()
        {
            RC.BindDepthRT(MyGBuffer.Main.Get(MyGbufferSlot.DepthStencil), DepthStencilAccess.ReadOnly, MyGBuffer.Main.Get(MyGbufferSlot.LBuffer));
            RC.DeviceContext.Rasterizer.SetViewport(0, 0, MyRender11.ViewportResolution.X, MyRender11.ViewportResolution.Y);
            RC.DeviceContext.InputAssembler.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleList;

            var coneMesh = MyMeshes.GetMeshId(X.TEXT("Models/Debug/Cone.mwm"));
            var buffers  = MyMeshes.GetLodMesh(coneMesh, 0).Buffers;

            RC.SetVB(0, buffers.VB0.Buffer, buffers.VB0.Stride);
            RC.SetIB(buffers.IB.Buffer, buffers.IB.Format);

            RC.SetVS(SpotlightProxyVs);
            RC.SetIL(SpotlightProxyIL);

            RC.SetRS(MyRender11.m_invTriRasterizerState);

            var cb = MyCommon.GetObjectCB(sizeof(SpotlightConstants));

            RC.SetCB(1, cb);
            RC.DeviceContext.PixelShader.SetSampler(13, MyRender11.m_alphamaskSamplerState);
            RC.DeviceContext.PixelShader.SetSampler(14, MyRender11.m_shadowmapSamplerState);
            RC.DeviceContext.PixelShader.SetSampler(15, MyRender11.m_shadowmapSamplerState);

            int index       = 0;
            int casterIndex = 0;

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

                var mapping = MyMapping.MapDiscard(cb);
                mapping.WriteAndPosition(ref Spotlights[index]);
                mapping.Unmap();

                RC.DeviceContext.PixelShader.SetShaderResource(13, MyTextures.GetView(MyLights.Spotlights[id.Index].ReflectorTexture));

                if (id.CastsShadowsThisFrame)
                {
                    RC.DeviceContext.PixelShader.SetShaderResource(14, MyRender11.DynamicShadows.ShadowmapsPool[casterIndex].ShaderView);
                    casterIndex++;
                }

                RC.SetPS(SpotlightPs_Pixel);
                if (MyRender11.MultisamplingEnabled)
                {
                    RC.SetDS(MyDepthStencilState.TestEdgeStencil, 0);
                }
                RC.DeviceContext.DrawIndexed(MyMeshes.GetLodMesh(coneMesh, 0).Info.IndicesNum, 0, 0);

                if (MyRender11.MultisamplingEnabled)
                {
                    RC.SetPS(SpotlightPs_Sample);
                    RC.SetDS(MyDepthStencilState.TestEdgeStencil, 0x80);
                    RC.DeviceContext.DrawIndexed(MyMeshes.GetLodMesh(coneMesh, 0).Info.IndicesNum, 0, 0);
                }

                index++;
                if (index >= SPOTLIGHTS_MAX)
                {
                    break;
                }
            }

            if (MyRender11.MultisamplingEnabled)
            {
                RC.SetDS(MyDepthStencilState.DefaultDepthState);
            }

            RC.SetRS(null);
        }
        internal unsafe static void RenderSpotlights()
        {
            RC.SetRtv(MyGBuffer.Main.DepthStencil, MyDepthStencilAccess.ReadOnly, MyGBuffer.Main.LBuffer);
            RC.SetViewport(0, 0, MyRender11.ViewportResolution.X, MyRender11.ViewportResolution.Y);
            RC.SetPrimitiveTopology(SharpDX.Direct3D.PrimitiveTopology.TriangleList);
            if (MyStereoRender.Enable)
            {
                MyStereoRender.PSBindRawCB_FrameConstants(RC);
                MyStereoRender.SetViewport(RC);
            }

            var coneMesh = MyMeshes.GetMeshId(X.TEXT_("Models/Debug/Cone.mwm"), 1.0f);
            var buffers  = MyMeshes.GetLodMesh(coneMesh, 0).Buffers;

            RC.SetVertexBuffer(0, buffers.VB0.Buffer, buffers.VB0.Stride);
            RC.SetIndexBuffer(buffers.IB.Buffer, buffers.IB.Format);

            RC.VertexShader.Set(SpotlightProxyVs);
            RC.SetInputLayout(SpotlightProxyIL);
            RC.PixelShader.Set(SpotlightPs_Pixel);

            RC.SetRasterizerState(MyRasterizerStateManager.InvTriRasterizerState);

            var cb = MyCommon.GetObjectCB(sizeof(SpotlightConstants));

            RC.AllShaderStages.SetConstantBuffer(1, cb);
            RC.PixelShader.SetSampler(13, MySamplerStateManager.Alphamask);
            RC.PixelShader.SetSampler(14, MySamplerStateManager.Shadowmap);
            RC.PixelShader.SetSampler(15, MySamplerStateManager.Shadowmap);

            int index       = 0;
            int casterIndex = 0;

            foreach (var id in VisibleSpotlights)
            {
                SpotlightConstants spotlight = new SpotlightConstants();
                MyLights.WriteSpotlightConstants(id, ref spotlight);

                var mapping = MyMapping.MapDiscard(cb);
                mapping.WriteAndPosition(ref spotlight);
                mapping.Unmap();

                RC.PixelShader.SetSrv(13, MyLights.Spotlights[id.Index].ReflectorTexture);

                if (id.CastsShadowsThisFrame)
                {
                    RC.PixelShader.SetSrv(14, MyRender11.DynamicShadows.ShadowmapsPool[casterIndex]);
                    casterIndex++;
                }

                if (MyRender11.MultisamplingEnabled)
                {
                    RC.SetDepthStencilState(MyDepthStencilStateManager.TestEdgeStencil, 0);
                    RC.PixelShader.Set(SpotlightPs_Pixel);
                }
                RC.DrawIndexed(MyMeshes.GetLodMesh(coneMesh, 0).Info.IndicesNum, 0, 0);

                if (MyRender11.MultisamplingEnabled)
                {
                    RC.PixelShader.Set(SpotlightPs_Sample);
                    RC.SetDepthStencilState(MyDepthStencilStateManager.TestEdgeStencil, 0x80);
                    RC.DrawIndexed(MyMeshes.GetLodMesh(coneMesh, 0).Info.IndicesNum, 0, 0);
                }

                index++;
                if (index >= SPOTLIGHTS_MAX)
                {
                    break;
                }
            }

            if (MyRender11.MultisamplingEnabled)
            {
                RC.SetDepthStencilState(MyDepthStencilStateManager.DefaultDepthState);
            }

            RC.SetRasterizerState(null);
            RC.SetRtv(null);
        }