コード例 #1
0
        public override void Update(GameTime gameTime)
        {
            // Move the directional light in a circle.
            float deltaTimeF = (float)gameTime.ElapsedGameTime.TotalSeconds;

            _lightAngle += 0.3f * deltaTimeF;
            var position = Quaternion.CreateRotationY(_lightAngle).Rotate(new Vector3(6, 6, 0));

            // Make the light look at the world space origin.
            var lightTarget  = Vector3.Zero;
            var lookAtMatrix = Matrix.CreateLookAt(position, lightTarget, Vector3.Up);

            // A look-at matrix is the inverse of a normal world or pose matrix.
            _mainDirectionalLightNode.PoseWorld =
                new Pose(lookAtMatrix.Translation, lookAtMatrix.Minor).Inverse;

            // Compute shadow matrix for the new light direction.
            var lightRayDirection = (lightTarget - position);

            _shadowMatrix = ProjectedShadowRenderer.CreateShadowMatrix(
                new Plane(new Vector3(0, 1, 0), 0.01f), new Vector4(-lightRayDirection, 0));

            // Update the scene - this must be called once per frame.
            _scene.Update(gameTime.ElapsedGameTime);

            base.Update(gameTime);
        }
コード例 #2
0
    public ProjectedShadowSample(Microsoft.Xna.Framework.Game game)
    : base(game)
    {
      SampleFramework.IsMouseVisible = false;
      var delegateGraphicsScreen = new DelegateGraphicsScreen(GraphicsService)
      {
        RenderCallback = Render,
      };
      GraphicsService.Screens.Insert(0, delegateGraphicsScreen);

      // Create a new empty scene.
      _scene = new Scene();
      Services.Register(typeof(IScene), null, _scene);

      // Add a custom game object which controls the camera.
      _cameraObject = new CameraObject(Services);
      _cameraObject.ResetPose(new Vector3F(-8, 6, 8), -ConstantsF.PiOver4, -0.4f);
      GameObjectService.Objects.Add(_cameraObject);

      // Add a default light setup (ambient light + 3 directional lights).
      var defaultLightsObject = new DefaultLightsObject(Services);
      GameObjectService.Objects.Add(defaultLightsObject);

      // Get the main directional light.
      _mainDirectionalLightNode = ((LightNode)_scene.GetSceneNode("KeyLight"));

      // Add a ground plane model to the scene graph.
      var grid = ContentManager.Load<ModelNode>("Ground/Ground").Clone();
      grid.ScaleLocal = new Vector3F(0.3f);
      _scene.Children.Add(grid);

      // Add a tank model to the scene graph.
      var tank = ContentManager.Load<ModelNode>("Tank/tank").Clone();
      _scene.Children.Add(tank);

      // Remember the mesh nodes of tank node.
      _tankMeshNodes = tank.GetSubtree().Where(n => n is MeshNode).ToArray();

      // Create the renderers.
      _meshRenderer = new MeshRenderer();

      var spriteFont = UIContentManager.Load<SpriteFont>("UI Themes/BlendBlue/Default");
      _debugRenderer = new DebugRenderer(GraphicsService, spriteFont);

      _projectedShadowRenderer = new ProjectedShadowRenderer(GraphicsService)
      {
        // The plane onto which the shadows are projected. It is positioned a bit above the ground
        // plane to avoid z-fighting.
        ShadowedPlane = new Plane(new Vector3F(0, 1, 0), 0.01f),

        // The shadow color is a transparent black.
        ShadowColor = new Vector4F(0, 0, 0, 0.4f),

        // The light position is set in Update().
        //LightPosition = ...
      };
    }
コード例 #3
0
        public ProjectedShadowSample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            SampleFramework.IsMouseVisible = false;
            var delegateGraphicsScreen = new DelegateGraphicsScreen(GraphicsService)
            {
                RenderCallback = Render,
            };

            GraphicsService.Screens.Insert(0, delegateGraphicsScreen);

            // Create a new empty scene.
            _scene = new Scene();
            Services.Register(typeof(IScene), null, _scene);

            // Add a custom game object which controls the camera.
            _cameraObject = new CameraObject(Services);
            _cameraObject.ResetPose(new Vector3F(-8, 6, 8), -ConstantsF.PiOver4, -0.4f);
            GameObjectService.Objects.Add(_cameraObject);

            // Add a default light setup (ambient light + 3 directional lights).
            var defaultLightsObject = new DefaultLightsObject(Services);

            GameObjectService.Objects.Add(defaultLightsObject);

            // Get the main directional light.
            _mainDirectionalLightNode = ((LightNode)_scene.GetSceneNode("KeyLight"));

            // Add a ground plane model to the scene graph.
            var grid = ContentManager.Load <ModelNode>("Ground/Ground").Clone();

            grid.ScaleLocal = new Vector3F(0.3f);
            _scene.Children.Add(grid);

            // Add a tank model to the scene graph.
            var tank = ContentManager.Load <ModelNode>("Tank/tank").Clone();

            _scene.Children.Add(tank);

            // Remember the mesh nodes of tank node.
            _tankMeshNodes = tank.GetSubtree().Where(n => n is MeshNode).ToArray();

            // Create the renderers.
            _meshRenderer = new MeshRenderer();

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

            _debugRenderer = new DebugRenderer(GraphicsService, spriteFont);

            _projectedShadowRenderer = new ProjectedShadowRenderer(GraphicsService)
            {
                // The plane onto which the shadows are projected. It is positioned a bit above the ground
                // plane to avoid z-fighting.
                ShadowedPlane = new Plane(new Vector3F(0, 1, 0), 0.01f),

                // The shadow color is a transparent black.
                ShadowColor = new Vector4F(0, 0, 0, 0.4f),

                // The light position is set in Update().
                //LightPosition = ...
            };
        }