wpf动态加载obj格式的3D模型
前几天一个网友发现一个工具中有一个3D加载的代码,于是抽了出来加以修改。我也有幸拿到一份。现在拿出来和大家分享。
以下是我的一简单的加载代码:obj文件所在的路径请自行修改
1: PerspectiveCamera myPCamera;
2:
3: public MainWindow()
4: {
5: InitializeComponent();
6: WavefrontObjLoader wfl = new WavefrontObjLoader();
7: slider1.ValueChanged += new RoutedPropertyChangedEventHandler<double>(slider1_ValueChanged);
8: slider2.ValueChanged += new RoutedPropertyChangedEventHandler<double>(slider1_ValueChanged);
9: slider3.ValueChanged +=new RoutedPropertyChangedEventHandler<double>(slider1_ValueChanged);
10:
11: myPCamera = new PerspectiveCamera();
12:
13: // Specify where in the 3D scene the camera is.
14: myPCamera.Position = new Point3D(0, 0, 200);
15:
16: // Specify the direction that the camera is pointing.
17: myPCamera.LookDirection = new Vector3D(0, 0, -1);
18:
19: // Define camera's horizontal field of view in degrees.
20: myPCamera.FieldOfView = 1000;
21:
22: // Asign the camera to the viewport
23: vp.Camera = myPCamera;
24:
25: Model3DGroup myModel3DGroup = new Model3DGroup();
26:
27: DirectionalLight myDirectionalLight = new DirectionalLight();
28: myDirectionalLight.Color = Colors.White;
29: myDirectionalLight.Direction = new Vector3D(-0.61, -0.5, -0.61);
30:
31: myModel3DGroup.Children.Add(myDirectionalLight);
32: var m = wfl.LoadObjFile(@"d:\3d\1.obj");
33: m.Content =myModel3DGroup;
34: vp.Children.Add(m);
35: }
36:
37: void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
38: {
39: myPCamera.Position = new Point3D(slider2.Value , slider1.Value, slider3.Value);
40: }
41:
此类有一部份的obj文件加载不到,可能是因为支持不了太新的3Dmax工具生成的文件.