Fun WPF Fourth bullet: 3D rotation with Viewport2dvisual3d

Source: Internet
Author: User

The effect is such an effect, but we have to play with imagination, such as making a button that can be rotated and so on.

It's good to define a resource like this.

<Window.Resources>        <DiffuseMaterial x:Key="DiffuseMaterialStyle" Viewport2DVisual3D.IsVisualHostMaterial="True"                          Brush="White"/></Window.Resources>

The key is to put such a thing in the grid:

 <Viewport3D x:name="View" ClipToBounds="True"  Renderoptions.edgemode="aliased">                     <viewport3d.camera>          <perspectivecamera x:name="Perspectivecam" fieldofview=  " Position" ="0.5,0.5,2" lookdirection= "0,0,-1">              <perspectivecamera.transform>                   <rotatetransform3d x:name="rot" centery="0.5" CenterX ="0.5" centerz=" -0.5">                        <rotatetransform3d.rotation>                                <axisanglerotation3d x:name="Axisanglerot" Axis="0,1,0"  Angle="0"/>                        </rotatetransform3d.rotation>                   </rotatetransform3d>               </perspectivecamera.transform>           </Perspectivecamera>      </Viewport3d.camera>                            <ModelVisual3D>          <modelvisual3d.content>               <ambientlight Color="white" />          </modelvisual3d.content>      </ModelVisual3D></Viewport3D>

Positive:

<viewport2dvisual3d Material="{StaticResource diffusematerialstyle}">    <viewport2dvisual3d.geometry>         <MeshGeometry3D Positions="0,1,0 0,0,0 1,0,0 1,1,0"texturecoordinates ="0,0 0,1 1,0"triangleindices="0 1 2 0 2 3"/>                                </viewport2dvisual3d.geometry>    <Border borderthickness= "ten" x:name="frontside"  BorderBrush="Blue" cornerradius= "1"previewmousedown="Frontside_ PreviewMouseDown " >                  <TextBlock Text="Welcome to my Blog" Foreground="Green" / >   </Border></Viewport2dvisual3d>

Right:

<viewport2dvisual3d Material="{StaticResource diffusematerialstyle}">      <viewport2dvisual3d.geometry>            <MeshGeometry3D Positions="1,1,0 1,0,0 1,0,-1 1,1,-1"Texturecoordin Ates="0,0 0,1 1,0"triangleindices="0 1 2 0 2 3"/>                                       </viewport2dvisual3d.geometry>       <Border borderthickness="1" x:name="Rightside"  BorderBrush= "Lime" cornerradius= "4"previewmousedown=" Rightside_previewmousedown " >                      <border.background>                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">                     <gradientstop Color="white" />                </LinearGradientBrush>            </border.background>            <TextBlock Text="Thank you for your support" FontSize="/> "       </Border></Viewport2dvisual3d>

We can see how the front cutouts come from the above two.

Left:

<viewport2dvisual3d Material="{StaticResource diffusematerialstyle}">      <viewport2dvisual3d.geometry>            <MeshGeometry3D Positions="0,1,-1 0,0,-1 0,0,0 0,1,0"texturecoordinates ="0,0 0,1 1,0"triangleindices="0 1 3 0 2 3"/>                                      </viewport2dvisual3d.geometry>           <Border borderthickness=" x:name" = "leftside"  BorderBrush="White" cornerradius= "1"previewmousedown=" Leftside_previewmousedown " >                                 <border.background>                  <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">                       <gradientstop Color="Black" />                   </LinearGradientBrush>               </border.background>          <TextBlock Text="Have a problem with a direct comment just fine" Foreground="Lime"/ >     </Border></Viewport2dvisual3d>

Rear:

<viewport2dvisual3d Material="{StaticResource diffusematerialstyle}">    <viewport2dvisual3d.geometry>        <MeshGeometry3D Positions="1,1,-1 1,0,-1 0,0,-10,1,-1 0,0.5,-1" 
     
      texturecoordinates=
      "0,0 0,1 1,0"
      triangleindices=
      "0 1 2 0 2 4"/>
                            
         </viewport2dvisual3d.geometry>    <Border borderthickness="1" x:name="backside" BorderBrush ="White" cornerradius="4"previewmousedown="Backside_ PreviewMouseDown " >                  <border.background>           <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">                <gradientstop Color="white" />           </LinearGradientBrush>        </border.background>        <TextBlock Text="Often come oh ..." FontSize="/> "   </Border></Viewport2dvisual3d>

People don't care what meshgeometry3d these things are, I'll try to explain them in a quick and easy way.

So take a look inside the program first.

     Public Partial classMainwindow:window {DispatcherTimer Dispattime =NULL;DoubleAxangle = -; Public MainWindow() {InitializeComponent ();if(Dispattime = =NULL) Dispattime =NewDispatcherTimer (); Dispattime.tick + =NewEventHandler (Dt_tick); Dispattime.interval =NewTimeSpan (0,0,0,0,2); }Private void Dt_tick(Objectsender, EventArgs e) {axisanglerot.angle + =1;if(Axisanglerot.angle >= Axangle) dispattime.stop (); }Private void Frontside_previewmousedown(Objectsender, MouseButtonEventArgs e) {axisanglerot.angle =0; Axangle = -;        Dispattime.start (); }Private void Leftside_previewmousedown(Objectsender, MouseButtonEventArgs e) {axangle = the;        Dispattime.start (); }Private void Backside_previewmousedown(Objectsender, MouseButtonEventArgs e) {axangle = the;        Dispattime.start (); }Private void Rightside_previewmousedown(Objectsender, MouseButtonEventArgs e) {axangle = the;        Dispattime.start (); }    }

A program like this will definitely involve a timer, which is the Dt_tick method. I set it to rotate 1 degrees each time, and the following line is the time interval.

dispatTime.Interval = new TimeSpan(0, 0, 0, 0, 2);

Each of the remaining methods is used to adjust the angle, and you can see that the angle is reset once on the front.

So let's see what positions means next.

Forgive me for putting the cube on the wrong side, with arrows pointing to the "front", "right" and so on.

Let's see, positions is a group of 3 numbers, right, that's a point I've identified in the diagram. And the triangleindices is also a group of 3 numbers, these 3 numbers indicate the combined index in positions (starting from 0), and then 3 numbers make up the triangle, the middle arrow points to.

And Texturecoordinates is WPF's 3D texture coordinates, here is not to delve into, behind can delve into writing a blog.

We can also take the source code back to play slowly ...

So that's the end of the blog post. The ...

Fun WPF Fourth bullet: 3D rotation with Viewport2dvisual3d

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.