创建一个椎体三维图:
相机
<Viewport3D.Camera> <PerspectiveCamera Position="-1 2 4" LookDirection="0.4 -0.5 -1"/> </Viewport3D.Camera>
创建椎体图形
<GeometryModel3D.Geometry> <MeshGeometry3D Positions="0,0,0 1,0,0 1,0,1 0,0,1 0.5,1,0.5" TriangleIndices="0 1 2 2 3 0 0 3 4 0 1 4 1 2 4 2 3 4" /> </GeometryModel3D.Geometry> <GeometryModel3D.Material> <DiffuseMaterial> <DiffuseMaterial.Brush> <SolidColorBrush Color="Cyan" Opacity="0.5"/> </DiffuseMaterial.Brush> </DiffuseMaterial> </GeometryModel3D.Material> <GeometryModel3D.BackMaterial> <DiffuseMaterial> <DiffuseMaterial.Brush> <SolidColorBrush Color="Yellow" Opacity="0.5"/> </DiffuseMaterial.Brush> </DiffuseMaterial> </GeometryModel3D.BackMaterial>
设置动画移动参数:
<GeometryModel3D.Geometry> <MeshGeometry3D Positions="0,0,0 1,0,0 1,0,1 0,0,1 0.5,1,0.5" TriangleIndices="0 1 2 2 3 0 0 3 4 0 1 4 1 2 4 2 3 4" /> </GeometryModel3D.Geometry> <GeometryModel3D.Material> <DiffuseMaterial> <DiffuseMaterial.Brush> <SolidColorBrush Color="Cyan" Opacity="0.5"/> </DiffuseMaterial.Brush> </DiffuseMaterial> </GeometryModel3D.Material> <GeometryModel3D.BackMaterial> <DiffuseMaterial> <DiffuseMaterial.Brush> <SolidColorBrush Color="Yellow" Opacity="0.5"/> </DiffuseMaterial.Brush> </DiffuseMaterial> </GeometryModel3D.BackMaterial>
设置灯光投影:
<DirectionalLight Direction="1 0 0" Color="White"/>
最后添加旋转动画触发
<!--add animation play--> <Viewport3D.Triggers> <EventTrigger RoutedEvent="Viewport3D.Loaded"> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetName="myAngleRotation" Storyboard.TargetProperty="Angle" From="0" To="360" Duration="0:0:10" RepeatBehavior="Forever" /> </Storyboard> </BeginStoryboard> </EventTrigger> </Viewport3D.Triggers>
完整代码:
<Window x:Class="Geometry.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <Viewport3D Width="auto" Margin="0"> <Viewport3D.Camera> <PerspectiveCamera Position="-1 2 4" LookDirection="0.4 -0.5 -1"/> </Viewport3D.Camera> <ModelVisual3D> <ModelVisual3D.Content> <Model3DGroup> <GeometryModel3D> <GeometryModel3D.Geometry> <MeshGeometry3D Positions="0,0,0 1,0,0 1,0,1 0,0,1 0.5,1,0.5" TriangleIndices="0 1 2 2 3 0 0 3 4 0 1 4 1 2 4 2 3 4" /> </GeometryModel3D.Geometry> <GeometryModel3D.Material> <DiffuseMaterial> <DiffuseMaterial.Brush> <SolidColorBrush Color="Cyan" Opacity="0.5"/> </DiffuseMaterial.Brush> </DiffuseMaterial> </GeometryModel3D.Material> <GeometryModel3D.BackMaterial> <DiffuseMaterial> <DiffuseMaterial.Brush> <SolidColorBrush Color="Yellow" Opacity="0.5"/> </DiffuseMaterial.Brush> </DiffuseMaterial> </GeometryModel3D.BackMaterial> <GeometryModel3D.Transform> <RotateTransform3D> <RotateTransform3D.Rotation> <AxisAngleRotation3D x:Name="myAngleRotation" Axis="0,1,0" Angle="0"/> </RotateTransform3D.Rotation> </RotateTransform3D> </GeometryModel3D.Transform> </GeometryModel3D> <DirectionalLight Direction="1 0 0" Color="White"/> </Model3DGroup> </ModelVisual3D.Content> </ModelVisual3D> <!--add animation play--> <Viewport3D.Triggers> <EventTrigger RoutedEvent="Viewport3D.Loaded"> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetName="myAngleRotation" Storyboard.TargetProperty="Angle" From="0" To="360" Duration="0:0:10" RepeatBehavior="Forever" /> </Storyboard> </BeginStoryboard> </EventTrigger> </Viewport3D.Triggers> </Viewport3D> </Grid> </Window>
End.
Thanks.