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;
}