pointerMove method

dynamic pointerMove(
  1. Pointer pointer
)

Implementation

pointerMove(Pointer pointer) {
  // TODO if not when change axis will cause object position change. why???
  if (pointer.x == _pointer0?.x && pointer.y == _pointer0?.y && pointer.button == _pointer0?.button) {
    return;
  }
  _pointer0 = pointer;

  var axis = this.axis;
  var mode = this.mode;
  var object = this.object;
  var space = this.space;

  if (mode == 'scale') {
    space = 'local';
  } else if (axis == 'E' || axis == 'XYZE' || axis == 'XYZ') {
    space = 'world';
  }

  if (object == null || axis == null || dragging == false || pointer.button != 1) return;

  _raycaster.setFromCamera(Vector2(pointer.x, pointer.y), camera);

  var planeIntersect = intersectObjectWithRay(_plane, _raycaster, true);

  if (planeIntersect == null || planeIntersect == false) return;

  pointEnd.copy(planeIntersect.point).sub(worldPositionStart);

  if (mode == 'translate') {
    // Apply translate

    _offset.copy(pointEnd).sub(pointStart);

    if (space == 'local' && axis != 'XYZ') {
      _offset.applyQuaternion(_worldQuaternionInv);
    }

    if (axis.indexOf('X') == -1) _offset.x = 0;
    if (axis.indexOf('Y') == -1) _offset.y = 0;
    if (axis.indexOf('Z') == -1) _offset.z = 0;

    if (space == 'local' && axis != 'XYZ') {
      _offset.applyQuaternion(_quaternionStart).divide(_parentScale);
    } else {
      _offset.applyQuaternion(_parentQuaternionInv).divide(_parentScale);
    }

    object.position.copy(_offset).add(_positionStart);

    // Apply translation snap

    if (translationSnap != null) {
      if (space == 'local') {
        object.position.applyQuaternion(_tempQuaternion.copy(_quaternionStart).invert());

        if (axis.indexOf('X') != -1) {
          object.position.x = Math.round(object.position.x / translationSnap) * translationSnap;
        }

        if (axis.indexOf('Y') != -1) {
          object.position.y = Math.round(object.position.y / translationSnap) * translationSnap;
        }

        if (axis.indexOf('Z') != -1) {
          object.position.z = Math.round(object.position.z / translationSnap) * translationSnap;
        }

        object.position.applyQuaternion(_quaternionStart);
      }

      if (space == 'world') {
        if (object.parent != null) {
          object.position.add(_tempVector.setFromMatrixPosition(object.parent.matrixWorld));
        }

        if (axis.indexOf('X') != -1) {
          object.position.x = Math.round(object.position.x / translationSnap) * translationSnap;
        }

        if (axis.indexOf('Y') != -1) {
          object.position.y = Math.round(object.position.y / translationSnap) * translationSnap;
        }

        if (axis.indexOf('Z') != -1) {
          object.position.z = Math.round(object.position.z / translationSnap) * translationSnap;
        }

        if (object.parent != null) {
          object.position.sub(_tempVector.setFromMatrixPosition(object.parent.matrixWorld));
        }
      }
    }
  } else if (mode == 'scale') {
    if (axis.indexOf('XYZ') != -1) {
      var d = pointEnd.length() / pointStart.length();

      if (pointEnd.dot(pointStart) < 0) d *= -1;

      _tempVector2.set(d, d, d);
    } else {
      _tempVector.copy(pointStart);
      _tempVector2.copy(pointEnd);

      _tempVector.applyQuaternion(_worldQuaternionInv);
      _tempVector2.applyQuaternion(_worldQuaternionInv);

      _tempVector2.divide(_tempVector);

      if (axis.indexOf('X') == -1) {
        _tempVector2.x = 1;
      }

      if (axis.indexOf('Y') == -1) {
        _tempVector2.y = 1;
      }

      if (axis.indexOf('Z') == -1) {
        _tempVector2.z = 1;
      }
    }

    // Apply scale

    object.scale.copy(_scaleStart).multiply(_tempVector2);

    if (scaleSnap != null) {
      if (axis.indexOf('X') != -1) {
        var x = Math.round(object.scale.x / scaleSnap) * scaleSnap;

        object.scale.x = x != 0 ? x : scaleSnap;
      }

      if (axis.indexOf('Y') != -1) {
        var y = Math.round(object.scale.y / scaleSnap) * scaleSnap;

        object.scale.y = y != 0 ? y : scaleSnap;
      }

      if (axis.indexOf('Z') != -1) {
        var z = Math.round(object.scale.z / scaleSnap) * scaleSnap;

        object.scale.z = z != 0 ? z : scaleSnap;
      }
    }
  } else if (mode == 'rotate') {
    _offset.copy(pointEnd).sub(pointStart);

    var rotationSpeed = 20 / worldPosition.distanceTo(_tempVector.setFromMatrixPosition(camera.matrixWorld));

    if (axis == 'E') {
      rotationAxis.copy(eye);
      rotationAngle = pointEnd.angleTo(pointStart);

      _startNorm.copy(pointStart).normalize();
      _endNorm.copy(pointEnd).normalize();

      rotationAngle *= (_endNorm.cross(_startNorm).dot(eye) < 0 ? 1 : -1);
    } else if (axis == 'XYZE') {
      rotationAxis.copy(_offset).cross(eye).normalize();
      rotationAngle = _offset.dot(_tempVector.copy(rotationAxis).cross(eye)) * rotationSpeed;
    } else if (axis == 'X' || axis == 'Y' || axis == 'Z') {
      rotationAxis.copy(_unit[axis]);

      _tempVector.copy(_unit[axis]);

      if (space == 'local') {
        _tempVector.applyQuaternion(worldQuaternion);
      }

      rotationAngle = _offset.dot(_tempVector.cross(eye).normalize()) * rotationSpeed;
    }

    // Apply rotation snap

    if (rotationSnap != null) {
      rotationAngle = Math.round(rotationAngle / rotationSnap) * rotationSnap;
    }

    // Apply rotate
    if (space == 'local' && axis != 'E' && axis != 'XYZE') {
      object.quaternion.copy(_quaternionStart);
      object.quaternion.multiply(_tempQuaternion.setFromAxisAngle(rotationAxis, rotationAngle)).normalize();
    } else {
      rotationAxis.applyQuaternion(_parentQuaternionInv);
      object.quaternion.copy(_tempQuaternion.setFromAxisAngle(rotationAxis, rotationAngle));
      object.quaternion.multiply(_quaternionStart).normalize();
    }
  }

  dispatchEvent(_changeEvent);
  dispatchEvent(_objectChangeEvent);
}