CSDN上下载一个资源,
3D模型导入wpf_wpf加载obj模型光线和相机配置-C#代码类资源-CSDN下载
从VS中打开,运行如下;
出来一个模型;
可以用鼠标旋转,翻转模型,从不同角度方位查看模型;
项目结构如下;
看一下鼠标功能是如何实现的;
在整个解决方案中找不到任何和鼠标事件相关的代码;那么是如何实现鼠标功能的呢?
看一下项目引用了3DTools这个DLL;
然后整个3D场景,
<Grid>
<tools:TrackballDecorator>
<Viewport3D
是包括在<tools:TrackballDecorator>标签中的;可能是此标签实现了鼠标控制功能;
下面来进一步看一下;
新建一个WPF项目,创建一个立方体;运行;
只能看到正面;没有任何的鼠标功能;
然后添加3DTools;
XAML中加入命名空间和前面的标签,然后再按下鼠标拖动,立方体确实可以翻转旋转了;
<Window x:Class="lftxz.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 >
<tools:TrackballDecorator>
<Viewport3D Margin="10">
<Viewport3D.Camera>
<PerspectiveCamera Position="0,0,8" x:Name="camera"></PerspectiveCamera>
</Viewport3D.Camera>
<Viewport3D.Children>
<ModelVisual3D>
<ModelVisual3D.Content>
<Model3DGroup >
<GeometryModel3D x:Name="F1">
<GeometryModel3D.Material>
<DiffuseMaterial Brush="Green"/>
</GeometryModel3D.Material>
<GeometryModel3D.Geometry>
<MeshGeometry3D Positions="0,0,0 2,0,0 2,2,0 0,2,0"
TriangleIndices="0,2,1 0,3,2">
</MeshGeometry3D>
</GeometryModel3D.Geometry>
</GeometryModel3D>
<GeometryModel3D x:Name="F2">
<GeometryModel3D.Material>
<DiffuseMaterial Brush="Blue"/>
</GeometryModel3D.Material>
<GeometryModel3D.Geometry>
<MeshGeometry3D Positions="0,0,0 0,0,2 0,2,2 0,2,0"
TriangleIndices="0,1,2 0,2,3">
</MeshGeometry3D>
</GeometryModel3D.Geometry>
</GeometryModel3D>
<GeometryModel3D x:Name="F3">
<GeometryModel3D.Material>
<DiffuseMaterial Brush="Gray"/>
</GeometryModel3D.Material>
<GeometryModel3D.Geometry>
<MeshGeometry3D Positions="0,0,0 0,0,2 2,0,2 2,0,0"
TriangleIndices="0,2,1 0,3,2">
</MeshGeometry3D>
</GeometryModel3D.Geometry>
</GeometryModel3D>
<GeometryModel3D x:Name="F4">
<GeometryModel3D.Material>
<DiffuseMaterial Brush="Bisque"/>
</GeometryModel3D.Material>
<GeometryModel3D.Geometry>
<MeshGeometry3D Positions="2,0,0 2,2,0 2,2,2 2,0,2"
TriangleIndices="0,1,2 0,2,3">
</MeshGeometry3D>
</GeometryModel3D.Geometry>
</GeometryModel3D>
<GeometryModel3D x:Name="F5">
<GeometryModel3D.Material>
<DiffuseMaterial Brush="Yellow"/>
</GeometryModel3D.Material>
<GeometryModel3D.Geometry>
<MeshGeometry3D Positions="0,2,2 2,2,2 2,2,0 0,2,0"
TriangleIndices="0,1,2 0,2,3">
</MeshGeometry3D>
</GeometryModel3D.Geometry>
</GeometryModel3D>
<GeometryModel3D x:Name="F6">
<GeometryModel3D.Material>
<DiffuseMaterial Brush="Red"/>
</GeometryModel3D.Material>
<GeometryModel3D.Geometry>
<MeshGeometry3D Positions="0,2,2 2,2,2 0,0,2 2,0,2"
TriangleIndices="0,2,3 0,3,1">
</MeshGeometry3D>
</GeometryModel3D.Geometry>
</GeometryModel3D>
</Model3DGroup>
</ModelVisual3D.Content>
</ModelVisual3D>
<ModelVisual3D x:Name="light">
<ModelVisual3D.Content>
<AmbientLight></AmbientLight>
</ModelVisual3D.Content>
</ModelVisual3D>
</Viewport3D.Children>
</Viewport3D>
</tools:TrackballDecorator>
</Grid>
</Window>