I have a WPF 3D scene where I can pan, rotate and zoom using the TrackballDecorator from the 3DTools library. I would like to save the camera settings (transformation) and be able to re-apply them when the application restarts the next time (so the view is restored).
I tried to save each individual value of the Camera:
private void SaveCameraSettings()
{
var d = Properties.Settings.Default;
d.CameraPositionX = camera.Position.X;
d.CameraPositionY = camera.Position.Y;
...
d.Save();
}
This doesn’t work, I guess because those settings are not updated according to the transformations applied to the camera (I always get the initial values set in xaml).
I checked the the Transformation3D class but couldn’t find any way to set its value…
The problem is what values do I need to get from the PerspectiveCamera in order to be able to restore it the way it was when I closed my application the last time. The camera is set to a default position (in Xaml), then a transformation is applied to this camera by the TrackBallDecorator. How can I save this transformation (what values to store)? And how can I re-apply them at a later time?
This is going to be a bit long, so bear with me…
1st, you need to modify the 3DTools library so you can apply a transformation to the
TrackballDecoratoras follow:The
GetXXXTransform3Dmethods are extension methods defined as follow:2nd, you need to declare a
Transformto yourPerspectiveCameraas follow:(the example is taken from Sasha Barber’s Elements3D project which I used to test this)
3rd, since we are going to store each part of the whole transformation in a separate value, you need to create the relevant properties in your settings file, i.e.
CameraScaleX,CameraScaleY,CameraScaleZ,CameraTranslateX,CameraTranslateY,CameraTranslateZ,CameraRotateAxisX,CameraRotateAxisY,CameraRotateAxisZandCameraRotateAngle. All are of typedoubleand are stored in User scope.4th and last step is to actually save and load these settings into the camera using the following code:
Hopefully, I didn’t forget anything. If you need more help or don’t understand something, please don’t hesitate to ask 😉