1
\$\begingroup\$

Im trying to display a mesh in front of the camera and FACING the camera ( I'm developing an small editor in wpf using helix3d).

Pretty much something like this question

I load my mesh into a ModelVisual3D and I set it 20 units in front of the camera with this code:

var camera = ((PerspectiveCamera) viewport.Camera);
var distanceFromCamera = 20;

var direction = camera.LookDirection;
direction.Normalize();
direction = direction * distanceFromCamera;

var meshPosition = camera.Position + direction;


 var compositeTransform = new Transform3DGroup();

 compositeTransform.Children.Add(new RotationTransform(???));
 compositeTransform.Children.Add(new TranslateTransform3D(meshPosition));



ModelVisual3D mesh = LoadMesh(...);
mesh.Transform = compositeTransform

It already shows correctly in front of the camera, but I'm missing the rotation, how can I extract the rotation of the camera and set it to my mesh so is facing the camera?

Edit

I tried to use the following function to get the rotation matrix but is not working either:

    static Matrix3D CalculateLookAt(Vector3D eye, Vector3D at, Vector3D up)
    {
        if (Math.Abs(up.Length - 0.0) < double.Epsilon) up = new Vector3D(0, 1, 0);
        var zaxis = (at - eye);
        zaxis.Normalize();
        var xaxis = Vector3D.CrossProduct(up, zaxis);
        xaxis.Normalize();
        var yaxis = Vector3D.CrossProduct(zaxis, xaxis);


        return new Matrix3D(
            xaxis.X, yaxis.X, zaxis.X, 0,
            xaxis.Y, yaxis.Y, zaxis.Y, 0,
            xaxis.Z, yaxis.Z, zaxis.Z, 0,
            Vector3D.DotProduct(xaxis, -eye), Vector3D.DotProduct(yaxis, -eye), Vector3D.DotProduct(zaxis, -eye), 1
            );
    }
\$\endgroup\$
  • \$\begingroup\$ Are you using C# or JavaScript? Your code looks like having java declaration. But still, you don't have to extract the rotation of the camera. You just have rotate the object towards the position of the camera. \$\endgroup\$ – MAnd Dec 2 '15 at 20:13
  • \$\begingroup\$ is pseudo-code, I re wrote it in notepad from my original c# code. and exactly how do I rotate the object towards the position of the camera? \$\endgroup\$ – elios264 Dec 2 '15 at 20:19
  • \$\begingroup\$ Are you using Unity or any other engine? \$\endgroup\$ – MAnd Dec 2 '15 at 20:22
  • \$\begingroup\$ no I'm not, just helix-toolkit.org, but is more like a 3d viewer for WPF applications. \$\endgroup\$ – elios264 Dec 2 '15 at 20:25
1
\$\begingroup\$

I had to ask a friend that is good at 3D graphics this is what he did to make it right:

 ProjectionCamera camera = (ProjectionCamera) viewport.Camera;

Vector3D ax1 = camera.LookDirection.Normalized();
Vector3D ax2 = Vector3D.CrossProduct(ax1, camera.UpDirection).Normalized() * -1;
Vector3D ax3 = camera.UpDirection.Normalized();

Vector3D translation = (ax1*20 + camera.Position).ToVector3D();
Quaternion rotation = new Quaternion(ax3, -90) * QuaternionUtils.CreateFromRotationAxes(ax1,ax2,ax3);

Transform3DGroup transformGroup = new Transform3DGroup();

transformGroup.Children.Add(new RotateTransform3D(new QuaternionRotation3D(rotation)));
transformGroup.Children.Add(new TranslateTransform3D(translation));

mesh.Transform = transformGroup;
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.