如何旋转3D对象 [英] How to rotate 3D objects

查看:130
本文介绍了如何旋转3D对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。

在我的应用程序中我有3D立方体。

在立方体边上我会放置任何控件。

在边界我有一个按钮可以将这个立方体旋转到+ -90度。

但实际上我已经旋转了相机,而不是立方体。而且我想完全旋转立方体。

< Window.Resources> 
< DiffuseMaterial x:Key =CubeSideMaterialViewport2DVisual3D.IsVisualHostMaterial =TrueBrush =LimeGreen/>
< LinearGradientBrush x:Key =BlackBackgroundEndPoint =1,0StartPoint =0,0.3>
< GradientStop Color =LightGrayOffset =0.8/>
< GradientStop Color =DarkGrayOffset =0.7/>
< GradientStop Color =BlackOffset =0/>
< / LinearGradientBrush>
< LinearGradientBrush x:Key =GreenBackgroundEndPoint =0.5,1StartPoint =0.5,0>
< GradientStop Color =GreenYellowOffset =0.824/>
< GradientStop Color =DarkGreenOffset =0/>
< / LinearGradientBrush>
< LinearGradientBrush x:Key =PressedButtonEndPoint =0.5,1StartPoint =0.5,0MappingMode =RelativeToBoundingBox>
< GradientStop Color =BlueOffset =0/>
< GradientStop Color =WhiteOffset =0.816/>
< GradientStop Color =VioletOffset =0.744/>
< / LinearGradientBrush>
< ControlTemplate x:Key =ButtonLook>
< Grid Background =KhakiShowGridLines =True>
<矩形x:名称=矩形宽度=20高度=11>
< Rectangle.Stroke>
< LinearGradientBrush EndPoint =0.5,1StartPoint =0.5,0>
< GradientStop Color =BlackOffset =1/>
< GradientStop Color =Green/>
< / LinearGradientBrush>
< /Rectangle.Stroke>
< Rectangle.Fill>
< LinearGradientBrush EndPoint =0.5,1StartPoint =0.5,0>
< GradientStop Color =GrayOffset =1/>
< GradientStop Color =Green/>
< / LinearGradientBrush>
< /Rectangle.Fill>
< / Rectangle>
< ContentPresenter VerticalAlignment =CenterHorizo​​ntalAlignment =Center/>
< / Grid>
< / ControlTemplate>
< /Window.Resources>
<网格背景=黑色>
< Viewport3D x:Name =viewMargin =0,0,0,-139ClipToBounds =FalseRenderOptions.EdgeMode =AliasedIsHitTestVisible =True>
< Viewport3D.Camera>
< PerspectiveCamera x:Name =cameraFieldOfView =60Position =0,-0.2,6LookDirection =0.000001,-0.0005,-0.08UpDirection =0,1,0> ;
< PerspectiveCamera.Transform>
< RotateTransform3D x:Name =rotCenterY =0CenterX =0CenterZ =0>
< RotateTransform3D.Rotation>
<! - rotation - >
< AxisAngleRotation3D x:Name =camRotationAxis =0,1,0Angle =0/>
< /RotateTransform3D.Rotation>
< / RotateTransform3D>
< /PerspectiveCamera.Transform>
< / PerspectiveCamera>
< /Viewport3D.Camera>
< ModelVisual3D x:Name =model>
< ModelVisual3D.Content>
< Model3DGroup x:Name =group>
< AmbientLight Color =White/>
< DirectionalLight Color =WhiteDirection = - 1,-1,-10>< / DirectionalLight>
< / Model3DGroup>
< /ModelVisual3D.Content>
< / ModelVisual3D>
< Viewport2DVisual3D Material ={StaticResource CubeSideMaterial}>
< Viewport2DVisual3D.Geometry>
< MeshGeometry3D Positions = - 1.77,-1,1.77 1.77,-1,1.77 1.77,1,1.77 -1.77,1,1.77
TextureCoordinates =0,1 1,1 1, 0 0,0
TriangleIndices =0,2,3 0,1,2/>
< /Viewport2DVisual3D.Geometry>
< Grid x:Name =FrontPanelShowGridLines =FalseIsHitTestVisible =TrueMargin =0Background ={StaticResource BlackBackground}>
< Grid.RowDefinitions>
< RowDefinition />
< RowDefinition Height =15/>
< RowDefinition Height =15MaxHeight =15/>
< RowDefinition />
< /Grid.RowDefinitions>
< Grid.ColumnDefinitions>
< ColumnDefinition Width =20/>
< ColumnDefinition />
< ColumnDefinition />
< ColumnDefinition Width =20/>
< /Grid.ColumnDefinitions>
< Label Content =FrontGrid.Row =0Grid.Column =2Foreground =LightBlue/>
< Button Grid.Row =1Grid.Column =4Grid.ColumnSpan =2
Content =+ 90
Foreground =Black
FlowDirection =LeftToRight
FontSize =3
VerticalContentAlignment =Center
Horizo​​ntalAlignment =Right
Width =20
Height =10
Margin =0,2,0,2
ToolTip =ТипаткнисюдаиОпа!
Click =SeeOthers_Click/>
<! - < Image Grid.Column =4Grid.Row =4Grid.RowSpan =2Horizo​​ntalAlignment =RightMargin =2,1,2,12名称=image1Stretch =FillWidth =25Height =20Source =monitor2.jpg/> - >
< Button Grid.Column =0
Grid.Row =1
Height =10
Width =20
Content = -90
Click =SeeOthers_Click
FontSize =3
BorderThickness =0
Foreground =Black/>
< / Grid>
< / Viewport2DVisual3D>
<! - 立方体右侧 - >
< Viewport2DVisual3D Material ={StaticResource CubeSideMaterial}>
< Viewport2DVisual3D.Geometry>
< MeshGeometry3D Positions =1.77,-1,1.77 1.77,1,1.77 1.77,1,-1.77,1.77,-1,-1.77
TextureCoordinates =0,1 0,0 1 ,0 1,1
TriangleIndices =0,3,2 0,2,1/>
< /Viewport2DVisual3D.Geometry>
< Grid x:Name =RightPanelIsHitTestVisible =TrueBackground ={StaticResource BlackBackground}>
< Grid.RowDefinitions>
< RowDefinition />
< RowDefinition Height =15/>
< RowDefinition Height =15MaxHeight =15/>
< RowDefinition />
< /Grid.RowDefinitions>
< Grid.ColumnDefinitions>
< ColumnDefinition Width =30/>
< ColumnDefinition />
< ColumnDefinition />
< ColumnDefinition Width =30/>
< /Grid.ColumnDefinitions>
< Button Grid.Row =1Grid.Column =4Grid.ColumnSpan =2
Content =+ 90
Foreground =Black
FlowDirection =LeftToRight
FontSize =3
VerticalContentAlignment =Center
Horizo​​ntalAlignment =Right
Width =20
Height =10
Margin =0,2,0,2
ToolTip =ТипаткнисюдаиОпа!
Click =SeeOthers_Click/>
< Label Content =RightGrid.Row =0Grid.Column =2Foreground =LightBlue/>
< Button Grid.Column =0
Grid.Row =1
Height =10
Width =20
Content = -90
Click =SeeOthers_Click
FontSize =3
BorderThickness =0
Foreground =Black/>
< / Grid>
< / Viewport2DVisual3D>
<! - 立方体背面 - >
< Viewport2DVisual3D Material ={StaticResource CubeSideMaterial}>
< Viewport2DVisual3D.Geometry>
< MeshGeometry3D Positions =1.77,1,-1.77 1.77,-1,-1.77 -1.77,1,-1.77,-1.77,-1,-1.77
TextureCoordinates =0,0 0,1 1,0 1,1
TriangleIndices =1,3,2 1,2,0/>
< /Viewport2DVisual3D.Geometry>
< Grid x:Name =BackPanelBackground ={StaticResource BlackBackground}IsHitTestVisible =True>
< Grid.RowDefinitions>
< RowDefinition />
< RowDefinition Height =15/>
< RowDefinition Height =15MaxHeight =15/>
< RowDefinition />
< /Grid.RowDefinitions>
< Grid.ColumnDefinitions>
< ColumnDefinition Width =30/>
< ColumnDefinition />
< ColumnDefinition />
< ColumnDefinition Width =30/>
< /Grid.ColumnDefinitions>
< Button Grid.Row =1Grid.Column =4Grid.ColumnSpan =2
Content =+ 90
Foreground =Black
FlowDirection =LeftToRight
FontSize =3
VerticalContentAlignment =Center
Horizo​​ntalAlignment =Right
Width =20
Height =10
Margin =0,2,0,2
ToolTip =ТипаткнисюдаиОпа!
Click =SeeOthers_Click/>
< Label Content =RearGrid.Row =0Grid.Column =2Foreground =LightBlue/>
< Button Grid.Column =0
Grid.Row =1
Height =10
Width =20
Content = -90
Click =SeeOthers_Click
FontSize =3
BorderThickness =0
Foreground =Black/>
< / Grid>
< / Viewport2DVisual3D>
<! - 立方体左侧 - >
< Viewport2DVisual3D Material ={StaticResource CubeSideMaterial}>
< Viewport2DVisual3D.Geometry>
< MeshGeometry3D Positions = - 1.77,-1,-1.77 -1.77,1,-1.77 -1.77,1,1.77,-1.77,-1,1.77
TextureCoordinates =0,1 0,0 1,0 1,1
TriangleIndices =0,3,2 0,2,1/>
< /Viewport2DVisual3D.Geometry>
< Grid x:Name =LeftPanelBackground ={StaticResource BlackBackground}IsHitTestVisible =True>
< Grid.RowDefinitions>
< RowDefinition />
< RowDefinition Height =15/>
< RowDefinition Height =15MaxHeight =15/>
< RowDefinition />
< /Grid.RowDefinitions>
< Grid.ColumnDefinitions>
< ColumnDefinition Width =30/>
< ColumnDefinition />
< ColumnDefinition />
< ColumnDefinition Width =30/>
< /Grid.ColumnDefinitions>
< Button Grid.Row =1Grid.Column =4Grid.ColumnSpan =2
Content =+ 90
Foreground =Black
FlowDirection =LeftToRight
FontSize =3
VerticalContentAlignment =Center
Horizo​​ntalAlignment =Right
Width =20
Height =10
Margin =0,2,0,2
ToolTip =ТипаткнисюдаиОпа!
Click =SeeOthers_Click/>
< Label Content =LeftGrid.Row =0Grid.Column =2Foreground =LightBlue/>
< Button Grid.Column =0
Grid.Row =1
Height =10
Width =20
Content = -90
Click =SeeOthers_Click
FontSize =3
BorderThickness =0
Foreground =Black/>
< / Grid>
< / Viewport2DVisual3D>
< / Viewport3D>
< Button Height =92Horizo​​ntalAlignment =LeftMargin =56,0,0,40Name =button1VerticalAlignment =BottomWidth =186Click =button1_Click>按钮和LT; /按钮>
< / Grid>



这是C#代码

使用System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;使用System.Windows
;使用System.Windows.Controls
;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;使用System.Windows.Navigation
;
使用System.Windows.Shapes;
使用System.Windows.Media.Media3D;
使用Microsoft.Win32;
namespace WpfApplication4
{
public partial class Window1:Window
{
System.Windows.Threading.DispatcherTimer clock = new System.Windows.Threading.DispatcherTimer();
System.Windows.Threading.DispatcherTimer Zoomclock = new System.Windows.Threading.DispatcherTimer();
public Window1()
{
InitializeComponent();
//旋转时钟
clock.Tick + = new EventHandler(clock_Tick);
clock.Interval = new TimeSpan(0,0,0,0,20);
Zoomclock.Tick + = new EventHandler(Zoomclock_Tick);
Zoomclock.Interval = new TimeSpan(0,0,0,0,15);
}
//这是用于缩放的事件
void Global_MouseWheel(对象发送者,MouseWheelEventArgs e)
{
camera.Position = new Point3D(
camera) .Position.X,
camera.Position.Y,
camera.Position.Z - e.Delta / 250D);

}
//旋转角度 - 在Y轴上旋转
double rotAngle = 90;
double CamRot = 0;
bool flagfirst = true;
void clock_Tick(object sender,EventArgs e)
{
if(flagfirst == true)rotAngle = rotAngle + CamRot;
if(rotAngle> 0)
{
camRotation.Angle + = 5;
if(camRotation.Angle> = rotAngle)
{
clock.Stop();
CamRot = camRotation.Angle;
flagfirst = true;
camera.FieldOfView = 50;
}
}
其他
{
camRotation.Angle - = 5;
if(camRotation.Angle< = rotAngle)
{
clock.Stop();
CamRot = camRotation.Angle;
flagfirst = true;
camera.FieldOfView = 50;
}
}
flagfirst = false;
}
double CamDist = 60;
void Zoomclock_Tick(对象发送者,EventArgs e)
{
camera.FieldOfView + = 5;
if(camera.FieldOfView> = 100)Zoomclock.Stop();
}

//这需要是全局密钥但我没时间完成这个
//当某些控件让焦点立方体停止旋转时它有问题
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
双角= 5;
if(e.Key == Key.Left)
if(camRotation.Angle == 0)camRotation.Angle = 360 - angle;
else camRotation.Angle - = angle;
else if(e.Key == Key.Right)camRotation.Angle + = angle;
}

//导航按钮转到用户面板
private void SeeOthers_Click(object sender,RoutedEventArgs e)
{
Zoomclock.Start() ;
Rotar(发件人,e);
}
double CurrentAngle = 0;
private void Rotar(对象发送者,RoutedEventArgs e)
{
if(Convert.ToString(((System.Windows.Controls.ContentControl)(e.OriginalSource))。内容)== +90)
{
rotAngle = CurrentAngle + 90;
clock.Start();
}
if(Convert.ToString(((System.Windows.Controls.ContentControl)(e.OriginalSource))。Content)== - 90)
{
rotAngle = CurrentAngle - 90;
clock.Start();
}
CurrentAngle = rotAngle;
if(CurrentAngle> = 360){CurrentAngle = 0; }
}
private void button1_Click(object sender,RoutedEventArgs e)
{
this.Close();
}
private void label3_MouseLeftButtonUp(object sender,MouseButtonEventArgs e)
{
Transform3D My3DTrans = new RotateTransform3D();
// My3DTrans。
}
}
}





请帮助我,还有一个。

谁知道请建议 - 为什么我有向前和向后轮换的麻烦?



谢谢。

解决方案

我认为你应该做的是将你的所有立方体边包裹在一个新的 ModelVisual3D 中,如下所示:



 <   ModelVisual3D     x:名称  =  cube  >  
< Viewport2DVisual3D 材料 = {StaticResource CubeSideMaterial} >
您的多维数据集内容在这里...
< / Viewport2DVisual3D >
<! - 立方体右侧 - >
< Viewport2DVisual3D 材料 = {StaticResource CubeSideMaterial} >
...此处...
< / Viewport2DVisual3D >
<! - < span class =code-keyword> cube backside < span class =code-comment> - >
< Viewport2DVisual3D 材料 = {StaticResource CubeSideMaterial} >
...此处...
< / Viewport2DVisual3D >
<! - 立方体左侧 - >
< Viewport2DVisual3D 材料 = {StaticResource CubeSideMaterial} >
......还在这里!
< / Viewport2DVisual3D >
< / ModelVisual3D >





请注意,我已将所有多维数据集内容完好无损并将其包装在内ModelVisual3D ,通过命名该模型,您可以使用旋转变换的动画将它们全部旋转到一起:



 私人 Rotation3D轮换; 
private RotateTransform3D转换;
public Window1()
{
InitializeComponent();
rotation = new AxisAngleRotation3D( new Vector3D( 0 1 0 ), 0 );
transform = new RotateTransform3D(旋转);
cube.Transform = transform;
}
私有 void SeeOthers_Click( object sender,RoutedEventArgs e)
{
DoubleAnimation animation = new DoubleAnimation( 90 new 持续时间(TimeSpan.FromSeconds( 1 )));
rotation.BeginAnimation(AxisAngleRotation3D.AngleProperty,动画);
}





在这个例子中,我只是将它旋转90度,但我想你可以搞清楚如何用其他角度调用它:)



希望这会有所帮助,

Fredrik Bornander


< blockquote>要旋转立方体(在我的示例中称为'cube'),但它可以是任何3D对象,使用中心点旋转很简单:





  //  这定义了中心点 - 一半通过所有3轴中的对象 
Point3D centerPoint =新Point3D( 2 5 2 5 2 5 );

// 我想要旋转的角度。尚未指定哪个轴
int angleRot = 45 ;

// 这里,在Vector3D中,用axis(x,y,z)指定应用角度
RotateTransForm myRotateTransform = New RotateTransform3D(New AxisAngleRotation3D(New Vector3D( 0 1 0 ),angleRot),centerPoint);

// 应用转换
cube.Transform = myRotateTransform ;





当你想改变角度时,只需应用一个新的rotateTransform


Hello everybody.
In my application I have 3D cube.
On cube sides I will place any controls.
At the borders I have a buttons for rotate this cube to +-90 degree.
But in fact I have rotate the camera, not cube. And I'd like to rotate exactly Cube.

<Window.Resources>
        <DiffuseMaterial x:Key="CubeSideMaterial" Viewport2DVisual3D.IsVisualHostMaterial="True" Brush="LimeGreen"/>
        <LinearGradientBrush x:Key="BlackBackground" EndPoint="1,0" StartPoint="0,0.3">
            <GradientStop Color="LightGray" Offset="0.8"/>
            <GradientStop Color="DarkGray" Offset="0.7"/>
            <GradientStop Color="Black" Offset="0"/>
        </LinearGradientBrush>
        <LinearGradientBrush x:Key="GreenBackground" EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="GreenYellow" Offset="0.824"/>
            <GradientStop Color="DarkGreen" Offset="0"/>
        </LinearGradientBrush>
        <LinearGradientBrush x:Key="PressedButton" EndPoint="0.5,1" StartPoint="0.5,0" MappingMode="RelativeToBoundingBox">
            <GradientStop Color="Blue" Offset="0"/>
            <GradientStop Color="White" Offset="0.816"/>
            <GradientStop Color="Violet" Offset="0.744"/>
        </LinearGradientBrush>
        <ControlTemplate x:Key="ButtonLook">
            <Grid Background="Khaki" ShowGridLines="True" >
                <Rectangle x:Name="Rect" Width="20" Height="11">
                    <Rectangle.Stroke>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="Black" Offset="1"/>
                            <GradientStop Color="Green"/>
                        </LinearGradientBrush>
                    </Rectangle.Stroke>
                    <Rectangle.Fill>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="Gray" Offset="1"/>
                            <GradientStop Color="Green"/>
                        </LinearGradientBrush>
                    </Rectangle.Fill>
                </Rectangle>
                <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" />
            </Grid>
        </ControlTemplate>
    </Window.Resources>
    <Grid Background="Black">
        <Viewport3D x:Name="view" Margin="0,0,0,-139"  ClipToBounds="False" RenderOptions.EdgeMode="Aliased" IsHitTestVisible="True">
            <Viewport3D.Camera>
                <PerspectiveCamera x:Name="camera" FieldOfView="60" Position="0,-0.2,6" LookDirection="0.000001,-0.0005,-0.08" UpDirection="0,1,0">
                    <PerspectiveCamera.Transform>
                        <RotateTransform3D x:Name="rot" CenterY="0" CenterX="0" CenterZ="0">
                            <RotateTransform3D.Rotation>
                                <!-- rotation -->
                                <AxisAngleRotation3D x:Name="camRotation" Axis="0,1,0" Angle="0"/>
                            </RotateTransform3D.Rotation>
                        </RotateTransform3D>
                    </PerspectiveCamera.Transform>                
                </PerspectiveCamera>
            </Viewport3D.Camera>
            <ModelVisual3D x:Name="model">
                <ModelVisual3D.Content>
                    <Model3DGroup x:Name="group">
                        <AmbientLight Color="White" />
                        <DirectionalLight Color="White" Direction="-1,-1,-10"></DirectionalLight>
                    </Model3DGroup>
                </ModelVisual3D.Content>
            </ModelVisual3D>
            <Viewport2DVisual3D Material="{StaticResource CubeSideMaterial}">
                <Viewport2DVisual3D.Geometry>
                    <MeshGeometry3D Positions="-1.77,-1,1.77 1.77,-1,1.77 1.77,1,1.77 -1.77,1,1.77"
                                    TextureCoordinates="0,1 1,1 1,0 0,0"
                                    TriangleIndices="0,2,3 0,1,2"/>
                </Viewport2DVisual3D.Geometry>
                <Grid x:Name="FrontPanel" ShowGridLines="False" IsHitTestVisible="True" Margin="0" Background="{StaticResource BlackBackground}">
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition Height="15"/>
                        <RowDefinition Height="15" MaxHeight="15"/>
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="20"/>
                        <ColumnDefinition />
                        <ColumnDefinition/>
                        <ColumnDefinition Width="20"/>
                    </Grid.ColumnDefinitions>
                    <Label Content="Front" Grid.Row="0" Grid.Column="2" Foreground="LightBlue"/>
                    <Button Grid.Row="1" Grid.Column="4" Grid.ColumnSpan="2" 
                            Content="+90" 
                            Foreground="Black" 
                            FlowDirection="LeftToRight" 
                            FontSize="3" 
                            VerticalContentAlignment= "Center"
                            HorizontalAlignment="Right"
                            Width="20"
                            Height="10"
                            Margin="0,2,0,2"
                            ToolTip="Типа ткни сюда и Опа!" 
                            Click="SeeOthers_Click" />
                    <!--<Image Grid.Column="4" Grid.Row="4" Grid.RowSpan="2" HorizontalAlignment="Right" Margin="2,1,2,12" Name="image1" Stretch="Fill" Width="25" Height="20" Source="monitor2.jpg"/>-->
                    <Button Grid.Column="0" 
                            Grid.Row="1" 
                            Height="10" 
                            Width="20" 
                            Content="-90" 
                            Click="SeeOthers_Click" 
                            FontSize="3" 
                            BorderThickness="0" 
                            Foreground="Black" />
                </Grid>
            </Viewport2DVisual3D>
            <!-- cube right side -->
            <Viewport2DVisual3D Material="{StaticResource CubeSideMaterial}">
                <Viewport2DVisual3D.Geometry>
                    <MeshGeometry3D Positions="1.77,-1,1.77 1.77,1,1.77 1.77,1,-1.77, 1.77,-1,-1.77"
                                    TextureCoordinates="0,1 0,0 1,0 1,1 "
                                    TriangleIndices="0,3,2 0,2,1"/>
                </Viewport2DVisual3D.Geometry>
                <Grid x:Name="RightPanel" IsHitTestVisible="True" Background="{StaticResource BlackBackground}">
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition Height="15"/>
                        <RowDefinition Height="15" MaxHeight="15"/>
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="30"/>
                        <ColumnDefinition />
                        <ColumnDefinition/>
                        <ColumnDefinition Width="30"/>
                    </Grid.ColumnDefinitions>
                    <Button Grid.Row="1" Grid.Column="4" Grid.ColumnSpan="2" 
                            Content="+90" 
                            Foreground="Black" 
                            FlowDirection="LeftToRight" 
                            FontSize="3" 
                            VerticalContentAlignment= "Center"
                            HorizontalAlignment="Right"
                            Width="20"
                            Height="10"
                            Margin="0,2,0,2"
                            ToolTip="Типа ткни сюда и Опа!" 
                            Click="SeeOthers_Click" />
                    <Label Content="Right" Grid.Row="0" Grid.Column="2" Foreground="LightBlue"/>
                    <Button Grid.Column="0" 
                            Grid.Row="1" 
                            Height="10" 
                            Width="20" 
                            Content="-90" 
                            Click="SeeOthers_Click" 
                            FontSize="3" 
                            BorderThickness="0" 
                            Foreground="Black" />
                </Grid>
            </Viewport2DVisual3D>
            <!-- cube back side -->
            <Viewport2DVisual3D Material="{StaticResource CubeSideMaterial}">
                <Viewport2DVisual3D.Geometry>
                    <MeshGeometry3D Positions="1.77,1,-1.77 1.77,-1,-1.77 -1.77,1,-1.77, -1.77,-1,-1.77"
                                    TextureCoordinates=" 0,0 0,1 1,0 1,1"
                                    TriangleIndices="1,3,2 1,2,0"/>
                </Viewport2DVisual3D.Geometry>
                <Grid x:Name="BackPanel"  Background="{StaticResource BlackBackground}" IsHitTestVisible="True">
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition Height="15"/>
                        <RowDefinition Height="15" MaxHeight="15"/>
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="30"/>
                        <ColumnDefinition />
                        <ColumnDefinition/>
                        <ColumnDefinition Width="30"/>
                    </Grid.ColumnDefinitions>
                    <Button Grid.Row="1" Grid.Column="4" Grid.ColumnSpan="2" 
                            Content="+90" 
                            Foreground="Black" 
                            FlowDirection="LeftToRight" 
                            FontSize="3" 
                            VerticalContentAlignment= "Center"
                            HorizontalAlignment="Right"
                            Width="20"
                            Height="10"
                            Margin="0,2,0,2"
                            ToolTip="Типа ткни сюда и Опа!" 
                            Click="SeeOthers_Click" />
                    <Label Content="Rear" Grid.Row="0" Grid.Column="2" Foreground="LightBlue"/>
                    <Button Grid.Column="0" 
                            Grid.Row="1" 
                            Height="10" 
                            Width="20" 
                            Content="-90" 
                            Click="SeeOthers_Click" 
                            FontSize="3" 
                            BorderThickness="0" 
                            Foreground="Black" />
                </Grid>
            </Viewport2DVisual3D>
<!-- cube left side -->
            <Viewport2DVisual3D Material="{StaticResource CubeSideMaterial}">
                <Viewport2DVisual3D.Geometry>
                    <MeshGeometry3D Positions="-1.77,-1,-1.77 -1.77,1,-1.77 -1.77,1,1.77, -1.77,-1,1.77"
                                    TextureCoordinates=" 0,1 0,0 1,0 1,1"
                                    TriangleIndices="0,3,2 0,2,1"/>
                </Viewport2DVisual3D.Geometry>
                <Grid x:Name="LeftPanel"  Background="{StaticResource BlackBackground}" IsHitTestVisible="True">
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition Height="15"/>
                        <RowDefinition Height="15" MaxHeight="15"/>
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="30"/>
                        <ColumnDefinition />
                        <ColumnDefinition/>
                        <ColumnDefinition Width="30"/>
                    </Grid.ColumnDefinitions>
                    <Button Grid.Row="1" Grid.Column="4" Grid.ColumnSpan="2" 
                            Content="+90" 
                            Foreground="Black" 
                            FlowDirection="LeftToRight" 
                            FontSize="3" 
                            VerticalContentAlignment= "Center"
                            HorizontalAlignment="Right"
                            Width="20"
                            Height="10"
                            Margin="0,2,0,2"
                            ToolTip="Типа ткни сюда и Опа!" 
                            Click="SeeOthers_Click" />
                    <Label Content="Left" Grid.Row="0" Grid.Column="2" Foreground="LightBlue"/>
                    <Button Grid.Column="0" 
                            Grid.Row="1" 
                            Height="10" 
                            Width="20" 
                            Content="-90" 
                            Click="SeeOthers_Click" 
                            FontSize="3" 
                            BorderThickness="0" 
                            Foreground="Black" />
                </Grid>
            </Viewport2DVisual3D>
        </Viewport3D>
        <Button Height="92" HorizontalAlignment="Left" Margin="56,0,0,40" Name="button1" VerticalAlignment="Bottom" Width="186" Click="button1_Click">Button</Button>
    </Grid>


And this is C# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Media3D;
using Microsoft.Win32;
namespace WpfApplication4
{
        public partial class Window1 : Window
    {
        System.Windows.Threading.DispatcherTimer clock = new System.Windows.Threading.DispatcherTimer();
        System.Windows.Threading.DispatcherTimer Zoomclock = new System.Windows.Threading.DispatcherTimer();
        public Window1()
        {
            InitializeComponent();
            //rotation clock
            clock.Tick += new EventHandler(clock_Tick);
            clock.Interval = new TimeSpan(0, 0, 0, 0, 20);
            Zoomclock.Tick += new EventHandler(Zoomclock_Tick);
            Zoomclock.Interval = new TimeSpan(0, 0, 0, 0, 15);
        }
        //this is event for zooming
        void Global_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            camera.Position = new Point3D(
            camera.Position.X,
            camera.Position.Y,
            camera.Position.Z - e.Delta / 250D);
                 
        }
        //rotation angle - for rotation on Y-axis
        double rotAngle = 90;
        double CamRot=0;
        bool flagfirst = true;
        void clock_Tick(object sender, EventArgs e)
        {
            if (flagfirst==true) rotAngle = rotAngle + CamRot;
            if (rotAngle > 0) 
            { 
                camRotation.Angle += 5;
                if (camRotation.Angle >= rotAngle)
                {
                    clock.Stop();
                    CamRot = camRotation.Angle;
                    flagfirst = true;
                    camera.FieldOfView = 50;
                }
            }
            else 
            { 
                camRotation.Angle -= 5; 
                if (camRotation.Angle <= rotAngle) 
                { 
                    clock.Stop(); 
                    CamRot = camRotation.Angle;
                    flagfirst = true;
                    camera.FieldOfView = 50;
                }
            }
            flagfirst = false;
        }
        double CamDist = 60;
        void Zoomclock_Tick(object sender, EventArgs e)
        {
            camera.FieldOfView += 5;
            if (camera.FieldOfView >= 100) Zoomclock.Stop();
        }

        //this would need to be global key but i didn't have time to finish this
        //it have problem when some control get focus cube stop to rotate
        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            double angle = 5;
            if (e.Key == Key.Left)
                if (camRotation.Angle == 0) camRotation.Angle = 360 - angle;
                else camRotation.Angle -= angle;
            else if (e.Key == Key.Right) camRotation.Angle += angle;
        }

        //navigation button which goes to users panel
        private void SeeOthers_Click(object sender, RoutedEventArgs e)
        {
            Zoomclock.Start();
            Rotar(sender, e);
        }
        double CurrentAngle = 0;
        private void Rotar(object sender, RoutedEventArgs e)
        {
            if (Convert.ToString(((System.Windows.Controls.ContentControl)(e.OriginalSource)).Content) == "+90")
            {
                rotAngle = CurrentAngle + 90;
                clock.Start();
            }
            if (Convert.ToString(((System.Windows.Controls.ContentControl)(e.OriginalSource)).Content) == "-90")
            {
                rotAngle = CurrentAngle - 90;
                clock.Start();
            }
            CurrentAngle = rotAngle;
            if (CurrentAngle >= 360) { CurrentAngle = 0; }
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        private void label3_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Transform3D My3DTrans = new RotateTransform3D();
            //My3DTrans.
        }
    }
}



Please help me, and one more.
Who know please advice - Why I have a troubles with rotation forward and backward?

Thanks.

解决方案

What I think you should do is wrap all your cube sides in a new ModelVisual3D like this:

<ModelVisual3D x:Name="cube">
    <Viewport2DVisual3D Material="{StaticResource CubeSideMaterial}">
Your cube stuff goes here...
    </Viewport2DVisual3D>
    <!-- cube right side -->
    <Viewport2DVisual3D Material="{StaticResource CubeSideMaterial}">
...and here...
    </Viewport2DVisual3D>
    <!-- cube back side -->
    <Viewport2DVisual3D Material="{StaticResource CubeSideMaterial}">
...and here...
    </Viewport2DVisual3D>
    <!-- cube left side -->
    <Viewport2DVisual3D Material="{StaticResource CubeSideMaterial}">
...and also here!
    </Viewport2DVisual3D>
</ModelVisual3D>



Notice that I've left all your cube stuff intact and just wrapped them inside a ModelVisual3D, and by naming that model you can rotate all of them together using an animation of a rotation transform:

private Rotation3D rotation;
private RotateTransform3D transform;
public Window1()
{
    InitializeComponent();
    rotation = new AxisAngleRotation3D(new Vector3D(0, 1, 0), 0);
    transform = new RotateTransform3D(rotation);
    cube.Transform = transform;
}
private void SeeOthers_Click(object sender, RoutedEventArgs e)
{
    DoubleAnimation animation = new DoubleAnimation(90, new Duration(TimeSpan.FromSeconds(1)));
    rotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, animation);
}



In this example I'm just rotating it 90 degrees but I think you can figure out how to call that with other angles as well :)

Hope this helps,
Fredrik Bornander


To rotate a cube (in my example just called 'cube'), but it could be any 3D object, it is simple to rotate using the centre point:


//this defines the centre point - half-way thru the object in all 3 axis
Point3D centerPoint = New Point3D(2.5, 2.5, 2.5);  

//the angle I want to rotate by. Not specified yet which axis
int angleRot = 45;

//Here, in the Vector3D, specify with axis (x, y, z) to apply the angle
RotateTransForm myRotateTransform = New RotateTransform3D(New AxisAngleRotation3D(New Vector3D(0, 1, 0), angleRot), centerPoint);

//apply the transform
cube.Transform = myRotateTransform;



Just apply a new rotateTransform when you want to change angle


这篇关于如何旋转3D对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆