Click or drag to resize
AB4D logo

SceneCamera Class

SceneCamera is a camera that looks at the whole 3D scene defined by the Viewport3D.
Inheritance Hierarchy
SystemObject
  System.Windows.ThreadingDispatcherObject
    System.WindowsDependencyObject
      System.Windows.MediaVisual
        System.WindowsUIElement
          System.WindowsFrameworkElement
            Ab3d.CamerasBaseCamera
              Ab3d.CamerasSphericalCamera
                Ab3d.CamerasBaseTargetPositionCamera
                  Ab3d.CamerasBaseTargetRect3DCamera
                    Ab3d.CamerasSceneCamera

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

The SceneCamera type exposes the following members.

Constructors
 NameDescription
Public methodSceneCamera Constructor
Top
Methods
 NameDescription
Protected methodCheckTargetRect3D Checks if the scene size or position is changed
(Overrides BaseTargetRect3DCameraCheckTargetRect3D)
Protected methodForceRefresh Forces a refresh of the camera.
(Overrides BaseTargetRect3DCameraForceRefresh)
Protected methodUpdateCamera Updates the PerspectiveCamera
(Overrides BaseTargetRect3DCameraUpdateCamera(ProjectionCamera))
Top
Remarks

SceneCamera is a camera that looks the whole 3D scene defined by the Viewport3D from the specified angle and distance. SceneCamera is the same as ThirdPersonCamera with its CenterObject set to the whole 3D scene.


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

The distance from the camera to the center point of the scene is defined by Distance and IsDistancePercent properties.

If IsDistancePercent is false (by default) the Distance value is used as a distance from the center point of the scene 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.

Center point is calculated from the Bounds of the whole 3D scene.


When the position or the size of Bounds of the whole 3D scene is changed, the SceneCamera is not automatically updated to affect the changes. 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.

Note  Note
If you know when the objects on the scene are changed (for example if they are 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 on every rendering pass (even if there is no change).


SceneCamera 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 SceneCamera 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 few 3D objects and a SceneCamera that is looking at the scene 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" CenterPosition="30 10 30" 
                                 Size="20 20 20" Material="Blue"/>
            <visuals:SphereVisual3D CenterPosition="-30 10 0" Radius="10"
                                    Material="Red"/>
            <visuals:PyramidVisual3D BottomCenterPosition="0 0 30" Size="20 20 20"
                                     Material="Green"/>
        </Viewport3D>

        <cameras:SceneCamera Heading="30" Attitude="-20" 
                                    Distance="2" IsDistancePercent="True"/>

        <ab3dControls:MouseCameraController/>

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