1
2

More than 3 years have passed since last update.

WPFで滑らかマウス操作を実現

Posted at

WPFとMainWindowで立方体を作成

以下のような立方体を作成し、マウス操作で空間回転できるようにしました。
キャプチャ.PNG

WPFのソースコード

・以下のようになりました。

MainWindow.xaml
<Window x:Class="RotateTheCube.Rotate"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="" Height="600" Width="900" Background="Black" MouseLeftButtonUp="MouseUp3D">
    <Viewport3D>
        <Viewport3D.Camera>
            <PerspectiveCamera LookDirection="0,0,-1" Position="0,0,5" />
        </Viewport3D.Camera>
        <ModelVisual3D>
            <ModelUIElement3D>
                <DirectionalLight Color="Red" Direction="-0,-0.5,-0.6" />
            </ModelUIElement3D>
            <ModelUIElement3D>
                <DirectionalLight Color="White" Direction="0.612372,-0.5,-0.612372" />
            </ModelUIElement3D>
            <ModelUIElement3D MouseMove="MouseMove3D" MouseLeftButtonDown="MouseDown3D" >
                <GeometryModel3D>
                    <GeometryModel3D.Geometry>
                        <MeshGeometry3D
                                Positions="-0.5  0.5  0.5,  0.5  0.5  0.5,
                                           -0.5 -0.5  0.5,  0.5 -0.5  0.5,
                                            0.5  0.5 -0.5, -0.5  0.5 -0.5,
                                            0.5 -0.5 -0.5, -0.5 -0.5 -0.5,
                                           -0.5  0.5 -0.5, -0.5  0.5  0.5,
                                           -0.5 -0.5 -0.5, -0.5 -0.5  0.5,
                                            0.5  0.5  0.5,  0.5  0.5 -0.5,
                                            0.5 -0.5  0.5,  0.5 -0.5 -0.5,
                                           -0.5  0.5 -0.5,  0.5  0.5 -0.5,
                                           -0.5  0.5  0.5,  0.5  0.5  0.5,
                                            0.5 -0.5 -0.5, -0.5 -0.5 -0.5,
                                            0.5 -0.5  0.5, -0.5 -0.5  0.5"
                                TriangleIndices=" 0  2  1,  1  2  3
                                                  4  6  5,  5  6  7,
                                                  8 10  9,  9 10 11,
                                                 12 14 13, 13 14 15
                                                 16 18 17, 17 18 19
                                                 20 22 21, 21 22 23"/>
                    </GeometryModel3D.Geometry>
                    <GeometryModel3D.Material>
                        <DiffuseMaterial Brush="White"/>
                    </GeometryModel3D.Material>
                    <GeometryModel3D.Transform>
                        <MatrixTransform3D   x:Name="myTransform"/>
                    </GeometryModel3D.Transform>
                </GeometryModel3D>
            </ModelUIElement3D>
        </ModelVisual3D>
    </Viewport3D>
</Window>

C#のソースコード

・続いてC#のコードです。

MainWindow.xaml.cs
// ***************************
// * 立方体を空間で回転させる
// *   2020.07.05  ProOJI
// ***************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Media3D; //3D行列使用
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace RotateTheCube
{
    public partial class Rotate : Window
    {
        Matrix3D m = Matrix3D.Identity;
        private bool _isDrag = false;
        private Point _Offset;
        private void MouseDown3D(object sender, MouseButtonEventArgs e)
        {
            _isDrag = true;
            _Offset = e.GetPosition(this);
        }
        private void MouseUp3D(object sender, MouseButtonEventArgs e)
        {
            _isDrag = false;
        }
        private void MouseMove3D(object sender, MouseEventArgs e)
        {
            if (_isDrag == true)
            {
                Point pt = e.GetPosition(this);
                m.Rotate(new Quaternion(new Vector3D(0, 1, 0), (pt.X - _Offset.X) / 50));
                m.Rotate(new Quaternion(new Vector3D(1, 0, 0), (pt.Y - _Offset.Y) / 50));
                myTransform.Matrix = m;
            }
        }
    }
}

まとめ

線形代数がこんなところで役立つとは…
数学をもっと学ぼうと思いました。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2