|
InstanceDataGetHitInstanceIndex Method |
GetHitInstanceIndex returns index of the instance that was hit in hit testing (defined by hitBounds).
The method retuns -1 if hitBounds is not found (was not hit).
Namespace: Ab3d.DirectXAssembly: Ab3d.DXEngine.Wpf (in Ab3d.DXEngine.Wpf.dll) Version: 7.1.9105.2048
Syntax public static int GetHitInstanceIndex(
Rect3D hitBounds,
Rect3D instancedMeshBounds,
InstanceData[] instanceData,
bool useOnlyMatrixTranslation = false
)
Parameters
- hitBounds Rect3D
- Rect3D that defines the bounds of the hit model
- instancedMeshBounds Rect3D
- Rect3D that defines the bounds of the MeshGeometry3D that was used in object instancing
- instanceData InstanceData
- array of InstanceData
- useOnlyMatrixTranslation Boolean (Optional)
- true if World matrices in instanceData define only translation (OffsetX, OffsetY, OffsetZ) and no scale or rotation (in this case a faster code path can be taken); if false (by default) a full matrix transformation is executed on each position
Return Value
Int32index of the instance that was hit or -1 if no instance is hit
Remarks
The following code shows a method that changes the color of the instance with mouse over it.
The following method is called from Ab3d.Utilities.EventManager3D after is is subscribed to MouseMove event.
Example
private void VisualEventSource3DOnMouseMove(object sender, Mouse3DEventArgs e)
{
if (e.RayHitResult == null || e.RayHitResult.ModelHit == null)
return; // This should not happen, but it is safer to have this check anyway
// Check if this model was already selected
if (ReferenceEquals(e.RayHitResult.ModelHit, _lastHitModel))
return;
var hitBounds = e.RayHitResult.ModelHit.Bounds;
var hitInstanceIndex = InstanceData.GetHitInstanceIndex(hitBounds, _instanceMeshGeometry3D.Bounds, _instancedData, useOnlyMatrixTranslation: true);
if (hitInstanceIndex == -1)
return; // We did not find the instance index
// Set color of hit object to Red
_instancedData[hitInstanceIndex].DiffuseColor = Colors.Red.ToColor4();
// After each change of instaceData we need to call Update method on InstancedMeshGeometryVisual3D
_instancedMeshGeometryVisual3D.Update();
}
See Also