cesium——lookAt锁定位置解锁

cesium的lookAt方法在定位后会锁定位置,无法使用鼠标中键改变视角

viewer.camera.lookAt(target, offset)
viewer.camera.lookAtTransform(Matrix4.IDENTITY)

附上lookAt源代码

const scratchLookAtMatrix4 = new Matrix4();
function lookAt (target, offset) {
  //>>includeStart('debug', pragmas.debug);
  if (!defined(target)) {
    throw new DeveloperError("target is required");
  }
  if (!defined(offset)) {
    throw new DeveloperError("offset is required");
  }
  if (this._mode === SceneMode.MORPHING) {
    throw new DeveloperError("lookAt is not supported while morphing.");
  }
  //>>includeEnd('debug');

  const transform = Transforms.eastNorthUpToFixedFrame(
    target,
    Ellipsoid.WGS84,
    scratchLookAtMatrix4
  );
  this.lookAtTransform(transform, offset);
};

function lookAtTransform (transform, offset) {
  //>>includeStart('debug', pragmas.debug);
  if (!defined(transform)) {
    throw new DeveloperError("transform is required");
  }
  if (this._mode === SceneMode.MORPHING) {
    throw new DeveloperError(
      "lookAtTransform is not supported while morphing."
    );
  }
  //>>includeEnd('debug');

  this._setTransform(transform);
  if (!defined(offset)) {
    return;
  }

  let cartesianOffset;
  if (defined(offset.heading)) {
    cartesianOffset = offsetFromHeadingPitchRange(
      offset.heading,
      offset.pitch,
      offset.range
    );
  } else {
    cartesianOffset = offset;
  }

  if (this._mode === SceneMode.SCENE2D) {
    Cartesian2.clone(Cartesian2.ZERO, this.position);

    Cartesian3.negate(cartesianOffset, this.up);
    this.up.z = 0.0;

    if (Cartesian3.magnitudeSquared(this.up) < CesiumMath.EPSILON10) {
      Cartesian3.clone(Cartesian3.UNIT_Y, this.up);
    }

    Cartesian3.normalize(this.up, this.up);

    this._setTransform(Matrix4.IDENTITY);

    Cartesian3.negate(Cartesian3.UNIT_Z, this.direction);
    Cartesian3.cross(this.direction, this.up, this.right);
    Cartesian3.normalize(this.right, this.right);

    const frustum = this.frustum;
    const ratio = frustum.top / frustum.right;
    frustum.right = Cartesian3.magnitude(cartesianOffset) * 0.5;
    frustum.left = -frustum.right;
    frustum.top = ratio * frustum.right;
    frustum.bottom = -frustum.top;

    this._setTransform(transform);

    return;
  }

  Cartesian3.clone(cartesianOffset, this.position);
  Cartesian3.negate(this.position, this.direction);
  Cartesian3.normalize(this.direction, this.direction);
  Cartesian3.cross(this.direction, Cartesian3.UNIT_Z, this.right);

  if (Cartesian3.magnitudeSquared(this.right) < CesiumMath.EPSILON10) {
    Cartesian3.clone(Cartesian3.UNIT_X, this.right);
  }

  Cartesian3.normalize(this.right, this.right);
  Cartesian3.cross(this.right, this.direction, this.up);
  Cartesian3.normalize(this.up, this.up);

  this._adjustOrthographicFrustum(true);
};

const scratchLookAtHeadingPitchRangeOffset = new Cartesian3();
const scratchLookAtHeadingPitchRangeQuaternion1 = new Quaternion();
const scratchLookAtHeadingPitchRangeQuaternion2 = new Quaternion();
const scratchHeadingPitchRangeMatrix3 = new Matrix3();

function offsetFromHeadingPitchRange(heading, pitch, range) {
  pitch = CesiumMath.clamp(
    pitch,
    -CesiumMath.PI_OVER_TWO,
    CesiumMath.PI_OVER_TWO
  );
  heading = CesiumMath.zeroToTwoPi(heading) - CesiumMath.PI_OVER_TWO;

  const pitchQuat = Quaternion.fromAxisAngle(
    Cartesian3.UNIT_Y,
    -pitch,
    scratchLookAtHeadingPitchRangeQuaternion1
  );
  const headingQuat = Quaternion.fromAxisAngle(
    Cartesian3.UNIT_Z,
    -heading,
    scratchLookAtHeadingPitchRangeQuaternion2
  );
  const rotQuat = Quaternion.multiply(headingQuat, pitchQuat, headingQuat);
  const rotMatrix = Matrix3.fromQuaternion(
    rotQuat,
    scratchHeadingPitchRangeMatrix3
  );

  const offset = Cartesian3.clone(
    Cartesian3.UNIT_X,
    scratchLookAtHeadingPitchRangeOffset
  );
  Matrix3.multiplyByVector(rotMatrix, offset, offset);
  Cartesian3.negate(offset, offset);
  Cartesian3.multiplyByScalar(offset, range, offset);
  return offset;
}

function _adjustOrthographicFrustum (zooming) {
  if (!(this.frustum instanceof OrthographicFrustum)) {
    return;
  }

  if (!zooming && this._positionCartographic.height < 150000.0) {
    return;
  }

  this.frustum.width = calculateOrthographicFrustumWidth(this);
};
const scratchAdjustOrthographicFrustumMousePosition = new Cartesian2();
const scratchPickRay = new Ray();
const scratchRayIntersection = new Cartesian3();
const scratchDepthIntersection = new Cartesian3();

function calculateOrthographicFrustumWidth(camera) {
  // Camera is fixed to an object, so keep frustum width constant.
  if (!Matrix4.equals(Matrix4.IDENTITY, camera.transform)) {
    return Cartesian3.magnitude(camera.position);
  }

  const scene = camera._scene;
  const globe = scene.globe;

  const mousePosition = scratchAdjustOrthographicFrustumMousePosition;
  mousePosition.x = scene.drawingBufferWidth / 2.0;
  mousePosition.y = scene.drawingBufferHeight / 2.0;

  let rayIntersection;
  if (defined(globe)) {
    const ray = camera.getPickRay(mousePosition, scratchPickRay);
    rayIntersection = globe.pickWorldCoordinates(
      ray,
      scene,
      true,
      scratchRayIntersection
    );
  }

  let depthIntersection;
  if (scene.pickPositionSupported) {
    depthIntersection = scene.pickPositionWorldCoordinates(
      mousePosition,
      scratchDepthIntersection
    );
  }

  let distance;
  if (defined(rayIntersection) || defined(depthIntersection)) {
    const depthDistance = defined(depthIntersection)
      ? Cartesian3.distance(depthIntersection, camera.positionWC)
      : Number.POSITIVE_INFINITY;
    const rayDistance = defined(rayIntersection)
      ? Cartesian3.distance(rayIntersection, camera.positionWC)
      : Number.POSITIVE_INFINITY;
    distance = Math.min(depthDistance, rayDistance);
  } else {
    distance = Math.max(camera.positionCartographic.height, 0.0);
  }
  return distance;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值