該庫的主要部分是:
-
Cameras (SceneCamera, FirstPersonCamera, ThirdPersonCamera, 等)
-
Camera Controllers (MouseCameraController, CameraControlPanel)
-
3D Models and Visuals (Sphere, Box, Cylinder,等)
-
3D Lines
-
Event Manager 3D (簡化了3D對象的事件處理)
Cameras, 3D Models和Lines可以在XAML中定義。并且結果能夠馬上就顯示在Visual Studio Designer(可視化工作室設計器)中。
使用Ab3d.PowerToys進行3D編程式在容易不過了!
Cameras 與 Camera Controllers
該庫定義了一些新的Cameras,它能被用于代替目前WPF的攝像頭。Ab3d Cameras與WPFcameras的主要區別是,Ab3d Cameras不需要使用Vectors來定義LookDirection,而是利用角度來定義它。這使得更自然。例如,如果您想查看的場景需要偏上和偏右一點點,您只需要定義Heading角度為30度和Attitude角度為-45度。您也可以定義場景的Distance。對于Ab3d Cameras最重要的是:SceneCamera, FirstPersonCamera 和ThirdPersonCamera。所有的Ab3d Cameras及他們的屬性能夠在類圖上可以看到。
Camera Controllers用于控制攝像頭。MouseCameraController能夠被用于使用鼠標改變攝像頭的角度和距離。這種方法可以非常簡單的左右旋轉攝像頭。CameraControlPanel顯示的漂亮按鈕用于旋轉攝像頭和移動攝像頭的遠近。這里還有一個CameraPreviewPanel,它可以圖形化地顯示當前攝像機正在注視的對象或場景。
下面的代碼展示的是一個WPF應用程序只需要幾行xaml代碼就可以使用攝像頭來顯示整個場景(SceneCamera)并能夠通過鼠標(MouseCameraController)或通過漂亮按鈕(CameraControlPanel)旋轉攝像頭。這里還有一個攝像頭的預覽,它顯示攝像頭特定的角度的注視的場景。此外,如果WindGeneratorModel模型不包含燈光,一個攝像頭的燈光會自定地被添加當場景中并從攝像頭的位置照亮場景(ShowCameraLight=”Auto”)。
01.<Window x:Class="Ab3d.PowerToys.Samples.Window1"
02.xmlns="//schemas.microsoft.com/winfx/2006/xaml/presentation"
03.xmlns:x="//schemas.microsoft.com/winfx/2006/xaml"
04.xmlns:cameras="clr-namespace:Ab3d.Cameras;assembly=Ab3d.PowerToys"
05.xmlns:ab3d="clr-namespace:Ab3d.Controls;assembly=Ab3d.PowerToys">
06. <Grid>
07. <Viewport3D Name="MainViewport3D">
08. <ModelVisual3D Content="{StaticResource WindGeneratorModel}"/>
09. </Viewport3D>
10.
11. <cameras:SceneCamera Name="SceneCamera1"
12. Heading="30"
13. Attitude="-30"
14. Distance="3" IsDistancePercent="True"
15. IsDynamicTarget="True"
16. ShowCameraLight="Auto"/>
17.
18. <ab3d:MouseCameraController TargetCameraName="SceneCamera1"
19. IsMouseWheelZoomEnabled="True"/>
20.
21. <ab3d:CameraControlPanel TargetCameraName="SceneCamera1"
22. VerticalAlignment="Bottom"
23. HorizontalAlignment="Left"/>
24.
25. <ab3d:CameraPreviewPanel TargetCameraName="SceneCamera1"
26. Width="100" Height="100"
27. VerticalAlignment="Bottom"
28. HorizontalAlignment="Right"/>
29. </Grid>
30.</Window>
EventManager3D
EventManager3D類是一個輔助類,它可是使得用戶能夠簡單地預定3D對象上的鼠標事件。支持一下鼠標事件:ouseEnter, MouseLeave, MouseDown, MouseUp, MouseClick, BeginMouseDrag, MouseDrag, EndMouseDrag and MouseDoubleClick。這樣您就不需要做復雜的3D沖擊測試了。EventManager3D也是比WPF的ModelUIElement3D更好,因為它支持更多的鼠標事件(包括鼠標在3D中的拖放),提供更好的事件數據并保存3D模型的結構。這樣您的編碼更簡單,更好組織。
下面的代碼顯示的是一個使用EventManager3D的樣例:
01.
02.ModelEventSource3D eventSource;
03.EventManager3D eventManager;
04.
05.eventManager = new ModelEventSource3D(MainViewport);
06.
07.
08.eventSource = new EventSource3D();
09.eventSource.TargetModel3D = myButton3D;
10.eventSource.MouseClick += new MouseButton3DEventHandler(myButton3D_MouseClick);
11.
12.eventManager.RegisterEventSource3D(eventSource);
13.
14.
15.eventSource = new ModelEventSource3D();
16.eventSource.TargetModel3D = myMovableObject3D;
17.eventSource.BeginMouseDrag += new Mouse3DEventHandler(myMovableObject3D_BeginMouseDrag);
18.eventSource.MouseDrag += new MouseDrag3DEventHandler(myMovableObject3D_MouseDrag);
19.eventSource.EndMouseDrag += new Mouse3DEventHandler(myMovableObject3D_EndMouseDrag);
20.
21.eventManager.RegisterEventSource3D(eventSource);
3D Models
Ab3d.PowerToys庫還包含一些類用于以代碼或XAML來創建基本的3D模型。它可以創建Plane(平面), Circle(圓), Box(箱), Pyramid(金字塔), Sphere(球), Cone(圓錐)和Cylinder(圓柱)3D模型。
下面的XAML創建了一些3D對象:WireGrid, 3D Sphere, 3D Box和3D Cone。需要注意的是Sphere也擁有ToolTip定義。另一件有趣的事情是通過提供的顏色的名字定義Materials.
01.
02.<Page x:Class="Ab3d.PowerToys.Samples.Objects3D.UIElementsToolTipSample"
03. xmlns="//schemas.microsoft.com/winfx/2006/xaml/presentation"
04. xmlns:x="//schemas.microsoft.com/winfx/2006/xaml"
05. xmlns:cameras="clr-namespace:Ab3d.Cameras;assembly=Ab3d.PowerToys"
06. xmlns:visuals="clr-namespace:Ab3d.Visuals;assembly=Ab3d.PowerToys"
07. xmlns:uiElements="clr-namespace:Ab3d.UIElements;assembly=Ab3d.PowerToys">
08. <Grid>
09. <Viewport3D Name="MainViewport">
10. <visuals:WireGridVisual3D CenterPosition="0 0 0" Size="70 30"
11. WidthCellsCount="7" HeightCellsCount="3"
12. LineColor="Gray" LineThickness="2"/>
13.
14. <uiElements:SphereUIElement3D CenterPosition="-20 5 0"
15. Radius="5"
16. Material="Green"
17. ToolTip="This is a simple tooltip"/>
18.
19. <uiElements:BoxUIElement3D CenterPosition="20 5 0"
20. Size="10 10 10"
21. Material="Red"/>
22.
23. <visuals:ConeVisual3D BottomCenterPosition="0 0 0"
24. BottomRadius="5" TopRadius="2"
25. Height="10" Segments="6"
26. IsSmooth="False" Material="Blue"/>
27. </Viewport3D>
28.
29. <cameras:SceneCamera Heading="30" Attitude="-20" Bank="0"
30. Distance="100" ShowCameraLight="Always"/>
31. </Grid>
32.</Page>
下面的圖像顯示的是能夠通過Ab3d.PowerToys創建的幾乎所有的3D對象。該圖像顯示的3D對象在Design Time in Visual Studio一直可見。這里還有一些Ab3d.PowerToys項在工具箱中(不幸地是它不能在Toolbox中顯示3D對象)。屬性編輯器顯示的當前被選定的ConeVisual3D的屬性。
下面的圖像時摘自一個球形樣例,它顯示的是一個在3D球形上繪制三角形和法線。對于該球形的相關設置顯示在圖像的右下角。
3D Lines
另外,還支持具有優化機制的3D Lines。它使得為WPF提供更先進的和具有更好性能的3Dlines實現。
Ab3d.PowerToys is the ultimate helper library for work with WPF 3D.
The main parts of the library are:
-
Cameras (SceneCamera, FirstPersonCamera, ThirdPersonCamera, etc.)
-
Camera Controllers (MouseCameraController, CameraControlPanel)
-
3D Models and Visuals (Sphere, Box, Cylinder, etc.)
-
3D Lines
-
Event Manager 3D (simplified event handling on 3D objects)
Cameras, 3D Models and Lines can be defined in XAML. The results are immediately shown in Visual Studio Designer.
With Ab3d.PowerToys programming with 3D cannot be easier!

Cameras and Camera Controllers
The library defines a few new Cameras that can be used instead of the current WPF's cameras. The main difference between Ab3d Cameras and WPF cameras is that Ab3d Cameras does not use Vectors to define the LookDirection, but instead use angles in degrees to define it. This is much more natural. For example if you want to look at the scene a little bit from the right and from above, you just define the Heading to be 30 and Attitude to be -45. You can also define the Distance from the scene. The most important Ab3d Cameras are: SceneCamera, FirstPersonCamera and ThirdPersonCamera. All the Ab3d Cameras with their properties can be seen on the class diagram.
Camera Controllers are used to control the camera. The MouseCameraController can be used to change the angle and distance of the camera with the mouse. This way it is very simple to rotate the camera around. The CameraControlPanel shows nice buttons to rotate the camera and move the camera closer or farther away. There is also a CameraPreviewPanel that graphically shows at which angle the camera is looking at the object or scene.
The following code demonstarates that with only a few lines of xaml a WPF application can use a camera that is showing the whole scene (SceneCamera) and can be rotated by the mouse (MouseCameraController) or by nice buttons (CameraControlPanel). There is also a preview of the camera that is showing from which angle the camera is looking at the scene. Also if the WindGeneratorModel model does not contain a light, a camera light is automatically added to the scene and it is iluminating the scene from the camera's position (ShowCameraLight="Auto").
01.<Window x:Class="Ab3d.PowerToys.Samples.Window1"
02.xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
03.xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
04.xmlns:cameras="clr-namespace:Ab3d.Cameras;assembly=Ab3d.PowerToys"
05.xmlns:ab3d="clr-namespace:Ab3d.Controls;assembly=Ab3d.PowerToys">
06. <Grid>
07. <Viewport3D Name="MainViewport3D">
08. <ModelVisual3D Content="{StaticResource WindGeneratorModel}"/>
09. </Viewport3D>
10.
11. <cameras:SceneCamera Name="SceneCamera1"
12. Heading="30"
13. Attitude="-30"
14. Distance="3" IsDistancePercent="True"
15. IsDynamicTarget="True"
16. ShowCameraLight="Auto"/>
17.
18. <ab3d:MouseCameraController TargetCameraName="SceneCamera1"
19. IsMouseWheelZoomEnabled="True"/>
20.
21. <ab3d:CameraControlPanel TargetCameraName="SceneCamera1"
22. VerticalAlignment="Bottom"
23. HorizontalAlignment="Left"/>
24.
25. <ab3d:CameraPreviewPanel TargetCameraName="SceneCamera1"
26. Width="100" Height="100"
27. VerticalAlignment="Bottom"
28. HorizontalAlignment="Right"/>
29. </Grid>
30.</Window>
EventManager3D
The EventManager3D class is a helper class that enables user to simply subscribe to mouse events on 3D objects. The following mouse events are supported: MouseEnter, MouseLeave, MouseDown, MouseUp, MouseClick, BeginMouseDrag, MouseDrag, EndMouseDrag and MouseDoubleClick. This way you do not need to do the complicated 3D hit testing any more. The EventManager3D is also better than WPF's ModelUIElement3D because it supports more mouse events (including mouse drag in 3D), provides better event data and preserves the structure of the 3D models. This way your code is much simpler and better organized.
The following code shows a sample used of EventManager3D:
01.
02.ModelEventSource3D eventSource;
03.EventManager3D eventManager;
04.
05.eventManager = new ModelEventSource3D(MainViewport);
06.
07.
08.eventSource = new EventSource3D();
09.eventSource.TargetModel3D = myButton3D;
10.eventSource.MouseClick += new MouseButton3DEventHandler(myButton3D_MouseClick);
11.
12.eventManager.RegisterEventSource3D(eventSource);
13.
14.
15.eventSource = new ModelEventSource3D();
16.eventSource.TargetModel3D = myMovableObject3D;
17.eventSource.BeginMouseDrag += new Mouse3DEventHandler(myMovableObject3D_BeginMouseDrag);
18.eventSource.MouseDrag += new MouseDrag3DEventHandler(myMovableObject3D_MouseDrag);
19.eventSource.EndMouseDrag += new Mouse3DEventHandler(myMovableObject3D_EndMouseDrag);
20.
21.eventManager.RegisterEventSource3D(eventSource);
3D Models
The Ab3d.PowerToys library also contains classes to create basic 3D models in code or XAML. It is possible to create Plane, Circle, Box, Pyramid, Sphere, Cone and Cylinder 3D models.
The following XAML creates some of the 3D objects: WireGrid, 3D Sphere, 3D Box and 3D Cone. Note that the Sphere also has a ToolTip defined. Another interesting thing that is that the Materials are defined only by providing the name of the Color.
01.
02.<Page x:Class="Ab3d.PowerToys.Samples.Objects3D.UIElementsToolTipSample"
03. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
04. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
05. xmlns:cameras="clr-namespace:Ab3d.Cameras;assembly=Ab3d.PowerToys"
06. xmlns:visuals="clr-namespace:Ab3d.Visuals;assembly=Ab3d.PowerToys"
07. xmlns:uiElements="clr-namespace:Ab3d.UIElements;assembly=Ab3d.PowerToys">
08. <Grid>
09. <Viewport3D Name="MainViewport">
10. <visuals:WireGridVisual3D CenterPosition="0 0 0" Size="70 30"
11. WidthCellsCount="7" HeightCellsCount="3"
12. LineColor="Gray" LineThickness="2"/>
13.
14. <uiElements:SphereUIElement3D CenterPosition="-20 5 0"
15. Radius="5"
16. Material="Green"
17. ToolTip="This is a simple tooltip"/>
18.
19. <uiElements:BoxUIElement3D CenterPosition="20 5 0"
20. Size="10 10 10"
21. Material="Red"/>
22.
23. <visuals:ConeVisual3D BottomCenterPosition="0 0 0"
24. BottomRadius="5" TopRadius="2"
25. Height="10" Segments="6"
26. IsSmooth="False" Material="Blue"/>
27. </Viewport3D>
28.
29. <cameras:SceneCamera Heading="30" Attitude="-20" Bank="0"
30. Distance="100" ShowCameraLight="Always"/>
31. </Grid>
32.</Page>
The following image shows almost all the 3D objects that can be created with Ab3d.PowerToys. The image shows that the 3D objects are already visible at Design Time in Visual Studio. There are also some Ab3d.PowerToys item in Toolbox (unfortunatlly it is not possible to show 3D objects in Toolbox). The Property Editor shows the properties for the currently selected ConeVisual3D.

The following image is taken from Sphere sample and shows the 3D sphere with drawn triangles and normals. The settings for the sphere are shown on the lower right part of the image.

3D Lines
There is also support for 3D Lines with optimized update mechanism that make the 3D lines implementation the most advanced and with the best performance for WPF.
