前面已經(jīng)說(shuō)了,WPF項(xiàng)目中引入3DTools dll之后,在xaml中加入它的命名空間,
xmlns:tools="clr-namespace:_3DTools;assembly=3DTools"
把<Viewport3D>標(biāo)簽包含在<tools:TrackballDecorator>標(biāo)簽之中,就可以用鼠標(biāo)控制??<Viewport3D> 中的3d模型;
看一下單獨(dú)用3DTools dll能否加載3d模型;沒(méi)有資料;
用ILSpy看一下;
? ? 直接從3DTools包含的類(lèi),類(lèi)名看,有數(shù)學(xué)工具類(lèi),可能提供一些矩陣運(yùn)算;有前面的TrackballDecorator類(lèi);還有碰撞檢測(cè)類(lèi),hit;看不出是否有加載3d模型的類(lèi);有時(shí)間再看;
之前做過(guò)一些wpf 簡(jiǎn)單3D物體,都是直接寫(xiě)在xaml代碼里;
下面從cs代碼里做一下簡(jiǎn)單3D物體;
在xaml中給3d根節(jié)點(diǎn)加上名稱屬性,這樣可以在cs代碼中引用此節(jié)點(diǎn);
? ? <Viewport3D Name="v3">......</Viewport3D>
xaml代碼如下;
<Window x:Class="mytest1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:tools="clr-namespace:_3DTools;assembly=3DTools"
Title="MainWindow" Height="350" Width="525">
<Grid Width="300" Height="200" ShowGridLines="True">
<tools:TrackballDecorator>
<Viewport3D Name="v3">
</Viewport3D>
</tools:TrackballDecorator>
</Grid>
</Window>
cs代碼如下;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Media3D;
namespace mytest1
{
/// <summary>
/// MainWindow.xaml 的交互邏輯
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// 創(chuàng)建一個(gè)3D正方體
MeshGeometry3D mesh = new MeshGeometry3D();
mesh.Positions = new Point3DCollection()
{
new Point3D(-1, 1, 1),
new Point3D(1, 1, 1),
new Point3D(-1, -1, 1),
new Point3D(1, -1, 1),
new Point3D(-1, 1, -1),
new Point3D(1, 1, -1),
new Point3D(-1, -1, -1),
new Point3D(1, -1, -1),
};
mesh.TriangleIndices = new Int32Collection()
{
0, 1, 2,
1, 3, 2,
1, 5, 3,
5, 7, 3,
5, 4, 7,
4, 6, 7,
4, 0, 6,
0, 2, 6,
2, 3, 6,
3, 7, 6,
4, 5, 0,
5, 1, 0,
};
// 創(chuàng)建一個(gè)材質(zhì)
DiffuseMaterial material = new DiffuseMaterial(new SolidColorBrush(Colors.Blue));
// 創(chuàng)建一個(gè)模型
GeometryModel3D model = new GeometryModel3D(mesh, material);
// 創(chuàng)建一個(gè)模型組
Model3DGroup group = new Model3DGroup();
group.Children.Add(model);
// 將模型組添加到Viewport3D中
v3.Children.Add(new ModelVisual3D() { Content = group });
PerspectiveCamera camera = new PerspectiveCamera();
camera.Position = new Point3D(0, 0, 5);
camera.LookDirection = new Vector3D(0, 0, -1);
camera.FieldOfView = 60;
v3.Camera = camera;
}
}
}
? ? ?從cs代碼中創(chuàng)建一個(gè)立方體模型;攝像機(jī)也在cs代碼中創(chuàng)建;然后加入名為"v3"的節(jié)點(diǎn);
運(yùn)行如下;
?
? ? ? ?
順帶看一下ILSpy的許可證,它是MIT許可;
MIT許可證(The MIT License)是許多軟件授權(quán)條款中,被廣泛使用的其中一種;與其他常見(jiàn)的軟件授權(quán)條款(如GPL、LGPL、BSD)相比,MIT是相對(duì)寬松的軟件授權(quán)條款;文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-689721.html
MIT內(nèi)容與三條款BSD許可證(3-clause BSD license)內(nèi)容頗為近似,但是賦予軟件被授權(quán)人更大的權(quán)利與更少的限制;文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-689721.html
到了這里,關(guān)于wpf從cs代碼創(chuàng)建簡(jiǎn)單3D物體和3D Tools研究的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!