All files / engine/Source/Scene CameraFlightPath.js

98.07% Statements 204/208
91.57% Branches 87/95
100% Functions 22/22
98.07% Lines 204/208

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577                                      1x           23x 21x 21x 21x 21x 21x 2x             2x     1x 1x               22x 1x 1x 1x   1x 1x   1x 3x 3x 2x 2x     1x 1x     21x 226x                     24x 24x   24x 23x 23x 23x 23x 23x   23x 23x     23x               23x           24x 14x 14x   14x 14x   14x 230x 230x       10x 9x         46x             15x     46x 3x 43x 6x     46x     1x                       2x   2x 2x 2x 2x   2x               2x               3x   3x               3x 3x   2x       4x 3x   1x         16x 16x 1x 15x 5x       1x 1x                           20x 20x 20x   20x       20x 20x 20x   20x       20x 20x   20x   20x 5x   5x 5x   5x   5x   2x   2x   2x 2x   2x       1x   3x 3x       20x 4x   16x     20x             20x                         20x 20x 20x 20x   20x 226x   226x             226x                   20x                       2x   2x 2x   2x 2x                 3x   3x           3x   3x   3x 3x   3x 3x 3x 3x 3x   2x     1x 1x     11x                     9x 2x     9x   58x     1x 33x 33x     33x 1x   32x 1x     31x   31x 2x     29x 29x 29x 29x 29x 29x 29x 29x   29x 6x 6x     29x 29x 29x 5x     29x 29x 7x     7x     29x 29x 29x   29x 29x   29x 29x   29x   29x 29x     29x               29x                 29x                                   29x 5x     24x 24x 24x 24x   24x 4x 4x                       4x   4x 4x     4x     20x                         20x 20x   20x       20x 3x   17x       20x                              
import Cartesian2 from "../Core/Cartesian2.js";
import Cartesian3 from "../Core/Cartesian3.js";
import Cartographic from "../Core/Cartographic.js";
import Frozen from "../Core/Frozen.js";
import defined from "../Core/defined.js";
import DeveloperError from "../Core/DeveloperError.js";
import EasingFunction from "../Core/EasingFunction.js";
import CesiumMath from "../Core/Math.js";
import PerspectiveFrustum from "../Core/PerspectiveFrustum.js";
import PerspectiveOffCenterFrustum from "../Core/PerspectiveOffCenterFrustum.js";
import SceneMode from "./SceneMode.js";
 
/**
 * Creates tweens for camera flights.
 * <br /><br />
 * Mouse interaction is disabled during flights.
 *
 * @private
 */
const CameraFlightPath = {};
 
function getAltitude(frustum, dx, dy) {
  let near;
  let top;
  let right;
  if (frustum instanceof PerspectiveFrustum) {
    const tanTheta = Math.tan(0.5 * frustum.fovy);
    near = frustum.near;
    top = frustum.near * tanTheta;
    right = frustum.aspectRatio * top;
    return Math.max((dx * near) / right, (dy * near) / top);
  } else Iif (frustum instanceof PerspectiveOffCenterFrustum) {
    near = frustum.near;
    top = frustum.top;
    right = frustum.right;
    return Math.max((dx * near) / right, (dy * near) / top);
  }
 
  return Math.max(dx, dy);
}
 
const scratchCart = new Cartesian3();
const scratchCart2 = new Cartesian3();
 
function createPitchFunction(
  startPitch,
  endPitch,
  heightFunction,
  pitchAdjustHeight,
) {
  if (defined(pitchAdjustHeight) && heightFunction(0.5) > pitchAdjustHeight) {
    const startHeight = heightFunction(0.0);
    const endHeight = heightFunction(1.0);
    const middleHeight = heightFunction(0.5);
 
    const d1 = middleHeight - startHeight;
    const d2 = middleHeight - endHeight;
 
    return function (time) {
      const altitude = heightFunction(time);
      if (time <= 0.5) {
        const t1 = (altitude - startHeight) / d1;
        return CesiumMath.lerp(startPitch, -CesiumMath.PI_OVER_TWO, t1);
      }
 
      const t2 = (altitude - endHeight) / d2;
      return CesiumMath.lerp(-CesiumMath.PI_OVER_TWO, endPitch, 1 - t2);
    };
  }
  return function (time) {
    return CesiumMath.lerp(startPitch, endPitch, time);
  };
}
 
function createHeightFunction(
  camera,
  destination,
  startHeight,
  endHeight,
  optionAltitude,
) {
  let altitude = optionAltitude;
  const maxHeight = Math.max(startHeight, endHeight);
 
  if (!defined(altitude)) {
    const start = camera.position;
    const end = destination;
    const up = camera.up;
    const right = camera.right;
    const frustum = camera.frustum;
 
    const diff = Cartesian3.subtract(start, end, scratchCart);
    const verticalDistance = Cartesian3.magnitude(
      Cartesian3.multiplyByScalar(up, Cartesian3.dot(diff, up), scratchCart2),
    );
    const horizontalDistance = Cartesian3.magnitude(
      Cartesian3.multiplyByScalar(
        right,
        Cartesian3.dot(diff, right),
        scratchCart2,
      ),
    );
 
    altitude = Math.min(
      getAltitude(frustum, verticalDistance, horizontalDistance) * 0.2,
      1000000000.0,
    );
  }
 
  if (maxHeight < altitude) {
    const power = 8.0;
    const factor = 1000000.0;
 
    const s = -Math.pow((altitude - startHeight) * factor, 1.0 / power);
    const e = Math.pow((altitude - endHeight) * factor, 1.0 / power);
 
    return function (t) {
      const x = t * (e - s) + s;
      return -Math.pow(x, power) / factor + altitude;
    };
  }
 
  return function (t) {
    return CesiumMath.lerp(startHeight, endHeight, t);
  };
}
 
function adjustAngleForLERP(startAngle, endAngle) {
  if (
    CesiumMath.equalsEpsilon(
      startAngle,
      CesiumMath.TWO_PI,
      CesiumMath.EPSILON11,
    )
  ) {
    startAngle = 0.0;
  }
 
  if (endAngle > startAngle + Math.PI) {
    startAngle += CesiumMath.TWO_PI;
  } else if (endAngle < startAngle - Math.PI) {
    startAngle -= CesiumMath.TWO_PI;
  }
 
  return startAngle;
}
 
const scratchStart = new Cartesian3();
 
function createUpdateCV(
  scene,
  duration,
  destination,
  heading,
  pitch,
  roll,
  optionAltitude,
  optionPitchAdjustHeight,
) {
  const camera = scene.camera;
 
  const start = Cartesian3.clone(camera.position, scratchStart);
  const startPitch = camera.pitch;
  const startHeading = adjustAngleForLERP(camera.heading, heading);
  const startRoll = adjustAngleForLERP(camera.roll, roll);
 
  const heightFunction = createHeightFunction(
    camera,
    destination,
    start.z,
    destination.z,
    optionAltitude,
  );
 
  const pitchFunction = createPitchFunction(
    startPitch,
    pitch,
    heightFunction,
    optionPitchAdjustHeight,
  );
 
  function update(value) {
    const time = value.time / duration;
 
    camera.setView({
      orientation: {
        heading: CesiumMath.lerp(startHeading, heading, time),
        pitch: pitchFunction(time),
        roll: CesiumMath.lerp(startRoll, roll, time),
      },
    });
 
    Cartesian2.lerp(start, destination, time, camera.position);
    camera.position.z = heightFunction(time);
  }
  return update;
}
 
function useLongestFlight(startCart, destCart) {
  if (startCart.longitude < destCart.longitude) {
    startCart.longitude += CesiumMath.TWO_PI;
  } else {
    destCart.longitude += CesiumMath.TWO_PI;
  }
}
 
function useShortestFlight(startCart, destCart) {
  const diff = startCart.longitude - destCart.longitude;
  if (diff < -CesiumMath.PI) {
    startCart.longitude += CesiumMath.TWO_PI;
  } else if (diff > CesiumMath.PI) {
    destCart.longitude += CesiumMath.TWO_PI;
  }
}
 
const scratchStartCart = new Cartographic();
const scratchEndCart = new Cartographic();
 
function createUpdate3D(
  scene,
  duration,
  destination,
  heading,
  pitch,
  roll,
  optionAltitude,
  optionFlyOverLongitude,
  optionFlyOverLongitudeWeight,
  optionPitchAdjustHeight,
) {
  const camera = scene.camera;
  const projection = scene.mapProjection;
  const ellipsoid = projection.ellipsoid;
 
  const startCart = Cartographic.clone(
    camera.positionCartographic,
    scratchStartCart,
  );
  const startPitch = camera.pitch;
  const startHeading = adjustAngleForLERP(camera.heading, heading);
  const startRoll = adjustAngleForLERP(camera.roll, roll);
 
  const destCart = ellipsoid.cartesianToCartographic(
    destination,
    scratchEndCart,
  );
  startCart.longitude = CesiumMath.zeroToTwoPi(startCart.longitude);
  destCart.longitude = CesiumMath.zeroToTwoPi(destCart.longitude);
 
  let useLongFlight = false;
 
  if (defined(optionFlyOverLongitude)) {
    const hitLon = CesiumMath.zeroToTwoPi(optionFlyOverLongitude);
 
    const lonMin = Math.min(startCart.longitude, destCart.longitude);
    const lonMax = Math.max(startCart.longitude, destCart.longitude);
 
    const hitInside = hitLon >= lonMin && hitLon <= lonMax;
 
    if (defined(optionFlyOverLongitudeWeight)) {
      // Distance inside  (0...2Pi)
      const din = Math.abs(startCart.longitude - destCart.longitude);
      // Distance outside (0...2Pi)
      const dot = CesiumMath.TWO_PI - din;
 
      const hitDistance = hitInside ? din : dot;
      const offDistance = hitInside ? dot : din;
 
      if (
        hitDistance < offDistance * optionFlyOverLongitudeWeight &&
        !hitInside
      ) {
        useLongFlight = true;
      }
    } else Eif (!hitInside) {
      useLongFlight = true;
    }
  }
 
  if (useLongFlight) {
    useLongestFlight(startCart, destCart);
  } else {
    useShortestFlight(startCart, destCart);
  }
 
  const heightFunction = createHeightFunction(
    camera,
    destination,
    startCart.height,
    destCart.height,
    optionAltitude,
  );
  const pitchFunction = createPitchFunction(
    startPitch,
    pitch,
    heightFunction,
    optionPitchAdjustHeight,
  );
 
  // Isolate scope for update function.
  // to have local copies of vars used in lerp
  // Othervise, if you call nex
  // createUpdate3D (createAnimationTween)
  // before you played animation, variables will be overwriten.
  function isolateUpdateFunction() {
    const startLongitude = startCart.longitude;
    const destLongitude = destCart.longitude;
    const startLatitude = startCart.latitude;
    const destLatitude = destCart.latitude;
 
    return function update(value) {
      const time = value.time / duration;
 
      const position = Cartesian3.fromRadians(
        CesiumMath.lerp(startLongitude, destLongitude, time),
        CesiumMath.lerp(startLatitude, destLatitude, time),
        heightFunction(time),
        ellipsoid,
      );
 
      camera.setView({
        destination: position,
        orientation: {
          heading: CesiumMath.lerp(startHeading, heading, time),
          pitch: pitchFunction(time),
          roll: CesiumMath.lerp(startRoll, roll, time),
        },
      });
    };
  }
  return isolateUpdateFunction();
}
 
function createUpdate2D(
  scene,
  duration,
  destination,
  heading,
  pitch,
  roll,
  optionAltitude,
) {
  const camera = scene.camera;
 
  const start = Cartesian3.clone(camera.position, scratchStart);
  const startHeading = adjustAngleForLERP(camera.heading, heading);
 
  const startHeight = camera.frustum.right - camera.frustum.left;
  const heightFunction = createHeightFunction(
    camera,
    destination,
    startHeight,
    destination.z,
    optionAltitude,
  );
 
  function update(value) {
    const time = value.time / duration;
 
    camera.setView({
      orientation: {
        heading: CesiumMath.lerp(startHeading, heading, time),
      },
    });
 
    Cartesian2.lerp(start, destination, time, camera.position);
 
    const zoom = heightFunction(time);
 
    const frustum = camera.frustum;
    const ratio = frustum.top / frustum.right;
 
    const incrementAmount = (zoom - (frustum.right - frustum.left)) * 0.5;
    frustum.right += incrementAmount;
    frustum.left -= incrementAmount;
    frustum.top = ratio * frustum.right;
    frustum.bottom = -frustum.top;
  }
  return update;
}
 
const scratchCartographic = new Cartographic();
const scratchDestination = new Cartesian3();
 
function emptyFlight(complete, cancel) {
  return {
    startObject: {},
    stopObject: {},
    duration: 0.0,
    complete: complete,
    cancel: cancel,
  };
}
 
function wrapCallback(controller, cb) {
  function wrapped() {
    if (typeof cb === "function") {
      cb();
    }
 
    controller.enableInputs = true;
  }
  return wrapped;
}
 
CameraFlightPath.createTween = function (scene, options) {
  options = options ?? Frozen.EMPTY_OBJECT;
  let destination = options.destination;
 
  //>>includeStart('debug', pragmas.debug);
  if (!defined(scene)) {
    throw new DeveloperError("scene is required.");
  }
  if (!defined(destination)) {
    throw new DeveloperError("destination is required.");
  }
  //>>includeEnd('debug');
  const mode = scene.mode;
 
  if (mode === SceneMode.MORPHING) {
    return emptyFlight();
  }
 
  const convert = options.convert ?? true;
  const projection = scene.mapProjection;
  const ellipsoid = projection.ellipsoid;
  const maximumHeight = options.maximumHeight;
  const flyOverLongitude = options.flyOverLongitude;
  const flyOverLongitudeWeight = options.flyOverLongitudeWeight;
  const pitchAdjustHeight = options.pitchAdjustHeight;
  let easingFunction = options.easingFunction;
 
  if (convert && mode !== SceneMode.SCENE3D) {
    ellipsoid.cartesianToCartographic(destination, scratchCartographic);
    destination = projection.project(scratchCartographic, scratchDestination);
  }
 
  const camera = scene.camera;
  const transform = options.endTransform;
  if (defined(transform)) {
    camera._setTransform(transform);
  }
 
  let duration = options.duration;
  if (!defined(duration)) {
    duration =
      Math.ceil(Cartesian3.distance(camera.position, destination) / 1000000.0) +
      2.0;
    duration = Math.min(duration, 3.0);
  }
 
  const heading = options.heading ?? 0.0;
  const pitch = options.pitch ?? -CesiumMath.PI_OVER_TWO;
  const roll = options.roll ?? 0.0;
 
  const controller = scene.screenSpaceCameraController;
  controller.enableInputs = false;
 
  const complete = wrapCallback(controller, options.complete);
  const cancel = wrapCallback(controller, options.cancel);
 
  const frustum = camera.frustum;
 
  let empty = scene.mode === SceneMode.SCENE2D;
  empty =
    empty &&
    Cartesian2.equalsEpsilon(camera.position, destination, CesiumMath.EPSILON6);
  empty =
    empty &&
    CesiumMath.equalsEpsilon(
      Math.max(frustum.right - frustum.left, frustum.top - frustum.bottom),
      destination.z,
      CesiumMath.EPSILON6,
    );
 
  empty =
    empty ||
    (scene.mode !== SceneMode.SCENE2D &&
      Cartesian3.equalsEpsilon(
        destination,
        camera.position,
        CesiumMath.EPSILON10,
      ));
 
  empty =
    empty &&
    CesiumMath.equalsEpsilon(
      CesiumMath.negativePiToPi(heading),
      CesiumMath.negativePiToPi(camera.heading),
      CesiumMath.EPSILON10,
    ) &&
    CesiumMath.equalsEpsilon(
      CesiumMath.negativePiToPi(pitch),
      CesiumMath.negativePiToPi(camera.pitch),
      CesiumMath.EPSILON10,
    ) &&
    CesiumMath.equalsEpsilon(
      CesiumMath.negativePiToPi(roll),
      CesiumMath.negativePiToPi(camera.roll),
      CesiumMath.EPSILON10,
    );
 
  if (empty) {
    return emptyFlight(complete, cancel);
  }
 
  const updateFunctions = new Array(4);
  updateFunctions[SceneMode.SCENE2D] = createUpdate2D;
  updateFunctions[SceneMode.SCENE3D] = createUpdate3D;
  updateFunctions[SceneMode.COLUMBUS_VIEW] = createUpdateCV;
 
  if (duration <= 0.0) {
    const newOnComplete = function () {
      const update = updateFunctions[mode](
        scene,
        1.0,
        destination,
        heading,
        pitch,
        roll,
        maximumHeight,
        flyOverLongitude,
        flyOverLongitudeWeight,
        pitchAdjustHeight,
      );
      update({ time: 1.0 });
 
      Eif (typeof complete === "function") {
        complete();
      }
    };
    return emptyFlight(newOnComplete, cancel);
  }
 
  const update = updateFunctions[mode](
    scene,
    duration,
    destination,
    heading,
    pitch,
    roll,
    maximumHeight,
    flyOverLongitude,
    flyOverLongitudeWeight,
    pitchAdjustHeight,
  );
 
  Eif (!defined(easingFunction)) {
    const startHeight = camera.positionCartographic.height;
    const endHeight =
      mode === SceneMode.SCENE3D
        ? ellipsoid.cartesianToCartographic(destination).height
        : destination.z;
 
    if (startHeight > endHeight && startHeight > 11500.0) {
      easingFunction = EasingFunction.CUBIC_OUT;
    } else {
      easingFunction = EasingFunction.QUINTIC_IN_OUT;
    }
  }
 
  return {
    duration: duration,
    easingFunction: easingFunction,
    startObject: {
      time: 0.0,
    },
    stopObject: {
      time: duration,
    },
    update: update,
    complete: complete,
    cancel: cancel,
  };
};
export default CameraFlightPath;