博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPF动态加载3D 放大-旋转-平移
阅读量:5890 次
发布时间:2019-06-19

本文共 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/

你可能感兴趣的文章
C语言写单链表的创建、释放、追加(即总是在最后的位置增加节点)
查看>>
C# LINQ详解(一)
查看>>
视频直播点播nginx-rtmp开发手册中文版
查看>>
ruby学习总结04
查看>>
Binary Tree Paths
查看>>
Ueditor自定义ftp上传
查看>>
线程以及多线程
查看>>
PHP队列的实现
查看>>
单点登录加验证码例子
查看>>
[T-SQL]从变量与数据类型说起
查看>>
occActiveX - ActiveX with OpenCASCADE
查看>>
iOS8中定位服务的变化(CLLocationManager协议方法不响应,无法回掉GPS方法,不出现获取权限提示)...
查看>>
BeanUtils\DBUtils
查看>>
VC 创建托盘,托盘tooltip。右键托盘菜单,点击别的地方会隐藏掉的问题。
查看>>
第一天,新的定义
查看>>
WPF EventSetter Handler Command
查看>>
polya定理,环形涂色
查看>>
day4-装饰器前奏
查看>>
forward和redirect的区别
查看>>
使用JavaMail完成邮件的编写
查看>>