C#?WPF實(shí)現(xiàn)3D操作幾何體效果
操作幾何體
據(jù)說我的世界是三個(gè)程序員用一周開發(fā)出來的,那一個(gè)程序員用半天開發(fā)出一個(gè)乞丐版的我的世界,講道理是完全沒有問題的。
而眾所周知,我的世界就是無數(shù)個(gè)像素塊的集合,而像素塊也就是立方體。關(guān)于新建立方體,這個(gè)大家已經(jīng)非常熟練了,不僅能新建一個(gè)立方體,甚至能新建要多少有多少的立方體。
新建正方體
但目前并不能用手新建,所以接下來添加一個(gè)快捷方式Ctrl+N來快速創(chuàng)建立方體。當(dāng)按下Ctrl+N時(shí)效果為

這對(duì)于已經(jīng)能生成一排立方體的人來說絕對(duì)是小意思了,首先在構(gòu)造函數(shù)中綁定快捷方式
public MainWindow()
{
InitializeComponent();
KeyDown += MainWindow_KeyDown;
}
MainWindow_KeyDown定義為
private void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
if ((Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) && Keyboard.IsKeyDown(Key.N))
{
MeshGeometry3D mesh = MakeCubeMesh(0, 0, 0, 1);
Color color = Color.FromArgb(255, 0, 255, 0);
DiffuseMaterial material = new DiffuseMaterial(new SolidColorBrush(color));
GeometryModel3D model = new GeometryModel3D(mesh, material);
group3d.Children.Add(model);
}
}
其中MakeCubeMesh為自定義的函數(shù),早在本系列第一篇博客就已經(jīng)寫過了,其他諸如光效等亦然。唯一的區(qū)別是這次并不一開始就生成多個(gè)立方體,所以無需DefineModel函數(shù)。
設(shè)置立方體位置
剛剛雖然新建了一個(gè)立方體,但并不能確定立方體的位置,接下來就要新建一個(gè)對(duì)話框,用以設(shè)置新建立方體的位置。
右鍵項(xiàng)目,新建窗口,名為ParaDialog,其xaml為
<Window x:Class="MyWord.ParaDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="輸入?yún)?shù)" Height="180" Width="300">
<DockPanel LastChildFill="True">
<StackPanel DockPanel.Dock="Bottom"
Orientation="Horizontal" HorizontalAlignment="Right">
<Button Content="設(shè)置" Click="okButton_Click" Margin="5" Width="60" Height="25"/>
<Button Content="取消" IsCancel="True" Margin="5" Width="60" Height="25"/>
</StackPanel>
<UniformGrid Columns="1" Margin="5" x:Name="ufgLabel"/>
<UniformGrid Columns="1" Margin="5" x:Name="ufgTextBox"/>
??????? </DockPanel>
</Window>
其cs的核心代碼為
public partial class ParaDialog : Window
{
static readonly string[] labels = new string[4] { "x坐標(biāo)", "y坐標(biāo)", "z坐標(biāo)", "邊長(zhǎng)" };
List<TextBox> paraBoxes = new List<TextBox>();
public double[] para;
public ParaDialog(double[] para)
{
this.para = para;
InitializeComponent();
string tmp;
for (int i = 0; i < 4; i++)
{
//向UniformGrid中歐給填充文字塊
ufgLabel.Children.Add(new TextBlock() { Text = labels[i] });
paraBoxes.Add(new TextBox());
ufgTextBox.Children.Add(paraBoxes[i]);
}
}
private void okButton_Click(object sender, RoutedEventArgs e)
{
for (int i = 0; i < 4; i++)
para[i] = double.Parse(paraBoxes[i].Text);
DialogResult = true;
}
}
最后,更改Ctrl+N的響應(yīng)代碼
//...前面不用改
if ((Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) && Keyboard.IsKeyDown(Key.N))
{
double[] para = new double[4];
ParaDialog pDialog = new ParaDialog(para);
pDialog.ShowDialog();
if (pDialog.DialogResult != true)
return;
MeshGeometry3D mesh = MakeCubeMesh(para[0],para[1],para[2],para[3]);
//...后面不用改
}

選中立方體
若想操作幾何體,前提是選中它,接下來就綁定一個(gè)鼠標(biāo)動(dòng)作,來完成選中的操作,先更改xaml代碼,將Viewport3D放到一個(gè)border中
<Border Name="mainBorder" Background="White" MouseDown="mainBorder_MouseDown">
<Viewport3D x:Name="v3dMain">
</Viewport3D>
</Border>
然后新建mainBorder_MouseDown函數(shù)
private void mainBorder_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Released) return;
Color color = Color.FromArgb(255, 0, 255, 0);
var material = new DiffuseMaterial(new SolidColorBrush(color));
//獲取鼠標(biāo)在對(duì)象中的位置
Point mousePos = e.GetPosition(v3dMain);
// 執(zhí)行點(diǎn)擊操作
HitTestResult result = VisualTreeHelper.HitTest(v3dMain, mousePos);
//此即鼠標(biāo)點(diǎn)擊到曲面上的結(jié)果
var meshResult = result as RayMeshGeometry3DHitTestResult;
GeometryModel3D model = null;
if ((meshResult != null) && (meshResult.ModelHit is GeometryModel3D))
model = meshResult.ModelHit as GeometryModel3D;
//如果剛才選了別的模型,則使之恢復(fù)綠色
if (SelectedModel != null)
SelectedModel.Material = material;
//選擇新的模型
SelectedModel = model;
if (model != null)
model.Material = new DiffuseMaterial(Brushes.Fuchsia);
}
其效果為

挪動(dòng)幾何體
既然已經(jīng)可以選中了,那么挪動(dòng)什么的絕對(duì)就是小意思了。
回顧此前的鼠標(biāo)操作攝像機(jī),其基本流程是,鼠標(biāo)點(diǎn)擊之后,綁定另一個(gè)函數(shù),當(dāng)松開鼠標(biāo)時(shí)解綁。
在WPF 3D中,提供了Tranform成員,可用于移動(dòng)幾何體,所以在新建幾何體時(shí),務(wù)必注意添加一行
//MainWindow_KeyDown函數(shù) model.Transform = new TranslateTransform3D(0, 0, 0);
然后添加全局變量用以保存舊的位置
private Point3D oldPoint;
然后修改mainBorder_MouseDown,其實(shí)只需在末尾添加
mainBorder.CaptureMouse(); mainBorder.MouseMove += MainBorder_MouseMove; mainBorder.MouseUp += MainBorder_MouseUp;
接下來是鼠標(biāo)挪動(dòng)和彈起時(shí)的動(dòng)作,其中彈起時(shí)非常簡(jiǎn)單,無非是解綁鼠標(biāo)動(dòng)作;而鼠標(biāo)挪動(dòng)時(shí),則需更改幾何體的變化參數(shù)。
private void MainBorder_MouseUp(object sender, MouseButtonEventArgs e)
{
mainBorder.ReleaseMouseCapture();
mainBorder.MouseMove -= MainBorder_MouseMove;
mainBorder.MouseUp -= MainBorder_MouseUp;
}
private void MainBorder_MouseMove(object sender, MouseEventArgs e)
{
Point newPoint = e.GetPosition(mainBorder);
var res = VisualTreeHelper.HitTest(v3dMain, newPoint);
if (res == null) return;
var newResult = res as RayMeshGeometry3DHitTestResult;
var deltaPt = newResult.PointHit - oldPoint;
var trans = SelectedModel.Transform as TranslateTransform3D;
trans.OffsetX += deltaPt.X;
trans.OffsetY += deltaPt.Y;
trans.OffsetZ += deltaPt.Z;
oldPoint = newResult.PointHit;
}
效果為

以上就是C# WPF實(shí)現(xiàn)3D操作幾何體效果的詳細(xì)內(nèi)容,更多關(guān)于C# WPF 3D操作幾何體的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SuperSocket入門--Telnet服務(wù)器和客戶端請(qǐng)求處理
本文的控制臺(tái)項(xiàng)目是根據(jù)SuperSocket官方Telnet示例代碼進(jìn)行調(diào)試的,官方示例代碼:Telnet示例。下面跟著小編一起來看下吧2017-01-01
c# 從IE瀏覽器獲取當(dāng)前頁(yè)面的內(nèi)容
從IE瀏覽器獲取當(dāng)前頁(yè)面內(nèi)容可能有多種方式,今天我所介紹的是其中一種方法?;驹恚寒?dāng)鼠標(biāo)點(diǎn)擊當(dāng)前IE頁(yè)面時(shí),獲取鼠標(biāo)的坐標(biāo)位置,根據(jù)鼠標(biāo)位置獲取當(dāng)前頁(yè)面的句柄,然后根據(jù)句柄,調(diào)用win32的東西進(jìn)而獲取頁(yè)面內(nèi)容。感興趣的朋友可以參考下本文2021-06-06
使用C#開源文件實(shí)時(shí)監(jiān)控工具Tail&TailUI介紹
本篇文章小編為大家介紹,使用C#開源文件實(shí)時(shí)監(jiān)控工具Tail&TailUI介紹。需要的朋友參考下2013-04-04
C#實(shí)現(xiàn)IDisposable接口釋放非托管資源
這篇文章主要為大家介紹了C#實(shí)現(xiàn)IDisposable接口釋放非托管資源,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
深入解析C#編程中struct所定義的結(jié)構(gòu)
這篇文章主要介紹了C#編程中struct所定義的結(jié)構(gòu),與C++一樣,C#語(yǔ)言同時(shí)擁有類和結(jié)構(gòu),需要的朋友可以參考下2016-01-01
基于WPF實(shí)現(xiàn)3D畫廊動(dòng)畫效果的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何基于WPF實(shí)現(xiàn)簡(jiǎn)單的3D畫廊動(dòng)畫效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-02-02

