public InGameUISample(Microsoft.Xna.Framework.Game game) : base(game) { // Add gravity and damping to the physics simulation. Simulation.ForceEffects.Add(new Gravity()); Simulation.ForceEffects.Add(new Damping()); // Create the DeferredGraphicsScreen. var graphicsScreen = new DeferredGraphicsScreen(Services); graphicsScreen.DrawReticle = true; GraphicsService.Screens.Insert(0, graphicsScreen); Services.Register(typeof(DebugRenderer), null, graphicsScreen.DebugRenderer); Services.Register(typeof(IScene), null, graphicsScreen.Scene); // some 3D objects var cameraGameObject = new CameraObject(Services); GameObjectService.Objects.Add(cameraGameObject); graphicsScreen.ActiveCameraNode = cameraGameObject.CameraNode; GameObjectService.Objects.Add(new GrabObject(Services)); GameObjectService.Objects.Add(new StaticSkyObject(Services)); GameObjectService.Objects.Add(new GroundObject(Services)); for (int i = 0; i < 10; i++) { GameObjectService.Objects.Add(new DynamicObject(Services, 3)); } // Create the UIScreen which is rendered into the back buffer. Theme theme = ContentManager.Load <Theme>("UI Themes/Aero/Theme"); UIRenderer renderer = new UIRenderer(Game, theme); _normalUIScreen = new NormalUIScreen(renderer); UIService.Screens.Add(_normalUIScreen); // Handle the InputProcessed event of the Image control. _normalUIScreen.Image.InputProcessed += OnGameViewControlInputProcessed; // Create the DelegateGraphicsScreen. This graphics screen is on top of the // DeferredGraphicsScreen and instructs the graphics service to render the // previous screen into an off-screen render target. var delegateGraphicsScreen = new DelegateGraphicsScreen(GraphicsService) { RenderCallback = Render, RenderPreviousScreensToTexture = true, SourceTextureFormat = new RenderTargetFormat( (int)_normalUIScreen.Image.Width, (int)_normalUIScreen.Image.Height, false, SurfaceFormat.Color, DepthFormat.Depth24), }; GraphicsService.Screens.Insert(1, delegateGraphicsScreen); // Create the UIScreen that is rendered into a render target and mapped onto the 3D game objects. _inGameUIScreenRenderTarget = new RenderTarget2D(GraphicsService.GraphicsDevice, 600, 250, false, SurfaceFormat.Color, DepthFormat.None); _inGameScreen = new InGameUIScreen(Services, renderer) { InputEnabled = false, Width = _inGameUIScreenRenderTarget.Width, Height = _inGameUIScreenRenderTarget.Height, }; UIService.Screens.Add(_inGameScreen); // We can use the off-screen render target anywhere in the 3D scene. Here, we // use it to replace the normal "TestCard" texture of the TV objects and the ProjectorLights. foreach (var node in graphicsScreen.Scene.GetDescendants()) { var meshNode = node as MeshNode; if (meshNode != null) { foreach (var material in meshNode.Mesh.Materials) { if (material.Name == "TestCard") { material["Material"].Set("EmissiveTexture", (Texture)_inGameUIScreenRenderTarget); material["Material"].Set("Exposure", 0.1f); } } continue; } var lightNode = node as LightNode; if (lightNode != null && lightNode.Light is ProjectorLight) { ((ProjectorLight)lightNode.Light).Texture = _inGameUIScreenRenderTarget; } } }
public InGameUISample(Microsoft.Xna.Framework.Game game) : base(game) { // Add gravity and damping to the physics simulation. Simulation.ForceEffects.Add(new Gravity()); Simulation.ForceEffects.Add(new Damping()); // Create the DeferredGraphicsScreen. var graphicsScreen = new DeferredGraphicsScreen(Services); graphicsScreen.DrawReticle = true; GraphicsService.Screens.Insert(0, graphicsScreen); Services.Register(typeof(DebugRenderer), null, graphicsScreen.DebugRenderer); Services.Register(typeof(IScene), null, graphicsScreen.Scene); // some 3D objects var cameraGameObject = new CameraObject(Services); GameObjectService.Objects.Add(cameraGameObject); graphicsScreen.ActiveCameraNode = cameraGameObject.CameraNode; GameObjectService.Objects.Add(new GrabObject(Services)); GameObjectService.Objects.Add(new StaticSkyObject(Services)); GameObjectService.Objects.Add(new GroundObject(Services)); for (int i = 0; i < 10; i++) GameObjectService.Objects.Add(new DynamicObject(Services, 3)); // Create the UIScreen which is rendered into the back buffer. Theme theme = ContentManager.Load<Theme>("UI Themes/Aero/Theme"); UIRenderer renderer = new UIRenderer(Game, theme); _normalUIScreen = new NormalUIScreen(renderer); UIService.Screens.Add(_normalUIScreen); // Handle the InputProcessed event of the Image control. _normalUIScreen.Image.InputProcessed += OnGameViewControlInputProcessed; // Create the DelegateGraphicsScreen. This graphics screen is on top of the // DeferredGraphicsScreen and instructs the graphics service to render the // previous screen into an off-screen render target. var delegateGraphicsScreen = new DelegateGraphicsScreen(GraphicsService) { RenderCallback = Render, RenderPreviousScreensToTexture = true, SourceTextureFormat = new RenderTargetFormat( (int)_normalUIScreen.Image.Width, (int)_normalUIScreen.Image.Height, false, SurfaceFormat.Color, DepthFormat.Depth24), }; GraphicsService.Screens.Insert(1, delegateGraphicsScreen); // Create the UIScreen that is rendered into a render target and mapped onto the 3D game objects. _inGameUIScreenRenderTarget = new RenderTarget2D(GraphicsService.GraphicsDevice, 600, 250, false, SurfaceFormat.Color, DepthFormat.None); _inGameScreen = new InGameUIScreen(Services, renderer) { InputEnabled = false, Width = _inGameUIScreenRenderTarget.Width, Height = _inGameUIScreenRenderTarget.Height, }; UIService.Screens.Add(_inGameScreen); // We can use the off-screen render target anywhere in the 3D scene. Here, we // use it to replace the normal "TestCard" texture of the TV objects and the ProjectorLights. foreach (var node in graphicsScreen.Scene.GetDescendants()) { var meshNode = node as MeshNode; if (meshNode != null) { foreach (var material in meshNode.Mesh.Materials) { if (material.Name == "TestCard") { material["Material"].Set("EmissiveTexture", (Texture)_inGameUIScreenRenderTarget); material["Material"].Set("Exposure", 0.1f); } } continue; } var lightNode = node as LightNode; if (lightNode != null && lightNode.Light is ProjectorLight) { ((ProjectorLight)lightNode.Light).Texture = _inGameUIScreenRenderTarget; } } }