2023西安数博会CIM演示-【前端】-Web
AdaKing88
2023-08-21 bc03b832caa49bbcd2674fe4cae3701b5059bf95
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
 
// 键盘控制相机
// 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;