本文共 4093 字,大约阅读时间需要 13 分钟。
WavefrontObjLoader.cs
第二步:ModelVisual3DWithName.cs
public class ModelVisual3DWithName:ModelVisual3D
{ public string Name { get; set; }public object Tag { get; set; }
}
第三步:MainWindow.xmal
<Grid x:Name="lay">
<Viewport3D x:Name="vp" Margin="0"> <Viewport3D.Camera> <PerspectiveCamera FieldOfView="45" FarPlaneDistance="100" LookDirection="0,0,-3.4142135623731" NearPlaneDistance="0.1" Position="0,0,3.4142135623731" UpDirection="0,3,0"/> </Viewport3D.Camera> </Viewport3D> <Slider Height="23" HorizontalAlignment="Left" Margin="220,288,0,0" Name="slider1" VerticalAlignment="Top" Width="100" Maximum="2000" /> <Slider Height="23" HorizontalAlignment="Left" Margin="49,288,0,0" Name="slider2" VerticalAlignment="Top" Width="100" Maximum="1000" /> <Slider Height="23" HorizontalAlignment="Left" Margin="367,288,0,0" Name="slider3" VerticalAlignment="Top" Width="100" Minimum="200" Maximum="2000" /> <Slider Height="23" HorizontalAlignment="Left" Margin="567,288,0,0" Name="slider4" VerticalAlignment="Top" Width="100" Minimum="200" Maximum="2000" /> </Grid>第四步:MainWindow.xmal
PerspectiveCamera myPCamera;
DirectionalLight myDirectionalLight; Model3DGroup myModel3DGroup; WavefrontObjLoader wfl; ModelVisual3DWithName mv3dw; public MainWindow() { InitializeComponent(); wfl = new WavefrontObjLoader(); slider1.ValueChanged += new RoutedPropertyChangedEventHandler<double>(slider1_ValueChanged); slider2.ValueChanged += new RoutedPropertyChangedEventHandler<double>(slider2_ValueChanged); slider3.ValueChanged += new RoutedPropertyChangedEventHandler<double>(slider3_ValueChanged); slider4.ValueChanged += new RoutedPropertyChangedEventHandler<double>(slider4_ValueChanged); createCamera(); createLight(); createModel3D(); create360(); //createAnimation(); } #region //光源 private void createLight() { myDirectionalLight = new DirectionalLight(); myDirectionalLight.Color = Colors.White; myDirectionalLight.Direction = new Vector3D(-0.61, -0.5, -0.61); } #endregion #region //摄像机 private void createCamera() { myPCamera = new PerspectiveCamera(); //myPCamera.Position = new Point3D(0,-1743,-4000);//看的方向 myPCamera.Position = new Point3D(0, 0, 1000); myPCamera.LookDirection = new Vector3D(0,0,-1000);//摄影机看的方向 myPCamera.UpDirection = new Vector3D(0, 1, -0); myPCamera.FieldOfView = 45;//法向量 摄影机上下颠倒,左转右转 myPCamera.NearPlaneDistance = 0.1; myPCamera.FarPlaneDistance = 11050;vp.Camera = myPCamera;
} #endregion #region //模型 private void createModel3D() { myModel3DGroup = new Model3DGroup(); myModel3DGroup.Children.Add(myDirectionalLight);var m = wfl.LoadObjFile(@"C:/Users/Adminstrator/Desktop/3D动态加载/WpfApplication2/xx.obj");
m.Content = myModel3DGroup;vp.Children.Add(m);
} #endregion #region //360旋转动作 RotateTransform3D rtf3D; AxisAngleRotation3D aar; private void create360() { rtf3D = new RotateTransform3D(); aar = new AxisAngleRotation3D(); this.RegisterName("myAngleRotation", aar); aar.Angle = 0; aar.Axis = new Vector3D(0, 3, 0); rtf3D.Rotation = aar; myModel3DGroup.Transform = rtf3D; myPCamera.Transform = rtf3D; } Storyboard sbd; DoubleAnimation dan; private void createAnimation() { sbd = new Storyboard(); dan = new DoubleAnimation(0,360,new Duration(TimeSpan.FromSeconds(10))); dan.RepeatBehavior = RepeatBehavior.Forever; Storyboard.SetTargetName(dan,"myAngleRotation"); Storyboard.SetTargetProperty(dan, new PropertyPath(AxisAngleRotation3D.AngleProperty)); sbd.Children.Add(dan); sbd.BeginTime = TimeSpan.FromSeconds(5);//开始时间 sbd.Begin(this); } #endregion void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) { myPCamera.Position = new Point3D(slider1.Value , slider2.Value, slider3.Value); } void slider2_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) { myPCamera.Position = new Point3D(slider1.Value, slider2.Value, slider3.Value); } void slider3_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) { myPCamera.Position = new Point3D(slider1.Value, slider2.Value, slider3.Value); } void slider4_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) { aar.Angle = slider4.Value; }
转载地址:http://cibsx.baihongyu.com/