Delen via


Camera Control

In this sample, we create a plug-in to move the camera to a random location in the world every time a HTML button is pressed.  It demonstrates basic plug-in loading, writing and using a CameraController, and communicating between a plug-in and the hosting web page via Events.

See it in action.

Camera controllers are important because it provides a single and consistent way of updating the camera.  Consistency is important because the camera needs to be in one position throughout an entire frame -- otherwise one part of the code may think the camera is in one place while another thinks it's elsewhere.  This is why you normally read the camera through its Snapshot in the SceneState object, and can only set it via a controller, of which there may only be one active at a time.

Normally you position the camera with two properties, the position and the orientation.  Either can be set in terms of global or local coordinates.  Global essentially means relative to the center of the earth, while local means relative to your current location on the globe.  For position, global coordinates are in terms of Vector3D, local in terms of latitude, longitude, and altitude.  For orientation, you would normally set it globally in terms of lookat and lookup, Vector3Ds, and locally in terms of roll, pitch, and yaw.  See the Basic Math entry for more.

When this plug-in is Activated, it creates a new JumpController and begins listening to an event from CommunicationManager, but otherwise doesn't do anything.  The script on TestPage.htm is then able to raise that event, which causes JumpHandler to be fired.  A new random position is generated and given to the JumpController.  The JumpController is then set to be the current CameraController.  The next time the render thread calls the CameraStep, the JumpController will be called instead of the Default controller.  The JumpController's Activate function is called first, and it indicates that HasArrived
is false, which means it still has work to do.  The MoveCamera is called, and the JumpController is allowed to modify its Camera property.  The CameraStep will use these values to set up the next scene and frame for rendering.  In addition, the JumpController sets HasArrived to true, indicating it has
completed its work.  This causes the system to automatically swap JumpController's Next controller to be the Current one, in this case simply restoring control to the Default.

If we wanted to animate to the location, we would adjust the current position a little at a time over a series of calls to MoveCamera, and only when we are down would we set HasArrived to true.  We could also take over completely, using Bindings to detect user input and making changes to Camera as needed,
and only setting HasArrived when we want to return control to the Default.

Note, at this time we only support one camera and one viewpoint at a time.

Get the code!

Comments

  • Anonymous
    December 25, 2008
    Hi there, I’m working on porting my Location Aware WPF Application to Virtual Earth 3D Managed WinForms