Skip to content

Instantly share code, notes, and snippets.

@sakapon
Last active November 18, 2016 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sakapon/635dca42214a603f0f014d876e38eed9 to your computer and use it in GitHub Desktop.
Save sakapon/635dca42214a603f0f014d876e38eed9 to your computer and use it in GitHub Desktop.
Wpf3DSample / DiceRotationWpf
<Window x:Class="DiceRotationWpf.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DiceRotationWpf"
Title="Dice Rotation" Height="600" Width="900">
<Window.Resources>
<Style x:Key="FaceStyle" TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="White"/>
<Setter Property="Width" Value="30"/>
<Setter Property="Height" Value="30"/>
<Setter Property="TextAlignment" Value="Center"/>
<Setter Property="FontSize" Value="20"/>
</Style>
<Style x:Key="RepeatButtonStyle" TargetType="{x:Type RepeatButton}">
<Setter Property="Width" Value="50"/>
<Setter Property="Height" Value="50"/>
<Setter Property="FontSize" Value="32"/>
</Style>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid x:Name="DicePanel" Background="#FF333333">
<Grid Visibility="Hidden">
<TextBlock x:Name="Face1" Style="{DynamicResource FaceStyle}" Text="1" Background="#FF222222"/>
<TextBlock x:Name="Face2" Style="{DynamicResource FaceStyle}" Text="2" Background="#FFDF2C2C"/>
<TextBlock x:Name="Face3" Style="{DynamicResource FaceStyle}" Text="3" Background="#FFEE9319"/>
<TextBlock x:Name="Face4" Style="{DynamicResource FaceStyle}" Text="4" Background="#FFE3E60A"/>
<TextBlock x:Name="Face5" Style="{DynamicResource FaceStyle}" Text="5" Background="#FF29D214"/>
<TextBlock x:Name="Face6" Style="{DynamicResource FaceStyle}" Text="6" Background="#FF4444BB"/>
</Grid>
<Viewport3D>
<Viewport3D.Camera>
<PerspectiveCamera Position="0,0,10"/>
</Viewport3D.Camera>
<ModelVisual3D>
<ModelVisual3D.Content>
<AmbientLight/>
</ModelVisual3D.Content>
</ModelVisual3D>
<ModelVisual3D>
<ModelVisual3D.Transform>
<Transform3DGroup>
<RotateTransform3D>
<RotateTransform3D.Rotation>
<AxisAngleRotation3D Axis="-0.8,0.3,0.5" Angle="60"/>
</RotateTransform3D.Rotation>
</RotateTransform3D>
<MatrixTransform3D x:Name="matrixTransform"/>
</Transform3DGroup>
</ModelVisual3D.Transform>
<!-- ModelVisual3D.Content は省略 -->
</ModelVisual3D>
</Viewport3D>
</Grid>
<Grid Background="#FFF8F8F8" Grid.Column="1" Width="300">
<Canvas Height="230" Width="230">
<RepeatButton Content="↑" CommandParameter="-x" Style="{DynamicResource RepeatButtonStyle}" Canvas.Left="90" Canvas.Top="20" Click="Rotate_Click"/>
<RepeatButton Content="↓" CommandParameter="+x" Style="{DynamicResource RepeatButtonStyle}" Canvas.Left="90" Canvas.Top="160" Click="Rotate_Click"/>
<RepeatButton Content="←" CommandParameter="-y" Style="{DynamicResource RepeatButtonStyle}" Canvas.Left="20" Canvas.Top="90" Click="Rotate_Click"/>
<RepeatButton Content="→" CommandParameter="+y" Style="{DynamicResource RepeatButtonStyle}" Canvas.Left="160" Canvas.Top="90" Click="Rotate_Click"/>
<RepeatButton Content="↘" CommandParameter="-z" Style="{DynamicResource RepeatButtonStyle}" Canvas.Left="160" Canvas.Top="20" Click="Rotate_Click"/>
<RepeatButton Content="↙" CommandParameter="+z" Style="{DynamicResource RepeatButtonStyle}" Canvas.Left="20" Canvas.Top="20" Click="Rotate_Click"/>
</Canvas>
</Grid>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media.Media3D;
namespace DiceRotationWpf
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
const double AngleDelta = 5.0;
static readonly Dictionary<string, Vector3D> Axes = new Dictionary<string, Vector3D>
{
{ "-x", Vector3D.Parse("-1,0,0") },
{ "+x", Vector3D.Parse("1,0,0") },
{ "-y", Vector3D.Parse("0,-1,0") },
{ "+y", Vector3D.Parse("0,1,0") },
{ "-z", Vector3D.Parse("0,0,-1") },
{ "+z", Vector3D.Parse("0,0,1") },
};
void Rotate_Click(object sender, RoutedEventArgs e)
{
var button = (RepeatButton)sender;
var command = (string)button.CommandParameter;
matrixTransform.Rotate(Axes[command], AngleDelta);
}
}
public static class Media3DUtility
{
public static void Rotate(this MatrixTransform3D transform, Vector3D axis, double angle)
{
var matrix = transform.Matrix;
matrix.Rotate(new Quaternion(axis, angle));
transform.Matrix = matrix;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment