Click or drag to resize
AB4D logo

ThirdPersonCamera Class

ThirdPersonCamera is a camera that looks at the CenterObject from the specified angle and distance.
Inheritance Hierarchy
SystemObject
  System.Windows.ThreadingDispatcherObject
    System.WindowsDependencyObject
      System.Windows.MediaVisual
        System.WindowsUIElement
          System.WindowsFrameworkElement
            Ab3d.CamerasBaseCamera
              Ab3d.CamerasSphericalCamera
                Ab3d.CamerasBaseTargetPositionCamera
                  Ab3d.CamerasBaseTargetRect3DCamera
                    Ab3d.CamerasThirdPersonCamera

Namespace: Ab3d.Cameras
Assembly: Ab3d.PowerToys (in Ab3d.PowerToys.dll) Version: 11.2.9104.2045
Syntax
C#
public class ThirdPersonCamera : BaseTargetRect3DCamera

The ThirdPersonCamera type exposes the following members.

Constructors
 NameDescription
Public methodThirdPersonCamera Constructor
Top
Properties
 NameDescription
Public propertyCode exampleCenterObject Gets or sets an object that specifies the 3D object the camera is looking at. CenterObject can be Model3D, Visual3D or UIElement3D.
Top
Methods
 NameDescription
Protected methodStatic memberCenterObjectPropertyChanged CenterObjectPropertyChanged
Protected methodCheckTargetRect3D Checks if TargetRect was changed - in this case update the camera
(Overrides BaseTargetRect3DCameraCheckTargetRect3D)
Protected methodForceRefresh Forces a refresh of the camera.
(Overrides BaseTargetRect3DCameraForceRefresh)
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 BaseTargetPositionCameraIsValid(Boolean))
Protected methodUpdateCamera Updates the PerspectiveCamera
(Overrides BaseTargetRect3DCameraUpdateCamera(ProjectionCamera))
Top
Fields
 NameDescription
Public fieldStatic memberCenterObjectProperty CenterObjectProperty
Top
Remarks

ThirdPersonCamera is a camera that looks at the CenterObject from the specified angle and distance. When the CenterObject is the root 3D object, the camera is looking at the whole 3D scene and is the same as if a SceneCamera would be used.


The angle is specified by Heading, Attitude and Bank properties.

The distance from the camera to the CenterObject is defined by Distance and IsDistancePercent properties.

If IsDistancePercent is false (by default) the Distance value is used as a distance from the CenterObject to the camera.

If IsDistancePercent is true, than the Distance value is specified in percent. For example if Distance is 3.0, that means that the distance of the camera from the center of the CenterObject will be 3 times the size of the CenterObject's (diagonal length). This can be very useful when you just want to show the 3D object regardless in which size the object is defined.


When the position or the size of CenterObject is changed, the ThirdPersonCamera is not automatically updated to follow the CenterObject. To manually update the camera, call the Refresh method. Another possibility is to enable automatic camera updating. This can be done with setting the IsDynamicTarget to true. This will subscribe the camera to CompositionTarget.Rendering event and will constantly monitor the position and size of the CenterObject and move the camera according to the change.

If you know when the CenterObject is changed (for example if it is changed manually from the code), it is recommended that the Refresh method is used instead of setting IsDynamicTarget to true. This way the check for the change will be done only after the change and not all the times (even if there were no change).


ThirdPersonCamera 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 ThirdPersonCamera 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.

Example

The following xaml creates a Viewport3D with a 3D Box and a ThirdPersonCamera that is looking at the Box from from left and from above. The Distance is defined as percent. 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 x:Name="MyBoxModel" Size="60 40 20" Material="Blue"/>
        </Viewport3D>

        <cameras:ThirdPersonCamera Heading="30" Attitude="-20" 
                                   Distance="2" IsDistancePercent="True"
                                   CenterObject="{Binding ElementName=MyBoxModel}"/>

        <ab3dControls:MouseCameraController/>

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