예제 #1
0
 private static Viewport GetViewport(CascadedShadow shadow, int split)
 {
     return(new Viewport
     {
         X = split * shadow.ShadowMap.Width / shadow.NumberOfCascades,
         Y = 0,
         Width = shadow.ShadowMap.Width / shadow.NumberOfCascades,
         Height = shadow.ShadowMap.Height,
         MinDepth = 0,
         MaxDepth = 1,
     });
 }
예제 #2
0
        public CascadedShadowSample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            SampleFramework.IsMouseVisible = false;
            _graphicsScreen = new DeferredGraphicsScreen(Services)
            {
                // For debugging: Disable materials and only show light buffer.
                DebugMode = DeferredGraphicsDebugMode.VisualizeDiffuseLightBuffer
            };
            _graphicsScreen.DrawReticle = true;
            GraphicsService.Screens.Insert(0, _graphicsScreen);

            GameObjectService.Objects.Add(new DeferredGraphicsOptionsObject(Services));

            Services.Register(typeof(DebugRenderer), null, _graphicsScreen.DebugRenderer);
            Services.Register(typeof(IScene), null, _graphicsScreen.Scene);

            // Add gravity and damping to the physics simulation.
            Simulation.ForceEffects.Add(new Gravity());
            Simulation.ForceEffects.Add(new Damping());

            // Add a custom game object which controls the camera.
            var cameraGameObject = new CameraObject(Services);

            GameObjectService.Objects.Add(cameraGameObject);
            _graphicsScreen.ActiveCameraNode = cameraGameObject.CameraNode;

            GameObjectService.Objects.Add(new GrabObject(Services));

            ShadowSample.CreateScene(Services, ContentManager, _graphicsScreen);

            // Get the cascaded shadow of the sunlight (which was created by the DynamicSkyObject).
            _cascadedShadow = (CascadedShadow)((LightNode)_graphicsScreen.Scene.GetSceneNode("Sunlight")).Shadow;

            CreateGuiControls();
        }
        public CompositeShadowSample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            SampleFramework.IsMouseVisible = false;
            _graphicsScreen = new DeferredGraphicsScreen(Services)
            {
                // For debugging: Disable materials and only show light buffer.
                DebugMode = DeferredGraphicsDebugMode.VisualizeDiffuseLightBuffer
            };
            _graphicsScreen.DrawReticle = true;
            GraphicsService.Screens.Insert(0, _graphicsScreen);

            GameObjectService.Objects.Add(new DeferredGraphicsOptionsObject(Services));

            Services.Register(typeof(DebugRenderer), null, _graphicsScreen.DebugRenderer);
            Services.Register(typeof(IScene), null, _graphicsScreen.Scene);

            // Add gravity and damping to the physics simulation.
            Simulation.ForceEffects.Add(new Gravity());
            Simulation.ForceEffects.Add(new Damping());

            // Add a custom game object which controls the camera.
            var cameraGameObject = new CameraObject(Services);

            GameObjectService.Objects.Add(cameraGameObject);
            _graphicsScreen.ActiveCameraNode = cameraGameObject.CameraNode;

            GameObjectService.Objects.Add(new GrabObject(Services));

            // Create test scene.
            ShadowSample.CreateScene(Services, ContentManager, _graphicsScreen);

            // Create 3 different shadows:
            // The VarianceShadow covers the whole level.
            _vsmShadow = new VarianceShadow
            {
                Prefer16Bit      = false,
                PreferredSize    = 512,
                MinLightDistance = 200,
                MaxDistance      = 200,
                FadeOutRange     = 0,
                ShadowFog        = 0,
                TargetArea       = new Aabb(new Vector3(-100, 0, -100), new Vector3(100, 50, 100))
            };
            _vsmShadow.Filter = new Blur(GraphicsService);
            _vsmShadow.Filter.NumberOfPasses = 1;
            _vsmShadow.Filter.InitializeGaussianBlur(11, 3, false);

            // The CascadedShadow for static objects.
            _staticCascadedShadow = new CascadedShadow
            {
                PreferredSize    = 1024,
                Prefer16Bit      = true,
                Distances        = new Vector4(4, 12, 20, 80),
                MinLightDistance = 200,
            };

            // The CascadedShadow for dynamic objects covering a smaller distance.
            _dynamicCascadedShadow = new CascadedShadow
            {
                PreferredSize    = 1024,
                Prefer16Bit      = true,
                NumberOfCascades = 2,
                Distances        = new Vector4(4, 12, 0, 0),
                MinLightDistance = 200,
            };

            // Get directional light created by the DynamicSkyObject and replace the default
            // shadow with our a CompositeShadow.
            _lightNode        = _graphicsScreen.Scene.GetDescendants().OfType <LightNode>().First(n => n.Shadow is CascadedShadow);
            _lightNode.Shadow = new CompositeShadow
            {
                Shadows =
                {
                    _vsmShadow,
                    _staticCascadedShadow,
                    _dynamicCascadedShadow,
                }
            };

            // We do not want to render the same objects into all 3 shadow maps. We use a custom
            // render callback to render each objects into only one of the shadow maps.
            _graphicsScreen.ShadowMapRenderer.RenderCallback = context =>
            {
                // Query all shadow casters.
                var query = context.Scene.Query <ShadowCasterQuery>(context.CameraNode, context);
                if (query.ShadowCasters.Count == 0)
                {
                    return(false);
                }

                // Get the shadow which is currently being rendered.
                var shadow = context.Object;

                // Create a list of scene nodes for the current shadow.
                var list = _tempList;
                if (shadow == _vsmShadow)
                {
                    // Get the hills and skyscrapers which have been marked with a user flag
                    // in ShadowSample.CreateScene.
                    foreach (var node in query.ShadowCasters)
                    {
                        if (node.UserFlags == 1)
                        {
                            _tempList.Add(node);
                        }
                    }
                }
                else if (shadow == _staticCascadedShadow)
                {
                    // Get all static objects except the hills/skyscrapers.
                    foreach (var node in query.ShadowCasters)
                    {
                        if (node.UserFlags == 0 && node.IsStatic)
                        {
                            _tempList.Add(node);
                        }
                    }
                }
                else if (shadow == _dynamicCascadedShadow)
                {
                    // Get all dynamic objects.
                    foreach (var node in query.ShadowCasters)
                    {
                        if (!node.IsStatic)
                        {
                            _tempList.Add(node);
                        }
                    }
                }
                else
                {
                    // Other shadows of other lights.
                    list = query.ShadowCasters;
                }

                // Render the selected objects into the shadow map.
                _graphicsScreen.MeshRenderer.Render(list, context);

                _tempList.Clear();
                return(true);
            };

            // Register the custom renderers for the VarianceShadow.
            _graphicsScreen.ShadowMapRenderer.Renderers.Add(new VarianceShadowMapRenderer(_graphicsScreen.ShadowMapRenderer.RenderCallback));
            _graphicsScreen.ShadowMaskRenderer.Renderers.Add(new VarianceShadowMaskRenderer(GraphicsService));

            CreateGuiControls();
        }
 private static Viewport GetViewport(CascadedShadow shadow, int split)
 {
     return new Viewport
       {
     X = split * shadow.ShadowMap.Width / shadow.NumberOfCascades,
     Y = 0,
     Width = shadow.ShadowMap.Width / shadow.NumberOfCascades,
     Height = shadow.ShadowMap.Height,
     MinDepth = 0,
     MaxDepth = 1,
       };
 }