unprojectOnTbPlane method

dynamic unprojectOnTbPlane(
  1. dynamic camera,
  2. dynamic cursorX,
  3. dynamic cursorY,
  4. dynamic canvas, [
  5. dynamic initialDistance = false,
])

Unproject the cursor on the plane passing through the center of the trackball orthogonal to the camera @param {Camera} camera The virtual camera @param {Number} cursorX Cursor horizontal coordinate on screen @param {Number} cursorY Cursor vertical coordinate on screen @param {HTMLElement} canvas The canvas where the renderer draws its output @param {Boolean} initialDistance If 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

unprojectOnTbPlane(camera, cursorX, cursorY, canvas, [initialDistance = false]) {
  if (camera.type == 'OrthographicCamera') {
    _v2_1.copy(getCursorPosition(cursorX, cursorY, canvas));
    _v3_1.set(_v2_1.x, _v2_1.y, 0);

    return _v3_1.clone();
  } else if (camera.type == 'PerspectiveCamera') {
    _v2_1.copy(getCursorNDC(cursorX, cursorY, canvas));

    //unproject cursor on the near plane
    _v3_1.set(_v2_1.x, _v2_1.y, -1);
    _v3_1.applyMatrix4(camera.projectionMatrixInverse);

    var rayDir = _v3_1.clone().normalize(); //unprojected ray direction

    //	  camera
    //		|\
    //		| \
    //		|  \
    //	h	|	\
    //		| 	 \
    //		| 	  \
    //	_ _ | _ _ _\ _ _  near plane
    //			l

    var h = _v3_1.z;
    var l = Math.sqrt(Math.pow(_v3_1.x, 2) + Math.pow(_v3_1.y, 2));
    var 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.set(0, 0, 0);
      return rayDir;
    }

    var m = h / l;
    var q = cameraGizmoDistance;
    var x = -q / m;

    var rayLength = Math.sqrt(Math.pow(q, 2) + Math.pow(x, 2));
    rayDir.multiplyScalar(rayLength);
    rayDir.z = 0;
    return rayDir;
  }
}