c# - wpf 创建 3d 立方体并沿 x、y 和 z 轴旋转它

标签 c# wpf 3d viewport

我有以下代码:

<Window x:Class="Demo3D.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 Name="viewport3D1">
            <Viewport3D.Camera>
                <PerspectiveCamera x:Name="camMain" Position="6 5 4" LookDirection="-6 -5 -4">
                </PerspectiveCamera>
            </Viewport3D.Camera>
            <ModelVisual3D>
                <ModelVisual3D.Content>
                    <DirectionalLight x:Name="dirLightMain" Direction="-1,-1,-1">
                    </DirectionalLight>
                </ModelVisual3D.Content>
            </ModelVisual3D>
            <ModelVisual3D x:Name="MyModel">
                <ModelVisual3D.Content>
                    <GeometryModel3D>
                        <GeometryModel3D.Geometry>
                            <MeshGeometry3D x:Name="meshMain"
                                Positions="0 0 0  1 0 0  0 1 0  1 1 0  0 0 1  1 0 1  0 1 1  1 1 1"
                                TriangleIndices="2 3 1  2 1 0  7 1 3  7 5 1  6 5 7  6 4 5  6 2 0  2 0 4  2 7 3  2 6 7  0 1 5  0 5 4">
                            </MeshGeometry3D>
                        </GeometryModel3D.Geometry>
                        <GeometryModel3D.Material>
                            <DiffuseMaterial x:Name="matDiffuseMain">
                                <DiffuseMaterial.Brush>
                                    <SolidColorBrush Color="Red"/>
                                </DiffuseMaterial.Brush>
                            </DiffuseMaterial>
                        </GeometryModel3D.Material>
                    </GeometryModel3D>
                </ModelVisual3D.Content>
                <ModelVisual3D.Transform>
                    <RotateTransform3D>
                        <RotateTransform3D.Rotation>
                            <AxisAngleRotation3D x:Name="rotate" Axis="1 0 0"/>
                        </RotateTransform3D.Rotation>
                    </RotateTransform3D>
                </ModelVisual3D.Transform>
            </ModelVisual3D>
        </Viewport3D>
        <Slider Height="23" HorizontalAlignment="Left" 
                Margin="12,12,0,0" Name="slider1"
                VerticalAlignment="Top" Width="187" 
                Maximum="360"
                Value="{Binding ElementName=rotate, Path=Angle}" />

    </Grid>
</Window>

我可以通过滑动 slider 来旋转立方体。问题是立方体不会围绕它的中心旋转。我怎样才能让那个立方体围绕它的中心旋转?换句话说,如果我能做类似 http://www.youtube.com/watch?feature=player_embedded&v=a7mTytwRGqI 的事情就好了。

最佳答案

改变

<RotateTransform3D>

<RotateTransform3D CenterX="0.5" CenterY="0.5" CenterZ="0.5" >

结果: enter image description here enter image description here

关于c# - wpf 创建 3d 立方体并沿 x、y 和 z 轴旋转它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15640619/

相关文章:

c# - 对于我按下的每个按钮,打开相同的表单。(C#)

c# - 在我的类中使用未分配的局部变量(c#,silverlight)

c# - WebAPI 与 Ninject

WPF Treeview - 与 ViewModel 同步?

c# - WPF 控件模板 - 如何创建自定义属性?

c# - await 语句后的代码不运行

c# - Ajax 不将数据发布到 Controller

3d - 多边形索引列表中的相邻多边形

python - 在 Python 中绘制 3D 圆柱面图

IOS/ARKIT - 如何做上一个和下一个按钮,点击时用动画改变 3D 模型?