Click or drag to resize
AB4D logo

InstancedMeshGeometryVisual3D Class

InstancedMeshGeometryVisual3D is a WPF Visual3D object that can be used to define a MeshGeometry3D that will be rendered multiple times as instances where each instace can have its own color and its own world matrix (defines scale, rotation and translation). When rendered with DirectX, InstancedMeshGeometryVisual3D provides the fastest way to render many MeshGeometry3D instances. When rendered with WPF, InstancedMeshGeometryVisual3D is rendered as multiple GeometryModel3D objects inside Model3DGroup.
Inheritance Hierarchy
SystemObject
  System.Windows.ThreadingDispatcherObject
    System.WindowsDependencyObject
      System.Windows.Media.Media3DVisual3D
        System.Windows.Media.Media3DModelVisual3D
          Ab3d.VisualsBaseDXVisual3D
            Ab3d.VisualsInstancedMeshGeometryVisual3D

Namespace: Ab3d.Visuals
Assembly: Ab3d.DXEngine.Wpf (in Ab3d.DXEngine.Wpf.dll) Version: 7.0.8865.1045
Syntax
C#
public class InstancedMeshGeometryVisual3D : BaseDXVisual3D

The InstancedMeshGeometryVisual3D type exposes the following members.

Constructors
 NameDescription
Public methodInstancedMeshGeometryVisual3D Constructor
Top
Properties
 NameDescription
Public propertyInstancesData Gets or sets an array of InstanceData structs that define the world transformations (positions, scales, rotations) and colors or instanced geometry objects. After each change of InstancesData user must call Update method.
Public propertyIsBackFaceMaterial Gets or sets a Boolean that specifies if this InstancedMeshGeometryVisual3D is rendering front or back faces of the MeshGeometry3D. Default value is false which means that front face materials are rendered.
Public propertyIsPreMultipliedAlphaColor When IsPreMultipliedAlphaColor is true (false by default) and Alpha is less then 1, then the instance colors components are already multiplied with alpha value to produce pre-multiplied colors. When false, then non-pre-multiplied color is converted in pre-multiplied color when this is required by the shader.
Public propertyIsSolidColorMaterial Gets or sets a Boolean that specifies if instanced objects are rendered with a solid color (without any shading based on lighting calculations). Default value is false.
Public propertyIsWpf3DVisible Gets a Boolean that specifies if this BaseDXVisual3D can show its content when rendered with WPF 3D objects.
(Overrides BaseDXVisual3DIsWpf3DVisible)
Public propertyIsWpfHitTestVisible Gets or sets a Boolean that specifies if WPF hit testing can be done on the instanced 3D objects when rendered with DirectX hardware acceleration. When IsWpfHitTestVisible is true then InstancedMeshGeometryVisual3D always creates WPF's GeometryModel3D objects (one for each instance) that are used by WPF's hit testing. This can greatly increase initialization time. Default value is false. When showing instanced objects, it is recommended to use DXEngine hit testing that support fast hit testing on instanced objects and leave this property on false.
Public propertyMeshGeometry3D Gets MeshGeometry3D that is used by this InstancedMeshGeometryVisual3D
Public propertySpecularMaterial Gets material that is used as a base material for this InstancedMeshGeometryVisual3D. BaseMaterial can provide additional specular material properties. After changing the property the UpdateMaterial method must be called.
Public propertyTextureAlphaClipThreshold TextureAlphaClipThreshold can be set when TextureBitmap is set. When its value is above 0 then pixels in texture with alpha color values below this value will be clipped (not rendered and their depth will not be written to depth buffer). Expected values are between 0 and 1. When 0 (by default) then alpha clipping is disabled - this means that also pixels with alpha value 0 are fully processed (they are not visible but its depth value is still written so objects that are rendered afterwards and are behind the pixel will not be visible).
Public propertyTextureBitmap When TextureBitmap is set to a BitmapSource, then this texture is used for all instances of the mesh (instance color is not used).
Public propertyTextureBlendState Gets or sets a BlendState that is used to render the texture. If null then Opaque or PremultipliedAlphaBlend (for transparent materials) will be used.
Public propertyUseAlphaBlend Gets or sets a Boolean that specifies if alpha blending is used when rendering model instances. This can be used to render semi-transparent objects. Default value is false.
Public propertyUseDynamicInstanceBuffer Gets or sets a Boolean that specifies if dynamic instance buffer is created instead of immutable instance buffer. Dynamic instance buffer has better performance when instance data is changed often. On the other side the immutable buffer is better when the instance data is not changed or changed only a few times. Default value is false - use immutable buffer.
Public propertyUseScreenSpaceScaling When true (false by default) then each instance is scaled in the shader so that the value specified in matrix's M11 defines the size of the rendered object on screen (when the size of the mesh is 1).
Top
Methods
 NameDescription
Public methodGenerateModel3DGroup GenerateModel3DGroup converts this InstancedMeshGeometryVisual3D into a WPF's Model3DGroup object with many GeometryModel3D objects (each is created form one InstanceData).
Public methodUpdate Update method updates the data for all instances. It must be called after InstancesData are changed. When only some other instances were changed, it is highly recommended to call Update(Int32, Int32, Boolean) and pass startIndex and count as parameters.
Public methodUpdate(Boolean) Update method updates the data for all instances and provides a parameter to specify if bounding box need to be updated.
Public methodUpdate(Int32, Int32, Boolean) Update method updates instance data specified with startIndex, count parameters and updateBounds. This method can be called after some instance transformations or colors were changed. NOTE: If multiple sections of data are changed, do not call this method multiple times - it is faster to call Update only once.
Public methodUpdateMaterial UpdateMaterial updates the used specular material. It must be called after the SpecularMaterial is changed.
Public methodUseInstanceIdColor After calling UseInstanceIdColor method then the color of each instance is defined by the id (index) of each instance. The addedColor4 is added the id (usually a Color4.Black is used). The lowers 8 bytes of the instance id number are written to blue color, then to the green color, then to red color and the highest byte is written to alpha color. The color can be also used when rendering multiple instanced objects to distinguish between them (for example by using different alpha value). To disable using instance id color call UseInstanceObjectColor or UseSingleObjectColor(Color).
Public methodUseInstanceObjectColor UseInstanceObjectColor method can be called after calling the UseSingleObjectColor(Color). This method resets using diffuse color from instances data.
Public methodUseSingleObjectColor When UseSingleObjectColor is called, the specified objectColor is used as diffuse color for all instances. This overrides the color defined in the instances data. When color's alpha value is less than 1, then alpha blending is used. To use instance color after calling UseSingleObjectColor, call the UseInstanceObjectColor method.
Top
Fields
 NameDescription
Public fieldStatic memberInstanceIndexProperty InstanceIndexProperty - set to GeometryModel3D objects that are created with InstancedMeshGeometryVisual3D. Its value is set to instance index and can be used in WPDF hit testing to get the index of the hit instance. Default value is -1 (returned when InstanceIndexProperty is not set).
Top
Extension Methods
 NameDescription
Public Extension MethodClearDXAttribute Clears the value from DXAttributeType so that IsDXAttributeSet(Visual3D, DXAttributeType) method returns false for that DXAttributeType.
(Defined by Extensions)
Public Extension MethodGetDXAttribute Gets the value of the specified DXAttributeType from the specified Visual3D. If the attributeType does not exists then an exception is thrown. Use GetDXAttributeOrDefaultT(Visual3D, DXAttributeType) or GetDXAttributeOrDefaultT(Visual3D, DXAttributeType, T) to prevent throwing exception when the value is not set.
(Defined by Extensions)
Public Extension MethodGetDXAttributeCollection Gets a DXAttributeCollection will a collection of all DXEngine's attributes set to the specified WPF's object. Returns null when no DXAttributeCollection was attached to the WPF object.
(Defined by Extensions)
Public Extension MethodGetDXAttributeOrDefaultT Gets the value of the specified DXAttributeType from the specified Visual3D. If the attributeType does not exists then a default value for the type T is returned.
(Defined by Extensions)
Public Extension MethodGetDXAttributeOrDefaultT Gets the value of the specified DXAttributeType from the specified Visual3D. If the attributeType does not exists then the specified defaultValue is returned.
(Defined by Extensions)
Public Extension MethodIsDXAttributeSet Returns true if the specified attributeType was set (result is true even if the value is null).
(Defined by Extensions)
Public Extension MethodSetDXAttribute Annotates the specified wpfObject with adding or updating a DXEngine's attribute to it. The attribute is specified with attributeType and its value. This can be used to annotate WPF's objects with DXEngine's attributes.
(Defined by Extensions)
Top
Remarks

InstancedMeshGeometryVisual3D is a WPF Visual3D object that can be used to define a MeshGeometry3D that will be rendered multiple times as instances where each instace can have its own color and its own world matrix (defines scale, rotation and translation).

When rendered with DirectX, InstancedMeshGeometryVisual3D provides the fastest way to render many MeshGeometry3D instances.

The MeshGeometry3D object that is rendered with this InstancedMeshGeometryVisual3D need to be specified in the InstancedMeshGeometryVisual3D constructor. In the constructor you can also specify the SpecularMaterial that can be used for all instances (but can be null).

The data about each instance that will be rendered are specified with InstancesData property. InstancesData is an array of InstanceData structs. Each InstanceData defines a Matrix and Color. Matrix can be used to position the object (by defining the M41, M42, M43 fields), scale of the object (by defining M11, M22, M33) and rotation (with using method on the Matrix class). Color defines the color of this instance - same as using DiffuseMaterial with SolidColorBrush. If color has alpha value less the 1 (is semi-transparent), then see below for more information on how to render semi-transparent objects.

To render back faces (back side of triangles) defined in the MeshGeometry3D, set the IsBackFaceMaterial to true.

By default the InstancedMeshGeometryVisual3D is not hit test visible - this means that by default you cannot use WPF hit testing or subscribe on mouse events (MouseOver, MouseClick, etc.) with EventManager3D from Ab3d.PowerToys. To enable hit testing, set the IsWpfHitTestVisible property to true.
IMPORTANT:
When IsWpfHitTestVisible is true then InstancedMeshGeometryVisual3D always creates WPF's GeometryModel3D objects (one for each instance) that are used by WPF's hit testing. This can greatly increase initialization time.

When GeometryModel3D objects are created with this InstancedMeshGeometryVisual3D, then a customInstanceIndexProperty property is set to each created GeometryModel3D object. This property can be read with using modelHit.GetValue(InstancedMeshGeometryVisual3D.InstanceIndexProperty) to get the instance index that was used to create the GeometryModel3D object.

When the values in the InstancesData are changed after the InstancedMeshGeometryVisual3D has been rendered, you need to call the Update or Update(Int32, Int32, Boolean) method (call the overload without parameters when data for all instances are changed, call Update with parameters when only some instances data are changed).

If you want to render many instances of Model3DGroup objects, you can use the InstancedModelGroupVisual3D.

When rendered with WPF, InstancedMeshGeometryVisual3D is rendered as multiple GeometryModel3D objects inside Model3DGroup.

Rendering semi-transparent instances

When instances use semi-transparent colors (color's alpha is less than 1), the UseAlphaBlend property must be set to true. This will use alpha bending to blend the semi-transparent colors with already rendered 3D objects.

If only some objects have transparent colors and the colors do not change, then it is recommended to split the instance objects into two InstancedMeshGeometryVisual3D objects - one with solid and the other with semi-transparent instances.

When rendering semi-transparent objects, you need to define the InstancedMeshGeometryVisual3D after opaque 3D objects (in the WPF's tree view). Note that TransparencyHelper from Ab3d.PowerToys does not work with InstancedMeshGeometryVisual3D - it always considers InstancedMeshGeometryVisual3D as opaque object.

Because semi-transparent objects are rendered with using alpha blending (blending semi-transparent color with already rendered objects), the order of positions need to be such, that the instances that farther away from the camera need to be added before those that are closer to the camera.

See Also