基于WPF实现3D画廊动画效果的示例代码
下面是一个基于WPF实现3D画廊动画效果的示例代码:
usingSystem;usingSystem.Windows;
usingSystem.Windows.Controls;
usingSystem.Windows.Input;
usingSystem.Windows.Media;
usingSystem.Windows.Media.Animation;
usingSystem.Windows.Media.Media3D;
namespaceWPF3DGallery
{
publicpartialclassMainWindow:Window
{
privateconstdoubleAngleIncrement=30;
privateconstdoubleScaleIncrement=0.1;
privatePerspectiveCamera_camera;
privateModel3DGroup_modelGroup;
privatePoint_lastMousePosition;
publicMainWindow()
{
InitializeComponent();
CreateScene();
}
privatevoidCreateScene()
{
//创建3D场景
_modelGroup=newModel3DGroup();
_modelGroup.Children.Add(CreateCubeModel(newPoint3D(-100,0,0),Colors.Red));
_modelGroup.Children.Add(CreateCubeModel(newPoint3D(0,0,0),Colors.Green));
_modelGroup.Children.Add(CreateCubeModel(newPoint3D(100,0,0),Colors.Blue));
//创建灯光
vardirectionalLight=newDirectionalLight(Colors.White,newVector3D(0,-0.5,-1));
_modelGroup.Children.Add(directionalLight);
//设置相机
_camera=newPerspectiveCamera(newPoint3D(0,0,200),newVector3D(0,0,-1),newVector3D(0,1,0));
//将模型和相机添加到Viewport3D中
varviewport=newViewport3D();
viewport.Camera=_camera;
viewport.Children.Add(_modelGroup);
//设置Viewport3D为窗口的内容
Content=viewport;
}
privateGeometryModel3DCreateCubeModel(Point3Dposition,Colorcolor)
{
varmesh=newMeshGeometry3D();
mesh.Positions.Add(newPoint3D(-50,-50,50));
mesh.Positions.Add(newPoint3D(50,-50,50));
mesh.Positions.Add(newPoint3D(-50,50,50));
mesh.Positions.Add(newPoint3D(50,50,50));
mesh.Positions.Add(newPoint3D(-50,-50,-50));
mesh.Positions.Add(newPoint3D(50,-50,-50));
mesh.Positions.Add(newPoint3D(-50,50,-50));
mesh.Positions.Add(newPoint3D(50,50,-50));
mesh.TriangleIndices.Add(0);
mesh.TriangleIndices.Add(1);
mesh.TriangleIndices.Add(2);
mesh.TriangleIndices.Add(2);
mesh.TriangleIndices.Add(1);
mesh.TriangleIndices.Add(3);
mesh.TriangleIndices.Add(4);
mesh.TriangleIndices.Add(5);
mesh.TriangleIndices.Add(6);
mesh.TriangleIndices.Add(6);
mesh.TriangleIndices.Add(5);
mesh.TriangleIndices.Add(7);
mesh.TriangleIndices.Add(2);
mesh.TriangleIndices.Add(3);
mesh.TriangleIndices.Add(6);
mesh.TriangleIndices.Add(6);
mesh.TriangleIndices.Add(3);
mesh.TriangleIndices.Add(7);
mesh.TriangleIndices.Add(0);
mesh.TriangleIndices.Add(2);
mesh.TriangleIndices.Add(4);
mesh.TriangleIndices.Add(4);
mesh.TriangleIndices.Add(2);
mesh.TriangleIndices.Add(6);
mesh.TriangleIndices.Add(1);
mesh.TriangleIndices.Add(0);
mesh.TriangleIndices.Add(5);
mesh.TriangleIndices.Add(5);
mesh.TriangleIndices.Add(0);
mesh.TriangleIndices.Add(4);
mesh.TriangleIndices.Add(1);
mesh.TriangleIndices.Add(5);
mesh.TriangleIndices.Add(3);
mesh.TriangleIndices.Add(3);
mesh.TriangleIndices.Add(5);
mesh.TriangleIndices.Add(7);
varmaterial=newDiffuseMaterial(newSolidColorBrush(color));
returnnewGeometryModel3D(mesh,material)
{
Transform=newTranslateTransform3D(position.X,position.Y,position.Z)
};
}
protectedoverridevoidOnMouseLeftButtonDown(MouseButtonEventArgse)
{
_lastMousePosition=e.GetPosition(this);
CaptureMouse();
}
protectedoverridevoidOnMouseLeftButtonUp(MouseButtonEventArgse)
{
ReleaseMouseCapture
版权声明
本文仅代表作者观点,不代表米安网络立场。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。