Original Post
Hi, I have another question on Java3D. For my camera class I want to provide a screenToWorld() method that takes the mouse position and a depth and returns the corresponding world coordinates. The problem is that I can't find any proper methods/functionality in Java3D to do this. There is a getInverseVworldProjection(...) method in Canvas3D but the returned matrix seems very odd. As an example, if I transform (0,0,1) with that matrix I get something like (0, 10000, 10000) with my camera being located at (0,20,20) looking at (0,0,0) and far plane distance = 100). If the camera moves the values for y and z seem to change exponentially. I found a workaround that does the job but I'd like to have a proper calculation. The workaround is as follows: Does anyone know how to correctly and efficiently unproject screen coordinates in Java3D?
public Point3d screenToWorld(Point2d pPoint, double pDepth) {
//get the only canvas
Canvas3D tCanvas3D = view.getCanvas3D(0);
//get eye position
Point3d tEyePos = new Point3d();
tCanvas3D.getCenterEyeInImagePlate(tEyePos);
//get the screen coords of the point
Point3d tMouseCoords = new Point3d();
tCanvas3D.getPixelLocationInImagePlate(pPoint, tMouseCoords);
//project eye and screen pos to world
Transform3D tTransform = new Transform3D();
tCanvas3D.getImagePlateToVworld(tTransform);
tTransform.transform(tMouseCoords);
tTransform.transform(tEyePos);
//create a direction vector from eye to screen with the desired length (depth * distance of far plane)
Vector3d tDir = new Vector3d(tMouseCoords);
tDir.sub(tEyePos);
tDir.normalize();
tDir.scale(pDepth * view.getBackClipDistance());
//world pos is eye + direction vector
Point3d tWorldCoords = new Point3d(tEyePos);
tWorldCoords.add(tDir);
return tWorldCoords;
}