[WPF] テクスチャへ動画の貼り付け

3Dモデルの面に、テクスチャで動画を貼り付けすることができます。

 

 

 

1. 3D平面へ動画を貼り付ける

まずはシンプルな四角形へ動画を貼り付けてみます。

環境:

[実行結果]

[プログラムソース "MainWindow.xaml"]
イメージ(静止画)版のプログラムとの違いはわずかです。”黄色” でマーキングします。
<Window x:Class="_3D_Texture_Movie.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:_3D_Texture_Movie"
        mc:Ignorable="d"
        Title="MainWindow" Height="480" Width="640">
    <Grid>

        <Viewport3D>
            <!-- カメラ -->
            <Viewport3D.Camera>
                <PerspectiveCamera Position="-3,3,7" LookDirection="3,-3,-7" UpDirection="0,1,0" FieldOfView="45"/>
            </Viewport3D.Camera>

            <ModelVisual3D>
                <ModelVisual3D.Content>
                    <Model3DGroup>
                        <!-- ライト -->
                        <!--
                        <DirectionalLight Color="White" Direction="0,-1,-1"/>
                        -->
                        <AmbientLight Color="White"/>

                        <GeometryModel3D>
                            <GeometryModel3D.Geometry>
                                <MeshGeometry3D>
                                    <!-- 頂点 -->
                                    <MeshGeometry3D.Positions>
                                        -1,1,0
                                        1,1,0
                                        -1,-1,0
                                        1,-1,0
                                    </MeshGeometry3D.Positions>

                                    <!-- テクスチャ -->
                                    <MeshGeometry3D.TextureCoordinates>
                                        0,0 1,0 0,1 1,1
                                    </MeshGeometry3D.TextureCoordinates>

                                    <!-- 三角形 -->
                                    <MeshGeometry3D.TriangleIndices>
                                        0,2,3
                                        0,3,1
                                    </MeshGeometry3D.TriangleIndices>
                                </MeshGeometry3D>
                            </GeometryModel3D.Geometry>

                            <!-- マテリアル -->
                            <GeometryModel3D.Material>
                                <DiffuseMaterial>
                                    <DiffuseMaterial.Brush>
                                        <VisualBrush>
                                            <VisualBrush.Visual>
                                                <MediaElement Source="wildlife.wmv"/>
                                            </VisualBrush.Visual>
                                        </VisualBrush>
                                    </DiffuseMaterial.Brush>
                                </DiffuseMaterial>
                            </GeometryModel3D.Material>
                            <GeometryModel3D.BackMaterial>
                                <DiffuseMaterial>
                                    <DiffuseMaterial.Brush>
                                        Green
                                    </DiffuseMaterial.Brush>
                                </DiffuseMaterial>
                            </GeometryModel3D.BackMaterial>

                        </GeometryModel3D>
                    </Model3DGroup>
                </ModelVisual3D.Content>
            </ModelVisual3D>
        </Viewport3D>

    </Grid>
</Window>

 

2. 3Dグラフィックスを回転する

Camera(カメラ)をY軸を中心に回転させることで、3Dグラフィックスを回転させてみます。
裏面にも同じ動画を張っています。
裏面の動画は音量を”0”にしています。

[実行結果]

回転しながら動画("wildlife.wmv")を再生します。

[プログラムソース "MainWindow.xaml"]
イメージ(静止画)版のプログラムとの違いはわずかです。”黄色” でマーキングします。  
<Window x:Class="_3D_Texture_Movie.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:_3D_Texture_Movie"
        mc:Ignorable="d"
        Title="MainWindow" Height="480" Width="640">
    <Grid>

        <Viewport3D>
            <!-- カメラ -->
            <Viewport3D.Camera>
                <PerspectiveCamera Position="-3,3,7" LookDirection="3,-3,-7" UpDirection="0,1,0" FieldOfView="45">
                    <PerspectiveCamera.Transform>
                        <RotateTransform3D>
                            <RotateTransform3D.Rotation>
                                <AxisAngleRotation3D x:Name="RotateCamera" Axis="0,1,0"/>
                            </RotateTransform3D.Rotation>
                        </RotateTransform3D>
                    </PerspectiveCamera.Transform>
                </PerspectiveCamera>
            </Viewport3D.Camera>

            <ModelVisual3D>
                <ModelVisual3D.Content>
                    <Model3DGroup>
                        <!-- ライト -->
                        <!--
                        <DirectionalLight Color="White" Direction="0,-1,-1"/>
                        -->
                        <AmbientLight Color="White"/>

                        <GeometryModel3D>
                            <GeometryModel3D.Geometry>
                                <MeshGeometry3D>
                                    <!-- 頂点 -->
                                    <MeshGeometry3D.Positions>
                                        -1,1,0
                                        1,1,0
                                        -1,-1,0
                                        1,-1,0
                                    </MeshGeometry3D.Positions>

                                    <!-- テクスチャ -->
                                    <MeshGeometry3D.TextureCoordinates>
                                        0,0 1,0 0,1 1,1
                                    </MeshGeometry3D.TextureCoordinates>

                                    <!-- 三角形 -->
                                    <MeshGeometry3D.TriangleIndices>
                                        0,2,3
                                        0,3,1
                                    </MeshGeometry3D.TriangleIndices>
                                </MeshGeometry3D>
                            </GeometryModel3D.Geometry>

                            <!-- マテリアル -->
                            <GeometryModel3D.Material>
                                <DiffuseMaterial>
                                    <DiffuseMaterial.Brush>
                                        <VisualBrush>
                                            <VisualBrush.Visual>
                                                <MediaElement Name="myMediaElementMaterial">
                                                    <MediaElement.Triggers>
                                                        <EventTrigger RoutedEvent="MediaElement.Loaded">
                                                            <EventTrigger.Actions>
                                                                <BeginStoryboard>
                                                                    <Storyboard>
                                                                        <MediaTimeline Source="wildlife.wmv" Storyboard.TargetName="myMediaElementMaterial" RepeatBehavior="Forever"/>
                                                                    </Storyboard>
                                                                </BeginStoryboard>
                                                            </EventTrigger.Actions>
                                                        </EventTrigger>
                                                    </MediaElement.Triggers>
                                                </MediaElement>
                                            </VisualBrush.Visual>
                                        </VisualBrush>
                                    </DiffuseMaterial.Brush>
                                </DiffuseMaterial>
                            </GeometryModel3D.Material>
                            <GeometryModel3D.BackMaterial>
                                <DiffuseMaterial>
                                    <DiffuseMaterial.Brush>
                                        <VisualBrush>
                                            <VisualBrush.Visual>
                                                <MediaElement Name="myMediaElementBackMaterial" Volume="0">
                                                    <MediaElement.Triggers>
                                                        <EventTrigger RoutedEvent="MediaElement.Loaded">
                                                            <EventTrigger.Actions>
                                                                <BeginStoryboard>
                                                                    <Storyboard>
                                                                        <MediaTimeline Source="wildlife.wmv" Storyboard.TargetName="myMediaElementBackMaterial" RepeatBehavior="Forever"/>
                                                                    </Storyboard>
                                                                </BeginStoryboard>
                                                            </EventTrigger.Actions>
                                                        </EventTrigger>
                                                    </MediaElement.Triggers>
                                                </MediaElement>
                                            </VisualBrush.Visual>
                                        </VisualBrush>
                                    </DiffuseMaterial.Brush>
                                </DiffuseMaterial>
                            </GeometryModel3D.BackMaterial>

                        </GeometryModel3D>
                    </Model3DGroup>
                </ModelVisual3D.Content>
            </ModelVisual3D>

            <Viewport3D.Triggers>
                <EventTrigger RoutedEvent="Viewport3D.Loaded">
                    <BeginStoryboard>
                        <Storyboard x:Name="myStoryboard">
                            <DoubleAnimation Storyboard.TargetName="RotateCamera" Storyboard.TargetProperty="Angle" RepeatBehavior="Forever" From="0" To="360" Duration="0:0:10"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Viewport3D.Triggers>
        </Viewport3D>

    </Grid>
</Window>

 

 

 

ライセンス

本ページの情報は、特記無い限り下記 MIT ライセンスで提供されます。

The MIT License (MIT)

Copyright © 2015-2022 Kinoshita Hidetoshi

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 

 


参考

 


 

変更履歴

2022-07-18 - ページデザイン更新
2015-08-06 - 新規作成

 

Programming Items トップページ

プライバシーポリシー