Richard210363
Richard210363

Reputation: 8406

How do I add a BeginAnimation to a WPF Viewport3D?

I'm trying to rotate an image in a Viewport3D but I can't see which object to apply the animation to.

This works in code

DoubleAnimation da = new DoubleAnimation();
da.From = 0;
da.To = 100;
da.Duration = new Duration(TimeSpan.FromSeconds(1));
Button1.BeginAnimation(Button.HeightProperty, da);

and This also works

RotateTransform3D myRotateTransform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(1, 0, 0), 30));
Model.Transform = myRotateTransform;

So why can't I do this?

Rotation3DAnimation r3D = new Rotation3DAnimation();
r3D.From = new AxisAngleRotation3D(new Vector3D(1, 0, 0), 0);
r3D.To = new AxisAngleRotation3D(new Vector3D(1, 0, 0), 45);
r3D.Duration = new Duration(TimeSpan.FromSeconds(10));
Model.BeginAnimation(ModelVisual3D.TransformProperty, r3D, HandoffBehavior.SnapshotAndReplace);

I get an error on the Model.BeginAnimation line. I've tried a number of objects instead of ModelVisual3D but I get different errors each time. What object type should I be using?

Thanks for any help Richard

XAML

<ModelVisual3D x:Name="ModelContainer">
    <ModelVisual3D.Content>
        <GeometryModel3D x:Name="Model" d:Bounds="-37.2388059701493,-10,0,74.4776119402985,20,0">
            <GeometryModel3D.Geometry>
                <MeshGeometry3D Normals="0,0,1 ..." />                      
            </GeometryModel3D.Geometry>

            <GeometryModel3D.Material>
                <DiffuseMaterial>
                    <DiffuseMaterial.Brush>
                        <ImageBrush ImageSource="pack://siteoforigin:,,,/Staff-4.tif" Stretch="Fill"/>
                    </DiffuseMaterial.Brush>
                </DiffuseMaterial>
            </GeometryModel3D.Material>
        </GeometryModel3D>
    </ModelVisual3D.Content>

    <ModelVisual3D.Transform>
        <Transform3DGroup>
            <RotateTransform3D>
                <RotateTransform3D.Rotation>
                    <AxisAngleRotation3D Axis="1,0,0" Angle="0"/>
                </RotateTransform3D.Rotation>
            </RotateTransform3D>
        </Transform3DGroup>
    </ModelVisual3D.Transform>

</ModelVisual3D>

<ModelVisual3D x:Name="AmbientContainer">
    <ModelVisual3D.Content>
        <AmbientLight x:Name="Ambient" Color="Gray"/>
    </ModelVisual3D.Content>
</ModelVisual3D>

<ModelVisual3D x:Name="DirectionalContainer">
    <ModelVisual3D.Content>
        <DirectionalLight x:Name="Directional" Color="#FF7F7F7F" Direction="0,0,-1">
            <DirectionalLight.Transform>
                <TranslateTransform3D OffsetZ="3" OffsetX="0" OffsetY="0"/>
            </DirectionalLight.Transform>
        </DirectionalLight>
    </ModelVisual3D.Content>
</ModelVisual3D>

Upvotes: 1

Views: 1287

Answers (1)

Richard210363
Richard210363

Reputation: 8406

The answer is that you can't use the AxisAngleRotation3D in an animation. You have to break apart it's 2 components of vector and angle and animate them independently.

Upvotes: 1

Related Questions