Click or drag to resize
AB4D logo

BaseCameraGetWorldToViewportMatrix Method

Gets Matrix3D that can be used to convert 3D coordinates to 2D viewport (screen) coordinates. If the method returns true, than the worldToViewportMatrix was set; if false is returned than the matrix cannot be calculated.

Namespace:  Ab3d.Cameras
Assembly:  Ab3d.PowerToys (in Ab3d.PowerToys.dll) Version: 10.1.8147.1045
Syntax
C#
public bool GetWorldToViewportMatrix(
	ref Matrix3D worldToViewportMatrix,
	bool forceMatrixRefresh
)

Parameters

worldToViewportMatrix
Type: System.Windows.Media.Media3DMatrix3D
reference to Matrix3D
forceMatrixRefresh
Type: SystemBoolean
if true than the worldToViewportMatrix is always recalculated; if false the recalculation is done only when the main camera properties were changed

Return Value

Type: Boolean
true if worldToViewportMatrix was set; false if the matrix cannot be calculated
Remarks

GetWorldToViewportMatrix gets Matrix3D that can be used to convert 3D coordinates to 2D viewport coordinates.

If the method returns true, than the worldToViewportMatrix was set; if false is returned than the matrix cannot be calculated (for example when MatrixCamera is used and the matrix cannot be inverted).

The cameras in Ab3d.PowerToys use protected field isWorldToViewportMatrixDirty that is used to determine if the cached worldToViewportMatrix must be recalculated when its value is required. The isWorldToViewportMatrixDirty is set to true when main camera properties are changed. But if some other property is changed (for example FarPlaneDistance), than forceMatrixRefresh parameter in this method need to be set to true to force recalculation of worldToViewportMatrix.

Important:
To calculate the screen coordinates of a 3D point you need to convert the Point3D to Point4D, multiply it with worldToViewportMatrix and than divide the X and Y coordinate with W value from Point4D struct. The following code shows that:

Point3D inputPoint3D;
Point screenPoint;
Matrix3D worldToViewportMatrix;
bool isworldToViewportMatrixValid = myCamera.GetWorldToViewportMatrix(ref worldToViewportMatrix, false);

if (isworldToViewportMatrixValid)
{
    Point4D point4d = ((Point4D)inputPoint3D) * worldToViewportMatrix;

    if (point4d.W != 0)
        screenPoint = new Point(point4d.X / point4d.W, point4d.Y / point4d.W);
    else
        screenPoint = new Point(double.NaN, double.NaN);
}
else
{
    screenPoint = new Point(double.NaN, double.NaN);
}
See Also