WPF 3D - 如何保存和加载相机视图?
我有一个 WPF 3D 场景,可以使用 3DTools 中的 TrackballDecorator
进行平移、旋转和缩放库。我想保存相机设置(转换),并能够在应用程序下次重新启动时重新应用它们(以便恢复视图)。
我尝试保存 Camera
的每个单独值:
private void SaveCameraSettings()
{
var d = Properties.Settings.Default;
d.CameraPositionX = camera.Position.X;
d.CameraPositionY = camera.Position.Y;
...
d.Save();
}
我猜这是行不通的,因为这些设置不会根据应用于相机的转换进行更新(我总是获取 xaml 中设置的初始值) )。
我检查了 Transformation3D 类,但找不到任何方法来设置其值...
问题是我需要从 PerspectiveCamera 获取什么值,以便能够将其恢复为关闭应用程序时的状态最后一次。将相机设置为默认位置(在 Xaml 中),然后由 TrackBallDecorator 将变换应用到该相机。如何保存此转换(要存储哪些值)?我以后如何重新应用它们?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这会有点长,所以请耐心等待...
第一,您需要修改 3DTools 库,以便可以将转换应用到
TrackballDecorator
,如下所示:GetXXXTransform3D
方法是扩展方法,定义如下:第二,您需要为您的
PerspectiveCamera
声明一个Transform
如下:(该示例取自 Sasha Barber 的 Elements3D 我用来测试这个的项目)
第三,因为我们要将整个转换的每个部分存储在一个单独的值中,所以您需要在设置文件中创建相关属性,即
CameraScaleX
、CameraScaleY
、CameraScaleZ
、CameraTranslateX
、CameraTranslateY
、CameraTranslateZ
>、CameraRotateAxisX
、CameraRotateAxisY
、CameraRotateAxisZ
和CameraRotateAngle
。所有的都是double
类型并且存储在用户范围内。第四步也是最后一步是使用以下代码实际保存这些设置并将其加载到相机中:
希望我没有忘记任何事情。如果您需要更多帮助或有不明白的地方,请随时询问;-)
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
TrackballDecorator
as follow:The
GetXXXTransform3D
methods are extension methods defined as follow:2nd, you need to declare a
Transform
to yourPerspectiveCamera
as 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
,CameraRotateAxisZ
andCameraRotateAngle
. All are of typedouble
and 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 ;-)
您将需要相机视图矩阵数据和投影矩阵数据。视图矩阵将包含有关相机的位置、旋转、缩放和平移的数据,投影矩阵将包含视场、近平面、远平面等数据。
抱歉,我无法帮助导出/导入该数据,因为我没有使用过 WPF,但如果它使用与 as3 内置 Matrix 类有关的任何内容,则可能会暴露原始数据属性,这通常是一个 as3 Vector。对象将矩阵 16 值公开为行排序浮点值。
You would need both the cameras view matrix data and the projection matrix data. The view matrix will contain the data about the position, rotation, scale and translation of the camera and the projection matrix will contain things like the field of view, near plane, far plane and other data.
Sorry I cant help with exporting/importing that data since I've not used WPF, but there might be rawdata properties exposed if it uses anything to do with as3's built in Matrix classes, this is usualy a as3 Vector. Object the the matrices 16 values exposed as row ordered floating point values.
我相信你需要的是Position、LookDirection、UpDirection、FieldOfView、NearPlaneDistance、FarPlaneDistance。所有上述属性都定义了相机。
I believe what you need is Position, LookDirection, UpDirection, FieldOfView, NearPlaneDistance, FarPlaneDistance. All the above properties define the camera.