-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMirror.ts
102 lines (88 loc) · 2.67 KB
/
Mirror.ts
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
import { Camera, Viewer } from "cesium";
import { createViewer } from "..";
/**
* 镜像功能,可以实现双屏模式
*
* @export
* @class Mirror
*/
export default class Mirror {
readonly master: Viewer;
readonly slave: Viewer;
/**
* Creates an instance of Mirror.
* @param {string} master 左侧参考viewer 标记主体
* @param {string} slave 右侧参考viewer 跟随标记
* @memberof Mirror
*/
constructor(master: string | Viewer, slave: string | Viewer) {
if (master === slave)
throw new Error(`container is same to viewer`);
if (typeof master === 'string')
this.master = createViewer(master);
else
this.master = master;
if (typeof slave === 'string')
this.slave = createViewer(slave);
else
this.slave = slave;
if (this.master === null || this.slave === null)
throw new Error(`create viewer failed !`);
this.init();
}
/**
* 获取左右两个viewer
*
* @return {*} {{ master: Viewer, slave: Viewer }}
* @memberof Mirror
*/
getViewers(): { master: Viewer, slave: Viewer } {
return { master: this.master, slave: this.slave };
}
/**
* 销毁
*
* @memberof Mirror
*/
destroy() {
this.master.destroy();
this.slave.destroy();
}
/**
* 初始化镜像功能
*
* @private
* @memberof Mirror
*/
private init() {
const leftCamera = this.master.camera;
const rightCamera = this.slave.camera;
const leftMirrorHandler = this.createMirrorHandler(rightCamera, leftCamera);
const rightMirrorHandler = this.createMirrorHandler(leftCamera, rightCamera);
leftCamera.changed.addEventListener(rightMirrorHandler);
this.master.scene.preRender.addEventListener(rightMirrorHandler);
rightCamera.changed.addEventListener(leftMirrorHandler);
this.slave.scene.preRender.addEventListener(leftMirrorHandler);
}
/**
* 创建镜像Handler 实现camera同步
*
* @private
* @param {Camera} masterCamera 主体相机
* @param {Camera} slaveCamera 跟随相机
* @return {*}
* @memberof Mirror
*/
private createMirrorHandler(masterCamera: Camera, slaveCamera: Camera): () => void {
return () => {
slaveCamera.setView({
destination: masterCamera.position.clone(),
orientation: {
heading: masterCamera.heading,
pitch: masterCamera.pitch,
roll: masterCamera.roll
}
})
}
}
}