forked from MapGIS/WebClient-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebSceneControl.js
More file actions
270 lines (246 loc) · 10.1 KB
/
WebSceneControl.js
File metadata and controls
270 lines (246 loc) · 10.1 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import { CesiumZondy } from '../core/Base';
/**
* 三维视图的主要类
* @alias WebSceneControl
* @constructor
* @class module:客户端视图管理.WebSceneControl
* @param {Element|String} elementId 放置视图的div的id
* @param {Object} [options] 包含以下属性的对象
* @param {String} [options.viewerMode=‘3D’] 初始视图模式默认为三维球视图 '2D'表示二维视图 'COLUMBUS_VIEW' 表示三维平面视图
* @param {Boolean} [options.showInfo=false] 是否显示默认的属性信息框
* @param {Boolean} [options.animation=true] 默认动画控制不显示
* @param {Boolean} [options.baseLayerPicker=true] If set to false, the BaseLayerPicker widget will not be created.
* @param {Boolean} [options.fullscreenButton=true] If set to false, the FullscreenButton widget will not be created.
* @param {Boolean} [options.vrButton=false] If set to true, the VRButton widget will be created.
* @param {Boolean} [options.onCopy=false] 是否禁用复制,默认为false禁用
* @example
* var webGlobe = new CesiumZondy.WebSceneControl('GlobeView');
*
* var webGlobe = new CesiumZondy.WebSceneControl('GlobeView',{showInfo:true});
* //或者如下
* var options ={
* showInfo:false,
* viewerMode:'3D',
* keyEventEnable:false
* };
* var webGlobe = new CesiumZondy.WebSceneControl('GlobeView',options);
*/
export default class WebSceneControl {
constructor(elementId, op) {
const options = Cesium.defaultValue(op, {});
/** 默认动画控制不显示 */
options.animation = Cesium.defaultValue(options.animation, false);
// 默认不显示图层控制显示
options.baseLayerPicker = Cesium.defaultValue(options.baseLayerPicker, false);
// 默认不显示全屏控制按钮
options.fullscreenButton = Cesium.defaultValue(options.fullscreenButton, false);
// 默认不显示地名查询框
options.geocoder = Cesium.defaultValue(options.geocoder, false);
// 默认不显示复位按钮
options.homeButton = Cesium.defaultValue(options.homeButton, false);
// 默认不显示信息框
options.infoBox = Cesium.defaultValue(options.infoBox, false);
// 默认不显示3D/2D选择器
options.sceneModePicker = Cesium.defaultValue(options.sceneModePicker, false);
// 默认不显示选取指示器组件
options.selectionIndicator = Cesium.defaultValue(options.selectionIndicator, false);
// 默认创建但不显示时间轴
options.timeline = Cesium.defaultValue(options.timeline, false);
// 默认不显示帮助按钮
options.navigationHelpButton = Cesium.defaultValue(options.navigationHelpButton, false);
options.navigationInstructionsInitiallyVisible = Cesium.defaultValue(options.navigationInstructionsInitiallyVisible, true);
// 默认不显示渲染错误信息面板
options.showRenderLoopErrors = Cesium.defaultValue(options.showRenderLoopErrors, false);
// 默认场景为三维球面视图
options.sceneMode = Cesium.defaultValue(options.sceneMode, Cesium.SceneMode.SCENE3D);
// 默认地图投影为web 墨卡托
options.mapProjection = Cesium.defaultValue(options.mapProjection, new Cesium.WebMercatorProjection());
// 默认可视化数据源集合
options.dataSources = Cesium.defaultValue(options.dataSources, new Cesium.DataSourceCollection());
// 默认支持阴影
options.shadows = Cesium.defaultValue(options.shadows, false);
// 使用 ThreeJS 默认要关闭自动渲染
if (this._useThreeJs) {
options.useDefaultRenderLoop = false;
}
this._threeContainer = undefined;
// 管理append添加的图层组
this._appendCollection = [];
// 默认支持键盘事件
this._keyEventEnable = Cesium.defaultValue(options.keyEventEnable, true);
// 创建默认视图对象
this._viewer = new Cesium.Viewer(elementId, options);
//隐藏版权信息
this._viewer.cesiumWidget.creditContainer.style.display = 'none';
// 场景对象
this._scene = this._viewer.scene;
this._screenSpaceEventHandler = new Cesium.ScreenSpaceEventHandler(this._viewer.scene.canvas);
this._elementID = elementId;
this._popupContain = []; // 用于管理多个popup,主要考虑到多个popup场景变化时需响应其事件,改变其位置
const screenSpaceCameraController = this._viewer.scene.screenSpaceCameraController;
//默认关闭hdr
this._viewer.scene.highDynamicRange = false;
screenSpaceCameraController.minimumZoomDistance = 1;
screenSpaceCameraController.maximumZoomDistance = 2400000000000000;
this._viewer.canvas.onclick = function () {
this.focus();
};
this._cameraParameter = {};
const flags = {
looking: false,
rotateLeft: false,
rotateRight: false,
moveUp: false,
moveDown: false,
moveLeft: false,
moveRight: false,
goHome: false,
wireFrame: false,
showFPS: false
};
//与activex球保持一致
function getFlagForKeyCode(keyCode) {
switch (keyCode) {
case 'W'.charCodeAt(0): //向下平移镜头
return 'moveDown';
case 'S'.charCodeAt(0): //向上平移镜头
return 'moveUp';
case 'A'.charCodeAt(0): //向右平移镜头
return 'moveRight';
case 'D'.charCodeAt(0): //向左平移镜头
return 'moveLeft';
case 'Q'.charCodeAt(0): //向右旋转镜头
return 'rotateRight';
case 'E'.charCodeAt(0): //向左旋转镜头
return 'rotateLeft';
case 'Z'.charCodeAt(0): //空格键复位
return 'goHome';
case 'G'.charCodeAt(0): //G键显示网
return 'wireFrame';
case 'F'.charCodeAt(0): //F键显示帧率
return 'showFPS';
default:
return undefined;
}
}
document.addEventListener(
'keydown',
function (e) {
const flagName = getFlagForKeyCode(e.keyCode);
if (typeof flagName !== 'undefined') {
flags[flagName] = true;
}
},
false
);
document.addEventListener(
'keyup',
function (e) {
const flagName = getFlagForKeyCode(e.keyCode);
if (typeof flagName !== 'undefined') {
flags[flagName] = false;
}
},
false
);
this._shouldAnimate = Cesium.defaultValue(options.shouldAnimate, false); //记录全局是否允许动画
var that = this;
this._viewer.clock.onTick.addEventListener(function () {
//获取相机高度
if (that.keyEventEnable) {
const position = that._viewer.camera.position;
const cameraHeight = that._viewer.scene.globe.ellipsoid.cartesianToCartographic(position).height;
const moveRate = cameraHeight / 40.0;
if (flags.rotateLeft) {
that._viewer.camera.rotateLeft(0.01);
}
if (flags.rotateRight) {
that._viewer.camera.rotateRight(0.01);
}
if (flags.moveUp) {
that._viewer.camera.moveBackward(moveRate);
}
if (flags.moveDown) {
that._viewer.camera.moveForward(moveRate);
}
if (flags.moveLeft) {
that._viewer.camera.moveLeft(moveRate);
}
if (flags.moveRight) {
that._viewer.camera.moveRight(moveRate);
}
if (flags.goHome) {
that._viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(104, 30, 15682725)
});
}
if (flags.wireFrame) {
var bShowWireframe = that._viewer.scene.globe._surface.tileProvider._debug.wireframe;
that._viewer.scene.globe._surface.tileProvider._debug.wireframe = !bShowWireframe;
flags.wireFrame = false;
}
if (flags.showFPS) {
var bShowFPS = that._viewer.scene.debugShowFramesPerSecond;
that._viewer.scene.debugShowFramesPerSecond = !bShowFPS;
flags.showFPS = false;
}
}
});
/**
* 禁用右键菜单
*/
document.oncontextmenu = function () {
event.returnValue = false;
};
// /**
// * 禁用选中功能
// */
// document.onselectstart = function(){
// event.returnValue = false;
// };
/**
* 禁用复制功能
*/
document.oncopy = function () {
event.returnValue = that._onCopy;
};
this.scene.skyAtmosphere.showGroundAtmosphere = false;
this._isRecoverExplosion = false;
}
/**
* 视图
* @memberof WebSceneControl.prototype
* @type {Viewer}
* @readonly
*/
get viewer() {
return this._viewer;
}
/**
* 场景
* @memberof WebSceneControl.prototype
* @readonly
* @type {Scene}
*/
get scene() {
return this._scene;
}
/**
* 事件句柄
* @memberof WebSceneControl.prototype
* @readonly
*/
get screenSpaceEventHandler() {
return this._screenSpaceEventHandler;
}
/**
* 当前椭球
* @memberof WebSceneControl.prototype
* @type {Ellipsoid}
* @readonly
*/
get ellipsoid() {
return this._viewer.scene.globe.ellipsoid;
}
}
CesiumZondy.WebSceneControl = WebSceneControl;