Rendering reflections and many other great new features with new versions of Ab3d.DXEngine and Ab3d.PowerToys

by abenedik 11. February 2016 23:01

The new version of Ab3d.DXEngine brings three big new features:

  1. Added support for rendering reflections with environmental and reflection maps.
  2. Added support for rendering unlimited number of lights with using multi-pass rendering.
  3. Improved rendering instanced objects with adding support for rendering many instances of Model3DGroup objects. Simplified hit testing on instanced objects with a new GetHitInstanceIndex method.

Rendering DirectX reflection in .Net with Ab3d.DXEngine


The Ab3d.PowerToys library also got some great new features. Some of the main new features are:

  1. Added ModelRotatorVisual3D that allows users to rotate selected 3D model. Improved ModelMoverVisual3D so that it also works in Ab3d.DXEngine.
  2. Greatly reduced the initialization time when creating many instances of SphereVisual3D and BoxVisual3D objects.
  3. Simplified working with hierarchically organized 3D models with new model iterators.

Rotating 3D models with ModelRotatorVisual3D

Let’s see some more details about the new features.</p> <p>Showing reflections pushes the boundaries of what is possible to achieve with WPF 3D API even further. The above image with reflective teapot shows a fully reflective 3D model. The level of reflection can be adjusted and specified for the whole object. Or, it can be specified for each part of the model with using a reflection map. The following image shows that is action (the reflection map is shown on the right):

Using DirectX ReflectionMap to specify reflection for each part of the 3D model

The handle and top of the teapot are shown with green color (specified in the teapot’s texture on the bottom image) and do not reflect the environment – those parts are almost black on the reflection map (upper image). But the rest of the teapot is fully reflective – defined by white color on the reflection map (in the bottom 1/3 of the image and on the smaller white rectangle on the right side).

To support scenarios where additional DXEngine properties need to be added to the existing WPF objects, the new version of Ab3d.DXEngine introduces a new set of extensions methods that allow adding additional attributes to WPF objects. 

For example the following two lines set EnvironmantMap and ReflectionMap to an existing WPF material:

usedMaterial.SetDXAttribute(DXAttributeType.Material_EnvironmentMap, _dxCubeMap);
usedMaterial.SetDXAttribute(DXAttributeType.Material_ReflectionMap, bitmapImage);

The new version of Ab3d.DXEngine also adds support to render unlimited number of lights. Previous version supported rendering only 16 lights + ambient light. This limit is now lifted with using multi-pass rendering. This means that when there are more than 16 lights in the scene it is rendered multiple times – each time different 16 lights are used and then all the rendered scenes are combined into the final image. The following screenshot shows a sample that animates the intensity of 64 PointLights:

Rendering many lights with using multiple-pass rendering

Multi-pass rendering is great for such scenarios. But it also has some disadvantages. If you need to use many lights, please check the additional comments in the “Many lights” sample.


I would also like to write a few words about new object instancing capabilities.

Object instancing is the ultimate performance optimization – if you show really many 3D objects and you convert your code to use object instancing than you have almost reached the peak of the performance. Some additional performance gains are still possible with tweaking the shaders, but most of the work was already done.

The trick is that when object instancing is used, the applications sends one mesh geometry to the graphics card and then tells it to render it many times – for each instance of the mesh you can specify different color and different transformation. Because the data about all the instances is send in one draw call, this can be done very quickly on the CPU. So the usual performance problem where GPU waits for the CPU to send commands is completely eliminated. 

Because instancing is so great for improving performance, the new version also allows you to render many instances of Model3DGroup objects (and not only many instances of MeshGeometry3D). The following screenshot shows many instances of RobotArm model:

Rendering many instances of Model3DGroup

Another improvement with instancing is that a GetHitInstanceIndex method was added to the InstanceData class. That method can be used in hit testing to get the index of the hit instance. This way you can easily connect the hit object with the background data that are connected to the hit instance.

If you are rendering many 3D objects, I would really advice you to try to convert your code to use instancing. You will be amazed on how fast the graphics cards can become when they are not waiting for the CPU (a newer graphics card recommended).


There are still lots of other new features and fixes. Also Ab3d.PowerToys library has been greatly improved.

But I do not want to make this blog post too long. 

To check new features in action, please check the new samples that come with the libraries. And for the record here are the full list of changes:


Ab3d.DXEngine v1.2:

  • Added support for rendering reflections with using EnvironmentalMaps
  • Added support for ReflectionMaps
  • Added support for rendering more than 16 lights (+ ambient light) with using multi-pass rendering.
  • Added support for Transform on InstancedMeshGeometryVisual3D
  • Added InstancedModelGroupVisual3D that can render many instances of all 3D models defined in the Model3DGroup.
  • Added GetHitInstanceIndex method to InstanceData to get an index of hit instance
  • Fixed showing transparent objects in some cases
  • Added TextureBlendState to IDiffuseTextureMaterial interface
  • Fixed rendering textures from files that use different DPI settings
  • Added extension methods that simplify adding additional DXEngine attributes to the existing WPF's objects (SetDXAttribute, GetDXAttributeCollection, GetDXAttribute, IsDXAttributeSet, ClearDXAttribute, GetDXAttributeOrDefault). This is currently used to specify the EnvironmentalMap and ReflectionMap.
  • Prevented memory leak when 3D model that was shown inside WireframeVisual3D was changed (further performance improvements in this case will follow).
  • Prevented rendering strange 3D lines that sometimes occur when the 3D lines were completely behind the camera
  • Fixed rendering transparent 3D lines
  • Fixed rendering transparent objects with emissive materials
  • Some other smaller bug fixes and improvements

The InstancedGeometryVisual3D was renamed into InstancedMeshGeometryVisual3D – the renaming was needed because a new InstancedModelGroupVisual3D was introduced and the previous name did not describe the class well enough.


Ab3d.PowerToys v7.4:

  • Added ModelRotatorVisual3D that allows user to rotate selected Model3D around any axes.
  • Added SubscribeWithEventManager3D method to ModelMoverVisual3D and ModelRotatorVisual3D to allow them to use EventManager3D for processing mouse events. This allows using the ModelMoverVisual3D and ModelRotatorVisual3D in Ab3d.DXEngine.
  • Greatly reduced the initialization time when creating many instances of SphereVisual3D or BoxVisual3D objects
  • Added ModelIterator class and two extension methods (ForEachVisual3D and ForEachGeometryModel3D) to simplify working with hierarchacly organized 3D models.
  • Added FitIntoView and GetFitIntoViewDistanceOrCameraWidth methods to TargetPositionCamera, SceneCamera, TargetRect3DCamera and ThirdPersonCamera. The method has greatly improved algorithm then it was available in the "Scene Editor" sample in the previous versions of Ab3d.PowerToys.
  • Fixed showing transparent 3D lines when LineColor's alpha value is less than 255.
  • Improved support for TextureCoordinates in ModelOptimizer
  • Added WidthDirection and HeightDirection to WireGridVisual3D that allows to set custom direction of the WireGrid (not only horizontal or horizontal in another coordinate system)
  • Added CreateWireGrid to Line3DFactory that allows creating WireGrid object with custom widthDirection and heightDirection vectors.
  • Added GetTargetViewport3DSceneBounds method to all Camera classes in Ab3d.PowerToys – the method calculates the scene bounding box
  • Change validation of Size property on BoxVisual3D, WireBoxVisual3D, MultiMaterialBoxVisual3D and PyramidVisual3D to allow having one component of size zero.
  • Fixed calculating scene size in SceneCamera when the scene hierarchy is complex
  • Added GetBounds and CombineTransform methods to Ab3d.Utilities.ModelUtils
  • Added CompositionRenderingHelper to help work with CompositionTarget.Rendering (allowing subscribed objects to be recycled by Garbage Collection and therefore preventing infinite rendering subscription in case when the Rendering is not unsubscribed)
  • Improved ModelMoverVisual3D so that it can also use EventManager3D for mouse event processing - this allows using ModelMoverVisual3D inside Ab3d.DXEngine.
  • Prevented throwing "Object reference" exception in ModelOptimiter that could occur sometimes when ImageBrush is used.
  • Added HeightDirection to TubeVisual3D and TubeMesh3D – this allows orienting the object in any direction and not only in up (0, 1, 0) direction.
  • Fixed throwing exception when Is3DAxesShown is initially set to false on CameraAxisPanel
  • Added "Custom Up Axis" sample that shows how to show data in another coordinate system - for example where Z is up.
  • Added "Perspective Transformation" sample that shows how to convert 3D positions to the 2D positions on the screen

Moved the ModelMovedEventArgs class from Ab3d.PowerToys.Common to Ab3d.Common namespace

Tags: , , , , ,

Ab3d.PowerToys | DXEngine

Greatly improved performance, new features and a new real-time HLSL shader editor come with new versions of Ab3d.DXEngine and Ab3d.PowerToys

by abenedik 3. December 2015 23:01

New versions of Ab3d.DXEngine and Ab3d.PowerToys libraries have been published.

The new versions bring improved performance, new features and many bug fixes. 

But let me talk about that later and start with a very interesting new sample that comes with Ab3d.DXEngine – a real-time HLSL shader editor. The following screenshot shows it in action:

Real-time shader editor in Ab3d.DXEngine

On the left side you can see the HLSL editor with full syntax highlighting. The editor is using great AvalonEdit component. The right side shows some options and a real-time preview of the 3D scene and used shader.

The real-time preview means that after each change of HLSL text, the HLSL text is compiled and the new shaders are used to render the preview image on the right.

The opened ComboBox shows that the editor comes with 6 shaders that can be used as a simple step by step HLSL tutorial. The first sample shows how to render all objects with a single color (you can experiment with changing the color components). The following effects add some interesting coloring. And the final shader shows how to create a shader with directional light lighting and with added fog.

So, if you have not tried to program in HLSL, this is your best and easiest chance to try it out.

And if you want to change the 3D scene, you can just open the XAML editor and change the 3D objects defined there.


And there are more great news. 

The new version of Ab3d.DXEngine has significantly better performance. In some cases (for example where many BoxVisual3D or similar objects are shown) the frame rate can be 3 times the previous frame rate. And already the previous version had some impressive performance. Also the time spend in the Update method is greatly reduced.

Also the problems with hardware accelerated 3D lines are now fixed. Because of some problems in the production version preparation, the previous version of Ab3d.DXEngine in many cases did not render the 3D lines with full hardware acceleration.

The biggest performance improvement in Ab3d.DXEngine can be achieved with using object instancing – rendering many instances of the same mesh. The performance is really amazing the following screenshot is showing 16.000 bunnies (each bummy model has 11.553 position) rendered at aroud 20 FPS (on i7 6700 and NVIDIA 970 GTX). Note that statistics in the lower right corner is showing that 184.848.000 positions are rendered:

Instancing in Ab3d.DXEngine

The problem with instancing in the previous version was that it did not support hit testing. This has been improved in the new version. Now you can set the IsWpfHitTestVisible property on InstancedGeometryVisual3D to true and hit testing (also the EventsManager3D from Ab3d.PowerToys) will begin to work. Though this will increase the initialization time because WPF objects need to be created before WPF hit testing can work. 

The new version also adds support for Binding on objects inside DXViewportView. For example, now you can bind IsVisible property on ModelVisual3D objects to CheckBox.IsChecked property.

The following is the full list of changes and improvements:

  • Fixed using Binding on objects inside DXViewportView.
  • Fixed rendering 3D lines with hardware accelerating geometry shader (instead of Ab3d.PowerToys's LinesUpdater).
  • Improved support for transformations on TileBrush (used on ImageBrush, VisualBrush and DrawingBrush).
  • Added IsWpfHitTestVisible to InstancedGeometryVisual3D - this allows WPF hit testing of instanced geometry (though this increased initialization time because WPF's GeometryModel3D objects needs to be created).
  • Improved InstancedGeometryVisual3D so that it is not needed any more to call Update method when the InstancesData is set for the first time. Also fixed problems when the objects were not shown if Update was called before the InstancedGeometryVisual3D was added to Visual tree.
  • Improved performance with moving some matrix calculations to vertex shader.
  • Added support for rendering WPF's UIElement3D objects. NOTE: WpfUIElement3DNode can only show 3D models but does not support the input events on the UIElement3D (MouseEnter, MouseMove, etc.). Those events cannot be supported because Viewport3D control is not visible and does not provide the events to the UIElement3D.
  • Greatly improved Update method call performance when many Visual3D objects from Ab3d.PowerToys are used (for example BoxVisual3D objects).
  • Added IsCheckingChildrenForChanges field to WpfModelVisual3DNode that can be used to skip checking ModelVisual3D's Childen collection and improve Update performance.
  • Added IsCheckingChildrenForChangesDefaultValue static field to WpfModelVisual3DNode - used to set the default value of the IsCheckingChildrenForChanges field.
  • Fixed problems where wrong image was shown when multiple DrawingImage brushes or VisualBrshes were used.
  • Prevented throwing exception when unsupported type of Visual3D (for example UIElement3D) or Model3D was used in the scene.
  • Added Refresh method to Ab3d.DirectX.Material and its derived classes. This allows user to manually update the materials properties and its resources (textures are regenerated).
  • Fixed hit testing on some Viewport3D objects (usually when the Viewport3D was removed from visual tree and then added to DXViewportView).
  • Fixed using IsAutomaticallyUpdatingDXScene when the DXScene was created after the IsAutomaticallyUpdatingDXScene property was set.


The Ab3d.PowerToys library also got some fixes. The following is a full list of changes:

  • Fixed showing 3D lines that were created with IsVisible property set to false. When later the IsVisible is set to true, sometimes the 3D lines were not shown.
  • Fixed rendering 3D lines with arrows that were not rendered correctly under some circumstances.
  • Fixed reporting MouseLeave event that was sometimes not triggered when CustomEventsSourceElement was used.
  • Fixed changing BoxVisual3D and SphereVisual3D objects after Position is changed under some circumstances.
  • Added GetCameraMatrixes to BaseCamera that can calculate view and projection camera even if TargetViewport3D is not assigned to the camera.
  • Prevented throwing null reference exception in Dumper.Dump method when TextureCoordinates or Normals collection was null.
  • Prevented throwing null reference exception that could sometimes occur in MouseCameraControllerInfo when in XAML designer.


The wrapper for assimp importer was also improved. The new library should read some models more correctly (especially models from fbx files). Also, the AssimpWpfImporter class now supports IDisposable interface. This means that it can be now easily disposed to release all managed and unmanaged resources. The following is list of all the changes in this library:

  • AssimpWpfImporter now implements IDisposable and have new Dispose method to easily dispose all managed and unmanaged resources.
  • Improved reading transformations - in case the transformation matrix is identity, the Transform property is set to null; in case the matrix is a simple translation, a TranslateTransform3D is created; in case of simple scale a ScaleTransform3D is created; otherwise a MatrixTransform3D is created.
  • Improved reading 3D models that are stored in left coordinate system.
  • Added ForceConvertToRightHandedCoordinateSystem property to AssimpWpfImporter and AssimpWpfConverter.


I hope that you share the excitement of the new versions with me. And I promise to bring you more great news in the future versions.

Tags: , , , ,

Ab3d.PowerToys | DXEngine