public static ScreenOffset Offset(Vector3 point, Rectangle bounds, Viewport viewport, Matrix projection,
Matrix view, Matrix currentAttitude)
{
// Create a World matrix for the point.
var world = Matrix.CreateWorld(point, new Vector3(0, 0, -1), new Vector3(0, 1, 0));
// Use Viewport.Project to project the point from 3D space into screen coordinates.
var projected = viewport.Project(Vector3.Zero, projection, view, world*currentAttitude);
if (projected.Z > 1 || projected.Z < 0)
{
// If the point is outside of this range, it is behind the camera.
// So hide the TextBlock for this point.
return default(ScreenOffset);
}
// Create a TranslateTransform to position the TextBlock.
// Offset by half of the TextBlock's RenderSize to center it on the point.
var tt = new ScreenOffset
{
TranslateX = projected.X - (bounds.Width/2),
TranslateY = projected.Y - (bounds.Height/2),
Scale = 1/projected.Z
};
return tt;
}
}