|
// 键盘控制相机
|
// W 前进移动
|
// S 后退移动
|
// A 向左移动
|
// D 向右移动
|
// Q 向上移动
|
// E 向下移动
|
// R 向 左歪头
|
// T 向 右歪头
|
// 箭头上 抬头
|
// 箭头下 低头
|
// 箭头左 向左转头
|
// 箭头右 向右转头
|
// Z 添加移动速度
|
// X 减小移动速度
|
// C 添加旋转速度
|
// V 减小旋转速度
|
|
let keyMoveCamera = function (viewer) {
|
this.viewer = viewer;
|
this.wPress = false
|
this.aPress = false
|
this.sPress = false
|
this.dPress = false
|
this.qPress = false
|
this.ePress = false
|
this.rPress = false
|
this.tPress = false
|
this.arrowUpPress = false
|
this.arrowDownPress = false
|
this.arrowLeftPress = false
|
this.arrowRightPress = false
|
|
this.moveSpeed = 0.1
|
this.rotateSpeed = 0.05
|
this.rotateChange = false
|
this.moveCameraTimeHandle = 0
|
|
this.keyDown = (e) => {
|
switch (e.code) {
|
case 'KeyW':
|
this.wPress = true
|
break;
|
case 'KeyS':
|
this.sPress = true
|
break;
|
case 'KeyD':
|
this.dPress = true
|
break;
|
case 'KeyA':
|
this.aPress = true
|
break;
|
case 'KeyQ':
|
this.qPress = true
|
break;
|
case 'KeyE':
|
this.ePress = true
|
break;
|
case 'KeyZ':
|
this.moveSpeed *= 1.1
|
break;
|
case 'KeyX':
|
this.moveSpeed *= 0.9
|
break;
|
case 'KeyC':
|
this.rotateSpeed *= 1.1
|
break;
|
case 'KeyV':
|
this.rotateSpeed *= 0.9
|
break;
|
case 'KeyR':
|
this.rPress = true
|
break;
|
case 'KeyT':
|
this.tPress = true
|
break;
|
case 'ArrowUp':
|
this.arrowUpPress = true
|
break;
|
case 'ArrowDown':
|
this.arrowDownPress = true
|
break;
|
case 'ArrowLeft':
|
this.arrowLeftPress = true
|
break;
|
case 'ArrowRight':
|
this.arrowRightPress = true
|
break;
|
default:
|
break;
|
}
|
}
|
this.keyUp = (e) => {
|
switch (e.code) {
|
case 'KeyW':
|
this.wPress = false
|
break;
|
case 'KeyS':
|
this.sPress = false
|
break;
|
case 'KeyD':
|
this.dPress = false
|
break;
|
case 'KeyA':
|
this.aPress = false
|
break;
|
case 'KeyQ':
|
this.qPress = false
|
break;
|
case 'KeyE':
|
this.ePress = false
|
break;
|
case 'KeyR':
|
this.rPress = false
|
break;
|
case 'KeyT':
|
this.tPress = false
|
break;
|
case 'ArrowUp':
|
this.arrowUpPress = false
|
break;
|
case 'ArrowDown':
|
this.arrowDownPress = false
|
break;
|
case 'ArrowLeft':
|
this.arrowLeftPress = false
|
break;
|
case 'ArrowRight':
|
this.arrowRightPress = false
|
break;
|
default:
|
break;
|
}
|
}
|
this.enable = () => {
|
if (!this.moveCameraTimeHandle) {
|
document.addEventListener('keydown', this.keyDown, false);
|
document.addEventListener('keyup', this.keyUp, false);
|
this.moveCameraTimeHandle = setInterval(() => {
|
if (this.wPress) {
|
this.viewer.camera.moveForward(this.moveSpeed);
|
}
|
if (this.aPress) {
|
this.viewer.camera.moveLeft(this.moveSpeed);
|
}
|
if (this.sPress) {
|
this.viewer.camera.moveBackward(this.moveSpeed);
|
}
|
if (this.dPress) {
|
this.viewer.camera.moveRight(this.moveSpeed);
|
}
|
let positionTemp = this.viewer.camera.positionWC;
|
let orientation = {
|
heading: this.viewer.camera.heading,
|
pitch: this.viewer.camera.pitch,
|
roll: this.viewer.camera.roll,
|
endTransform: Cesium.Matrix4.IDENTITY
|
}
|
this.rotateChange = false;
|
|
if (this.qPress) {
|
let cartographic = Cesium.Cartographic.fromCartesian(positionTemp);
|
positionTemp = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, cartographic.height + this.moveSpeed * 2)
|
this.rotateChange = true;
|
}
|
if (this.ePress) {
|
let cartographic = Cesium.Cartographic.fromCartesian(positionTemp);
|
positionTemp = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, cartographic.height - this.moveSpeed * 2)
|
this.rotateChange = true;
|
}
|
if (this.arrowUpPress) {
|
orientation.pitch = Cesium.Math.toRadians(Cesium.Math.toDegrees(this.viewer.camera.pitch) + this.rotateSpeed)
|
this.rotateChange = true;
|
}
|
if (this.arrowDownPress) {
|
orientation.pitch = Cesium.Math.toRadians(Cesium.Math.toDegrees(this.viewer.camera.pitch) - this.rotateSpeed)
|
this.rotateChange = true;
|
}
|
if (this.arrowLeftPress) {
|
orientation.heading = Cesium.Math.toRadians(Cesium.Math.toDegrees(this.viewer.camera.heading) - this.rotateSpeed)
|
this.rotateChange = true;
|
}
|
if (this.arrowRightPress) {
|
orientation.heading = Cesium.Math.toRadians(Cesium.Math.toDegrees(this.viewer.camera.heading) + this.rotateSpeed)
|
this.rotateChange = true;
|
}
|
if (this.rPress) {
|
orientation.roll = Cesium.Math.toRadians(Cesium.Math.toDegrees(this.viewer.camera.roll) + this.rotateSpeed)
|
this.rotateChange = true;
|
}
|
if (this.tPress) {
|
orientation.roll = Cesium.Math.toRadians(Cesium.Math.toDegrees(this.viewer.camera.roll) - this.rotateSpeed)
|
this.rotateChange = true;
|
}
|
if (this.rotateChange) {
|
this.viewer.camera.setView({ destination: positionTemp, orientation: orientation });
|
}
|
}, 40)
|
}
|
}
|
this.disable = () => {
|
document.removeEventListener('keydown', this.keyDown, false);
|
document.removeEventListener('keyup', this.keyUp, false);
|
clearInterval(this.moveCameraTimeHandle)
|
this.moveCameraTimeHandle = 0;
|
}
|
}
|
export default keyMoveCamera;
|