简体   繁体   English

将动画应用于情节提要中的 ModelVisual3D

[英]Apply animation to ModelVisual3D in storyboard

I have a ModelVisual3D.我有一个 ModelVisual3D。 I would like to animate its position in a Viewport3D.我想在 Viewport3D 中为其位置设置动画。 I have gotten the code to work with a BoxVisual3D (from HelixToolkit), but the code doesn't work for my ModelVisual3D.我已经获得了使用 BoxVisual3D(来自 HelixToolkit)的代码,但该代码不适用于我的 ModelVisual3D。 The ModelVisual3D should translate along the X axis, but instead it just sits still. ModelVisual3D 应该沿 X 轴平移,但它只是静止不动。

I have essentially copied the working code for my BoxVisual3D to my ModelVisual3D.我基本上已经将 BoxVisual3D 的工作代码复制到了 ModelVisual3D 中。 For now, I am just doing the OffsetX property, but later on I will need to have multiple properties on multiple ModelVisual3Ds animated at once, hence the storyboard.目前,我只是在做 OffsetX 属性,但稍后我将需要在多个 ModelVisual3D 上同时设置多个属性,因此是故事板。

this is the code that doesn't work:这是不起作用的代码:

Transform3DGroup modelTransformGroup = (Transform3DGroup)_myModel.Transform;
TranslateTransform3D curTransform = (TranslateTransform3D)modelTransformGroup.Children[1];
var moveX = new DoubleAnimation(0, 1000, TimeSpan.FromSeconds(5));
Storyboard.SetTarget(moveX, curTransform);
Storyboard.SetTargetProperty(moveX, new PropertyPath(TranslateTransform3D.OffsetXProperty));
var sb = new Storyboard();
sb.Children.Add(moveX);
sb.Begin();

Here is some code that works, but won't be sufficient since I need to synchronize many animations at once with the storyboard:这是一些有效的代码,但还不够,因为我需要一次将许多动画与情节提要同步:

Transform3DGroup modelTransformGroup = (Transform3DGroup)_myModel.Transform;
TranslateTransform3D curTransform = (TranslateTransform3D)modelTransformGroup.Children[1];
var moveX = new DoubleAnimation(0, 1000, TimeSpan.FromSeconds(5));
curTransform.BeginAnimation(TranslateTransform3D.OffsetXProperty, moveX);

Thanks for your help!谢谢你的帮助!

Edit: I wanted to add that _myModel.Transform is set as a Transform3DGroup earlier in the code and that the first child is a RotationTransform3D and the second child is a TranslateTransform3D.编辑:我想在代码前面添加 _myModel.Transform 设置为 Transform3DGroup,第一个孩子是 RotationTransform3D,第二个孩子是 TranslateTransform3D。

I found the answer in this post which also pointed to this post .我在这篇文章中找到了答案,它也指向了这篇文章 Turns out you have to register a name for your transformation and set the target name instead of the target.事实证明,您必须为您的转换注册一个名称并设置目标名称而不是目标。 This is because transformations are not part of FrameworkElement.这是因为转换不是 FrameworkElement 的一部分。

Below is my working set of code.下面是我的工作代码集。

Transform3DGroup modelTransformGroup = (Transform3DGroup)_myModel.Transform;
TranslateTransform3D curTransform = (TranslateTransform3D)modelTransformGroup.Children[1];
var moveX = new DoubleAnimation(0, 1000, TimeSpan.FromSeconds(5));
RegisterName("Translate", translate);
Storyboard.SetTargetName(moveX, "Translate");
Storyboard.SetTargetProperty(moveX, new PropertyPath(TranslateTransform3D.OffsetXProperty));
var sb = new Storyboard();
sb.Children.Add(moveX);
sb.Begin(this);

I have tested a couple different model types.我测试了几种不同的模型类型。 It seems this will work for any type of object that inherits Visual3D.这似乎适用于任何类型的继承 Visual3D 的对象。

I am sorry to ask a question here.很抱歉在这里问一个问题。 I really want to know why the codes occur some errors, like: Unable to type into "System. Windows. Media. Media3D. MatrixTransform3D" objects cast to type "System. Windows. Media. Media3D. Transform3DGroup.我真的很想知道为什么代码会出现一些错误,例如:无法键入“System.Windows.Media.Media3D.MatrixTransform3D”对象转换为类型“System.Windows.Media.Media3D.Transform3DGroup。

Transform3DGroup modelTransformGroup = (Transform3DGroup)_myModel.Transform;
TranslateTransform3D curTransform (TranslateTransform3D)modelTransformGroup.Children[1];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM