Search code examples
c#matrixvectorhelix-3d-toolkit

How to rotate a Text In HelixToolkit


I'm trying to insert a TextVisual3D with rotated alignment. If the origin point is zero, no problem, but when the origin is in other location, the transform moves 2 times the distance. I've tried many ways, with no success. I'm using the "Up" view, the UpDirection is using the Vector (0, 1, 0), to plain the text.

        var text = new TextVisual3D();
        text.Foreground = Brushes.Cyan;

        text.FontSize = size / 4;
        text.Height = size * 2;
        text.Text = value;
        text.UpDirection = new Vector3D(0, 1, 0);
        text.HorizontalAlignment = horizontal;
        text.VerticalAlignment = vertical;
        text.Position = origin.GetPoint3D();


        if (Rotation!=0)
        {
            var angle = Rotation.Round(0);
            if(angle.Abs()!=360)
            {
                var axis = new Vector3D(0,0,1);
                var matrix = text.Transform.Value;
                matrix.Rotate(new Quaternion(axis, angle));
                text.Transform = new MatrixTransform3D(matrix);
            }
        }

        return text;

The Result:

enter image description here

Expected:

enter image description here


Solution

  • After so many attempts, I've discovered the solution.

            var text = new TextVisual3D();
            text.Foreground = Brushes.Cyan;
            text.FontSize = size / 4;
            text.Height = size * 2;
            text.Text = value;
            text.UpDirection = new Vector3D(0, 1, 0);
            text.HorizontalAlignment = horizontal;
            text.VerticalAlignment = vertical;
            text.Position = origin.GetPoint3D();
    
    
            if (Rotation != 0)
            {
                var angle = Rotation.Round(0);
                if (angle.Abs() != 360)
                {
                    var axis = new Vector3D(0, 0, 1);
                    var matrix = text.Transform.Value;
    
    
                    var rotate = new RotateTransform3D();
                    var angle_axis = new AxisAngleRotation3D();
                    angle_axis.Angle = angle;
                    angle_axis.Axis = axis;
                    rotate.Rotation = angle_axis;
                    rotate.CenterX = origin.X;
                    rotate.CenterY = origin.Y;
                    text.Transform = rotate;
                }
            }
    
            return text;
    

    Result:

    enter image description here

    Reference: https://learn.microsoft.com/en-us/dotnet/desktop/wpf/graphics-multimedia/3-d-transformations-overview?view=netframeworkdesktop-4.8