unprojectOnTbPlane method
Vector3
unprojectOnTbPlane(])
- Unproject the cursor on the plane passing through the center of the trackball orthogonal to the camera
cameraThe virtual cameracursorXCursor horizontal coordinate on screencursorYCursor vertical coordinate on screeninitialDistanceIf initial distance between camera and gizmos should be used for calculations instead of current (Perspective only)- returns Vector3 The unprojected point on the trackball plane
Implementation
Vector3 unprojectOnTbPlane(Camera camera, double cursorX, double cursorY,[bool initialDistance = false]) {
if (camera is OrthographicCamera) {
_v2_1.setFrom(getCursorPosition(cursorX, cursorY));
_v3_1.setValues(_v2_1.x, _v2_1.y, 0);
return _v3_1.clone();
}
else if (camera is PerspectiveCamera) {
_v2_1.setFrom(getCursorNDC(cursorX, cursorY));
//unproject cursor on the near plane
_v3_1.setValues(_v2_1.x, _v2_1.y, -1);
_v3_1.applyMatrix4(camera.projectionMatrixInverse);
final rayDir = _v3_1.clone().normalize(); //unprojected ray direction
// camera
// |\
// | \
// | \
// h | \
// | \
// | \
// _ _ | _ _ _\ _ _ near plane
// l
final h = _v3_1.z;
final l = math.sqrt(math.pow(_v3_1.x, 2) + math.pow(_v3_1.y, 2));
dynamic cameraGizmoDistance;
if (initialDistance) {
cameraGizmoDistance = _v3_1
.setFromMatrixPosition(_cameraMatrixState0)
.distanceTo(
_v3_2.setFromMatrixPosition(_gizmoMatrixState0));
}
else {
cameraGizmoDistance = camera.position.distanceTo(_gizmos.position);
}
/*
* calculate intersection point between unprojected ray and the plane
*|y = mx + q
*|y = 0
*
* x = -q/m
*/
if (l == 0) {
//ray aligned with camera
rayDir.setValues(0, 0, 0);
return rayDir;
}
final m = h / l;
final q = cameraGizmoDistance;
final x = -q / m;
final rayLength = math.sqrt(math.pow(q, 2) + math.pow(x, 2));
rayDir.scale(rayLength);
rayDir.z = 0;
return rayDir;
}
return Vector3();
}