A. Wolff
A. Wolff

Reputation: 74410

View from inside of 3d element WPF

Im using SphereMeshGenerator to create a sphere element in a WPF application. This sphere is mapped with a media element (video).

I would like to place the camera in the middle of the sphere to get view from inside of the sphere.

Ive tried to play with camera position and direction without any success. My knowledge about wpf and 3d are quite limited unfortunately.

So my question is, if possible, how to do it?

My goal is to emulate a video panoramic animation.

Thank you.

PS: this question has already been asked but still unanswered. See comments section here:

Link Page

Upvotes: 1

Views: 839

Answers (1)

A. Wolff
A. Wolff

Reputation: 74410

Topic quite old but, as it could help other people in the same situation, i think i have to post the way to do that.

Code XAML using SphereMeshGenerator:

<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>

Thx to MABROUKI for the help on this one.

Upvotes: 1

Related Questions