Beispiel #1
0
        /// <summary>
        /// any PostProcessors present get to do their processing then we do the final render of the RenderTarget to the screen.
        /// In almost all cases finalRenderTarget will be null. The only time it will have a value is the first frame of a
        /// SceneTransition if the transition is requesting the render.
        /// </summary>
        /// <returns>The render.</returns>
        internal void PostRender(RenderTarget2D finalRenderTarget = null)
        {
            var enabledCounter = 0;

            if (EnablePostProcessing)
            {
                for (var i = 0; i < _postProcessors.Length; i++)
                {
                    if (_postProcessors.Buffer[i].Enabled)
                    {
                        var isEven = Mathf.IsEven(enabledCounter);
                        enabledCounter++;
                        _postProcessors.Buffer[i].Process(isEven ? _sceneRenderTarget : _destinationRenderTarget, isEven ? _destinationRenderTarget : _sceneRenderTarget);
                    }
                }
            }

            // deal with our Renderers that want to render after PostProcessors if we have any
            for (var i = 0; i < _afterPostProcessorRenderers.Length; i++)
            {
                if (i == 0)
                {
                    // we need to set the proper RenderTarget here. We want the last one that was the destination of our PostProcessors
                    GraphicsDeviceExt.SetRenderTarget(Core.GraphicsDevice, Mathf.IsEven(enabledCounter) ? _sceneRenderTarget : _destinationRenderTarget);
                }

                // force a Camera matrix update to account for the new Viewport size
                if (_afterPostProcessorRenderers.Buffer[i].Camera != null)
                {
                    _afterPostProcessorRenderers.Buffer[i].Camera.ForceMatrixUpdate();
                }
                _afterPostProcessorRenderers.Buffer[i].Render(this);
            }

            // if we have a screenshot request deal with it before the final render to the backbuffer
            if (_screenshotRequestCallback != null)
            {
                var tex  = new Texture2D(Core.GraphicsDevice, _sceneRenderTarget.Width, _sceneRenderTarget.Height);
                var data = new int[tex.Bounds.Width * tex.Bounds.Height];
                (Mathf.IsEven(enabledCounter) ? _sceneRenderTarget : _destinationRenderTarget).GetData <int>(data);
                tex.SetData <int>(data);
                _screenshotRequestCallback(tex);

                _screenshotRequestCallback = null;
            }

            // render our final result to the backbuffer or let our delegate do so
            if (_finalRenderDelegate != null)
            {
                _finalRenderDelegate.HandleFinalRender(finalRenderTarget, LetterboxColor, Mathf.IsEven(enabledCounter) ? _sceneRenderTarget : _destinationRenderTarget, _finalRenderDestinationRect, SamplerState);
            }
            else
            {
                GraphicsDeviceExt.SetRenderTarget(Core.GraphicsDevice, finalRenderTarget);
                Core.GraphicsDevice.Clear(LetterboxColor);
                Graphics.Instance.Batcher.Begin(BlendState.Opaque, SamplerState, null, null);
                Graphics.Instance.Batcher.Draw(Mathf.IsEven(enabledCounter) ? _sceneRenderTarget : _destinationRenderTarget, _finalRenderDestinationRect, Color.White);
                Graphics.Instance.Batcher.End();
            }
        }
Beispiel #2
0
        public virtual void Update()
        {
            // we set the RenderTarget here so that the Viewport will match the RenderTarget properly
            GraphicsDeviceExt.SetRenderTarget(Core.GraphicsDevice, _sceneRenderTarget);

            // update our lists in case they have any changes
            Entities.UpdateLists();

            // update our SceneComponents
            for (var i = _sceneComponents.Length - 1; i >= 0; i--)
            {
                if (_sceneComponents.Buffer[i].Enabled)
                {
                    _sceneComponents.Buffer[i].Update();
                }
            }

            // update our EntityProcessors
            if (EntityProcessors != null)
            {
                EntityProcessors.Update();
            }

            // update our Entities
            Entities.Update();

            if (EntityProcessors != null)
            {
                EntityProcessors.LateUpdate();
            }

            // we update our renderables after entity.update in case any new Renderables were added
            RenderableComponents.UpdateLists();
        }
Beispiel #3
0
 public override void Render(Batcher batcher)
 {
     GraphicsDeviceExt.SetRenderTarget(Core.GraphicsDevice, null);
     batcher.Begin(BlendState.NonPremultiplied, Core.DefaultSamplerState, DepthStencilState.None, null);
     batcher.Draw(PreviousSceneRender, Vector2.Zero, _color);
     batcher.End();
 }
Beispiel #4
0
 public override void Render(Graphics graphics)
 {
     GraphicsDeviceExt.SetRenderTarget(Core.GraphicsDevice, null);
     graphics.Batcher.Begin(BlendState.NonPremultiplied, Core.DefaultSamplerState, DepthStencilState.None, null, _windEffect);
     graphics.Batcher.Draw(PreviousSceneRender, _destinationRect, Color.White);
     graphics.Batcher.End();
 }
Beispiel #5
0
 /// <summary>
 /// do all of your rendering here.static This is a base implementation. Any special rendering should override
 /// this method.
 /// </summary>
 /// <param name="batcher">Batcher.</param>
 public virtual void Render(Batcher batcher)
 {
     GraphicsDeviceExt.SetRenderTarget(Core.GraphicsDevice, null);
     batcher.Begin(BlendState.Opaque, Core.DefaultSamplerState, DepthStencilState.None, null);
     batcher.Draw(PreviousSceneRender, System.Numerics.Vector2.Zero, Color.White);
     batcher.End();
 }
        public void OnSceneBackBufferSizeChanged(int newWidth, int newHeight)
        {
            // dont recreate the mosaic unless we really need to
            if (_lastMosaicScale != _scene.PixelPerfectScale)
            {
                CreateMosaicTexture(_scene.PixelPerfectScale);
                _lastMosaicScale = _scene.PixelPerfectScale;
            }

            if (_mosaicRenderTex != null)
            {
                _mosaicRenderTex.Dispose();
                _mosaicRenderTex = RenderTarget.Create(newWidth * _scene.PixelPerfectScale,
                                                       newHeight * _scene.PixelPerfectScale, DepthFormat.None);
            }
            else
            {
                _mosaicRenderTex = RenderTarget.Create(newWidth * _scene.PixelPerfectScale,
                                                       newHeight * _scene.PixelPerfectScale, DepthFormat.None);
            }

            // based on the look of games by: http://deepnight.net/games/strike-of-rage/
            // use the mosaic to render to a full sized RenderTarget repeating the mosaic
            GraphicsDeviceExt.SetRenderTarget(Core.GraphicsDevice, _mosaicRenderTex);
            Graphics.Instance.Batcher.Begin(BlendState.Opaque, SamplerState.PointWrap, DepthStencilState.None,
                                            RasterizerState.CullNone);
            Graphics.Instance.Batcher.Draw(_mosaicTexture, System.Numerics.Vector2.Zero,
                                           new Rectangle(0, 0, _mosaicRenderTex.Width, _mosaicRenderTex.Height), Color.White);
            Graphics.Instance.Batcher.End();

            // let our Effect know about our rendered, full screen mosaic
            _effect.Parameters["_secondTexture"].SetValue(_mosaicRenderTex);
        }
 public void HandleFinalRender(RenderTarget2D finalRenderTarget, Color letterboxColor, RenderTarget2D source, Rectangle finalRenderDestinationRect, SamplerState samplerState)
 {
     GraphicsDeviceExt.SetRenderTarget(Core.GraphicsDevice, finalRenderTarget);
     Core.GraphicsDevice.Clear(letterboxColor);
     Graphics.Instance.Batcher.Begin(BlendState.Opaque, samplerState, DepthStencilState.None, RasterizerState.CullNone, _effect);
     Graphics.Instance.Batcher.Draw(source, finalRenderDestinationRect, Color.White);
     Graphics.Instance.Batcher.End();
 }
Beispiel #8
0
 public override void PreRender(Graphics graphics)
 {
     GraphicsDeviceExt.SetRenderTarget(Core.GraphicsDevice, _maskRenderTarget);
     graphics.Batcher.Begin(BlendState.AlphaBlend, Core.DefaultSamplerState, DepthStencilState.None, null);
     graphics.Batcher.Draw(_maskTexture, _maskPosition, null, Color.White, _renderRotation, _maskOrigin, _renderScale, SpriteEffects.None, 0);
     graphics.Batcher.End();
     GraphicsDeviceExt.SetRenderTarget(Core.GraphicsDevice, null);
 }
Beispiel #9
0
        /// <summary>
        /// if a RenderTarget is used this will set it up. The Batcher is also started. The passed in Camera will be used to set the ViewPort
        /// (if a ViewportAdapter is present) and for the Batcher transform Matrix.
        /// </summary>
        /// <param name="cam">Cam.</param>
        protected virtual void BeginRender(Camera cam)
        {
            // if we have a renderTarget render into it
            if (RenderTexture != null)
            {
                GraphicsDeviceExt.SetRenderTarget(Core.GraphicsDevice, RenderTexture);
                Core.GraphicsDevice.Clear(RenderTargetClearColor);
            }

            _currentMaterial = Material;
            Graphics.Instance.Batcher.Begin(_currentMaterial, cam.TransformMatrix);
        }
Beispiel #10
0
		public override void Render(Graphics graphics)
		{
			GraphicsDeviceExt.SetRenderTarget(Core.GraphicsDevice, null);
			graphics.Batcher.Begin(BlendState.NonPremultiplied, Core.DefaultSamplerState, DepthStencilState.None, null);

			// we only render the previousSceneRender while fading to _color. It will be null after that.
			if (!_isNewSceneLoaded)
				graphics.Batcher.Draw(PreviousSceneRender, _destinationRect, Color.White);

			graphics.Batcher.Draw(_overlayTexture, new Rectangle(0, 0, Screen.Width, Screen.Height), _color);

			graphics.Batcher.End();
		}
Beispiel #11
0
        public override void Render(Batcher batcher)
        {
            GraphicsDeviceExt.SetRenderTarget(Core.GraphicsDevice, null);

            // if we are scaling out we dont need to render the previous scene anymore since we want the new scene to be visible
            if (!_isNewSceneLoaded)
            {
                batcher.Begin(BlendState.Opaque, Core.DefaultSamplerState, DepthStencilState.None, null);
                batcher.Draw(PreviousSceneRender, System.Numerics.Vector2.Zero, Color.White);
                batcher.End();
            }

            batcher.Begin(_blendState, Core.DefaultSamplerState, DepthStencilState.None, null);
            batcher.Draw(_maskRenderTarget, System.Numerics.Vector2.Zero, Color.White);
            batcher.End();
        }
Beispiel #12
0
        public override void Process(RenderTarget2D source, RenderTarget2D destination)
        {
            // first we process the rendered layer with the bloom effect
            base.Process(_layerRT, _tempRT);

            // we need to be careful here and ensure we use AlphaBlending since the layer we rendered is mostly transparent
            GraphicsDeviceExt.SetRenderTarget(Core.GraphicsDevice, destination);
            Graphics.Instance.Batcher.Begin(BlendState.AlphaBlend, SamplerState, DepthStencilState.None, RasterizerState.CullNone);

            // now we first draw the full scene (source), then draw our bloomed layer (tempRT) then draw the un-bloomed layer (layerRT)
            Graphics.Instance.Batcher.Draw(source, new Rectangle(0, 0, destination.Width, destination.Height), Color.White);
            Graphics.Instance.Batcher.Draw(_tempRT, new Rectangle(0, 0, destination.Width, destination.Height), Color.White);
            Graphics.Instance.Batcher.Draw(_layerRT, new Rectangle(0, 0, destination.Width, destination.Height), Color.White);

            Graphics.Instance.Batcher.End();
        }
Beispiel #13
0
        public override void Render(Graphics graphics)
        {
            GraphicsDeviceExt.SetRenderTarget(Core.GraphicsDevice, null);
            graphics.Batcher.Begin(BlendState.NonPremultiplied, Core.DefaultSamplerState, DepthStencilState.None, null, _squaresEffect);

            // we only render the previousSceneRender while populating the squares
            if (!_isNewSceneLoaded)
            {
                graphics.Batcher.Draw(PreviousSceneRender, _destinationRect, Color.White);
            }
            else
            {
                graphics.Batcher.Draw(_overlayTexture, new Rectangle(0, 0, Screen.Width, Screen.Height), Color.Transparent);
            }

            graphics.Batcher.End();
        }
Beispiel #14
0
        internal void Render()
        {
            if (_renderers.Length == 0)
            {
                Debug.Error("There are no Renderers in the Scene!");
                return;
            }

            // Renderers should always have those that require a RenderTarget first. They clear themselves and set themselves as
            // the current RenderTarget when they render. If the first Renderer wants the sceneRenderTarget we set and clear it now.
            if (_renderers[0].WantsToRenderToSceneRenderTarget)
            {
                GraphicsDeviceExt.SetRenderTarget(Core.GraphicsDevice, _sceneRenderTarget);
                Core.GraphicsDevice.Clear(ClearColor);
            }


            var lastRendererHadRenderTarget = false;

            for (var i = 0; i < _renderers.Length; i++)
            {
                // MonoGame follows the XNA bullshit implementation so it will clear the entire buffer if we change the render target even if null.
                // Because of that, we track when we are done with our RenderTargets and clear the scene at that time.
                if (lastRendererHadRenderTarget && _renderers.Buffer[i].WantsToRenderToSceneRenderTarget)
                {
                    GraphicsDeviceExt.SetRenderTarget(Core.GraphicsDevice, _sceneRenderTarget);
                    Core.GraphicsDevice.Clear(ClearColor);

                    // force a Camera matrix update to account for the new Viewport size
                    if (_renderers.Buffer[i].Camera != null)
                    {
                        _renderers.Buffer[i].Camera.ForceMatrixUpdate();
                    }
                    Camera.ForceMatrixUpdate();
                }

                _renderers.Buffer[i].Render(this);
                lastRendererHadRenderTarget = _renderers.Buffer[i].RenderTexture != null;
            }
        }
Beispiel #15
0
        internal void Begin()
        {
            if (_renderers.Length == 0)
            {
                AddRenderer(new DefaultRenderer());
                Debug.Warn("Scene has begun with no renderer. A DefaultRenderer was added automatically so that something is visible.");
            }

            Physics.Reset();

            // prep our render textures
            UpdateResolutionScaler();
            GraphicsDeviceExt.SetRenderTarget(Core.GraphicsDevice, _sceneRenderTarget);

            if (EntityProcessors != null)
            {
                EntityProcessors.Begin();
            }
            Core.Emitter.AddObserver(CoreEvents.GraphicsDeviceReset, OnGraphicsDeviceReset);

            _didSceneBegin = true;
            OnStart();
        }
Beispiel #16
0
 /// <summary>
 /// helper for drawing a texture into a rendertarget, optionally using a custom shader to apply postprocessing effects.
 /// </summary>
 protected void DrawFullscreenQuad(Texture2D texture, RenderTarget2D renderTarget, Effect effect = null)
 {
     GraphicsDeviceExt.SetRenderTarget(Core.GraphicsDevice, renderTarget);
     DrawFullscreenQuad(texture, renderTarget.Width, renderTarget.Height, effect);
 }