Click or drag to resize
AB4D logo

FirstPersonCamera Class

FirstPersonCamera is a camera that simulates the person's view on the world.
Inheritance Hierarchy
SystemObject
  System.Windows.ThreadingDispatcherObject
    System.WindowsDependencyObject
      System.Windows.MediaVisual
        System.WindowsUIElement
          System.WindowsFrameworkElement
            Ab3d.CamerasBaseCamera
              Ab3d.CamerasSphericalCamera
                Ab3d.CamerasFirstPersonCamera

Namespace: Ab3d.Cameras
Assembly: Ab3d.PowerToys (in Ab3d.PowerToys.dll) Version: 11.2.9104.2045
Syntax
C#
public class FirstPersonCamera : SphericalCamera, 
	IMovableCamera

The FirstPersonCamera type exposes the following members.

Constructors
 NameDescription
Public methodFirstPersonCamera Constructor
Top
Properties
 NameDescription
Public propertyPosition Gets or sets a Point3D that is a position of the camera.
Top
Methods
 NameDescription
Protected methodBaseCreateFrom This method is called from CreateFrom method and actually sets the properties on this camera.
(Overrides SphericalCameraBaseCreateFrom(Camera))
Public methodIsValid Gets a Boolean that specifies if the camera is valid (has TargetViewport3D that has a valid size; has valid values for all properties). When camera is valid the Point3DTo2D(Point3D) returns correct value; when the camera is not valid, the Point3DTo2D method cannot calculate the 2D screen position.
(Overrides SphericalCameraIsValid(Boolean))
Public methodMoveBackward Moves the camera backward (in the opposite as look direction)
Public methodMoveCamera Moves the camera for the moveVector (regardless of the current camera's LookDirection and UpDirection)
Public methodMoveDown Moves the camera down (in the opposite up direction)
Public methodMoveForward Moves the camera forward (in the look direction)
Public methodMoveLeft Moves the camera left (strafe)
Public methodMoveRight Moves the camera right (strafe)
Public methodMoveUp Moves the camera up (in the up direction)
Protected methodStrafeCamera StrafeCamera moves the camera based on the current camera's LookDirection and UpDirection.
Public methodTurnTo(Point3D) Turns the camera to the direction of the specified position
Public methodTurnTo(Vector3D) Turns the camera to the specified direction
Public methodTurnTo(Point3D, Double, FuncDouble, Double) Turns the camera to the direction of the specified position
Public methodTurnTo(Vector3D, Double, FuncDouble, Double) Turns the camera to the specified direction
Protected methodUpdateCamera Updates the PerspectiveCamera
(Overrides SphericalCameraUpdateCamera(ProjectionCamera))
Top
Fields
 NameDescription
Public fieldStatic memberPositionProperty PositionProperty
Top
Remarks

FirstPersonCamera is a camera that simulates the person's view on the world. It is defined by the Position of the person and the direction of the camera - defined by Heading, Attitude and Bank properties.


FirstPersonCamera also has some other properties that are common to all cameras that derive from SphericalCamera class.

The ShowCameraLight and the CameraLight properties can be used to add a lights to the Viewport3D that is positioned at the same position as the camera and is looking at the cameras direction. In the real world this would be a light that is mounted to the camera. This way the 3D objects that are viewed with the camera are always illuminated regardless of the cameras position.

The ShowCameraLight defines when the camera light is shown. The possible values are:

  • Never – Never add additional camera light
  • Auto – (Default value) Show camera light only if there is no other light defined in the Viewport3D controlled by the camera.
  • Always – Always add a camera light

The CameraLight property is by default a DirectionalLight with White color. The value can be set to a new DirectionalLight with some other color. Other types of Lights are not supported - will not change according to the camera's changed.

Because SphericalCamera is derived from BaseCamera, the FirstPersonCamera also contains properties that are defined in BaseCamera and therefore common to all the cameras:

The Viewport3D that is controlled by this camera is defined by IsAutoViewport3DFindingEnabled, TargetViewport3D and TargetViewport3DName properties.

If the Window, Page or UserControl has only one Viewport3D, there is no need to set and of the above properties on the camera. By default the IsAutoViewport3DFindingEnabled is set to true and that means, that the Viewport3D is automatically find from the current UserControl, Page or Window and it is set to a TargetViewport3D property.

But if there are more Viewport3D objects defined, than by default the first Viewport3D will be used by the camera. With setting the TargetViewport3D or TargetViewport3DName it is possible to specify which Viewport3D will be controlled by the camera.

The First person camera also defines methods to simplify the "persons" movement:
MoveForward(Double), MoveBackward(Double), MoveRight(Double), MoveLeft(Double), MoveDown(Double), MoveUp(Double), StrafeCamera(Double, Double, Double) and MoveCamera(Vector3D).

Example

The following xaml creates a Viewport3D with a 3D Box and a FirstPersonCamera that is looking at the Box from (50, 50, 100). Because there is no light defined in the Viewport3D and the default value of the camera's ShowCameraLight property is true, the cameras adds a light that is illuminating the scene.

The sample also defines a MouseCameraController to control the camera with the mouse and a CameraControlPanel that shows some nice buttons that can be also used to control the camera.

XAML
<Page x:Class="PowerToysSamples"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:visuals="clr-namespace:Ab3d.Visuals;assembly=Ab3d.PowerToys"
    xmlns:ab3dControls="clr-namespace:Ab3d.Controls;assembly=Ab3d.PowerToys"  
    xmlns:cameras="clr-namespace:Ab3d.Cameras;assembly=Ab3d.PowerToys">
    <Grid>
        <Viewport3D>
            <visuals:BoxVisual3D Size="20 20 20" Material="Blue"/>
        </Viewport3D>

        <cameras:FirstPersonCamera Position="50 50 100" Heading="45" Attitude="-20"/>

        <ab3dControls:MouseCameraController/>

        <ab3dControls:CameraControlPanel VerticalAlignment="Bottom" HorizontalAlignment="Left"/>
    </Grid>
</Page>
See Also