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') {
    this._v2_1.copy(this.getCursorPosition(cursorX, cursorY, canvas));
    this._v3_1.set(this._v2_1.x, this._v2_1.y, 0);

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

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

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

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

    var h = this._v3_1.z;
    var l = Math.sqrt(Math.pow(this._v3_1.x, 2) + Math.pow(this._v3_1.y, 2));
    var cameraGizmoDistance;

    if (initialDistance) {
      cameraGizmoDistance = this
          ._v3_1
          .setFromMatrixPosition(this._cameraMatrixState0)
          .distanceTo(
              this._v3_2.setFromMatrixPosition(this._gizmoMatrixState0));
    } else {
      cameraGizmoDistance = camera.position.distanceTo(this._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;
  }
}