1

如何使 3d 立方体在 y 轴上连续旋转,在 wpf 中保持 x 和 z 轴不变?

4

1 回答 1

0

您可以在 a 中使用此代码UserControl

 <Viewport3D x:Name="CubeLogo"
            Height="400" Width="400" OpacityMask="#FF000000">
    <Viewport3D.Camera>
        <PerspectiveCamera x:Name="Camara"
                           FarPlaneDistance="20"
                           NearPlaneDistance="1"
                           LookDirection="0,-0.65,-1"
                           UpDirection="0.220607433377356,0.678217480739237,-0.440841362480504"
                           Position="0,1.6,2.6" FieldOfView="40" />
    </Viewport3D.Camera>

    <Viewport3D.Triggers>
        <EventTrigger RoutedEvent="FrameworkElement.Loaded">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation From="0" To="360" Duration="0:0:6"
                            Storyboard.TargetName="LogoRotation3D"
                            Storyboard.TargetProperty="Angle" RepeatBehavior="Forever" x:Name="Animacion"/>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Viewport3D.Triggers>

    <ModelVisual3D>
        <ModelVisual3D.Content>
            <Model3DGroup>
                <AmbientLight Color="White"/>

                <GeometryModel3D ScrollViewer.IsDeferredScrollingEnabled="False">
                    <GeometryModel3D.Geometry>
                                <MeshGeometry3D TriangleIndices="0,1,2 3,4,5 6,7,8 9,10,11 12,13,14 15,16,17 18,19,20 21,22,23 24,25,26 27,28,29 30,31,32 33,34,35 "
                                        Normals="0,0,-1 0,0,-1 0,0,-1 0,0,-1 0,0,-1 0,0,-1 0,0,1 0,0,1 0,0,1 0,0,1 0,0,1 0,0,1 0,-1,0 0,-1,0 0,-1,0 0,-1,0 0,-1,0 0,-1,0 1,0,0 1,0,0 1,0,0 1,0,0 1,0,0 1,0,0 0,1,0 0,1,0 0,1,0 0,1,0 0,1,0 0,1,0 -1,0,0 -1,0,0 -1,0,0 -1,0,0 -1,0,0 -1,0,0"
                                        TextureCoordinates="1,1 1,0 0,0 0,0 0,1 1,1 0,1 1,1 1,0 1,0 0,0 0,1 0,1 1,1 1,0 1,0 0,0 0,1 1,1 1,0 0,0 0,0 0,1 1,1 1,0 0,0 0,1 0,1 1,1 1,0 0,0 0,1 1,1 1,1 1,0 0,0"
                                        Positions="-0.5,-0.5,-0.5 -0.5,0.5,-0.5 0.5,0.5,-0.5 0.5,0.5,-0.5 0.5,-0.5,-0.5 -0.5,-0.5,-0.5 -0.5,-0.5,0.5 0.5,-0.5,0.5 0.5,0.5,0.5 0.5,0.5,0.5 -0.5,0.5,0.5 -0.5,-0.5,0.5 -0.5,-0.5,-0.5 0.5,-0.5,-0.5 0.5,-0.5,0.5 0.5,-0.5,0.5 -0.5,-0.5,0.5 -0.5,-0.5,-0.5 0.5,-0.5,-0.5 0.5,0.5,-0.5 0.5,0.5,0.5 0.5,0.5,0.5 0.5,-0.5,0.5 0.5,-0.5,-0.5 0.5,0.5,-0.5 -0.5,0.5,-0.5 -0.5,0.5,0.5 -0.5,0.5,0.5 0.5,0.5,0.5 0.5,0.5,-0.5 -0.5,0.5,-0.5 -0.5,-0.5,-0.5 -0.5,-0.5,0.5 -0.5,-0.5,0.5 -0.5,0.5,0.5 -0.5,0.5,-0.5"/>
                    </GeometryModel3D.Geometry>

                    <GeometryModel3D.Transform>
                        <RotateTransform3D>
                            <RotateTransform3D.Rotation>
                                <AxisAngleRotation3D x:Name="LogoRotation3D"
                                                     Angle="35"
                                                     Axis="1 1 1"/>
                            </RotateTransform3D.Rotation>
                        </RotateTransform3D>
                    </GeometryModel3D.Transform>

                    <!-- Background color of the cube -->
                    <GeometryModel3D.BackMaterial>
                        <DiffuseMaterial Brush="WhiteSmoke"/>
                    </GeometryModel3D.BackMaterial>

                    <!-- Content of the cube -->
                    <GeometryModel3D.Material>
                        <DiffuseMaterial>
                            <DiffuseMaterial.Brush>
                                <VisualBrush>
                                    <VisualBrush.Visual>
                                        <Image x:Name="Logo" />
                                    </VisualBrush.Visual>
                                </VisualBrush>
                            </DiffuseMaterial.Brush>
                        </DiffuseMaterial>
                    </GeometryModel3D.Material>
                </GeometryModel3D>
            </Model3DGroup>

        </ModelVisual3D.Content>
    </ModelVisual3D>

</Viewport3D>

然后在另一个中这样称呼它XAML

xmlns:local="clr-namespace:RotatoryCube"

<local:RotatoryCube x:Name="RotatoryCubeInterface"></local:RotatoryCube>

NotheDoubleAnimation表示旋转将持续多长时间,From以及To角度。还要注意说明AxisRotation3DAxis旋转哪个以及哪个角度。

于 2013-03-01T08:21:20.527 回答