scale method

Map<String, Matrix4>? scale(
  1. double size,
  2. Vector? point, [
  3. bool scaleGizmos = true
])
  • Perform uniform scale operation around a given point
  • size Scale factor
  • point Point around which scale
  • 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

Map<String, Matrix4>? scale(double size, Vector? point, [bool scaleGizmos = true]) {
    _scalePointTemp.setFrom(point ?? Vector3());
    double sizeInverse = 1 / size;

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

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

      camera.updateProjectionMatrix();

      _v3_1
          .setFromMatrixPosition(_gizmoMatrixState); //gizmos position

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

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

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

      final amount = _scalePointTemp.clone().scale(sizeInverse);
      _scalePointTemp.sub(amount);

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

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

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

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

      _offset
          .setFrom(_scalePointTemp)
          .sub(_v3_1)
          .normalize()
          .scale(amount);

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

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

        distance = pos.distanceTo(_scalePointTemp);
        amount = distance - (distance * sizeInverse);
        _offset
            .setFrom(_scalePointTemp)
            .sub(_v3_2)
            .normalize()
            .scale(amount);

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

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

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

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

      return _transformation;
    }

    return null;
  }