WPF绘制3D小demo
试过了WPF原生3D和HelixToolkit.Wpf,感觉还是SharpDX的效果比较好,所以使用了HelixToolkit.Wpf.SharpDX
初学,仅供参考,没搞懂怎么双面渲染,所以每个面用了2个三角形分别显示正面和反面
<Grid>
<hx:Viewport3DX EnableSwapChainRendering="True" FXAALevel="Low" BackgroundColor="White" Name="viewport3DX">
<hx:Viewport3DX.Camera>
<hx:PerspectiveCamera FarPlaneDistance="10000" x:Name="camera" Position="1,1,10" LookDirection="-1,-1,-10" UpDirection="0,1,0" FieldOfView="20" >
</hx:PerspectiveCamera>
</hx:Viewport3DX.Camera>
<hx:AmbientLight3D Color="White" />
<hx:DirectionalLight3D Direction="{Binding ElementName=camera,Path=LookDirection}" Color="White" />
<hx:Viewport3DX.InputBindings>
<MouseBinding Command="hx:ViewportCommands.Rotate" Gesture="LeftClick" />
<MouseBinding Command="hx:ViewportCommands.Zoom" Gesture="MiddleClick" />
<MouseBinding Command="hx:ViewportCommands.Pan" Gesture="RightClick" />
</hx:Viewport3DX.InputBindings>
</hx:Viewport3DX>
</Grid>
public MainWindow()
{
InitializeComponent();
viewport3DX.EffectsManager = new DefaultEffectsManager();
var MeshBuilder = new MeshBuilder();
var MeshBuilder2 = new MeshBuilder();
var MeshBuilder3 = new MeshBuilder();
Vector3Collection positions = new Vector3Collection()
{
new Vector3(-1,-1,-1),
new Vector3(1,-1,-1),
new Vector3(1,1,-1),
new Vector3(-1,1,-1),
new Vector3(-1,-1,1),
new Vector3(1,-1,1),
new Vector3(1,1,1),
new Vector3(-1,1,1),
};
AddTriangle(MeshBuilder,positions[0], positions[1], positions[2]);
AddTriangle(MeshBuilder, positions[2], positions[3], positions[0]);
AddTriangle(MeshBuilder, positions[4], positions[5], positions[6]);
AddTriangle(MeshBuilder, positions[6], positions[7], positions[4]);
AddTriangle(MeshBuilder, positions[0], positions[1], positions[5]);
AddTriangle(MeshBuilder, positions[5], positions[4], positions[0]);
AddTriangle(MeshBuilder, positions[3], positions[2], positions[6]);//后面
AddTriangle(MeshBuilder, positions[6], positions[7], positions[3]);
AddTriangle(MeshBuilder, positions[3], positions[0], positions[4]);//左面
AddTriangle(MeshBuilder, positions[4], positions[7], positions[3]);
AddTriangle(MeshBuilder, positions[5], positions[6], positions[2]);//右面
AddTriangle(MeshBuilder, positions[2], positions[1], positions[5]);
var geometryModel3D = new MeshGeometryModel3D
{
Geometry = MeshBuilder.ToMesh(),
Material = PhongMaterials.Green,
CullMode=SharpDX.Direct3D11.CullMode.Back//取消背面渲染
};
//大量三角面渲染测试
//for (int j = 0; j < 500; j++)
//{
// for (int i = 0; i < 500; i++)
// {
// AddTriangle(MeshBuilder, positions[0], positions[1], positions[5], offsetx: 2 * (i + 1), offsetz: 2 * j);
// AddTriangle(MeshBuilder, positions[5], positions[4], positions[0], offsetx: 2 * (i + 1), offsetz: 2 * j);
// }
//}
var model3DGroup = new GroupModel3D();
model3DGroup.Children.Add(geometryModel3D);
viewport3DX.Items.Add(model3DGroup);
}
private void AddTriangle(MeshBuilder MeshBuilder, Vector3 p0, Vector3 p1, Vector3 p2, float offsetx = 0,float offsety=0,float offsetz=0)
{
var m0 = new Vector3(p0.X + offsetx, p0.Y + offsety, p0.Z + offsetz);
var m1 = new Vector3(p1.X + offsetx, p1.Y + offsety, p1.Z + offsetz);
var m2 = new Vector3(p2.X + offsetx, p2.Y + offsety, p2.Z + offsetz);
MeshBuilder.AddTriangle(m0, m1, m2);
MeshBuilder.AddTriangle(m2, m1, m0);
}