execute method

  1. @override
Future<void> execute()
override

Implementation

@override
Future<void> execute() async {
  if (_executing) {
    return;
  }

  _executing = true;

  if (_queuedRotationDelta.length2 == 0.0 &&
      _queuedZoomDelta == 0.0 &&
      _queuedMoveDelta.length2 == 0.0) {
    _executing = false;
    return;
  }

  Matrix4 currentPlayerTransform = await viewer.getWorldTransform(player);

  // first we need to convert the move vector to player space
  var newTransform =
      Matrix4.translation(_queuedMoveDelta * movementSensitivity);

  _queuedMoveDelta = Vector3.zero();
  Matrix4 newPlayerTransform = newTransform * currentPlayerTransform;
  await viewer.setTransform(player, newPlayerTransform);

  if (_queuedZoomDelta != 0.0) {
    // Ignore zoom
  }

  var inverted = newPlayerTransform.clone()..invert();

  // camera is always looking at -Z, whereas models generally face towards +Z
  if (_queuedRotationDelta.length2 > 0.0) {
    double deltaX =
        _queuedRotationDelta.x * rotationSensitivity;
    double deltaY =
        _queuedRotationDelta.y * rotationSensitivity;

    cameraLookAt = Matrix4.rotationY(-deltaX) *
        Matrix4.rotationX(-deltaY) *
        cameraLookAt;
    _queuedRotationDelta = Vector2.zero();
  }

  var newCameraViewMatrix =
      makeViewMatrix(cameraPosition, cameraLookAt, cameraUp);
  newCameraViewMatrix.invert();
  var newCameraTransform = newPlayerTransform * newCameraViewMatrix;
  await camera.setTransform(newCameraTransform);

  await viewer.queueTransformUpdates(
      [camera.getEntity(), player], [newCameraTransform, newPlayerTransform]);
  onUpdate?.call(newPlayerTransform);
  _executing = false;
}