将 3d 对象与相机位置对齐

Align 3d Object to Camera position

我正在使用 Viewport 控件在 AB4D PowerToys 的帮助下可视化 3d 工具模型。

目前我正在尝试将我的 3D 文本(一组线条)与 camera 对齐,因此它总是旋转到相机并看起来像 2D 文本。

是否可以将我的视觉转换绑定到我的相机位置?

终于自己找到了答案:

应在 "CameraChanged" 事件中调用以下代码。

private void UpdateTransforms()
    {
        if (!this.IsAttachedToViewport3D())
        {
            return;
        }

        Matrix3D view, projection;
        if (Camera.GetCameraMatrixes(out view, out projection))
        {
            Vector3D up = new Vector3D(view.M12, view.M22, view.M32);
            Vector3D look = new Vector3D(view.M13, view.M23, view.M33);

            Transform3D transform = Transform;
            Matrix3D inverseWorld = this.GetTransform();
            inverseWorld.Invert();

            look = inverseWorld.Transform(look);
            look.Normalize();

            up = inverseWorld.Transform(up);
            up.Normalize();

            Quaternion q = LookAtRotation(look, up);

            Transform3DGroup grp = new Transform3DGroup();
            grp.Children.Add(new RotateTransform3D(new QuaternionRotation3D(q), CenterPosition));
            grp.Children.Add(transform);
            Transform = grp;

            Camera.Refresh();
        }
    }

    private static Quaternion LookAtRotation(Vector3D lookAt, Vector3D upDirection)
    {
        Vector3D forward = lookAt;
        Vector3D up = upDirection;

        // Orthonormalize
        forward.Normalize();
        up -= forward * Vector3D.DotProduct(up, forward);
        up.Normalize();

        Vector3D right = Vector3D.CrossProduct(up, forward);

        Quaternion q = new Quaternion
        {
            W = Math.Sqrt(1.0 + right.X + up.Y + forward.Z) * 0.5
        };

        double w4Recip = 1.0 / (4.0 * q.W);
        q.X = (up.Z - forward.Y) * w4Recip;
        q.Y = (forward.X - right.Z) * w4Recip;
        q.Z = (right.Y - up.X) * w4Recip;

        return q;
    }