Esempio n. 1
0
        internal static ISrvBindable WriteSpotlightConstants(LightId lid, ref SpotlightConstants data)
        {
            data.Spotlight = m_spotlights[lid.Index].Spotlight;
            data.Spotlight.ShadowsRange   = m_lights.Data[lid.Index].CastsShadowsThisFrame ? m_lights.Data[lid.Index].ShadowsDistance : 0;
            data.Spotlight.Light.Position = m_lights.Data[lid.Index].SpotPosition - MyRender11.Environment.Matrices.CameraPosition;
            data.Spotlight.Direction      = m_lights.Data[lid.Index].Direction;
            data.Spotlight.Up             = m_lights.Data[lid.Index].Up;

            float ratio = (float)Math.Sqrt(1 - data.Spotlight.ApertureCos * data.Spotlight.ApertureCos) / data.Spotlight.ApertureCos;
            float h     = ratio * data.Spotlight.Light.Range;

            Matrix viewProjAt0 = MyRender11.Environment.Matrices.ViewProjectionAt0;

            if (MyStereoRender.Enable)
            {
                if (MyStereoRender.RenderRegion == MyStereoRegion.LEFT)
                {
                    viewProjAt0 = MyStereoRender.EnvMatricesLeftEye.ViewProjectionAt0;
                }
                if (MyStereoRender.RenderRegion == MyStereoRegion.RIGHT)
                {
                    viewProjAt0 = MyStereoRender.EnvMatricesRightEye.ViewProjectionAt0;
                }
            }
            data.ProxyWorldViewProj = Matrix.Transpose(Matrix.CreateScale(2 * h, 2 * h, data.Spotlight.Light.Range) *
                                                       Matrix.CreateWorld(data.Spotlight.Light.Position, data.Spotlight.Direction, data.Spotlight.Up) *
                                                       viewProjAt0);

            data.ShadowMatrix = CreateShadowMatrix(lid);

            return(m_spotlights[lid.Index].ReflectorTexture);
        }
Esempio n. 2
0
        private static unsafe void RenderSpotlights()
        {
            RC.SetRtv(MyGBuffer.Main.DepthStencil, MyDepthStencilAccess.ReadOnly, MyGBuffer.Main.LBuffer);
            RC.SetViewport(0, 0, MyRender11.ViewportResolution.X, MyRender11.ViewportResolution.Y);
            RC.SetPrimitiveTopology(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);
            RC.SetIndexBuffer(buffers.IB);

            RC.VertexShader.Set(m_spotlightProxyVs);
            RC.SetInputLayout(m_spotlightProxyIl);
            RC.PixelShader.Set(m_spotlightPsPixel);

            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();
                var reflectorTexture         = MyLights.WriteSpotlightConstants(id, ref spotlight);

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

                RC.PixelShader.SetSrv(13, reflectorTexture);

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

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

                if (MyRender11.MultisamplingEnabled)
                {
                    RC.PixelShader.Set(m_spotlightPsSample);
                    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);
        }