我想做一个能从串口里面读取传感器传入的现实单摆的角度,然后能在WPF上模拟的3D动画,目前已经做出了3D模型和简单动画,但是没法动态修改AxisAngleRotation3D里的Angle值,它只能简单地从From到To……求高人帮忙改改。程序如下~假定我在C#里,传入的角度值是nowangle,目的就是想,让AxisAngleRotation3D的Angle值一直和nowangle保持一致,该怎么写呢?C#代码:public partial class MainWindow : Window
{
public double nowangle;
public Storyboard sb1 = new Storyboard();
public DoubleAnimation da1 = new DoubleAnimation();
public MainWindow()
{
this.InitializeComponent();
getanimation();
sb1.Begin();
}
private void getanimation() {
#region
da1.SetValue(Storyboard.TargetNameProperty, "rotation");
da1.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath(AxisAngleRotation3D.AngleProperty));
da1.From = rotation.Angle;
da1.To += nowangle;
//da1.Duration = TimeSpan.FromSeconds(0.5);
//da1.AutoReverse = true;
//da1.RepeatBehavior = RepeatBehavior.Forever;
sb1.Children.Add(da1);
this.Resources.Add("Storyboard", sb1);
#endregion
}
}
XAML: <Grid>
<Viewport3D ClipToBounds="False" Margin="56,0,62,13">
<Viewport3D.Camera>
<PerspectiveCamera FieldOfView="60" LookDirection="0,0,-230" Position="0,-80,230" UpDirection="0,1,0"/>
</Viewport3D.Camera>
<ModelVisual3D x:Name="World">
<ModelVisual3D.Transform>
<RotateTransform3D>
<RotateTransform3D.Rotation>
<AxisAngleRotation3D
x:Name="rotation"
Axis="0,0,1"
Angle="0"
/>
</RotateTransform3D.Rotation>
</RotateTransform3D>
</ModelVisual3D.Transform>
<ModelVisual3D x:Name="DirectionalLightContainer">
<ModelVisual3D.Content>
<DirectionalLight x:Name="DirectionalLight" Color="White" Direction="-120,-150,-230">
</DirectionalLight>
</ModelVisual3D.Content>
</ModelVisual3D>
<ModelVisual3D x:Name="RootGeometryContainer">
<ModelVisual3D x:Name="Sphere01">
<ModelVisual3D.Content>
<GeometryModel3D x:Name="DefaultMaterial" d:Bounds="-10,-110,-10,20,20,20">
<GeometryModel3D.Geometry>
<MeshGeometry3D Normals="..." TriangleIndices="..."/>
</GeometryModel3D.Geometry>
<GeometryModel3D.Material>
<DiffuseMaterial Brush="Red"/>
</GeometryModel3D.Material>
</GeometryModel3D>
</ModelVisual3D.Content>
</ModelVisual3D>
<ModelVisual3D x:Name="Cylinder01">
<ModelVisual3D.Content>
<GeometryModel3D x:Name="DefaultMaterial1" d:Bounds="-0.5,-100,-0.49210000038147,1,100,0.984800010919571">
<GeometryModel3D.Geometry>
<MeshGeometry3D Normals="..." TriangleIndices="..."/>
</GeometryModel3D.Geometry>
<GeometryModel3D.Material>
<DiffuseMaterial Brush="Brown"/>
</GeometryModel3D.Material>
</GeometryModel3D>
</ModelVisual3D.Content>
</ModelVisual3D>
</ModelVisual3D>
</ModelVisual3D>
</Viewport3D>
</Grid>
{
public double nowangle;
public Storyboard sb1 = new Storyboard();
public DoubleAnimation da1 = new DoubleAnimation();
public MainWindow()
{
this.InitializeComponent();
getanimation();
sb1.Begin();
}
private void getanimation() {
#region
da1.SetValue(Storyboard.TargetNameProperty, "rotation");
da1.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath(AxisAngleRotation3D.AngleProperty));
da1.From = rotation.Angle;
da1.To += nowangle;
//da1.Duration = TimeSpan.FromSeconds(0.5);
//da1.AutoReverse = true;
//da1.RepeatBehavior = RepeatBehavior.Forever;
sb1.Children.Add(da1);
this.Resources.Add("Storyboard", sb1);
#endregion
}
}
XAML: <Grid>
<Viewport3D ClipToBounds="False" Margin="56,0,62,13">
<Viewport3D.Camera>
<PerspectiveCamera FieldOfView="60" LookDirection="0,0,-230" Position="0,-80,230" UpDirection="0,1,0"/>
</Viewport3D.Camera>
<ModelVisual3D x:Name="World">
<ModelVisual3D.Transform>
<RotateTransform3D>
<RotateTransform3D.Rotation>
<AxisAngleRotation3D
x:Name="rotation"
Axis="0,0,1"
Angle="0"
/>
</RotateTransform3D.Rotation>
</RotateTransform3D>
</ModelVisual3D.Transform>
<ModelVisual3D x:Name="DirectionalLightContainer">
<ModelVisual3D.Content>
<DirectionalLight x:Name="DirectionalLight" Color="White" Direction="-120,-150,-230">
</DirectionalLight>
</ModelVisual3D.Content>
</ModelVisual3D>
<ModelVisual3D x:Name="RootGeometryContainer">
<ModelVisual3D x:Name="Sphere01">
<ModelVisual3D.Content>
<GeometryModel3D x:Name="DefaultMaterial" d:Bounds="-10,-110,-10,20,20,20">
<GeometryModel3D.Geometry>
<MeshGeometry3D Normals="..." TriangleIndices="..."/>
</GeometryModel3D.Geometry>
<GeometryModel3D.Material>
<DiffuseMaterial Brush="Red"/>
</GeometryModel3D.Material>
</GeometryModel3D>
</ModelVisual3D.Content>
</ModelVisual3D>
<ModelVisual3D x:Name="Cylinder01">
<ModelVisual3D.Content>
<GeometryModel3D x:Name="DefaultMaterial1" d:Bounds="-0.5,-100,-0.49210000038147,1,100,0.984800010919571">
<GeometryModel3D.Geometry>
<MeshGeometry3D Normals="..." TriangleIndices="..."/>
</GeometryModel3D.Geometry>
<GeometryModel3D.Material>
<DiffuseMaterial Brush="Brown"/>
</GeometryModel3D.Material>
</GeometryModel3D>
</ModelVisual3D.Content>
</ModelVisual3D>
</ModelVisual3D>
</ModelVisual3D>
</Viewport3D>
</Grid>
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货