scale method

dynamic scale(
  1. dynamic size,
  2. dynamic point, [
  3. dynamic scaleGizmos = true
])
  • Perform uniform scale operation around a given point
    • @param {Number} size Scale factor
      • @param {Vector3} point Point around which scale
      • @param {Boolean} scaleGizmos If gizmos should be scaled (Perspective only)
      • @returns {Object} Object with 'camera' and 'gizmo' fields containing transformation matrices resulting from the operation to be applied to the camera and gizmos

Implementation

scale(size, point, [scaleGizmos = true]) {
  _scalePointTemp.copy(point);
  var sizeInverse = 1 / size;

  if (this.camera is OrthographicCamera) {
    //camera zoom
    this.camera.zoom = this._zoomState;
    this.camera.zoom *= size;

    //check min and max zoom
    if (this.camera.zoom > this.maxZoom) {
      this.camera.zoom = this.maxZoom;
      sizeInverse = this._zoomState / this.maxZoom;
    } else if (this.camera.zoom < this.minZoom) {
      this.camera.zoom = this.minZoom;
      sizeInverse = this._zoomState / this.minZoom;
    }

    this.camera.updateProjectionMatrix();

    this
        ._v3_1
        .setFromMatrixPosition(this._gizmoMatrixState); //gizmos position

    //scale gizmos so they appear in the same spot having the same dimension
    this._scaleMatrix.makeScale(sizeInverse, sizeInverse, sizeInverse);
    this
        ._translationMatrix
        .makeTranslation(-this._v3_1.x, -this._v3_1.y, -this._v3_1.z);

    this
        ._m4_2
        .makeTranslation(this._v3_1.x, this._v3_1.y, this._v3_1.z)
        .multiply(this._scaleMatrix);
    this._m4_2.multiply(this._translationMatrix);

    //move camera and gizmos to obtain pinch effect
    _scalePointTemp.sub(this._v3_1);

    var amount = _scalePointTemp.clone().multiplyScalar(sizeInverse);
    _scalePointTemp.sub(amount);

    this._m4_1.makeTranslation(
        _scalePointTemp.x, _scalePointTemp.y, _scalePointTemp.z);
    this._m4_2.premultiply(this._m4_1);

    this.setTransformationMatrices(this._m4_1, this._m4_2);
    return _transformation;
  } else if (this.camera is PerspectiveCamera) {
    this._v3_1.setFromMatrixPosition(this._cameraMatrixState);
    this._v3_2.setFromMatrixPosition(this._gizmoMatrixState);

    //move camera
    var distance = this._v3_1.distanceTo(_scalePointTemp);
    var amount = distance - (distance * sizeInverse);

    //check min and max distance
    var newDistance = distance - amount;
    if (newDistance < this.minDistance) {
      sizeInverse = this.minDistance / distance;
      amount = distance - (distance * sizeInverse);
    } else if (newDistance > this.maxDistance) {
      sizeInverse = this.maxDistance / distance;
      amount = distance - (distance * sizeInverse);
    }

    _offset
        .copy(_scalePointTemp)
        .sub(this._v3_1)
        .normalize()
        .multiplyScalar(amount);

    this._m4_1.makeTranslation(_offset.x, _offset.y, _offset.z);

    if (scaleGizmos) {
      //scale gizmos so they appear in the same spot having the same dimension
      var pos = this._v3_2;

      distance = pos.distanceTo(_scalePointTemp);
      amount = distance - (distance * sizeInverse);
      _offset
          .copy(_scalePointTemp)
          .sub(this._v3_2)
          .normalize()
          .multiplyScalar(amount);

      this._translationMatrix.makeTranslation(pos.x, pos.y, pos.z);
      this._scaleMatrix.makeScale(sizeInverse, sizeInverse, sizeInverse);

      this
          ._m4_2
          .makeTranslation(_offset.x, _offset.y, _offset.z)
          .multiply(this._translationMatrix);
      this._m4_2.multiply(this._scaleMatrix);

      this._translationMatrix.makeTranslation(-pos.x, -pos.y, -pos.z);

      this._m4_2.multiply(this._translationMatrix);
      this.setTransformationMatrices(this._m4_1, this._m4_2);
    } else {
      this.setTransformationMatrices(this._m4_1);
    }

    return _transformation;
  }
}