首页 新闻 搜索 专区 学院

关于WPF 3D的问题

0
悬赏园豆:100 [已关闭问题] 关闭于 2011-10-20 17:19

想实现一个3D效果,比如有两个Button A和B

用3D做出如下效果

A在屏幕中间 B在A的左边(或右边,无所谓.)

A原地3D效果的旋转,B根据A的旋转角度,在A的周围围绕A旋转.

并且两个Button是半透明的,旋转到背面时,可以看到反向的内容.

求思路或代码.里面那些坐标参数,我实在看的迷糊...

 

herohua0509的主页 herohua0509 | 初学一级 | 园豆:120
提问于:2011-10-09 10:15
< >
分享
所有回答(1)
1

<UserControl x:
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:tools="clr-namespace:_3DTools;assembly=3DTools"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300" Width="300" Height="300" Background="Transparent" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0">
    <UserControl.Resources>
        <VisualBrush x:Key="brush">
                    <VisualBrush.Visual>
                        <Border BorderBrush="Yellow" BorderThickness="2">
                            <Button Content="1fdsfsfs" Click="Button_Click"/>
                        </Border>
                    </VisualBrush.Visual>
                </VisualBrush>
    </UserControl.Resources>
    <Grid>
        <tools:TrackballDecorator>
            <Viewport3D x:Name="viewPort3D">
               
                <Viewport3D.Camera>
                    <PerspectiveCamera Position="0,0,-5" LookDirection="0,0,1" UpDirection="0,1,0" FieldOfView="80"/>
                </Viewport3D.Camera>

                <!--x-->
                <tools:ScreenSpaceLines3D Points="-100,0,0 100,0,0" Thickness="1" Color="Red"></tools:ScreenSpaceLines3D>
                <!--y-->
                <tools:ScreenSpaceLines3D Points="0,-100,0 0,100,0" Thickness="1" Color="Blue"></tools:ScreenSpaceLines3D>
                <!--z-->
                <tools:ScreenSpaceLines3D Points="0,0,-100 0,0,100" Thickness="1" Color="Green"></tools:ScreenSpaceLines3D>

                <ModelVisual3D>
                    <ModelVisual3D.Content>
                        <DirectionalLight Color="White" Direction="0,0,-1"/>
                    </ModelVisual3D.Content>
                </ModelVisual3D>

                <ModelVisual3D>
                    <ModelVisual3D.Content>
                        <DirectionalLight Color="White" Direction="0,0,1"/>
                    </ModelVisual3D.Content>
                </ModelVisual3D>

              
               
                <Viewport2DVisual3D>
                    <Viewport2DVisual3D.Transform>
                        <Transform3DGroup>
                            <TranslateTransform3D OffsetX="0" OffsetY="0" OffsetZ="0"/>
                            <RotateTransform3D>
                            <RotateTransform3D.Rotation>
                                <AxisAngleRotation3D x:Name="axisUI" Axis="0,1,0" Angle="0"/>
                            </RotateTransform3D.Rotation>
                        </RotateTransform3D>
                            <RotateTransform3D>
                                <RotateTransform3D.Rotation>
                                    <AxisAngleRotation3D  Axis="1,0,0" Angle="0"/>
                                </RotateTransform3D.Rotation>
                            </RotateTransform3D>
                        </Transform3DGroup>
                    </Viewport2DVisual3D.Transform>
                       
                    <Viewport2DVisual3D.Geometry>
                        <MeshGeometry3D Positions="-1,-1,0 -1,1,0 1,1,0 1,-1,0" TriangleIndices="0,1,2 2,3,0" TextureCoordinates="1,1 1,0 0,0 0,1"/>
                    </Viewport2DVisual3D.Geometry>
                   
                    <Viewport2DVisual3D.Material>
                        <DiffuseMaterial Viewport2DVisual3D.IsVisualHostMaterial="True"/>
                    </Viewport2DVisual3D.Material>

                    <Viewport2DVisual3D.Visual>
                        <Border BorderBrush="Yellow" BorderThickness="2">
                            <Button Content="1fdsfsfs" Click="Button_Click"/>
                        </Border>
                    </Viewport2DVisual3D.Visual>
                </Viewport2DVisual3D>               
                <ModelVisual3D>
                    <ModelVisual3D.Content>
                        <GeometryModel3D>
                            <GeometryModel3D.Transform>
                                <Transform3DGroup>
                                    <TranslateTransform3D OffsetX="3" OffsetY="0" OffsetZ="0"/>
                                    <RotateTransform3D>
                                        <RotateTransform3D.Rotation>
                                            <AxisAngleRotation3D  Axis="0,1,0" Angle="0"/>
                                        </RotateTransform3D.Rotation>
                                    </RotateTransform3D>
                                    <RotateTransform3D>
                                        <RotateTransform3D.Rotation>
                                            <AxisAngleRotation3D  Axis="1,0,0" Angle="0"/>
                                        </RotateTransform3D.Rotation>
                                    </RotateTransform3D>
                                </Transform3DGroup>

                            </GeometryModel3D.Transform>
                            <GeometryModel3D.Geometry>
                                <MeshGeometry3D Positions="-1,-1,0 -1,1,0 1,1,0 1,-1,0" TriangleIndices="0,1,2 2,3,0" TextureCoordinates=" 1,1 1,0 0,0 0,1 "/>
                            </GeometryModel3D.Geometry>
                            <GeometryModel3D.BackMaterial>
                                <DiffuseMaterial Brush="{StaticResource ResourceKey=brush}">
                                   
                                </DiffuseMaterial>
                            </GeometryModel3D.BackMaterial>
                        </GeometryModel3D>
                    </ModelVisual3D.Content>
                </ModelVisual3D>

                <Viewport2DVisual3D>
                    <Viewport2DVisual3D.Transform>
                        <Transform3DGroup>
                            <RotateTransform3D>
                                <RotateTransform3D.Rotation>
                                    <AxisAngleRotation3D  Axis="0,1,0" Angle="0"/>
                                </RotateTransform3D.Rotation>
                            </RotateTransform3D>
                            <RotateTransform3D>
                                <RotateTransform3D.Rotation>
                                    <AxisAngleRotation3D  Axis="1,0,0" Angle="0"/>
                                </RotateTransform3D.Rotation>
                            </RotateTransform3D>
                            <TranslateTransform3D OffsetX="3" OffsetY="0" OffsetZ="0"/>
                        </Transform3DGroup>
                    </Viewport2DVisual3D.Transform>

                    <Viewport2DVisual3D.Geometry>
                        <MeshGeometry3D Positions="-1,-1,0 -1,1,0 1,1,0 1,-1,0" TriangleIndices="0,1,2 2,3,0" TextureCoordinates="1,1 1,0 0,0 0,1"/>
                    </Viewport2DVisual3D.Geometry>

                    <Viewport2DVisual3D.Material>
                        <DiffuseMaterial Viewport2DVisual3D.IsVisualHostMaterial="True"/>
                    </Viewport2DVisual3D.Material>

                    <Viewport2DVisual3D.Visual>
                        <Border BorderBrush="Yellow" BorderThickness="2">
                            <Button Content="1fdsfsfs" Click="Button_Click"/>
                        </Border>
                    </Viewport2DVisual3D.Visual>
                </Viewport2DVisual3D>
                <ModelVisual3D>
                    <ModelVisual3D.Content>
                        <GeometryModel3D>
                            <GeometryModel3D.Transform>
                                <Transform3DGroup>
                                    <TranslateTransform3D OffsetX="3" OffsetY="0" OffsetZ="0"/>
                                    <RotateTransform3D>
                                        <RotateTransform3D.Rotation>
                                            <AxisAngleRotation3D  Axis="1,1,0" Angle="0"/>
                                        </RotateTransform3D.Rotation>
                                    </RotateTransform3D>
                                </Transform3DGroup>

                            </GeometryModel3D.Transform>
                            <GeometryModel3D.Geometry>
                                <MeshGeometry3D Positions="-1,-1,0 -1,1,0 1,1,0 1,-1,0" TriangleIndices="0,1,2 2,3,0" TextureCoordinates=" 1,1 1,0 0,0 0,1 "/>
                            </GeometryModel3D.Geometry>
                            <GeometryModel3D.BackMaterial>
                                <DiffuseMaterial Brush="{StaticResource ResourceKey=brush}">

                                </DiffuseMaterial>
                            </GeometryModel3D.BackMaterial>
                        </GeometryModel3D>
                    </ModelVisual3D.Content>
                </ModelVisual3D>

            </Viewport3D>
        </tools:TrackballDecorator>
    </Grid>
</UserControl>

 

 

然后 同时修改两个模型的正反面 的Angle 可以看到效果

其实就是一个有偏移量,然后设置偏移量的属性要在Transform3DGroup的第一个,这样偏移过的就会围绕3D的中心来转,如果要原地转,那TranslateTransform3D 这个属性设置就放在 RotateTransform3D下面,依然会偏移,但不会按照3D的中心转,就原地转了

 

这里面有 Viewport2DVisual3D 和  ModelVisual3D各两个.ModelVisual3D是Viewport2DVisual3D 的背面

herohua0509 | 园豆:120 (初学一级) | 2011-10-20 17:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册