从3d元素WPF里面的看法

时间:2012-05-24 09:18:50

标签: wpf 3d camera

我使用SphereMeshGenerator在WPF应用程序中创建一个sphere元素。 此球体使用媒体元素(视频)进行映射。

我想将相机放在球体中间,以便从球体内部进行观察。

我试过玩相机的位置和方向而没有任何成功。 不幸的是,我对wpf和3d的了解非常有限。

所以我的问题是,如果可能的话,怎么做?

我的目标是模仿视频全景动画。

谢谢。

PS:这个问题已被提出但仍未得到答复。请参阅此处的评论部分:

Link Page

1 个答案:

答案 0 :(得分:1)

主题很老但是,因为它可以帮助处于相同情况的其他人,我想我必须发布这样做的方式。

使用SphereMeshGenerator编码XAML:

<Window x:Class="WpfSphereMeshGenerator.SphereMeshGeneratorMediaInto"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfSphereMeshGenerator"
    Title="SphereMeshGeneratorMediaInto" 
    Height="300" Width="300"
    Loaded="Window_Loaded">

<!-- OXYZ axis in WPF are in center of ViewPort3D-->

<Window.Resources>
  <!--sphere diameter--> 
    <local:SphereMeshGenerator 
    x:Key="MySphereMeshGenerator"
    Center="0 0 0"
    Radius="100.0" />
</Window.Resources>
<Grid>
    <Viewport3D>
        <!--camera in center of ViewPort => 0,0,0-->  
        <Viewport3D.Camera>
            <PerspectiveCamera 
                    x:Name="Camera"
                    UpDirection="0,1,0"
                    LookDirection="-2,-2,-2"
                    Position="0,0,0" 
                    FieldOfView="100" >

            </PerspectiveCamera>
        </Viewport3D.Camera>
        <ModelVisual3D>
            <ModelVisual3D.Content>
                <AmbientLight 
                        Color="White">
                </AmbientLight >
            </ModelVisual3D.Content>
        </ModelVisual3D>
        <!--ModelVisualD by default in center of ViewPort-->
        <ModelVisual3D
            x:Name="ModelA">
            <ModelVisual3D.Content>
                <GeometryModel3D 
                        Geometry="{Binding 
                        Source={StaticResource 
                        MySphereMeshGenerator}, 
                        Path=Geometry}">
                    <!--turn sphere-->
                    <GeometryModel3D.Transform>
                        <RotateTransform3D >
                            <RotateTransform3D.Rotation>
                                <AxisAngleRotation3D
                                            x:Name="Rotate"
                                            Axis="0,1,0"/>
                            </RotateTransform3D.Rotation>
                        </RotateTransform3D>
                    </GeometryModel3D.Transform>
                    <!--video put on BackMaterial-->
                    <!--inside face-->
                    <GeometryModel3D.BackMaterial>
                        <DiffuseMaterial>
                            <DiffuseMaterial.Brush>
                                <VisualBrush>
                                    <VisualBrush.Visual>
                                        <MediaElement>
                                            <MediaElement.Triggers>
                                                <EventTrigger RoutedEvent="MediaElement.Loaded">
                                                    <EventTrigger.Actions>
                                                        <BeginStoryboard>
                                                            <Storyboard>
                                                                <MediaTimeline 
                                                                    x:Name="mediaTimeline"
                                                                    RepeatBehavior="Forever" />
                                                            </Storyboard>
                                                        </BeginStoryboard>
                                                    </EventTrigger.Actions>
                                                </EventTrigger>
                                            </MediaElement.Triggers>
                                        </MediaElement>
                                    </VisualBrush.Visual>
                                </VisualBrush>
                            </DiffuseMaterial.Brush>
                        </DiffuseMaterial>
                    </GeometryModel3D.BackMaterial>
                </GeometryModel3D>
            </ModelVisual3D.Content>
        </ModelVisual3D>
    </Viewport3D>
</Grid>
<Window.Triggers>
    <EventTrigger  
        RoutedEvent="FrameworkElement.Loaded">
        <BeginStoryboard>
            <Storyboard>
                <DoubleAnimation
                    Storyboard.TargetName="Rotate"
                    Storyboard.TargetProperty="Angle"
                    From="0" To="360" Duration="0:0:10"
                    RepeatBehavior="Forever">
                </DoubleAnimation>
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger>
</Window.Triggers>

请向MABROUKI寻求帮助。