Esempio n. 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DebugGraphicsScreen" /> class.
        /// </summary>
        /// <param name="services">The services.</param>
        public DebugGraphicsScreen(IServiceLocator services)
            : base(services?.GetInstance <IGraphicsService>())
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            Coverage      = GraphicsScreenCoverage.Partial;
            _spriteBatch  = GraphicsService.GetSpriteBatch();
            _whiteTexture = GraphicsService.GetDefaultTexture2DWhite();

            var contentManager = services.GetInstance <ContentManager>();
            var spriteFont     = contentManager.Load <SpriteFont>("DigitalRune.Editor.Game/Fonts/DejaVuSans");

            DebugRenderer          = new DebugRenderer(GraphicsService, spriteFont);
            _internalDebugRenderer = new DebugRenderer(GraphicsService, spriteFont);

            // To count the update frame rate, we handle the GameLogicUpdating event.
            // (We cannot use GraphicsScreen.OnUpdate because it is only called at the same rate if
            // the graphics screen is registered in the graphics service. If it is not registered,
            // then OnUpdate and OnRender are always called together.)
            var editor = services.GetInstance <IEditorService>();

            _gameExtension = editor.Extensions.OfType <GameExtension>().FirstOrDefault();
            if (_gameExtension != null)
            {
                _gameExtension.GameLogicUpdating += OnGameLogicUpdating;
            }
        }
        public CustomCommandSample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            // Add a DelegateGraphicsScreen as the first graphics screen to the graphics
            // service. This lets us do the rendering in the Render method of this class.
            var graphicsScreen = new DelegateGraphicsScreen(GraphicsService)
            {
                RenderCallback = Render
            };

            GraphicsService.Screens.Insert(0, graphicsScreen);

            _spriteBatch = GraphicsService.GetSpriteBatch();

            // Load a few SpriteFonts for rendering.
            _textFont   = UIContentManager.Load <SpriteFont>("UI Themes/WindowsPhone7/Segoe15");
            _buttonFont = ContentManager.Load <SpriteFont>("ButtonImages/xboxControllerSpriteFont");

            // Add custom commands to input service.
            _buttonHoldCommand = new ButtonHoldCommand(Buttons.A, 1.0f)
            {
                Name = "Hold A"
            };
            _buttonTapCommand = new ButtonTapCommand(Buttons.A, 0.2f, 1.0f)
            {
                Name = "Tap A"
            };
            _buttonSequenceCommand = new ButtonSequenceCommand(new [] { Buttons.A, Buttons.B, Buttons.A, Buttons.B }, 2.0f)
            {
                Name = "A-B-A-B"
            };
            InputService.Commands.Add(_buttonHoldCommand);
            InputService.Commands.Add(_buttonTapCommand);
            InputService.Commands.Add(_buttonSequenceCommand);
        }
Esempio n. 3
0
        protected AnimationSample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            // Add a DelegateGraphicsScreen and use the OnRender method of this class to
            // do the rendering.
            var graphicsScreen = new DelegateGraphicsScreen(GraphicsService)
            {
                RenderCallback = OnRender,
            };

            // The order of the graphics screens is back-to-front. Add the screen at index 0,
            // i.e. behind all other screens. The screen should be rendered first and all other
            // screens (menu, GUI, help, ...) should be on top.
            GraphicsService.Screens.Insert(0, graphicsScreen);

            // Provide a SpriteBatch, SpriteFont and images for rendering.
            SpriteBatch = GraphicsService.GetSpriteBatch();
            SpriteFont  = UIContentManager.Load <SpriteFont>("UI Themes/BlendBlue/Default");
            Logo        = ContentManager.Load <Texture2D>("Logo");
            Reticle     = ContentManager.Load <Texture2D>("Reticle");
        }
        //--------------------------------------------------------------
        #region Creation & Cleanup
        //--------------------------------------------------------------

        public DeferredGraphicsScreen(IServiceLocator services)
            : base(services.GetInstance <IGraphicsService>())
        {
            _sampleFramework = services.GetInstance <SampleFramework>();
            var contentManager = services.GetInstance <ContentManager>();

            SpriteBatch = GraphicsService.GetSpriteBatch();

            // Let's create the necessary scene node renderers:
#if !XBOX360
            TerrainRenderer = new TerrainRenderer(GraphicsService);
#endif
            MeshRenderer = new MeshRenderer();

            // The _opaqueMeshSceneRenderer combines all renderers for opaque
            // (= not alpha blended) meshes.
            _opaqueMeshSceneRenderer = new SceneRenderer();
#if !XBOX360
            _opaqueMeshSceneRenderer.Renderers.Add(TerrainRenderer);
#endif
            _opaqueMeshSceneRenderer.Renderers.Add(MeshRenderer);

            _decalRenderer     = new DecalRenderer(GraphicsService);
            _billboardRenderer = new BillboardRenderer(GraphicsService, 2048)
            {
                EnableSoftParticles = true,

                // If you have an extreme amount of particles that cover the entire screen,
                // you can turn on offscreen rendering to improve performance.
                //EnableOffscreenRendering = true,
            };

            // The AlphaBlendSceneRenderer combines all renderers for transparent
            // (= alpha blended) objects.
            AlphaBlendSceneRenderer = new SceneRenderer();
            AlphaBlendSceneRenderer.Renderers.Add(MeshRenderer);
            AlphaBlendSceneRenderer.Renderers.Add(_billboardRenderer);
            AlphaBlendSceneRenderer.Renderers.Add(new WaterRenderer(GraphicsService));
            AlphaBlendSceneRenderer.Renderers.Add(new FogSphereRenderer(GraphicsService));
            AlphaBlendSceneRenderer.Renderers.Add(new VolumetricLightRenderer(GraphicsService));

#if !XBOX360
            // Update terrain clipmaps. (Only necessary if TerrainNodes are used.)
            _terrainClipmapRenderer = new TerrainClipmapRenderer(GraphicsService);
#endif

            // Renderer for cloud maps. (Only necessary if LayeredCloudMaps are used.)
            _cloudMapRenderer = new CloudMapRenderer(GraphicsService);

            // Renderer for SceneCaptureNodes. See also SceneCapture2DSample.
            // In the constructor we specify a method which is called in SceneCaptureRenderer.Render()
            // when the scene must be rendered for the SceneCaptureNodes.
            SceneCaptureRenderer = new SceneCaptureRenderer(context =>
            {
                // Get scene nodes which are visible by the current camera.
                CustomSceneQuery sceneQuery = Scene.Query <CustomSceneQuery>(context.CameraNode, context);
                // Render scene (with post-processing, with lens flares, no debug rendering, no reticle).
                RenderScene(sceneQuery, context, true, true, false, false);
            });

            // Renderer for PlanarReflectionNodes. See also PlanarReflectionSample.
            // In the constructor we specify a method which is called in PlanarReflectionRenderer.Render()
            // to create the reflection images.
            _planarReflectionRenderer = new PlanarReflectionRenderer(context =>
            {
                // Get scene nodes which are visible by the current camera.
                CustomSceneQuery sceneQuery = Scene.Query <CustomSceneQuery>(context.CameraNode, context);

                var planarReflectionNode = (PlanarReflectionNode)context.ReferenceNode;

                // Planar reflections are often for WaterNodes. These nodes should not be rendered
                // into their own reflection map because when the water surface is displaced by waves,
                // some waves could be visible in the reflection.
                // --> Remove the water node from the renderable nodes. (In our samples, the water
                // node is the parent of the reflection node.)
                if (planarReflectionNode.Parent is WaterNode)
                {
                    var index = sceneQuery.RenderableNodes.IndexOf(planarReflectionNode.Parent);
                    if (index >= 0)
                    {
                        sceneQuery.RenderableNodes[index] = null;
                    }
                }

                // Render scene (no post-processing, no lens flares, no debug rendering, no reticle).
                RenderScene(sceneQuery, context, false, false, false, false);
            });

            _waterWavesRenderer = new WaterWavesRenderer(GraphicsService);

            // The shadow map renderer renders a depth image from the viewpoint of the light and
            // stores it in LightNode.Shadow.ShadowMap.
            ShadowMapRenderer = new ShadowMapRenderer(context =>
            {
                var query = context.Scene.Query <ShadowCasterQuery>(context.CameraNode, context);
                if (query.ShadowCasters.Count == 0)
                {
                    return(false);
                }

                _opaqueMeshSceneRenderer.Render(query.ShadowCasters, context);
                return(true);
            });

            // The shadow mask renderer evaluates the shadow maps, does shadow filtering
            // and stores the resulting shadow factor in a screen space image
            //(see LightNode.Shadow.ShadowMask/ShadowMaskChannel).
            ShadowMaskRenderer = new ShadowMaskRenderer(GraphicsService, 2);

            // Optionally, we can blur the shadow mask to make the shadows smoother.
            var blur = new Blur(GraphicsService)
            {
                IsAnisotropic = false,
                IsBilateral   = true,
                EdgeSoftness  = 0.05f,
                Scale         = 1f,
                Enabled       = false, // Disable blur by default.
            };
            blur.InitializeGaussianBlur(11, 3, true);
            ShadowMaskRenderer.Filter = blur;

            // Renderers which create the intermediate render targets:
            // Those 2 renderers are implemented in this sample. Those functions could
            // be implemented directly in this class but we have created separate classes
            // to make the code more readable.
            _gBufferRenderer    = new GBufferRenderer(GraphicsService, _opaqueMeshSceneRenderer, _decalRenderer);
            LightBufferRenderer = new LightBufferRenderer(GraphicsService);

            // Other specialized renderers:
            _lensFlareRenderer      = new LensFlareRenderer(GraphicsService);
            _skyRenderer            = new SkyRenderer(GraphicsService);
            _fogRenderer            = new FogRenderer(GraphicsService);
            _internalDebugRenderer  = new DebugRenderer(GraphicsService, null);
            _rebuildZBufferRenderer = new RebuildZBufferRenderer(GraphicsService);

            Scene = new Scene();

            // This screen needs a HDR filter to map high dynamic range values back to
            // low dynamic range (LDR).
            PostProcessors = new PostProcessorChain(GraphicsService);
            PostProcessors.Add(new HdrFilter(GraphicsService)
            {
                EnableBlueShift = true,
                BlueShiftCenter = 0.0004f,
                BlueShiftRange  = 0.5f,
                //BlueShiftColor = new Vector3F(1.05f / 4f, 0.97f / 4f, 1.27f / 4f),  // Default physically-based blue-shift
                BlueShiftColor = new Vector3F(0.25f, 0.25f, 0.7f), // More dramatic blue-shift
                MinExposure    = 0,
                MaxExposure    = 10,
                BloomIntensity = 1,
                BloomThreshold = 0.6f,
            });
            _underwaterPostProcessor = new UnderwaterPostProcessor(GraphicsService, contentManager);
            PostProcessors.Add(_underwaterPostProcessor);

            // Use 2D texture for reticle.
            _reticle = contentManager.Load <Texture2D>("Reticle");

            // Use the sprite font of the GUI.
            var uiContentManager = services.GetInstance <ContentManager>("UIContent");
            var spriteFont       = uiContentManager.Load <SpriteFont>("UI Themes/BlendBlue/Default");
            DebugRenderer = new DebugRenderer(GraphicsService, spriteFont)
            {
                DefaultColor        = new Color(0, 0, 0),
                DefaultTextPosition = new Vector2F(10),
            };

            EnableLod = true;
        }
Esempio n. 5
0
        private void Render(RenderContext context)
        {
            var originalRenderTarget = context.RenderTarget;
            var originalViewport     = context.Viewport;

            var graphicsDevice = context.GraphicsService.GraphicsDevice;

            if (_updateCubeMap)
            {
                _updateCubeMap = false;

                _cloudMapRenderer.Render(_skyNodes, context);

                // Create a camera with 45° FOV for a single cube map face.
                var perspectiveProjection = new PerspectiveProjection();
                perspectiveProjection.SetFieldOfView(ConstantsF.PiOver2, 1, 1, 100);
                context.CameraNode = new CameraNode(new Camera(perspectiveProjection));

                var size      = _skybox.Texture.Size;
                var hdrFormat = new RenderTargetFormat(size, size, false, SurfaceFormat.HdrBlendable, DepthFormat.None);
                var hdrTarget = context.GraphicsService.RenderTargetPool.Obtain2D(hdrFormat);
                var ldrFormat = new RenderTargetFormat(size, size, false, SurfaceFormat.Color, DepthFormat.None);
                var ldrTarget = context.GraphicsService.RenderTargetPool.Obtain2D(ldrFormat);

                var spriteBatch = GraphicsService.GetSpriteBatch();
                for (int side = 0; side < 6; side++)
                {
                    // Rotate camera to face the current cube map face.
                    var cubeMapFace = (CubeMapFace)side;
                    context.CameraNode.View = Matrix44F.CreateLookAt(
                        new Vector3F(),
                        GraphicsHelper.GetCubeMapForwardDirection(cubeMapFace),
                        GraphicsHelper.GetCubeMapUpDirection(cubeMapFace));

                    // Render sky into HDR render target.
                    graphicsDevice.SetRenderTarget(hdrTarget);
                    context.RenderTarget = hdrTarget;
                    context.Viewport     = graphicsDevice.Viewport;
                    graphicsDevice.Clear(Color.Black);
                    _skyRenderer.Render(_skyNodes, context);

                    graphicsDevice.BlendState = BlendState.Opaque;

                    // Convert HDR to RGBM.
                    context.SourceTexture = hdrTarget;
                    context.RenderTarget  = ldrTarget;
                    _colorEncoder.Process(context);
                    context.SourceTexture = null;

                    // Copy RGBM texture into cube map face.
                    graphicsDevice.SetRenderTarget((RenderTargetCube)_skybox.Texture, cubeMapFace);
                    spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque, null, null, null);
                    spriteBatch.Draw(ldrTarget, new Vector2(0, 0), Color.White);
                    spriteBatch.End();
                }

                context.GraphicsService.RenderTargetPool.Recycle(ldrTarget);
                context.GraphicsService.RenderTargetPool.Recycle(hdrTarget);
            }

            graphicsDevice.BlendState        = BlendState.Opaque;
            graphicsDevice.DepthStencilState = DepthStencilState.Default;
            graphicsDevice.RasterizerState   = RasterizerState.CullCounterClockwise;

            context.CameraNode = _cameraObject.CameraNode;

            var tempFormat = new RenderTargetFormat(originalRenderTarget);

            tempFormat.SurfaceFormat = SurfaceFormat.HdrBlendable;
            var tempTarget = context.GraphicsService.RenderTargetPool.Obtain2D(tempFormat);

            graphicsDevice.SetRenderTarget(tempTarget);
            graphicsDevice.Viewport = originalViewport;
            context.RenderTarget    = tempTarget;
            context.Viewport        = originalViewport;

            _skyRenderer.Render(_skybox, context);

            context.SourceTexture = tempTarget;
            context.RenderTarget  = originalRenderTarget;
            _hdrFilter.Process(context);
            context.SourceTexture = null;

            context.GraphicsService.RenderTargetPool.Recycle(tempTarget);

            RenderDebugInfo(context);

            context.CameraNode = null;
        }