Click or drag to resize
AB4D logo

TransparencySorter Class

TransparencySorter class can be used to solve the problems with transparent objects in WPF 3D.
Inheritance Hierarchy
SystemObject
  Ab3d.UtilitiesTransparencySorter

Namespace:  Ab3d.Utilities
Assembly:  Ab3d.PowerToys (in Ab3d.PowerToys.dll) Version: 10.1.8147.1045
Syntax
C#
public class TransparencySorter

The TransparencySorter type exposes the following members.

Constructors
  NameDescription
Public methodTransparencySorter(Viewport3D)
Constructor with rootViewport3D. Sorting mode is set to Simple.
Public methodTransparencySorter(ContainerUIElement3D)
Constructor with rootVisual3DCollection. Sorting mode is set to Simple.
Public methodTransparencySorter(Model3DGroup)
Constructor with rootModelGroup. Sorting mode is set to Simple.
Public methodTransparencySorter(ModelVisual3D)
Constructor with rootModelVisual3D. Sorting mode is set to Simple.
Public methodTransparencySorter(Visual3DCollection)
Constructor with rootVisual3DCollection. Sorting mode is set to Simple.
Public methodTransparencySorter(Viewport3D, BaseCamera)
Constructor with rootViewport3D and usedCamera. Sorting mode is set to ByCameraDistance.
Public methodTransparencySorter(Model3DGroup, BaseCamera)
Constructor with rootModelGroup and usedCamera. Sorting mode is set to ByCameraDistance.
Public methodTransparencySorter(Viewport3D, BaseCamera, Double)
Constructor with rootViewport3D, usedCamera and cameraAngleChange. Sorting mode is set to ByCameraDistance.
Public methodTransparencySorter(Model3DGroup, BaseCamera, Double)
Constructor with rootModelGroup, usedCamera and cameraAngleChange. Sorting mode is set to ByCameraDistance.
Top
Properties
  NameDescription
Public propertyCacheBounds
Gets or sets a Boolean that specify if the objects bounds can be cached.
Public propertyCameraAngleChange
Gets or sets an angle in degrees that specify for how many degrees the camera can change before next automatic sorting is performed.
Public propertySortingMode
Gets or sets type of the transparency sorting.
Public propertyTransparentModels
Gets root group of transparent models.
Public propertyUsedCamera
Gets or sets a Camera that is used to make the sorting by camera distance.
Top
Methods
  NameDescription
Public methodAddTransparentModels(Model3D)
AddTransparentModels specifies that the models in the parameters should be treated as transparent models. This method must be called before the Sort method is called. If this method is called after Sort method, then a RecollectTransparentModels must be called for the models to be considered transparent.
Public methodAddTransparentModels(Visual3D)
AddTransparentModels specifies that the Visual3D objects in the parameters should be treated as transparent models. This method must be called before the Sort method is called. If this method is called after Sort method, then a RecollectTransparentModels must be called for the Visual3D objects to be considered transparent.
Protected methodCalculateSortedValue
CalculateSortedValue returns the sorted value for transparentModel3D and sortingType that will be used to set the model's order. The method can be overridden to provide custom objects ordering.
Public methodClearTransparentModels
Clears the Model3D or Visual3D objects that were added with AddTransparentModels method.
Protected methodIsRearangeNeeded(FreeCamera)
Returns true is the camera has changed enough to perform automatic sorting.
Protected methodIsRearangeNeeded(SphericalCamera)
Returns true is the camera has changed enough to perform automatic sorting.
Public methodRecollectTransparentModels
Refreshes the list of current transparent models (used in case when the opacity of some already collected models is changed).
Public methodRecollectTransparentModels(Viewport3D)
Refreshes the collection of transparent objects.
Public methodRecollectTransparentModels(Model3DGroup)
Refreshes the collection of transparent objects.
Public methodRecollectTransparentModels(ModelVisual3D)
Refreshes the collection of transparent objects.
Public methodRecollectTransparentModels(Visual3DCollection)
Refreshes the collection of transparent objects.
Public methodStatic memberSimpleSort(Viewport3D)
Sorts objects in Viewport3D with moving transparent objects to the back of the objects collection.
Public methodStatic memberSimpleSort(ContainerUIElement3D)
Sorts objects in containerUIElement3D with moving transparent objects to the back of the objects collection.
Public methodStatic memberSimpleSort(Model3DGroup)
Sorts objects in Model3DGroup with moving transparent objects to the back of the objects collection.
Public methodStatic memberSimpleSort(ModelVisual3D)
Sorts objects in ModelVisual3D with moving transparent objects to the back of the objects collection.
Public methodStatic memberSimpleSort(Visual3DCollection)
Sorts objects in Visual3DCollection with moving transparent objects to the back of the objects collection.
Public methodSort
Sort the objects.
Public methodSort(TransparencySorterSortingModeTypes)
Sort the objects with the sortingType.
Public methodStatic memberSortByCameraDistance(Viewport3D, BaseCamera)
Sorts objects in Viewport3D with moving transparent objects to the back of the objects collection. The transparent objects are arranged in that way that the objects that are farther away from the camera are rendered before objects that are closer to the camera.
Public methodStatic memberSortByCameraDistance(Viewport3D, Point3D)
Sorts objects in Viewport3D with moving transparent objects to the back of the objects collection. The transparent objects are arranged in that way that the objects that are farther away from the camera are rendered before objects that are closer to the camera.
Public methodStatic memberSortByCameraDistance(ContainerUIElement3D, Point3D)
Sorts objects in Visual3DCollection with moving transparent objects to the back of the objects collection. The transparent objects are arranged in that way that the objects that are farther away from the camera are rendered before objects that are closer to the camera.
Public methodStatic memberSortByCameraDistance(Model3DGroup, BaseCamera)
Sorts objects in Model3DGroup with moving transparent objects to the back of the objects collection. The transparent objects are arranged in that way that the objects that are farther away from the camera are rendered before objects that are closer to the camera.
Public methodStatic memberSortByCameraDistance(Model3DGroup, Point3D)
Sorts objects in Model3DGroup with moving transparent objects to the back of the objects collection. The transparent objects are arranged in that way that the objects that are farther away from the camera are rendered before objects that are closer to the camera.
Public methodStatic memberSortByCameraDistance(ModelVisual3D, BaseCamera)
Sorts objects in ModelVisual3D with moving transparent objects to the back of the objects collection. The transparent objects are arranged in that way that the objects that are farther away from the camera are rendered before objects that are closer to the camera.
Public methodStatic memberSortByCameraDistance(ModelVisual3D, Point3D)
Sorts objects in ModelVisual3D with moving transparent objects to the back of the objects collection. The transparent objects are arranged in that way that the objects that are farther away from the camera are rendered before objects that are closer to the camera.
Public methodStatic memberSortByCameraDistance(Visual3DCollection, BaseCamera)
Sorts objects in Visual3DCollection with moving transparent objects to the back of the objects collection. The transparent objects are arranged in that way that the objects that are farther away from the camera are rendered before objects that are closer to the camera.
Public methodStatic memberSortByCameraDistance(Visual3DCollection, Point3D)
Sorts objects in Visual3DCollection with moving transparent objects to the back of the objects collection. The transparent objects are arranged in that way that the objects that are farther away from the camera are rendered before objects that are closer to the camera.
Public methodStartSortingOnCameraChanged
StartSortingOnCameraChanged method starts checking the camera changed events and performs automatic sorting when the camera is changed for more than CameraAngleChange.
Public methodStopSortingOnCameraChanged
Stops automatic sorting on camera changed event.
Top
Events
  NameDescription
Public eventSortingCompleted
Notify that the sorting is completed.
Public eventTransparentModelIndexChanged
Notify that the specific Transparent model has been repositioned.
Top
Fields
  NameDescription
Public fieldConsiderNonSolidColoredBrushesAsTransparent
When true (by default), then objects with materials that use ImageBrush or VisualBrush are re-arranged so they are rendered before objects with non-transparent SolidColorObject materials.
Top
Remarks

WPF 3D can show transparent 3D objects. But to show them correctly, the transparent objects need to be defined after all the objects that are visible through them. This does not mean their position in 3D space but their index in the object's hierarchy (index in the parent's Children collection). The objects that are defined after the transparent objects cannot be seen through the transparent objects.

The following image shows the transparency problem in a simple 3D scene (because the red boxes are defined after the semi-transparent GlassPlane they are not visible throug the GlassPlane. The order of objects is visible in the right):

To solve the problem, the GlassPlane needs to be moved after the non-transparent objects. The following image is showing the correctly rendered scene.

For cases when the number of transparent objects is not big and if the transparent objects are not positioned in 3D space one after another, the simple transparency sorting is usually enough to produce correct rendering in WPF 3D.

But simply moving transparent objects after non-transparent objects is not always enough. For example if transparent object A is visible through transparent object B, than B must be defined after A. The problem in this case occurs when the camera is rotated so that B is visible through A. Now B must be defined before A. To solve this problem correctly, firstly the transparent objects must be moved after non-transparent objects and than transparent objects must be sorted by their distance to the camera. The sorting must be done after the camera is changed.

TransparencySorter class can be used to perform a simple transparency sorting with just moving all transparent objects after non-transparent objects, or to perform a ByCameraDistance transparency sorting where the transparent objects are sorted by their distance to the camera.

The sorting by camera distance can be also automated so the sorting is automatically done when the camera is changed. To improve performance sorting is not done on each camera change, but is done only if the angle of the camera is changed for more than CameraAngleChange. By default the CameraAngleChange is 5. This means that the transparent object will be resorted after the camera has changed for more than 5 degrees.

If it is not known if the scene contains any transparent objects (for example if it created from a external file), it is possible to check for transparent objects with the GetTransparentObjectsCount(Model3DGroup) method. If the objects are read with Ab3d.Reader3ds, it is possible to get the number of transparent objects with TransparentObjectsCount property.

The following line of code is enough to perform a simple transparency sorting:

Ab3d.Utilities.TransparencySorter.SimpleSort(myRootObjectsGroup);

The following line of code is enough to perform a transparency sorting by camera distance (sorting is performed only once):

Ab3d.Utilities.TransparencySorter.SortByCameraDistance(myRootObjectsGroup, myCamera);

Instead of using Ab3d.Camera (for example TargetPositionCamera), it is also possible to use camera's position (Point3D):

Ab3d.Utilities.TransparencySorter.SortByCameraDistance(myRootObjectsGroup, cameraPosition);

To automatically sort the objects when the camera is changed the following code can be used:

TransparencySorter transparencySorter;

transparencySorter = new TransparencySorter(myRootObjectsGroup, myCamera);
transparencySorter.StartSortingOnCameraChanged();

// transparencySorter.StopSortingOnCameraChanged(); // Stops the sorting on camera changed

When creating an instance of TransparencySorter class, it is possible to monitor the sorting with subscribing to TransparentModelIndexChanged and SortingCompleted events. To implement custom sorting or use custom values to sort the objects, it is possible to derive a custom class from TransparencySorter and override some of the metods that are used to perform the sorting.

See Also