protected override void OnUnload()
        {
            _cameraObject = null;

            // Detach and dispose the grouping scene node with all children.
            _groupNode.Parent.Children.Remove(_ambientLightNode);
            _groupNode.Dispose(false);

            // Dispose cloud textures.
            if (_enableCloudLayer)
            {
                _cloudMap0.Dispose();
                _cloudMap1.Dispose();
            }

            // Set all references to null.
            _groupNode         = null;
            _milkyWayNode      = null;
            _starfieldNode     = null;
            _sunNode           = null;
            _moonNode          = null;
            _scatteringSkyNode = null;
            _ambientLightNode  = null;
            _sunlightNode      = null;
            _moonlightNode     = null;
            _cloudLayerNode0   = null;
            _cloudLayerNode1   = null;
            _cloudMap0         = null;
            _cloudMap1         = null;
        }
Beispiel #2
0
        protected override void OnUnload()
        {
            // Detach the scene nodes from the scene.
            if (_skyGroupNode.Parent != null)
            {
                _skyGroupNode.Parent.Children.Remove(_skyGroupNode);
            }

            _lightGroupNode.Parent.Children.Remove(_lightGroupNode);

            // Dispose allocated resources.
            _skyGroupNode.Dispose(false);
            _lightGroupNode.Dispose(false);

            if (_enableClouds)
            {
                _cloudLayerNode0.CloudMap.Dispose();
                _cloudLayerNode1.CloudMap.Dispose();
            }

            if (_cacheSky)
            {
                _sceneCaptureNode.RenderToTexture.Texture.Dispose();
                _sceneCaptureNode.Dispose(false);
                _cloudMapRenderer.Dispose();
                _skyRenderer.Dispose();
                _colorEncoder.Dispose();
                _sceneCaptureRenderer.Dispose();
            }

            // Set references to null.
            _cameraObject         = null;
            _skyGroupNode         = null;
            _lightGroupNode       = null;
            _milkyWayNode         = null;
            _starfieldNode        = null;
            _sunNode              = null;
            _moonNode             = null;
            _scatteringSkyNode    = null;
            _ambientLightNode     = null;
            _sunlightNode         = null;
            _moonlightNode        = null;
            _cloudLayerNode0      = null;
            _cloudLayerNode1      = null;
            _sceneCaptureNode     = null;
            SkyboxNode            = null;
            _cloudMapRenderer     = null;
            _skyRenderer          = null;
            _colorEncoder         = null;
            _sceneCaptureRenderer = null;
        }
Beispiel #3
0
        private void InitializeClouds()
        {
            // Add a CloudLayerNode which renders dynamic clouds.
            // This layer represents dense, lit clouds at low altitude.
            var cloudMap0 = new LayeredCloudMap
            {
                Density  = 15,
                Coverage = 0.5f,
                Size     = 2048,
            };

            _cloudLayerNode0 = new CloudLayerNode(cloudMap0)
            {
                SkyCurvature           = 0.9f,
                TextureMatrix          = CreateScale(0.5f),
                ForwardScatterExponent = 10,
                ForwardScatterScale    = 10f,
                ForwardScatterOffset   = 0.3f,
                NumberOfSamples        = 16,
                HorizonFade            = 0.03f,
            };
            // Define the layers which are combined to a single CloudMap.Texture in the CloudMapRenderer.
            var   scale          = CreateScale(0.2f);
            float animationScale = _enableCloudAnimation ? 0.01f : 0.0f;

            cloudMap0.Layers[0] = new CloudMapLayer(null, scale * CreateScale(1), -0.5f, 1, 1 * animationScale);
            cloudMap0.Layers[1] = new CloudMapLayer(null, scale * CreateScale(1.7f), -0.5f, 1f / 2f, 5 * animationScale);
            cloudMap0.Layers[2] = new CloudMapLayer(null, scale * CreateScale(3.97f), -0.5f, 1f / 4f, 13 * animationScale);
            cloudMap0.Layers[3] = new CloudMapLayer(null, scale * CreateScale(8.1f), -0.5f, 1f / 8f, 27 * animationScale);
            cloudMap0.Layers[4] = new CloudMapLayer(null, scale * CreateScale(16, 17), -0.5f, 1f / 16f, 43 * animationScale);
            cloudMap0.Layers[5] = new CloudMapLayer(null, scale * CreateScale(32, 31), -0.5f, 1f / 32f, 77 * animationScale);
            cloudMap0.Layers[6] = new CloudMapLayer(null, scale * CreateScale(64, 67), -0.5f, 1f / 64f, 127 * animationScale);
            cloudMap0.Layers[7] = new CloudMapLayer(null, scale * CreateScale(128, 127), -0.5f, 1f / 64f, 200 * animationScale);
            _skyGroupNode.Children.Add(_cloudLayerNode0);

            // Add a second CloudLayerNode which renders dynamic clouds.
            // This layer represents light clouds at high altitude.
            var cloudMap1 = new LayeredCloudMap
            {
                Density  = 2,
                Coverage = 0.4f,
                Size     = 1024,
                Seed     = 77777,
            };

            _cloudLayerNode1 = new CloudLayerNode(cloudMap1)
            {
                SkyCurvature           = 0.9f,
                TextureMatrix          = CreateScale(0.5f),
                ForwardScatterExponent = 10,
                ForwardScatterScale    = 10f,
                ForwardScatterOffset   = 0f,
                NumberOfSamples        = 0, // No samples to disable lighting calculations.
                HorizonFade            = 0.03f,
                Alpha = 0.5f,               // Make these clouds more transparent.
            };
            scale = CreateScale(0.25f);
            cloudMap1.Layers[0] = new CloudMapLayer(null, scale * CreateScale(1), -0.5f, 1, 0);
            cloudMap1.Layers[1] = new CloudMapLayer(null, scale * CreateScale(1.7f), -0.5f, 1f / 2f, 0);
            cloudMap1.Layers[2] = new CloudMapLayer(null, scale * CreateScale(3.97f), -0.5f, 1f / 4f, 0);
            cloudMap1.Layers[3] = new CloudMapLayer(null, scale * CreateScale(8.1f), -0.5f, 1f / 8f, 0);
            cloudMap1.Layers[4] = new CloudMapLayer(null, scale * CreateScale(16, 17), -0.5f, 1f / 16f, 0);
            cloudMap1.Layers[5] = new CloudMapLayer(null, scale * CreateScale(32, 31), -0.5f, 1f / 32f, 0);
            cloudMap1.Layers[6] = new CloudMapLayer(null, scale * CreateScale(64, 67), -0.5f, 1f / 64f, 0);
            cloudMap1.Layers[7] = new CloudMapLayer(null, scale * CreateScale(128, 127), -0.5f, 1f / 128f, 0);
            _skyGroupNode.Children.Add(_cloudLayerNode1);
        }
Beispiel #4
0
        public LdrSkySample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            SampleFramework.IsMouseVisible = false;
            var delegateGraphicsScreen = new DelegateGraphicsScreen(GraphicsService)
            {
                RenderCallback = Render,
            };

            GraphicsService.Screens.Insert(0, delegateGraphicsScreen);

            // Add a custom game object which controls the camera.
            _cameraObject = new CameraObject(Services);
            GameObjectService.Objects.Add(_cameraObject);


            var spriteFont = UIContentManager.Load <SpriteFont>("UI Themes/BlendBlue/Default");

            _debugRenderer = new DebugRenderer(GraphicsService, spriteFont);

            _cieSkyFilter          = new CieSkyFilter(GraphicsService);
            _cieSkyFilter.Exposure = 5;
            _cieSkyFilter.Strength = 0.9f;

            _gradientSky = new GradientSkyNode();
            //_gradientSky.GroundColor = new Vector4F(0, 0, 1, 1);
            //_gradientSky.ZenithColor = new Vector4F(0, 0, 1, 1);
            //_gradientSky.FrontColor = new Vector4F(0, 0, 1, 1);
            //_gradientSky.BackColor = new Vector4F(0, 0, 1, 1);
            //_gradientSky.FrontZenithShift = 0.3f;
            //_gradientSky.FrontGroundShift = 0.1f;
            //_gradientSky.BackGroundShift = 0.1f;
            _gradientSky.CieSkyStrength = 0;

            _gradientTextureSky                = new GradientTextureSkyNode();
            _gradientTextureSky.TimeOfDay      = _time.TimeOfDay;
            _gradientTextureSky.Color          = new Vector4F(1);
            _gradientTextureSky.FrontTexture   = ContentManager.Load <Texture2D>("Sky/GradientSkyFront");
            _gradientTextureSky.BackTexture    = ContentManager.Load <Texture2D>("Sky/GradientSkyBack");
            _gradientTextureSky.CieSkyStrength = 1;

            _scatteringSky = new ScatteringSkyNode();
            _scatteringSky.SunIntensity *= 2;
            _scatteringSky.BetaMie      *= 2;
            _scatteringSky.GMie          = 0.75f;
            _scatteringSky.ScaleHeight   = _scatteringSky.AtmosphereHeight * 0.25f;

            InitializeStarfield();

            _cloudMapRenderer = new CloudMapRenderer(GraphicsService);
            _skyRenderer      = new SkyRenderer(GraphicsService);

            _milkyWay       = ContentManager.Load <TextureCube>("Sky/MilkyWay");
            _milkyWaySkybox = new SkyboxNode(_milkyWay)
            {
                Color = new Vector3F(0.05f)
            };

            _sun = new SkyObjectNode
            {
                GlowColor0    = new Vector3F(1, 1, 1) * 5,
                GlowExponent0 = 4000,

                //GlowColor1 = new Vector3F(0.4f) * 0.1f,
                //GlowExponent1 = 100
            };

            _moon = new SkyObjectNode
            {
                Texture         = new PackedTexture(ContentManager.Load <Texture2D>("Sky/Moon")),
                SunLight        = new Vector3F(1, 1, 1) * 1,
                AmbientLight    = new Vector3F(0.001f) * 1,
                LightWrap       = 0.1f,
                LightSmoothness = 1,
                AngularDiameter = new Vector2F(MathHelper.ToRadians(5)),

                GlowColor0          = new Vector3F(0.005f * 0),
                GlowCutoffThreshold = 0.001f,
                GlowExponent0       = 100
            };

            var cloudMap = new LayeredCloudMap
            {
                Density  = 10,
                Coverage = 0.5f,
                Size     = 1024,
            };
            var scale = CreateScale(0.2f);

            cloudMap.Layers[0] = new CloudMapLayer(null, scale * CreateScale(1), -0.5f, 1, 0.011f * 0);
            cloudMap.Layers[1] = new CloudMapLayer(null, scale * CreateScale(1.7f), -0.5f, 1f / 2f, 0.017f * 0);
            cloudMap.Layers[2] = new CloudMapLayer(null, scale * CreateScale(3.97f), -0.5f, 1f / 4f, 0.033f * 0);
            cloudMap.Layers[3] = new CloudMapLayer(null, scale * CreateScale(8.1f), -0.5f, 1f / 8f, 0.043f * 0);
            cloudMap.Layers[4] = new CloudMapLayer(null, scale * CreateScale(16, 17), -0.5f, 1f / 16f, 0.051f * 0);
            cloudMap.Layers[5] = new CloudMapLayer(null, scale * CreateScale(32, 31), -0.5f, 1f / 32f, 0.059f * 0);
            cloudMap.Layers[6] = new CloudMapLayer(null, scale * CreateScale(64, 67), -0.5f, 1f / 64f, 0.067f * 0);
            cloudMap.Layers[7] = new CloudMapLayer(null, scale * CreateScale(128, 127), -0.5f, 1f / 128f, 0.081f * 0);
            _cloudLayerNode    = new CloudLayerNode(cloudMap)
            {
                ForwardScatterScale  = 2.5f,
                ForwardScatterOffset = 0.3f,
                TextureMatrix        = CreateScale(0.5f),
                SkyCurvature         = 0.9f,
                NumberOfSamples      = 16,
            };

            _ephemeris = new Ephemeris();
            // Approx. location of Ternberg: Latitude = 48, Longitude = 15, Altitude = 300
            _ephemeris.Latitude  = 0;
            _ephemeris.Longitude = 15;
            _ephemeris.Altitude  = 300;
#if XBOX
            //_time = new DateTime(2013, 5, 1, 17, 17, 0, 0);
            _time = DateTime.Now;
#else
            _time = new DateTimeOffset(2013, 5, 1, 12, 0, 0, 0, TimeSpan.Zero);
            //_time = DateTimeOffset.UtcNow;
#endif
            UpdateEphemeris();

            _milkyWaySkybox.DrawOrder     = 0;
            _starfield.DrawOrder          = 1;
            _sun.DrawOrder                = 2;
            _moon.DrawOrder               = 3;
            _scatteringSky.DrawOrder      = 4;
            _gradientSky.DrawOrder        = 4;
            _gradientTextureSky.DrawOrder = 4;
            _cloudLayerNode.DrawOrder     = 5;

            _skyNodes = new SceneNode[]
            {
                _milkyWaySkybox,
                _starfield,
                _sun,
                _moon,
                _scatteringSky,
                //_gradientSky,
                //_gradientTextureSky,
                _cloudLayerNode,
            };

            var graphicsDevice = GraphicsService.GraphicsDevice;
            _skybox = new SkyboxNode(
                new RenderTargetCube(graphicsDevice, 512, false, SurfaceFormat.Color, DepthFormat.None))
            {
                Encoding = ColorEncoding.Rgbm,
            };

            _hdrFilter = new HdrFilter(GraphicsService)
            {
                MinExposure    = 0.5f,
                MaxExposure    = 2,
                BloomIntensity = 1,
                BloomThreshold = 0.6f,
                AdaptionSpeed  = 100,
            };

            _colorEncoder = new ColorEncoder(GraphicsService)
            {
                SourceEncoding = ColorEncoding.Rgb,
                TargetEncoding = _skybox.Encoding,
            };
        }
        protected override void OnLoad()
        {
            _inputService    = _services.GetInstance <IInputService>();
            _graphicsService = _services.GetInstance <IGraphicsService>();
            _scene           = _services.GetInstance <IScene>();
            var content = _services.GetInstance <ContentManager>();

            var gameObjectService = _services.GetInstance <IGameObjectService>();

            _cameraObject = (CameraObject)gameObjectService.Objects["Camera"];

            // This scene node is used as the parent of all other scene nodes created here.
            _groupNode = new SceneNode
            {
                Name     = "ScatteringSkyGroup",
                Children = new SceneNodeCollection()
            };
            _scene.Children.Add(_groupNode);

            // Add a skybox with milky way cube map.
            _milkyWayNode = new SkyboxNode(content.Load <TextureCube>("Sky/MilkyWay"))
            {
                Color = new Vector3F(MilkyWayLightScale),
            };
            _groupNode.Children.Add(_milkyWayNode);

            // Add a starfield with all visible stars from a star catalog.
            InitializeStarfield();
            _groupNode.Children.Add(_starfieldNode);

            // Add a node which draws a sun disk.
            _sunNode = new SkyObjectNode
            {
                GlowExponent0 = 4000,
            };
            _groupNode.Children.Add(_sunNode);

            // Add a node which draws a moon texture including moon phase.
            _moonNode = new SkyObjectNode
            {
                Texture         = new PackedTexture(content.Load <Texture2D>("Sky/Moon")),
                LightWrap       = 0.1f,
                LightSmoothness = 1,
                AngularDiameter = new Vector2F(MathHelper.ToRadians(5)),

                // Disable glow.
                GlowColor0 = new Vector3F(0),
                GlowColor1 = new Vector3F(0),
            };
            _groupNode.Children.Add(_moonNode);

            // Add a ScatteringSky which renders the sky colors using a physically-based model.
            _scatteringSkyNode = new ScatteringSkyNode
            {
                SunIntensity = 1,

                // Set a base color to get a dark blue instead of a pitch black night.
                BaseHorizonColor = new Vector3F(0.043f, 0.090f, 0.149f) * 0.01f,
                BaseZenithColor  = new Vector3F(0.024f, 0.051f, 0.102f) * 0.01f,
                BaseColorShift   = 0.5f
            };
            _groupNode.Children.Add(_scatteringSkyNode);

            if (_enableCloudLayer)
            {
                // Add a CloudLayerNode which renders dynamic clouds.
                // This layer represents dense, lit clouds at low altitude.
                _cloudMap0 = new LayeredCloudMap
                {
                    Density  = 15,
                    Coverage = 0.5f,
                    Size     = 2048,
                };
                _cloudLayerNode0 = new CloudLayerNode(_cloudMap0)
                {
                    SkyCurvature           = 0.9f,
                    TextureMatrix          = CreateScale(0.5f),
                    ForwardScatterExponent = 10,
                    ForwardScatterScale    = 10f,
                    ForwardScatterOffset   = 0.3f,
                    NumberOfSamples        = 16,
                    HorizonFade            = 0.03f,
                };
                // Define the layers which are combined to a single CloudMap.Texture in the CloudMapRenderer.
                var   scale          = CreateScale(0.2f);
                float animationScale = 0.01f;
                animationScale       = 0; // Uncomment to enable formation and dissolution of clouds.
                _cloudMap0.Layers[0] = new CloudMapLayer(null, scale * CreateScale(1), -0.5f, 1, 1 * animationScale);
                _cloudMap0.Layers[1] = new CloudMapLayer(null, scale * CreateScale(1.7f), -0.5f, 1f / 2f, 5 * animationScale);
                _cloudMap0.Layers[2] = new CloudMapLayer(null, scale * CreateScale(3.97f), -0.5f, 1f / 4f, 13 * animationScale);
                _cloudMap0.Layers[3] = new CloudMapLayer(null, scale * CreateScale(8.1f), -0.5f, 1f / 8f, 27 * animationScale);
                _cloudMap0.Layers[4] = new CloudMapLayer(null, scale * CreateScale(16, 17), -0.5f, 1f / 16f, 43 * animationScale);
                _cloudMap0.Layers[5] = new CloudMapLayer(null, scale * CreateScale(32, 31), -0.5f, 1f / 32f, 77 * animationScale);
                _cloudMap0.Layers[6] = new CloudMapLayer(null, scale * CreateScale(64, 67), -0.5f, 1f / 64f,
                                                         127 * animationScale);
                _cloudMap0.Layers[7] = new CloudMapLayer(null, scale * CreateScale(128, 127), -0.5f, 1f / 64f,
                                                         200 * animationScale);
                _groupNode.Children.Add(_cloudLayerNode0);

                // Add a second CloudLayerNode which renders dynamic clouds.
                // This layer represents light clouds at high altitude.
                _cloudMap1 = new LayeredCloudMap
                {
                    Density  = 2,
                    Coverage = 0.4f,
                    Size     = 1024,
                    Seed     = 77777,
                };
                _cloudLayerNode1 = new CloudLayerNode(_cloudMap1)
                {
                    SkyCurvature           = 0.9f,
                    TextureMatrix          = CreateScale(0.5f),
                    ForwardScatterExponent = 10,
                    ForwardScatterScale    = 10f,
                    ForwardScatterOffset   = 0f,
                    NumberOfSamples        = 0, // No samples to disable lighting calculations.
                    HorizonFade            = 0.03f,
                    Alpha = 0.5f,               // Make these clouds more transparent.
                };
                scale = CreateScale(0.25f);
                _cloudMap1.Layers[0] = new CloudMapLayer(null, scale * CreateScale(1), -0.5f, 1, 0);
                _cloudMap1.Layers[1] = new CloudMapLayer(null, scale * CreateScale(1.7f), -0.5f, 1f / 2f, 0);
                _cloudMap1.Layers[2] = new CloudMapLayer(null, scale * CreateScale(3.97f), -0.5f, 1f / 4f, 0);
                _cloudMap1.Layers[3] = new CloudMapLayer(null, scale * CreateScale(8.1f), -0.5f, 1f / 8f, 0);
                _cloudMap1.Layers[4] = new CloudMapLayer(null, scale * CreateScale(16, 17), -0.5f, 1f / 16f, 0);
                _cloudMap1.Layers[5] = new CloudMapLayer(null, scale * CreateScale(32, 31), -0.5f, 1f / 32f, 0);
                _cloudMap1.Layers[6] = new CloudMapLayer(null, scale * CreateScale(64, 67), -0.5f, 1f / 64f, 0);
                _cloudMap1.Layers[7] = new CloudMapLayer(null, scale * CreateScale(128, 127), -0.5f, 1f / 128f, 0);
                _groupNode.Children.Add(_cloudLayerNode1);
            }

            // The following scene nodes inherit from SkyNode and are rendered by a SkyRenderer
            // in the DeferredLightingScreen. We have to set the DrawOrder to render them from
            // back to front.
            _milkyWayNode.DrawOrder      = 0;
            _starfieldNode.DrawOrder     = 1;
            _sunNode.DrawOrder           = 2;
            _moonNode.DrawOrder          = 3;
            _scatteringSkyNode.DrawOrder = 4;
            if (_enableCloudLayer)
            {
                _cloudLayerNode1.DrawOrder = 5;
                _cloudLayerNode0.DrawOrder = 6;
            }

            // Add an ambient light.
            var ambientLight = new AmbientLight
            {
                HemisphericAttenuation = 1,
            };

            _ambientLightNode = new LightNode(ambientLight)
            {
                Name = "Ambient",
            };
            _groupNode.Children.Add(_ambientLightNode);

            // Add a directional light for the sun.
            _sunlightNode = new LightNode(new DirectionalLight())
            {
                Name     = "Sunlight",
                Priority = 10, // This is the most important light.

                // This light uses Cascaded Shadow Mapping.
                Shadow = new CascadedShadow
                {
                    PreferredSize     = 1024,
                    DepthBiasScale    = new Vector4F(0.99f),
                    DepthBiasOffset   = new Vector4F(0),
                    FadeOutDistance   = 20,
                    MaxDistance       = 30,
                    MinLightDistance  = 100,
                    ShadowFog         = 0.5f,
                    JitterResolution  = 3000,
                    SplitDistribution = 0.7f
                }
            };
            _groupNode.Children.Add(_sunlightNode);

            // Add a directional light for the moonlight.
            _moonlightNode = new LightNode(new DirectionalLight())
            {
                Name     = "Moonlight",
                Priority = 5,
            };
            _groupNode.Children.Add(_moonlightNode);

            // The Ephemeris class computes the positions of the sun and the moon as seen
            // from any place on the earth.
            _ephemeris = new Ephemeris();

            // Seattle, Washington
            _ephemeris.Latitude  = 47;
            _ephemeris.Longitude = 122;
            _ephemeris.Altitude  = 100;

#if XBOX
            _time = DateTime.Now;
#else
            _time = DateTimeOffset.Now;
#endif

            // Update the positions of sky objects and the lights.
            UpdateSky();
        }