Exemplo n.º 1
0
        public VolumetricCloudSample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            SampleFramework.IsMouseVisible = false;

            _graphicsScreen             = new DeferredGraphicsScreen(Services);
            _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));
            _dynamicSkyObject = new DynamicSkyObject(Services, false, false, true);
            GameObjectService.Objects.Add(_dynamicSkyObject);
            GameObjectService.Objects.Add(new GroundObject(Services));
            GameObjectService.Objects.Add(new ObjectCreatorObject(Services));
            GameObjectService.Objects.Add(new LavaBallsObject(Services));

            _particleCloud0 = new ParticleSystemNode(CreateParticleCloud(ContentManager))
            {
                PoseLocal = new Pose(new Vector3(-0, 100, -400)),
            };
            ParticleSystemService.ParticleSystems.Add(_particleCloud0.ParticleSystem);
            _graphicsScreen.Scene.Children.Add(_particleCloud0);

            _particleCloud1 = new ParticleSystemNode(CreateParticleCloud(ContentManager))
            {
                PoseLocal = new Pose(new Vector3(-200, 100, -200)),
            };
            ParticleSystemService.ParticleSystems.Add(_particleCloud1.ParticleSystem);
            _graphicsScreen.Scene.Children.Add(_particleCloud1);

            _particleCloud2 = new ParticleSystemNode(CreateParticleCloud(ContentManager))
            {
                PoseLocal = new Pose(new Vector3(400, 400, -400)),
            };
            ParticleSystemService.ParticleSystems.Add(_particleCloud2.ParticleSystem);
            _graphicsScreen.Scene.Children.Add(_particleCloud2);
        }
Exemplo n.º 2
0
    public VolumetricCloudSample(Microsoft.Xna.Framework.Game game)
      : base(game)
    {
      SampleFramework.IsMouseVisible = false;

      _graphicsScreen = new DeferredGraphicsScreen(Services);
      _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));
      _dynamicSkyObject = new DynamicSkyObject(Services, false, false, true);
      GameObjectService.Objects.Add(_dynamicSkyObject);
      GameObjectService.Objects.Add(new GroundObject(Services));
      GameObjectService.Objects.Add(new ObjectCreatorObject(Services));
      GameObjectService.Objects.Add(new LavaBallsObject(Services));

      _particleCloud0 = new ParticleSystemNode(CreateParticleCloud(ContentManager))
      {
        PoseLocal = new Pose(new Vector3F(-0, 100, -400)),
      };
      ParticleSystemService.ParticleSystems.Add(_particleCloud0.ParticleSystem);
      _graphicsScreen.Scene.Children.Add(_particleCloud0);

      _particleCloud1 = new ParticleSystemNode(CreateParticleCloud(ContentManager))
      {
        PoseLocal = new Pose(new Vector3F(-200, 100, -200)),
      };
      ParticleSystemService.ParticleSystems.Add(_particleCloud1.ParticleSystem);
      _graphicsScreen.Scene.Children.Add(_particleCloud1);

      _particleCloud2 = new ParticleSystemNode(CreateParticleCloud(ContentManager))
      {
        PoseLocal = new Pose(new Vector3F(400, 400, -400)),
      };
      ParticleSystemService.ParticleSystems.Add(_particleCloud2.ParticleSystem);
      _graphicsScreen.Scene.Children.Add(_particleCloud2);
    }
Exemplo n.º 3
0
        public OceanSample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            SampleFramework.IsMouseVisible = false;
            _graphicsScreen             = new DeferredGraphicsScreen(Services);
            _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;

            // More standard objects.
            GameObjectService.Objects.Add(new GrabObject(Services));
            GameObjectService.Objects.Add(new ObjectCreatorObject(Services));
            //GameObjectService.Objects.Add(new StaticSkyObject(Services));
            var dynamicSkyObject = new DynamicSkyObject(Services, true, false, true);

            GameObjectService.Objects.Add(dynamicSkyObject);

            // Add an island model.
            GameObjectService.Objects.Add(new StaticObject(Services, "Island/Island", new Vector3F(30), new Pose(new Vector3F(0, 0.75f, 0)), true, true));

            GameObjectService.Objects.Add(new DynamicObject(Services, 1));
            GameObjectService.Objects.Add(new DynamicObject(Services, 2));
            GameObjectService.Objects.Add(new DynamicObject(Services, 5));
            GameObjectService.Objects.Add(new DynamicObject(Services, 6));
            GameObjectService.Objects.Add(new DynamicObject(Services, 7));
            GameObjectService.Objects.Add(new FogObject(Services)
            {
                AttachToCamera = true
            });

            // The LavaBalls class controls all lava ball instances.
            var lavaBalls = new LavaBallsObject(Services);

            GameObjectService.Objects.Add(lavaBalls);

            // Add a few palm trees.
            Random random = new Random(12345);

            for (int i = 0; i < 20; i++)
            {
                Vector3F  position    = new Vector3F(random.NextFloat(-7, 4), 0, random.NextFloat(13, 18));
                Matrix33F orientation = Matrix33F.CreateRotationY(random.NextFloat(0, ConstantsF.TwoPi));
                float     scale       = random.NextFloat(0.8f, 1.2f);
                GameObjectService.Objects.Add(new StaticObject(Services, "PalmTree/palm_tree", scale, new Pose(position, orientation)));
            }

            // Define the appearance of the water.
            var waterOcean = new Water
            {
                SpecularColor = new Vector3F(20f),
                SpecularPower = 500,

                NormalMap0 = null,
                NormalMap1 = null,

                RefractionDistortion = 0.1f,
                ReflectionColor      = new Vector3F(0.2f),
                RefractionColor      = new Vector3F(0.6f),

                // Water is scattered in high waves and this makes the wave crests brighter.
                // ScatterColor defines the intensity of this effect.
                ScatterColor = new Vector3F(0.05f, 0.1f, 0.1f),

                // Foam is automatically rendered where the water intersects geometry and
                // where wave are high.
                FoamMap      = ContentManager.Load <Texture2D>("Water/Foam"),
                FoamMapScale = 5,
                FoamColor    = new Vector3F(1),
                FoamCrestMin = 0.3f,
                FoamCrestMax = 0.8f,

                // Approximate underwater caustics are computed in real-time from the waves.
                CausticsSampleCount = 3,
                CausticsIntensity   = 3,
                CausticsPower       = 100,
            };

            // If we do not specify a shape in the WaterNode constructor, we get an infinite
            // water plane.
            _waterNode = new WaterNode(waterOcean, null)
            {
                PoseWorld        = new Pose(new Vector3F(0, 0.5f, 0)),
                SkyboxReflection = _graphicsScreen.Scene.GetDescendants().OfType <SkyboxNode>().First(),

                // ExtraHeight must be set to a value greater than the max. wave height.
                ExtraHeight = 2,
            };
            _graphicsScreen.Scene.Children.Add(_waterNode);

            // OceanWaves can be set to displace water surface using a displacement map.
            // The displacement map is computed by the WaterWaveRenderer (see DeferredGraphicsScreen)
            // using FFT and a statistical ocean model.
            _waterNode.Waves = new OceanWaves
            {
                TextureSize    = 256,
                HeightScale    = 0.004f,
                Wind           = new Vector3F(10, 0, 10),
                Directionality = 1,
                Choppiness     = 1,
                TileSize       = 20,

                // If we enable CPU queries, we can call OceanWaves.GetDisplacement()
                // (see Update() method below).
                EnableCpuQueries = true,
            };

            // Optional: Use a planar reflection instead of the skybox reflection.
            // We add a PlanarReflectionNode as a child of the WaterNode.
            var renderToTexture = new RenderToTexture
            {
                Texture = new RenderTarget2D(GraphicsService.GraphicsDevice, 512, 512, false, SurfaceFormat.HdrBlendable, DepthFormat.None),
            };
            var planarReflectionNode = new PlanarReflectionNode(renderToTexture)
            {
                Shape       = _waterNode.Shape,
                NormalLocal = new Vector3F(0, 1, 0),
                IsEnabled   = false,
            };

            _waterNode.PlanarReflection = planarReflectionNode;
            _waterNode.Children         = new SceneNodeCollection(1)
            {
                planarReflectionNode
            };

            // To let rigid bodies swim, we add a Buoyancy force effect. This force effect
            // computes buoyancy of a flat water surface.
            Simulation.ForceEffects.Add(new Buoyancy
            {
                Surface     = new Plane(new Vector3F(0, 1, 0), _waterNode.PoseWorld.Position.Y),
                Density     = 1500,
                AngularDrag = 0.3f,
                LinearDrag  = 3,
            });
        }
Exemplo n.º 4
0
        public WaterSample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            SampleFramework.IsMouseVisible = false;
            _graphicsScreen             = new DeferredGraphicsScreen(Services);
            _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;

            // More standard objects.
            GameObjectService.Objects.Add(new GrabObject(Services));
            GameObjectService.Objects.Add(new ObjectCreatorObject(Services));
            //GameObjectService.Objects.Add(new StaticSkyObject(Services));
            var dynamicSkyObject = new DynamicSkyObject(Services, true, false, true);

            GameObjectService.Objects.Add(dynamicSkyObject);

            // Add a ground plane with some detail to see the water refractions.
            Simulation.RigidBodies.Add(new RigidBody(new PlaneShape(new Vector3(0, 1, 0), 0)));
            GameObjectService.Objects.Add(new StaticObject(Services, "Gravel/Gravel", 1, new Pose(new Vector3(0, 0.001f, 0))));

            GameObjectService.Objects.Add(new DudeObject(Services));
            GameObjectService.Objects.Add(new DynamicObject(Services, 1));
            GameObjectService.Objects.Add(new DynamicObject(Services, 2));
            GameObjectService.Objects.Add(new DynamicObject(Services, 5));
            GameObjectService.Objects.Add(new DynamicObject(Services, 6));
            GameObjectService.Objects.Add(new DynamicObject(Services, 7));
            GameObjectService.Objects.Add(new FogObject(Services)
            {
                AttachToCamera = true
            });

            // The LavaBalls class controls all lava ball instances.
            var lavaBalls = new LavaBallsObject(Services);

            GameObjectService.Objects.Add(lavaBalls);

            // Add a few palm trees.
            var random = new Random(12345);

            for (int i = 0; i < 10; i++)
            {
                Vector3 position    = new Vector3(random.NextFloat(-3, -8), 0, random.NextFloat(0, -5));
                Matrix  orientation = Matrix.CreateRotationY(random.NextFloat(0, ConstantsF.TwoPi));
                float   scale       = random.NextFloat(0.5f, 1.2f);
                GameObjectService.Objects.Add(new StaticObject(Services, "PalmTree/palm_tree", scale, new Pose(position, orientation)));
            }

            // Define the appearance of the water.
            var water = new Water
            {
                SpecularColor = new Vector3(10f),

                // Small water ripples/waves are created using scrolling normal maps.
                NormalMap0         = ContentManager.Load <Texture2D>("Water/Wave0"),
                NormalMap1         = ContentManager.Load <Texture2D>("Water/Wave1"),
                NormalMap0Scale    = 1.8f,
                NormalMap1Scale    = 2.2f,
                NormalMap0Velocity = new Vector3(-0.02f, 0, 0.03f),
                NormalMap1Velocity = new Vector3(0.02f, 0, -0.03f),
                NormalMap0Strength = 0.5f,
                NormalMap1Strength = 0.5f,

                ReflectionDistortion = 0.2f,
                ReflectionColor      = new Vector3(0.7f),
                RefractionDistortion = 0.05f,
            };

            // Create a box-shaped body of water.
            // We use a TransformedShape containing a BoxShape because the top of the
            // water body must be at height 0.
            var shape = new TransformedShape(new GeometricObject(
                                                 new BoxShape(10, 1, 20),
                                                 new Pose(new Vector3(0, -0.5f, 0))));

            _waterNode0 = new WaterNode(water, shape)
            {
                PoseWorld              = new Pose(new Vector3(-1, 0.5f, 0), Matrix.CreateRotationY(0.1f)),
                SkyboxReflection       = _graphicsScreen.Scene.GetDescendants().OfType <SkyboxNode>().First(),
                DepthBufferWriteEnable = true,
            };
            _graphicsScreen.Scene.Children.Add(_waterNode0);

            // Optional: Create a WaterFlow to move the water using a flow texture.
            _waterFlow0 = new WaterFlow
            {
                FlowMapSpeed     = 0.5f,
                FlowMap          = GenerateFlowMap(),
                CycleDuration    = 3f,
                NoiseMapStrength = 0.1f,
                NoiseMapScale    = 0.5f,
            };
            _waterNode0.Flow = _waterFlow0;

            // Optional: Use a planar reflection instead of the skybox reflection.
            // We add a PlanarReflectionNode as a child of the WaterNode.
            var renderToTexture = new RenderToTexture
            {
                Texture = new RenderTarget2D(GraphicsService.GraphicsDevice, 512, 512, false, SurfaceFormat.HdrBlendable, DepthFormat.None),
            };
            var planarReflectionNode = new PlanarReflectionNode(renderToTexture)
            {
                // Same shape as WaterNode.
                Shape = _waterNode0.Shape,

                // Reflection plane is horizontal.
                NormalLocal = new Vector3(0, 1, 0),
            };

            _waterNode0.PlanarReflection = planarReflectionNode;
            _waterNode0.Children         = new SceneNodeCollection(1)
            {
                planarReflectionNode
            };

            // Create a short river with an inclined water surface.
            // Using a WaterFlow with a SurfaceSlopeSpeed, the water automatically flows
            // down the inclined surface.
            _waterNode1 = new WaterNode(water, GetSpiralShape())
            {
                PoseWorld = new Pose(new Vector3(10, 1.5f, 0), Matrix.CreateRotationY(0.1f)),
                EnableUnderwaterEffect = false,
                SkyboxReflection       = _graphicsScreen.Scene.GetDescendants().OfType <SkyboxNode>().First(),
                Flow = new WaterFlow
                {
                    SurfaceSlopeSpeed = 0.5f,
                    CycleDuration     = 2f,
                    NoiseMapStrength  = 0.1f,
                    NoiseMapScale     = 1,
                }
            };
            _graphicsScreen.Scene.Children.Add(_waterNode1);
        }
Exemplo n.º 5
0
        public CloudQuadSample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            SampleFramework.IsMouseVisible = false;

            _graphicsScreen             = new DeferredGraphicsScreen(Services);
            _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));
            GameObjectService.Objects.Add(new GroundObject(Services));
            GameObjectService.Objects.Add(new DudeObject(Services));
            GameObjectService.Objects.Add(new ObjectCreatorObject(Services));
            GameObjectService.Objects.Add(new LavaBallsObject(Services));
            GameObjectService.Objects.Add(new FogObject(Services));
            GameObjectService.Objects.Add(new StaticObject(Services, "Barrier/Barrier", 0.9f, new Pose(new Vector3F(0, 0, -2))));
            GameObjectService.Objects.Add(new StaticObject(Services, "Barrier/Cylinder", 0.9f, new Pose(new Vector3F(3, 0, 0), QuaternionF.CreateRotationY(MathHelper.ToRadians(-20)))));

            // The DynamicSkyObject creates the dynamic sky and lights but no clouds.
            var dynamicSkyObject = new DynamicSkyObject(Services, false, false, false);

            GameObjectService.Objects.Add(dynamicSkyObject);

            // Add a few palm trees.
            Random random = new Random(12345);

            for (int i = 0; i < 10; i++)
            {
                Vector3F  position    = new Vector3F(random.NextFloat(-3, -8), 0, random.NextFloat(0, -5));
                Matrix33F orientation = Matrix33F.CreateRotationY(random.NextFloat(0, ConstantsF.TwoPi));
                float     scale       = random.NextFloat(0.5f, 1.2f);
                GameObjectService.Objects.Add(new StaticObject(Services, "PalmTree/palm_tree", scale, new Pose(position, orientation)));
            }

            // The model CloudQuad.fbx consists of a textured quad with a custom effect
            // "Cloud.fx". The effect uses several effect parameters. Constant effect
            // parameters are set in the Cloud.drmat material file.
            // The effect parameters, like "World", "WorldViewProjection", "CameraPosition",
            // are automatically updated by the graphics service. But the effect
            // contains 3 new effect parameters which must be set at runtime:
            // "SunDirection", "SunLight" and "SkyLight".
            // Therefore we add a custom effect interpreter and a custom effect binder
            // which tell the graphics manager what it should do with these parameters.
            // The effect interpreter and binder must be registered before the CloudQuad
            // model is loaded!
            _skyEffectInterpreter = GraphicsService.EffectInterpreters.OfType <SkyEffectInterpreter>().FirstOrDefault();
            if (_skyEffectInterpreter == null)
            {
                _skyEffectInterpreter = new SkyEffectInterpreter();
                GraphicsService.EffectInterpreters.Add(_skyEffectInterpreter);
            }

            _skyEffectBinder = GraphicsService.EffectBinders.OfType <SkyEffectBinder>().FirstOrDefault();
            if (_skyEffectBinder == null)
            {
                _skyEffectBinder = new SkyEffectBinder();
                GraphicsService.EffectBinders.Add(_skyEffectBinder);
            }

            // The effect binder defines several delegates which update the effect parameters
            // using values which are computed by the DynamicSkyObject.
            _skyEffectBinder.DynamicSkyObject = dynamicSkyObject;

            // Add several CloudQuad models in the sky with random scales and poses.
            for (int i = 0; i < 20; i++)
            {
                var scale = new Vector3F(
                    RandomHelper.Random.NextFloat(100, 200),
                    0,
                    RandomHelper.Random.NextFloat(100, 200));

                var position = new Vector3F(
                    RandomHelper.Random.NextFloat(-500, 500),
                    RandomHelper.Random.NextFloat(100, 200),
                    RandomHelper.Random.NextFloat(-500, 500));

                var orientation = Matrix33F.CreateRotationY(RandomHelper.Random.NextFloat(0, ConstantsF.TwoPi));
                GameObjectService.Objects.Add(new StaticObject(Services, "CloudQuad/CloudQuad", scale, new Pose(position, orientation), false, false));
            }
        }
Exemplo n.º 6
0
    public CloudQuadSample(Microsoft.Xna.Framework.Game game)
      : base(game)
    {
      SampleFramework.IsMouseVisible = false;

      _graphicsScreen = new DeferredGraphicsScreen(Services);
      _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));
      GameObjectService.Objects.Add(new GroundObject(Services));
      GameObjectService.Objects.Add(new DudeObject(Services));
      GameObjectService.Objects.Add(new ObjectCreatorObject(Services));
      GameObjectService.Objects.Add(new LavaBallsObject(Services));
      GameObjectService.Objects.Add(new FogObject(Services));
      GameObjectService.Objects.Add(new StaticObject(Services, "Barrier/Barrier", 0.9f, new Pose(new Vector3F(0, 0, -2))));
      GameObjectService.Objects.Add(new StaticObject(Services, "Barrier/Cylinder", 0.9f, new Pose(new Vector3F(3, 0, 0), QuaternionF.CreateRotationY(MathHelper.ToRadians(-20)))));

      // The DynamicSkyObject creates the dynamic sky and lights but no clouds.
      var dynamicSkyObject = new DynamicSkyObject(Services, false, false, false);
      GameObjectService.Objects.Add(dynamicSkyObject);

      // Add a few palm trees.
      Random random = new Random(12345);
      for (int i = 0; i < 10; i++)
      {
        Vector3F position = new Vector3F(random.NextFloat(-3, -8), 0, random.NextFloat(0, -5));
        Matrix33F orientation = Matrix33F.CreateRotationY(random.NextFloat(0, ConstantsF.TwoPi));
        float scale = random.NextFloat(0.5f, 1.2f);
        GameObjectService.Objects.Add(new StaticObject(Services, "PalmTree/palm_tree", scale, new Pose(position, orientation)));
      }

      // The model CloudQuad.fbx consists of a textured quad with a custom effect 
      // "Cloud.fx". The effect uses several effect parameters. Constant effect 
      // parameters are set in the Cloud.drmat material file. 
      // The effect parameters, like "World", "WorldViewProjection", "CameraPosition",
      // are automatically updated by the graphics service. But the effect 
      // contains 3 new effect parameters which must be set at runtime: 
      // "SunDirection", "SunLight" and "SkyLight".
      // Therefore we add a custom effect interpreter and a custom effect binder 
      // which tell the graphics manager what it should do with these parameters.
      // The effect interpreter and binder must be registered before the CloudQuad
      // model is loaded!
      _skyEffectInterpreter = GraphicsService.EffectInterpreters.OfType<SkyEffectInterpreter>().FirstOrDefault();
      if (_skyEffectInterpreter == null)
      {
        _skyEffectInterpreter = new SkyEffectInterpreter();
        GraphicsService.EffectInterpreters.Add(_skyEffectInterpreter);
      }

      _skyEffectBinder = GraphicsService.EffectBinders.OfType<SkyEffectBinder>().FirstOrDefault();
      if (_skyEffectBinder == null)
      {
        _skyEffectBinder = new SkyEffectBinder();
        GraphicsService.EffectBinders.Add(_skyEffectBinder);
      }

      // The effect binder defines several delegates which update the effect parameters
      // using values which are computed by the DynamicSkyObject.
      _skyEffectBinder.DynamicSkyObject = dynamicSkyObject;

      // Add several CloudQuad models in the sky with random scales and poses.
      for (int i = 0; i < 20; i++)
      {
        var scale = new Vector3F(
          RandomHelper.Random.NextFloat(100, 200),
          0,
          RandomHelper.Random.NextFloat(100, 200));

        var position = new Vector3F(
          RandomHelper.Random.NextFloat(-500, 500),
          RandomHelper.Random.NextFloat(100, 200),
          RandomHelper.Random.NextFloat(-500, 500));

        var orientation = Matrix33F.CreateRotationY(RandomHelper.Random.NextFloat(0, ConstantsF.TwoPi));
        GameObjectService.Objects.Add(new StaticObject(Services, "CloudQuad/CloudQuad", scale, new Pose(position, orientation), false, false));
      }
    }
Exemplo n.º 7
0
    public WaterSample(Microsoft.Xna.Framework.Game game)
      : base(game)
    {
      SampleFramework.IsMouseVisible = false;
      _graphicsScreen = new DeferredGraphicsScreen(Services);
      _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;

      // More standard objects.
      GameObjectService.Objects.Add(new GrabObject(Services));
      GameObjectService.Objects.Add(new ObjectCreatorObject(Services));
      //GameObjectService.Objects.Add(new StaticSkyObject(Services));
      var dynamicSkyObject = new DynamicSkyObject(Services, true, false, true);
      GameObjectService.Objects.Add(dynamicSkyObject);

      // Add a ground plane with some detail to see the water refractions.
      Simulation.RigidBodies.Add(new RigidBody(new PlaneShape(new Vector3F(0, 1, 0), 0)));
      GameObjectService.Objects.Add(new StaticObject(Services, "Gravel/Gravel", 1, new Pose(new Vector3F(0, 0.001f, 0))));

      GameObjectService.Objects.Add(new DudeObject(Services));
      GameObjectService.Objects.Add(new DynamicObject(Services, 1));
      GameObjectService.Objects.Add(new DynamicObject(Services, 2));
      GameObjectService.Objects.Add(new DynamicObject(Services, 5));
      GameObjectService.Objects.Add(new DynamicObject(Services, 6));
      GameObjectService.Objects.Add(new DynamicObject(Services, 7));
      GameObjectService.Objects.Add(new FogObject(Services) { AttachToCamera = true });

      // The LavaBalls class controls all lava ball instances.
      var lavaBalls = new LavaBallsObject(Services);
      GameObjectService.Objects.Add(lavaBalls);

      // Add a few palm trees.
      var random = new Random(12345);
      for (int i = 0; i < 10; i++)
      {
        Vector3F position = new Vector3F(random.NextFloat(-3, -8), 0, random.NextFloat(0, -5));
        Matrix33F orientation = Matrix33F.CreateRotationY(random.NextFloat(0, ConstantsF.TwoPi));
        float scale = random.NextFloat(0.5f, 1.2f);
        GameObjectService.Objects.Add(new StaticObject(Services, "PalmTree/palm_tree", scale, new Pose(position, orientation)));
      }

      // Define the appearance of the water.
      var water = new Water
      {
        SpecularColor = new Vector3F(10f),

        // Small water ripples/waves are created using scrolling normal maps.
        NormalMap0 = ContentManager.Load<Texture2D>("Water/Wave0"),
        NormalMap1 = ContentManager.Load<Texture2D>("Water/Wave1"),
        NormalMap0Scale = 1.8f,
        NormalMap1Scale = 2.2f,
        NormalMap0Velocity = new Vector3F(-0.02f, 0, 0.03f),
        NormalMap1Velocity = new Vector3F(0.02f, 0, -0.03f),
        NormalMap0Strength = 0.5f,
        NormalMap1Strength = 0.5f,

        ReflectionDistortion = 0.2f,
        ReflectionColor = new Vector3F(0.7f),
        RefractionDistortion = 0.05f,
      };

      // Create a box-shaped body of water.
      // We use a TransformedShape containing a BoxShape because the top of the 
      // water body must be at height 0.
      var shape = new TransformedShape(new GeometricObject(
        new BoxShape(10, 1, 20),
        new Pose(new Vector3F(0, -0.5f, 0))));
      _waterNode0 = new WaterNode(water, shape)
      {
        PoseWorld = new Pose(new Vector3F(-1, 0.5f, 0), Matrix33F.CreateRotationY(0.1f)),
        SkyboxReflection = _graphicsScreen.Scene.GetDescendants().OfType<SkyboxNode>().First(),
        DepthBufferWriteEnable = true,
      };
      _graphicsScreen.Scene.Children.Add(_waterNode0);

      // Optional: Create a WaterFlow to move the water using a flow texture.
      _waterFlow0 = new WaterFlow
      {
        FlowMapSpeed = 0.5f,
        FlowMap = GenerateFlowMap(),
        CycleDuration = 3f,
        NoiseMapStrength = 0.1f,
        NoiseMapScale = 0.5f,
      };
      _waterNode0.Flow = _waterFlow0;

      // Optional: Use a planar reflection instead of the skybox reflection.
      // We add a PlanarReflectionNode as a child of the WaterNode.
      var renderToTexture = new RenderToTexture
      {
        Texture = new RenderTarget2D(GraphicsService.GraphicsDevice, 512, 512, false, SurfaceFormat.HdrBlendable, DepthFormat.None),
      };
      var planarReflectionNode = new PlanarReflectionNode(renderToTexture)
      {
        // Same shape as WaterNode.
        Shape = _waterNode0.Shape,

        // Reflection plane is horizontal.
        NormalLocal = new Vector3F(0, 1, 0),
      };
      _waterNode0.PlanarReflection = planarReflectionNode;
      _waterNode0.Children = new SceneNodeCollection(1) { planarReflectionNode };

      // Create a short river with an inclined water surface.
      // Using a WaterFlow with a SurfaceSlopeSpeed, the water automatically flows
      // down the inclined surface.
      _waterNode1 = new WaterNode(water, GetSpiralShape())
      {
        PoseWorld = new Pose(new Vector3F(10, 1.5f, 0), Matrix33F.CreateRotationY(0.1f)),
        EnableUnderwaterEffect = false,
        SkyboxReflection = _graphicsScreen.Scene.GetDescendants().OfType<SkyboxNode>().First(),
        Flow = new WaterFlow
        {
          SurfaceSlopeSpeed = 0.5f,
          CycleDuration = 2f,
          NoiseMapStrength = 0.1f,
          NoiseMapScale = 1,
        }
      };
      _graphicsScreen.Scene.Children.Add(_waterNode1);
    }
Exemplo n.º 8
0
    public OceanSample(Microsoft.Xna.Framework.Game game)
      : base(game)
    {
      SampleFramework.IsMouseVisible = false;
      _graphicsScreen = new DeferredGraphicsScreen(Services);
      _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;

      // More standard objects.
      GameObjectService.Objects.Add(new GrabObject(Services));
      GameObjectService.Objects.Add(new ObjectCreatorObject(Services));
      //GameObjectService.Objects.Add(new StaticSkyObject(Services));
      var dynamicSkyObject = new DynamicSkyObject(Services, true, false, true);
      GameObjectService.Objects.Add(dynamicSkyObject);

      // Add an island model.
      GameObjectService.Objects.Add(new StaticObject(Services, "Island/Island", new Vector3F(30), new Pose(new Vector3F(0, 0.75f, 0)), true, true));

      GameObjectService.Objects.Add(new DynamicObject(Services, 1));
      GameObjectService.Objects.Add(new DynamicObject(Services, 2));
      GameObjectService.Objects.Add(new DynamicObject(Services, 5));
      GameObjectService.Objects.Add(new DynamicObject(Services, 6));
      GameObjectService.Objects.Add(new DynamicObject(Services, 7));
      GameObjectService.Objects.Add(new FogObject(Services) { AttachToCamera = true });

      // The LavaBalls class controls all lava ball instances.
      var lavaBalls = new LavaBallsObject(Services);
      GameObjectService.Objects.Add(lavaBalls);

      // Add a few palm trees.
      Random random = new Random(12345);
      for (int i = 0; i < 20; i++)
      {
        Vector3F position = new Vector3F(random.NextFloat(-7, 4), 0, random.NextFloat(13, 18));
        Matrix33F orientation = Matrix33F.CreateRotationY(random.NextFloat(0, ConstantsF.TwoPi));
        float scale = random.NextFloat(0.8f, 1.2f);
        GameObjectService.Objects.Add(new StaticObject(Services, "PalmTree/palm_tree", scale, new Pose(position, orientation)));
      }

      // Define the appearance of the water.
      var waterOcean = new Water
      {
        SpecularColor = new Vector3F(20f),
        SpecularPower = 500,

        NormalMap0 = null,
        NormalMap1 = null,

        RefractionDistortion = 0.1f,
        ReflectionColor = new Vector3F(0.2f),
        RefractionColor = new Vector3F(0.6f),

        // Water is scattered in high waves and this makes the wave crests brighter.
        // ScatterColor defines the intensity of this effect.
        ScatterColor = new Vector3F(0.05f, 0.1f, 0.1f),

        // Foam is automatically rendered where the water intersects geometry and
        // where wave are high.
        FoamMap = ContentManager.Load<Texture2D>("Water/Foam"),
        FoamMapScale = 5,
        FoamColor = new Vector3F(1),
        FoamCrestMin = 0.3f,
        FoamCrestMax = 0.8f,

        // Approximate underwater caustics are computed in real-time from the waves.
        CausticsSampleCount = 3,
        CausticsIntensity = 3,
        CausticsPower = 100,
      };

      // If we do not specify a shape in the WaterNode constructor, we get an infinite
      // water plane.
      _waterNode = new WaterNode(waterOcean, null)
      {
        PoseWorld = new Pose(new Vector3F(0, 0.5f, 0)),
        SkyboxReflection = _graphicsScreen.Scene.GetDescendants().OfType<SkyboxNode>().First(),

        // ExtraHeight must be set to a value greater than the max. wave height. 
        ExtraHeight = 2,
      };
      _graphicsScreen.Scene.Children.Add(_waterNode);

      // OceanWaves can be set to displace water surface using a displacement map.
      // The displacement map is computed by the WaterWaveRenderer (see DeferredGraphicsScreen)
      // using FFT and a statistical ocean model.
      _waterNode.Waves = new OceanWaves
      {
        TextureSize = 256,
        HeightScale = 0.004f,
        Wind = new Vector3F(10, 0, 10),
        Directionality = 1,
        Choppiness = 1,
        TileSize = 20,

        // If we enable CPU queries, we can call OceanWaves.GetDisplacement()
        // (see Update() method below).
        EnableCpuQueries = true,
      };

      // Optional: Use a planar reflection instead of the skybox reflection.
      // We add a PlanarReflectionNode as a child of the WaterNode.
      var renderToTexture = new RenderToTexture
      {
        Texture = new RenderTarget2D(GraphicsService.GraphicsDevice, 512, 512, false, SurfaceFormat.HdrBlendable, DepthFormat.None),
      };
      var planarReflectionNode = new PlanarReflectionNode(renderToTexture)
      {
        Shape = _waterNode.Shape,
        NormalLocal = new Vector3F(0, 1, 0),
        IsEnabled = false,
      };
      _waterNode.PlanarReflection = planarReflectionNode;
      _waterNode.Children = new SceneNodeCollection(1) { planarReflectionNode };

      // To let rigid bodies swim, we add a Buoyancy force effect. This force effect
      // computes buoyancy of a flat water surface.
      Simulation.ForceEffects.Add(new Buoyancy
      {
        Surface = new Plane(new Vector3F(0, 1, 0), _waterNode.PoseWorld.Position.Y),
        Density = 1500,
        AngularDrag = 0.3f,
        LinearDrag = 3,
      });
    }