Exemplo n.º 1
0
        public BitmapSource Render(Camera camera)
        {
            // Setup for rendering.
            var rasterizerState = RasterizerState.New(_device, new RasterizerStateDescription
            {
                CullMode                = CullMode.Back,
                FillMode                = FillMode.Solid,
                IsMultisampleEnabled    = true,
                IsFrontCounterClockwise = Options.TriangleWindingOrderReversed
            });

            _device.SetRasterizerState(rasterizerState);
            _device.Clear(_renderTexture, ConversionUtility.ToSharpDXColor(Options.BackgroundColor));
            _device.Clear(_depthStencilTexture, DepthStencilClearFlags.Depth, 1, 0);
            _device.SetRenderTargets(_depthStencilTexture, _renderTexture);

            _effect.LightingEnabled = Options.LightingEnabled;
            _effect.View            = ConversionUtility.ToSharpDXMatrix(camera.GetViewMatrix());
            _effect.Projection      = ConversionUtility.ToSharpDXMatrix(camera.GetProjectionMatrix(_aspectRatio));

            // Render scene.
            _device.SetBlendState(_device.BlendStates.Opaque);
            foreach (WarpMesh mesh in _meshes.Where(m => m.IsOpaque))
            {
                mesh.Draw(_effect);
            }
            _device.SetBlendState(_device.BlendStates.AlphaBlend);
            foreach (WarpMesh mesh in _meshes.Where(m => !m.IsOpaque))
            {
                mesh.Draw(_effect);
            }
            _device.SetBlendState(null);

            // Extract image from render target.
            _device.SetRenderTargets((RenderTargetView)null);
            _device.SetRasterizerState(null);
            rasterizerState.Dispose();

            using (var memoryStream = new MemoryStream())
            {
                _renderTexture.Save(memoryStream, ImageFileType.Png);

                var bi = new BitmapImage();
                bi.BeginInit();
                bi.CacheOption  = BitmapCacheOption.OnLoad;
                bi.StreamSource = memoryStream;
                bi.EndInit();

                return(bi);
            }
        }
Exemplo n.º 2
0
        public override void Process(ITimeService time)
        {
            IsEnabled = false;
            var device             = Services.GetService <IGraphicsDeviceService>().DirectXDevice;
            var cScreenshot        = Entities.First().GetComponent <ScreenshotComponent>();
            var backBuffer         = device.BackBuffer;
            var textureDescription = new Texture2DDescription()
            {
                CpuAccessFlags    = CpuAccessFlags.None,
                Format            = backBuffer.Format.Value,
                Height            = backBuffer.Height,
                Usage             = ResourceUsage.Default,
                Width             = backBuffer.Width,
                ArraySize         = 1,
                SampleDescription = new SampleDescription(1, 0),
                BindFlags         = BindFlags.RenderTarget | BindFlags.ShaderResource,
                MipLevels         = 1,
                OptionFlags       = ResourceOptionFlags.None
            };

            renderTarget = RenderTarget2D.New(device, textureDescription);

            var depthStencilDesc = new Texture2DDescription()
            {
                ArraySize         = 1,
                MipLevels         = 1,
                BindFlags         = BindFlags.DepthStencil,
                Format            = device.DepthStencilBuffer.Format.Value,
                Width             = textureDescription.Width,
                Height            = textureDescription.Height,
                SampleDescription = textureDescription.SampleDescription,
                CpuAccessFlags    = CpuAccessFlags.None,
                Usage             = ResourceUsage.Default,
                OptionFlags       = ResourceOptionFlags.None
            };

            depthStencil = DepthStencilBuffer.New(device, depthStencilDesc);


            ((DeviceContext)device).ResolveSubresource(backBuffer, 0, renderTarget, 0, textureDescription.Format);
            renderTarget.Initialize();
            depthStencil.Initialize();

            device.SetRenderTargets(depthStencil, renderTarget);
            renderTarget.Save("test.png", ImageFileType.Png);

            renderTarget.Dispose();
            depthStencil.Dispose();
            IsEnabled = false;
        }
Exemplo n.º 3
0
        private void TakeScreenshot(bool useGbRes)
        {
            var factor = useGbRes
                ? 1
                : 4;

            var buffer = new RenderTarget2D(GraphicsDevice, Constants.GbWidth * factor, Constants.GbHeight * factor);

            GraphicsDevice.SetRenderTarget(buffer);

            SpriteBatch.Begin(samplerState: SamplerState.PointClamp);
            SpriteBatch.Draw(_shaderbuffer, new Rectangle(0, 0, Constants.GbWidth * factor, Constants.GbHeight * factor),
                             Color.White);
            SpriteBatch.End();

            GraphicsDevice.SetRenderTarget(null);

            buffer.Save($"{Data.SaveDir}screenshots/{Guid.NewGuid()}.png");
        }