WPF dynamic load 3D Zoom-rotate-pan

Source: Internet
Author: User

Original: WPF dynamic load 3D Zoom-rotate-pan

WavefrontObjLoader.cs

Step Two: ModelVisual3DWithName.cs

public class Modelvisual3dwithname:modelvisual3d
   {
public string Name {get; set;}

public Object Tag {get; set;}

   }

Step Three: Mainwindow.xmal

<grid x:name= "Lay" >
<viewport3d x:name= "VP" margin= "0" >
<Viewport3D.Camera>
<perspectivecamerafieldofview= "farplanedistance=" lookdirection= "0,0,-3.4142135623731" Nearplanedistance= "0.1" position= "0,0,3.4142135623731" updirection= "0,3,0"/>
</Viewport3D.Camera>
</Viewport3D>
<slider height= "horizontalalignment=" "left" margin= "220,288,0,0" name= "Slider1" verticalalignment= "Top" Width= "Maximum=" "/>"
<slider height= "horizontalalignment=" "left" margin= "49,288,0,0" name= "Slider2" verticalalignment= "Top" Width= " "Maximum=" "/>"
<slider height= "horizontalalignment=" "left" margin= "367,288,0,0" name= "Slider3" verticalalignment= "Top" Width= "Minimum=" "maximum=" "/>"
<slider height= "horizontalalignment=" "left" margin= "567,288,0,0" name= "Slider4" verticalalignment= "Top" Width= "Minimum=" "maximum=" "/>"
</Grid>

Fourth Step: Mainwindow.xmal

Perspectivecamera Mypcamera;
DirectionalLight Mydirectionallight;
Model3DGroup Mymodel3dgroup;
Wavefrontobjloader WFL;
Modelvisual3dwithname MV3DW;
Public MainWindow ()
{
InitializeComponent ();
WFL = Newwavefrontobjloader ();Slider1. ValueChanged + = NewroutedpropertychangedeveNthandler<double> (slider1_valuechanged);
Slider2. ValueChanged + = NewroutedpropertychangedeveNthandler<double> (slider2_valuechanged);
Slider3. ValueChanged + = NewroutedpropertychangedeveNthandler<double> (slider3_valuechanged);
Slider4. ValueChanged + = NewroutedpropertychangedeveNthandler<double> (slider4_valuechanged);
Createcamera ();
Createlight ();
Createmodel3d ();
Create360 ();
Createanimation ();
}
#region//light source
private void Createlight ()
{
Mydirectionallight = new DirectionalLight ();
Mydirectionallight.color = Colors.white;
Mydirectionallight.direction = new Vector3D (-0.61, -0.5,-0.61);
}
#endregion
#region//Camera
private void Createcamera ()
{
Mypcamera = new Perspectivecamera ();
Mypcamera.position = new Point3D (0,-1743,-4000);//Direction of view
Mypcamera.position = new Point3D (0, 0, 1000);
Mypcamera.lookdirection = new Vector3D (0,0,-1000);//Camera Viewing direction
Mypcamera.updirection = new Vector3D (0, 1,-0);
Mypcamera.fieldofview = 45;//normal vector camera upside down, turn left and right Mypcamera.nearplanedistance = 0.1;
Mypcamera.farplanedistance = 11050;

                      VP. Camera = Mypcamera;
              }
              #endregion
              #region//model
              private void Createmodel3d ()
              {
                      mymodel3dgroup = new Model3DGroup ();
                      myModel3DGroup.Children.Add (mydirectionallight);

var m =wfl. Loadobjfile (@ "c:/users/adminstrator/desktop/3d dynamic load/wpfapplication2/xx.obj");
          < c21>m.content = Mymodel3dgroup;

Vp. Children.add (m);
}
#endregion
#region//360 Rotation action
RotateTransform3D rtf3d;
Axisanglerotation3d aar;
private void create360 ()
{
RTF3D = Newrotatetransform3d ();
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,newduration (Timespan.fromseconds (10));
Dan. RepeatBehavior = Repeatbehavior.forever;
Storyboard.settargetname (Dan, "Myanglerotation");
Storyboard.settargetproperty (Dan, Newpropertypath (Axisanglerotation3d.angleproperty));
Sbd. Children.add (Dan);
Sbd. BeginTime = Timespan.fromseconds (5);//Start time
Sbd. Begin (this);
}
#endregion
void Slider1_valuechanged (Object Sender,routedpropertychangedeveNTARGS&LT;DOUBLE&GT;E)
{
Mypcamera.position = new Point3D (slider1. Value, Slider2. Value,slider3. Value);
}
void Slider2_valuechanged (Object Sender,routedpropertychangedeveNTARGS&LT;DOUBLE&GT;E)
{
Mypcamera.position = new Point3D (slider1. Value, Slider2. Value,slider3. Value);
}
void Slider3_valuechanged (Object Sender,routedpropertychangedeveNTARGS&LT;DOUBLE&GT;E)
{
Mypcamera.position = new Point3D (slider1. Value, Slider2. Value,slider3. Value);
}
void Slider4_valuechanged (Object Sender,routedpropertychangedeveNTARGS&LT;DOUBLE&GT;E)
{
Aar. Angle = Slider4. Value;
}

WPF dynamic load 3D Zoom-rotate-pan

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.