cesium_dev_kit 1.0.50 → 1.0.51

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,1559 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.Primitive = Primitive;
7
- let Cesium = null;
8
- /**
9
- * 图元拓展模块
10
- * @param {*} viewer
11
- */
12
- function Primitive(viewer, cesiumGlobal) {
13
- if (viewer) {
14
- this._viewer = viewer;
15
- Cesium = cesiumGlobal;
16
- this._installPrimitives();
17
- }
18
- }
19
- Primitive.prototype = {
20
- /**
21
- * 安装拓展图元
22
- */
23
- _installPrimitives: function () {
24
- this._installProbingPrimitive();
25
- this._installTexturePrimitive();
26
- this._installWaterPrimitive();
27
- this._installPointsPrimitive();
28
- this._installShadowPrimitive();
29
- this._installArrowPolylinePrimitive();
30
- this._installXyzAxisPrimitive();
31
- this._installTetrahedronPrimitive();
32
- this._installCustomPrimitive();
33
- },
34
- /**
35
- * 自定义图元
36
- */
37
- _installCustomPrimitive: function () {
38
-
39
- // function CustomPrimitive(options) {
40
-
41
- // }
42
- },
43
- /**
44
- * 光锥图元
45
- */
46
- _installTetrahedronPrimitive: function () {
47
- try {
48
- var Cartesian3 = Cesium.Cartesian3,
49
- ComponentDatatype = Cesium.ComponentDatatype,
50
- PrimitiveType = Cesium.PrimitiveType,
51
- BoundingSphere = Cesium.BoundingSphere,
52
- GeometryAttribute = Cesium.GeometryAttribute,
53
- GeometryPipeline = Cesium.GeometryPipeline,
54
- Geometry = Cesium.Geometry,
55
- defined = Cesium.defined,
56
- RenderState = Cesium.RenderState,
57
- ShaderSource = Cesium.ShaderSource,
58
- ShaderProgram = Cesium.ShaderProgram,
59
- DrawCommand = Cesium.DrawCommand,
60
- Pass = Cesium.Pass,
61
- Appearance = Cesium.Appearance,
62
- BufferUsage = Cesium.BufferUsage,
63
- Color = Cesium.Color,
64
- VertexArray = Cesium.VertexArray,
65
- buildModuleUrl = Cesium.buildModuleUrl,
66
- Matrix4 = Cesium.Matrix4,
67
- Matrix3 = Cesium.Matrix3,
68
- Texture = Cesium.Texture,
69
- Resource = Cesium.Resource,
70
- Transforms = Cesium.Transforms,
71
- defaultValue = Cesium.defaultValue,
72
- _viewer = this._viewer;
73
- const TetrahedronPrimitive = function (options) {
74
- this.show = true;
75
- this._command = undefined;
76
- this._enuMatrix = undefined;
77
- this._scaleMatrix = undefined;
78
- this._localPosition = options.position;
79
- this._createCommand = createCommand;
80
- this._angle = 0;
81
- this._distance = defaultValue(options.distance, 1);
82
- this._setInterval = undefined;
83
- this._viewer = _viewer;
84
- this._speed = defaultValue(options.speed, 1.0);
85
- this._color = defaultValue(options.color, new Color(1.0, 1.0, 0.0, 0.8));
86
- this._scale = defaultValue(options.scale, new Cartesian3(10, 10, 15));
87
- this._texture = undefined;
88
- this._imageUrl = buildModuleUrl('static/data/images/Textures/fence.png');
89
- this._modelMatrix = computeModelMatrix(this);
90
- this._height = computeHeight(this);
91
- createTexture(this);
92
- };
93
- TetrahedronPrimitive.prototype.update = function (frameState) {
94
- if (!this.show) {
95
- return;
96
- }
97
- if (!defined(this._command)) {
98
- this._command = this._createCommand(frameState.context, this);
99
- this._command.pickId = 'v_pickColor';
100
- }
101
- if (defined(this._command)) {
102
- frameState.commandList.push(this._command);
103
- }
104
- };
105
- TetrahedronPrimitive.prototype.isDestroyed = function () {
106
- return false;
107
- };
108
- TetrahedronPrimitive.prototype.destroy = function () {
109
- if (defined(this._command)) {
110
- this._command.shaderProgram = this._command.shaderProgram && this._command.shaderProgram.destroy();
111
- }
112
- return destroyObject(this);
113
- };
114
- //开启动画
115
- TetrahedronPrimitive.prototype.startAnimate = function () {
116
- let that = this;
117
- this._setInterval = setInterval(animateFunc, 5);
118
- function animateFunc() {
119
- that._angle = that._angle + 0.01;
120
- Math.sin(that._angle) > 0 ? that._height = 0.01 : that._height = -0.01;
121
- let translation = new Cesium.Cartesian3(0, 0, that._height);
122
- Matrix4.multiplyByTranslation(that._modelMatrix, translation, that._modelMatrix);
123
- let rotationZ = Matrix4.fromRotationTranslation(Matrix3.fromRotationZ(Cesium.Math.toRadians(that._speed)));
124
- Matrix4.multiply(that._modelMatrix, rotationZ, that._modelMatrix);
125
- }
126
- };
127
- //关闭动画
128
- TetrahedronPrimitive.prototype.closeAnimate = function () {
129
- clearInterval(this._setInterval);
130
- };
131
-
132
- //创建command
133
- const createCommand = function (context, tetrahedronPrimitive) {
134
- var translucent = false;
135
- var closed = true;
136
- var vs = creaateVertexShader();
137
- var fs = createFragmentShader();
138
- // 借用一下Appearance.getDefaultRenderState
139
- var rawRenderState = Appearance.getDefaultRenderState(translucent, closed, undefined);
140
- var renderState = RenderState.fromCache(rawRenderState);
141
- var vertexShaderSource = new ShaderSource({
142
- sources: [vs]
143
- });
144
- var fragmentShaderSource = new ShaderSource({
145
- sources: [fs]
146
- });
147
- var uniformMap = {
148
- color: function () {
149
- return tetrahedronPrimitive._color;
150
- },
151
- myImage: function () {
152
- if (defined(tetrahedronPrimitive._texture)) {
153
- return tetrahedronPrimitive._texture;
154
- } else {
155
- return tetrahedronPrimitive._viewer.scene.context.defaultTexture;
156
- }
157
- }
158
- };
159
- let attributeLocations = {
160
- position: 0,
161
- textureCoordinates: 1
162
- };
163
- var shaderProgram = ShaderProgram.fromCache({
164
- context: context,
165
- vertexShaderSource: vertexShaderSource,
166
- fragmentShaderSource: fragmentShaderSource,
167
- attributeLocations: attributeLocations
168
- });
169
- return new DrawCommand({
170
- vertexArray: createVertexArray(context),
171
- primitiveType: PrimitiveType.TRIANGLES,
172
- renderState: renderState,
173
- shaderProgram: shaderProgram,
174
- uniformMap: uniformMap,
175
- owner: this,
176
- pass: Pass.TRANSLUCENT,
177
- modelMatrix: tetrahedronPrimitive._modelMatrix
178
- });
179
- };
180
-
181
- //创建vertexArray
182
- const createVertexArray = function (context) {
183
- let attributeLocations = {
184
- position: 0,
185
- textureCoordinates: 1
186
- };
187
- var positionsAndIndice = cereatePositionsAndIndice();
188
- var geometry = new Geometry({
189
- attributes: {
190
- position: new GeometryAttribute({
191
- // 使用double类型的position进行计算
192
- // componentDatatype : Cesium.ComponentDatatype.DOUBLE,
193
- componentDatatype: ComponentDatatype.FLOAT,
194
- componentsPerAttribute: 3,
195
- values: positionsAndIndice.positions
196
- }),
197
- textureCoordinates: new GeometryAttribute({
198
- componentDatatype: ComponentDatatype.FLOAT,
199
- componentsPerAttribute: 2,
200
- values: positionsAndIndice.sts
201
- })
202
- },
203
- // Workaround Internet Explorer 11.0.8 lack of TRIANGLE_FAN
204
- indices: positionsAndIndice.indices,
205
- primitiveType: PrimitiveType.TRIANGLES,
206
- boundingSphere: BoundingSphere.fromVertices(positionsAndIndice.positions)
207
- });
208
- //计算geometry的法向量
209
- var geometryNormal = GeometryPipeline.computeNormal(geometry);
210
- var vertexArray = VertexArray.fromGeometry({
211
- context: context,
212
- geometry: geometryNormal,
213
- attributeLocations: attributeLocations,
214
- bufferUsage: BufferUsage.STATIC_DRAW
215
- });
216
- return vertexArray;
217
- };
218
-
219
- //创建顶点数组与索引
220
- const cereatePositionsAndIndice = function () {
221
- var positions = new Float64Array(5 * 3);
222
- // position 0
223
- positions[0] = 0.0;
224
- positions[1] = 1.0;
225
- positions[2] = 0.0;
226
-
227
- // position 1
228
- positions[3] = -1.0;
229
- positions[4] = 0.0;
230
- positions[5] = 0.0;
231
-
232
- // position 2
233
- positions[6] = 0.0;
234
- positions[7] = -1.0;
235
- positions[8] = 0.0;
236
-
237
- // position 3
238
- positions[9] = 1.0;
239
- positions[10] = 0.0;
240
- positions[11] = 0.0;
241
-
242
- // position 4
243
- positions[12] = 0.0;
244
- positions[13] = 0.0;
245
- positions[14] = -1.0;
246
- var indices = new Uint16Array(6 * 3);
247
- // back triangle
248
- indices[0] = 4;
249
- indices[1] = 2;
250
- indices[2] = 3;
251
-
252
- // left triangle
253
- indices[3] = 4;
254
- indices[4] = 3;
255
- indices[5] = 0;
256
-
257
- // right triangle
258
- indices[6] = 4;
259
- indices[7] = 0;
260
- indices[8] = 1;
261
-
262
- // bottom triangle
263
- indices[9] = 4;
264
- indices[10] = 1;
265
- indices[11] = 2;
266
- // bottom triangle
267
- indices[12] = 1;
268
- indices[13] = 2;
269
- indices[14] = 3;
270
-
271
- // bottom triangle
272
- indices[15] = 1;
273
- indices[16] = 3;
274
- indices[17] = 0;
275
-
276
- // 1.3 定义纹理数组
277
- var sts = new Float32Array([0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.5, 0.5]);
278
- return {
279
- indices: indices,
280
- positions: positions,
281
- sts: sts
282
- };
283
- };
284
-
285
- //创建顶点着色器
286
- const creaateVertexShader = function () {
287
- var vertexShader = `
288
- attribute vec3 position;
289
- attribute vec3 normal;
290
- attribute vec2 st;
291
- attribute float batchId;
292
- varying vec3 v_positionEC;
293
- varying vec3 v_normalEC;
294
- varying vec2 v_st;
295
- varying vec4 v_pickColor;
296
- void main()
297
- {
298
- v_positionEC = (czm_modelView * vec4(position, 1.0)).xyz; // position in eye coordinates
299
- v_normalEC = czm_normal * normal; // normal in eye coordinates
300
- v_st = st;
301
- //v_pickColor = czm_batchTable_pickColor(batchId);
302
- gl_Position = czm_modelViewProjection * vec4(position, 1.0);
303
- }
304
- `;
305
- return vertexShader;
306
- };
307
-
308
- //创建片源着色器
309
- const createFragmentShader = function () {
310
- var fragmentShader = `
311
- varying vec3 v_positionEC;
312
- varying vec3 v_normalEC;
313
- varying vec2 v_st;
314
- uniform vec4 color;
315
- varying vec4 v_pickColor;
316
- uniform sampler2D myImage;
317
- void main()
318
- {
319
- vec3 positionToEyeEC = -v_positionEC;
320
- vec3 normalEC = normalize(v_normalEC);
321
- #ifdef FACE_FORWARD
322
- normalEC = faceforward(normalEC, vec3(0.0, 0.0, 1.0), -normalEC);
323
- #endif
324
- czm_materialInput materialInput;
325
- materialInput.normalEC = normalEC;
326
- materialInput.positionToEyeEC = positionToEyeEC;
327
- materialInput.st = v_st;
328
- vec2 st = materialInput.st;
329
- czm_material material = czm_getDefaultMaterial(materialInput);
330
- float dt_a11 = fract(czm_frameNumber / 100.0) * 3.14159265 * 2.0;
331
- float dt_a12 = sin(dt_a11);
332
- float vst=smoothstep(0.7, 1.0, dt_a12)+0.4;
333
- vec4 colorImage = texture2D(myImage, vec2(fract(st.s- czm_frameNumber*0.003), st.t));
334
- material.alpha =mix(0.1,1.0,clamp((1.0-st.t) * color.a,0.0,1.0)) +(1.0-sign(st.t-czm_frameNumber*0.001))*0.2*(1.0-colorImage.r)+0.4 ;
335
- material.diffuse =(1.0-colorImage.a)*vec3(1.0,2.0,1.0)+colorImage.rgb*vec3(1.0,2.0,1.0);
336
- #ifdef FLAT
337
- gl_FragColor = vec4(material.diffuse + material.emission, material.alpha);
338
- #else
339
- gl_FragColor = czm_phong(normalize(positionToEyeEC), material, czm_lightDirectionEC);
340
- #endif
341
- }
342
- `;
343
- return fragmentShader;
344
- };
345
-
346
- //创建纹理
347
- const createTexture = function (tetrahedronPrimitive) {
348
- Resource.createIfNeeded(tetrahedronPrimitive._imageUrl).fetchImage().then(function (image) {
349
- var vTexture;
350
- var context = tetrahedronPrimitive._viewer.scene.context;
351
- if (defined(image.internalFormat)) {
352
- vTexture = new Texture({
353
- context: context,
354
- pixelFormat: image.internalFormat,
355
- width: image.naturalWidth,
356
- height: image.naturalHeight,
357
- source: {
358
- arrayBufferView: image.bufferView
359
- }
360
- });
361
- } else {
362
- vTexture = new Texture({
363
- context: context,
364
- source: image
365
- });
366
- }
367
- tetrahedronPrimitive._texture = vTexture;
368
- });
369
- };
370
-
371
- //计算矩阵
372
- const computeModelMatrix = function (tetrahedronPrimitive) {
373
- let enuMatrix = Transforms.eastNorthUpToFixedFrame(tetrahedronPrimitive._localPosition);
374
- let scaleMatrix = Matrix4.fromScale(tetrahedronPrimitive._scale);
375
- let modelMatrix = Matrix4.multiply(enuMatrix, scaleMatrix, new Matrix4());
376
- tetrahedronPrimitive._scaleMatrix = scaleMatrix;
377
- tetrahedronPrimitive._enuMatrix = enuMatrix;
378
- return modelMatrix;
379
- };
380
-
381
- //计算高度
382
- const computeHeight = function (tetrahedronPrimitive) {
383
- let point = Cartesian3.fromElements(0, 0, tetrahedronPrimitive._distance, new Cesium.Cartesian3());
384
- let enuPoint = Cesium.Matrix4.multiplyByPoint(tetrahedronPrimitive._enuMatrix, point, new Cartesian3());
385
- let upPositionEC = Matrix4.multiplyByPoint(tetrahedronPrimitive._viewer.scene.camera._viewMatrix, enuPoint, new Cartesian3());
386
- let upPositionPC = Matrix4.multiplyByPoint(tetrahedronPrimitive._viewer.scene.camera.frustum.projectionMatrix, upPositionEC, new Cartesian3());
387
- return Cartesian3.normalize(upPositionPC, new Cartesian3()).z;
388
- };
389
- Cesium.Scene.TetrahedronPrimitive = TetrahedronPrimitive;
390
- } catch (error) {
391
- console.log(error);
392
- }
393
- },
394
- _installXyzAxisPrimitive: function () {
395
- function XyzAxisPrimitive(option) {
396
- this._viewer = option.viewer;
397
- this._model = option.model;
398
- this._matrix = option.matrix;
399
- this._radius = undefined;
400
- this._layer = new Cesium.CustomDataSource('xyz-axis');
401
- this._viewer.dataSources.add(this._layer);
402
- this._handler = new Cesium.ScreenSpaceEventHandler(this._viewer.scene.canvas);
403
- this._xyzState = false;
404
- this._xyzPid = undefined;
405
- this._build();
406
- }
407
- XyzAxisPrimitive.prototype = {
408
- _build() {
409
- this._createAxisXYZ();
410
- this._bindHandler();
411
- },
412
- remove() {
413
- this._layer._primitives.removeAll();
414
- this._handler.distory();
415
- },
416
- _createAxisXYZ: function () {
417
- this._model.readyPromise.then(m => {
418
- const center1 = Cesium.Matrix4.getTranslation(m.modelMatrix, new Cesium.Cartesian3());
419
- const boundingShpere = m.boundingSphere;
420
- const radius = boundingShpere.radius;
421
- const axisZ = new Cesium.ArrowPolylinePrimitive({
422
- id: 'axisZ',
423
- color: Cesium.Color.RED,
424
- position: center1,
425
- width: 3,
426
- headWidth: 5,
427
- length: radius * 2 + 50,
428
- headLength: 10
429
- });
430
- const axisX = new Cesium.ArrowPolylinePrimitive({
431
- id: 'axisX',
432
- color: Cesium.Color.GREEN,
433
- position: center1,
434
- width: 3,
435
- headWidth: 5,
436
- length: radius * 2 + 50,
437
- headLength: 10
438
- });
439
- const axisY = new Cesium.ArrowPolylinePrimitive({
440
- id: 'axisY',
441
- color: Cesium.Color.BLUE,
442
- position: center1,
443
- width: 3,
444
- headWidth: 5,
445
- length: radius * 2 + 50,
446
- headLength: 10
447
- });
448
- const mx = Cesium.Matrix3.fromRotationY(Cesium.Math.toRadians(90));
449
- this._rotationX = Cesium.Matrix4.fromRotationTranslation(mx);
450
- Cesium.Matrix4.multiply(axisX.geometryInstances[0].modelMatrix, this._rotationX, axisX.geometryInstances[0].modelMatrix);
451
- Cesium.Matrix4.multiply(axisX.geometryInstances[1].modelMatrix, this._rotationX, axisX.geometryInstances[1].modelMatrix);
452
- const my = Cesium.Matrix3.fromRotationX(Cesium.Math.toRadians(90));
453
- this._rotationY = Cesium.Matrix4.fromRotationTranslation(my);
454
- Cesium.Matrix4.multiply(axisY.geometryInstances[0].modelMatrix, this._rotationY, axisY.geometryInstances[0].modelMatrix);
455
- Cesium.Matrix4.multiply(axisY.geometryInstances[1].modelMatrix, this._rotationY, axisY.geometryInstances[1].modelMatrix);
456
- this._layer._primitives.add(axisZ);
457
- this._layer._primitives.add(axisX);
458
- this._layer._primitives.add(axisY);
459
- this._radius = boundingShpere.radius;
460
- this._createAxisSphere();
461
- });
462
- },
463
- _createAxisSphere: function () {
464
- const position = [];
465
- for (let i = 0; i <= 360; i += 3) {
466
- const sin = Math.sin(Cesium.Math.toRadians(i));
467
- const cos = Math.cos(Cesium.Math.toRadians(i));
468
- const x = this._radius * cos;
469
- const y = this._radius * sin;
470
- position.push(new Cesium.Cartesian3(x, y, 0));
471
- }
472
- const axisSphereZ = this._createAxisSpheres('axisSphereZ', position, this._matrix, Cesium.Color.RED);
473
- this._layer._primitives.add(axisSphereZ);
474
- const axisSphereY = this._createAxisSpheres('axisSphereY', position, this._matrix, Cesium.Color.GREEN);
475
- this._layer._primitives.add(axisSphereY);
476
- Cesium.Matrix4.multiply(axisSphereY.geometryInstances.modelMatrix, this._rotationY, axisSphereY.geometryInstances.modelMatrix);
477
- const axisSphereX = this._createAxisSpheres('axisSphereX', position, this._matrix, Cesium.Color.BLUE);
478
- this._layer._primitives.add(axisSphereX);
479
- Cesium.Matrix4.multiply(axisSphereX.geometryInstances.modelMatrix, this._rotationX, axisSphereX.geometryInstances.modelMatrix);
480
- },
481
- _createAxisSpheres: function (id, position, matrix, color) {
482
- const geometry = new Cesium.PolylineGeometry({
483
- positions: position,
484
- width: 10
485
- });
486
- const instnce = new Cesium.GeometryInstance({
487
- geometry: geometry,
488
- id: id,
489
- attributes: {
490
- color: Cesium.ColorGeometryInstanceAttribute.fromColor(color)
491
- }
492
- });
493
- return new Cesium.Primitive({
494
- geometryInstances: instnce,
495
- appearance: new Cesium.PolylineColorAppearance({
496
- translucent: false
497
- }),
498
- modelMatrix: matrix
499
- });
500
- },
501
- _updateAxis(cartesian) {
502
- if (this._layer) {
503
- let primitives = this._layer._primitives._primitives;
504
- for (let i = 1, j = primitives.length; i < j; i++) {
505
- let primitive = primitives[i];
506
- const translation = Cesium.Matrix4.fromTranslation(new Cesium.Cartesian3(cartesian.x, cartesian.y, cartesian.z));
507
- Cesium.Matrix4.multiply(primitive.modelMatrix, translation, primitive.modelMatrix);
508
- }
509
- }
510
- const translation = Cesium.Matrix4.fromTranslation(new Cesium.Cartesian3(cartesian.x, cartesian.y, cartesian.z));
511
- Cesium.Matrix4.multiply(this._model.modelMatrix, translation, this._model.modelMatrix);
512
- },
513
- _updateAxisSphere(angel) {
514
- if (this._layer) {
515
- let primitives = this._layer._primitives._primitives;
516
- for (let i = 1, j = primitives.length; i < j; i++) {
517
- let primitive = primitives[i];
518
- const rotation = Cesium.Matrix4.fromRotationTranslation(angel);
519
- Cesium.Matrix4.multiply(primitive.modelMatrix, rotation, primitive.modelMatrix);
520
- }
521
- }
522
- const rotation = Cesium.Matrix4.fromRotationTranslation(angel);
523
- Cesium.Matrix4.multiply(this._model.modelMatrix, rotation, this._model.modelMatrix);
524
- },
525
- _bindHandler() {
526
- //拖动
527
- this._handler.setInputAction(click => {
528
- if (this._xyzState && this._xyzPid) {
529
- switch (this._xyzPid) {
530
- case 'axisX-line':
531
- this._updateAxis({
532
- x: -1,
533
- y: 0,
534
- z: 0
535
- });
536
- break;
537
- case 'axisY-line':
538
- this._updateAxis({
539
- x: 0,
540
- y: -1,
541
- z: 0
542
- });
543
- break;
544
- case 'axisZ-line':
545
- this._updateAxis({
546
- x: 0,
547
- y: 0,
548
- z: -1
549
- });
550
- break;
551
- case 'axisX-arrow':
552
- this._updateAxis({
553
- x: 1,
554
- y: 0,
555
- z: 0
556
- });
557
- break;
558
- case 'axisY-arrow':
559
- this._updateAxis({
560
- x: 0,
561
- y: 1,
562
- z: 0
563
- });
564
- break;
565
- case 'axisZ-arrow':
566
- this._updateAxis({
567
- x: 0,
568
- y: 0,
569
- z: 1
570
- });
571
- break;
572
- case 'axisSphereX':
573
- this._updateAxisSphere(Cesium.Matrix3.fromRotationX(Cesium.Math.toRadians(1)));
574
- break;
575
- case 'axisSphereY':
576
- this._updateAxisSphere(Cesium.Matrix3.fromRotationY(Cesium.Math.toRadians(1)));
577
- break;
578
- case 'axisSphereZ':
579
- this._updateAxisSphere(Cesium.Matrix3.fromRotationZ(Cesium.Math.toRadians(1)));
580
- break;
581
- }
582
- }
583
- }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
584
-
585
- //拾取
586
- this._handler.setInputAction(click => {
587
- let pickObj = viewer.scene.pick(click.position);
588
- if (pickObj && pickObj.id) {
589
- this._xyzPid = pickObj.id, this._xyzState = true;
590
- }
591
- }, Cesium.ScreenSpaceEventType.LEFT_DOWN);
592
-
593
- //结束
594
- this._handler.setInputAction(() => {
595
- this._xyzState = false, this._xyzPid = undefined;
596
- }, Cesium.ScreenSpaceEventType.LEFT_UP);
597
- }
598
- };
599
- Cesium.Scene.XyzAxisPrimitive = XyzAxisPrimitive;
600
- },
601
- /**
602
- * 箭头线
603
- */
604
- _installArrowPolylinePrimitive: function () {
605
- function ArrowPolylinePrimitive(option = {}) {
606
- this._color = option.color || Cesium.Color.RED;
607
- this._width = option.width || 3;
608
- this._headWidth = option.headWidth || 2 * this._width;
609
- this._length = option.length || 300;
610
- this._headLength = option.headLength || 10;
611
- this._inverse = option.inverse || false;
612
- this.position = option.position;
613
- const id = option.id;
614
- //这里用的是圆锥几何对象,当topRadius和bottomRadius相同时,它就是一个圆柱
615
- const line = Cesium.CylinderGeometry.createGeometry(new Cesium.CylinderGeometry({
616
- length: this._length,
617
- topRadius: this._width,
618
- bottomRadius: this._width
619
- }));
620
- const arrow = Cesium.CylinderGeometry.createGeometry(new Cesium.CylinderGeometry({
621
- length: this._headLength,
622
- topRadius: 0,
623
- bottomRadius: this._headWidth
624
- }));
625
- let offset = (this._length + this._headLength) / 2;
626
- if (this._inverse) {
627
- offset = -offset;
628
- }
629
- translate(arrow, [0, 0, offset]);
630
- return new Cesium.Primitive({
631
- modelMatrix: Cesium.Transforms.eastNorthUpToFixedFrame(this.position),
632
- geometryInstances: [new Cesium.GeometryInstance({
633
- id: id + '-line',
634
- geometry: line
635
- }), new Cesium.GeometryInstance({
636
- id: id + '-arrow',
637
- geometry: arrow
638
- })],
639
- appearance: new Cesium.MaterialAppearance({
640
- material: Cesium.Material.fromType('Color', {
641
- color: this._color
642
- })
643
- })
644
- });
645
- }
646
-
647
- /**
648
- * 按上面的方法画出的箭头在线的中间,我们需要把它平移到线的一端
649
- */
650
- let translate = function (geometry, offset) {
651
- const scratchOffset = new Cesium.Cartesian3();
652
- if (offset.length) {
653
- scratchOffset.x = offset[0];
654
- scratchOffset.y = offset[1];
655
- scratchOffset.z = offset[2];
656
- } else {
657
- Cesium.Cartesian3.clone(offset, scratchOffset);
658
- }
659
- for (let i = 0; i < geometry.attributes.position.values.length; i += 3) {
660
- geometry.attributes.position.values[i] += scratchOffset.x;
661
- geometry.attributes.position.values[i + 1] += scratchOffset.y;
662
- geometry.attributes.position.values[i + 2] += scratchOffset.z;
663
- }
664
- };
665
- Cesium.Scene.ArrowPolylinePrimitive = ArrowPolylinePrimitive;
666
- },
667
- /**
668
- * 阴影图元
669
- */
670
- _installShadowPrimitive: function () {
671
- const ViewshedLineVS = 'ttribute vec3 position;\n\
672
- uniform mat4 u_modelViewMatrix;\n\
673
- void main()\n\
674
- {\n\
675
- gl_Position = czm_projection* u_modelViewMatrix* vec4(position.xyz,1.0);\n\
676
- }\n\
677
- ';
678
- const ViewshedLineFS = 'uniform vec4 u_bgColor;\n\
679
- void main()\n\
680
- {\n\
681
- gl_FragColor = u_bgColor;\n\
682
- }\n\
683
- ';
684
- function ShadowPrimitive(options) {
685
- options = Cesium.defaultValue(options, Cesium.defaultValue.EMPTY_OBJECT);
686
- var scene = options.scene;
687
- if (!Cesium.defined(scene)) {
688
- throw new Cesium.DeveloperError('scene is required.');
689
- }
690
- this._scene = scene;
691
- // options.context is an undocumented option
692
- var context = scene._context;
693
-
694
- // includeStart('debug', pragmas.debug);
695
- if (!Cesium.defined(context)) {
696
- throw new Cesium.DeveloperError('context is required.');
697
- }
698
- if (!Cesium.defined(options.viewerPosition)) {
699
- throw new Cesium.DeveloperError('this view position is required.');
700
- }
701
- this._viewerPosition = options.viewerPosition;
702
- this._positions = undefined;
703
- this._indices = undefined;
704
- this._drawLineCommand = undefined;
705
- this._depthCamera = new Cesium.Camera(scene);
706
- this._depthCamera.position = this._viewerPosition;
707
- this._direction = 0;
708
- this._pitch = 0;
709
- this._horizontalFov = Cesium.defaultValue(options.horizontalFov, 60);
710
- this._verticalFov = Cesium.defaultValue(options.verticalFov, 45);
711
- this._cameraUpdated = false;
712
- this._targetPoint = this._viewerPosition.clone();
713
- var t = new Cesium.Cartesian3(0, 0, 100);
714
- var matrix_ENU = Cesium.Transforms.eastNorthUpToFixedFrame(this._viewerPosition);
715
- //Cesium.Matrix4.inverse(matrix_ENU, matrix_ENU),
716
- Cesium.Matrix4.multiplyByPoint(matrix_ENU, t, this._targetPoint);
717
- this._distance = 100;
718
- this._cameraUpdated = false;
719
- this._modelMatrix = new Cesium.Matrix4(), this._shadowMap = new Cesium.ShadowMap({
720
- context: context,
721
- enabled: true,
722
- lightCamera: this._depthCamera,
723
- cascadesEnabled: false
724
- });
725
- //this._shadowMap.debugShow=true;
726
-
727
- this.show = true;
728
- this._invisiblyColor = Cesium.Color.RED;
729
- this._visiblyColor = Cesium.Color.GREEN;
730
- this._shadowMap.useCustomColor = true;
731
- this.shadowMap._customColor = {
732
- invisibly: Cesium.Color.RED,
733
- visibly: Cesium.Color.GREEN
734
- };
735
- this._initialize = function () {
736
- this._positions = new Float32Array(633), this._indices = new Uint16Array(408);
737
- var indices = this._indices,
738
- r = 0;
739
- indices[r++] = 0, indices[r++] = 1, indices[r++] = 0, indices[r++] = 21, indices[r++] = 0, indices[r++] = 85, indices[r++] = 0, indices[r++] = 105;
740
- for (var i = 0, n = 0; n < 5; ++n) {
741
- i++;
742
- for (var a = 0; a < 20; ++a) indices[r++] = i++, indices[r++] = i;
743
- }
744
- i++;
745
- for (var s = 0; s < 20; ++s) for (var l = 0; l < 5; ++l) indices[r++] = i, indices[r++] = 5 + i++;
746
- this._initialized = true;
747
- };
748
- //this._initialize();
749
-
750
- this._debugLightFrustum = undefined;
751
- this._debugShow = true;
752
- }
753
- Object.defineProperties(ShadowPrimitive.prototype, {
754
- shadowMap: {
755
- get: function () {
756
- return this._shadowMap;
757
- }
758
- },
759
- debugLightFrustum: {
760
- get: function () {
761
- return this._debugLightFrustum;
762
- }
763
- },
764
- debugShow: {
765
- get: function () {
766
- return this._debugShow;
767
- },
768
- set: function (bShow) {
769
- this.shadowMap.debugShow = bShow;
770
- this._debugShow = bShow;
771
- }
772
- },
773
- invisiblyColor: {
774
- get: function () {
775
- return this._invisiblyColor;
776
- },
777
- set: function (color) {
778
- this._invisiblyColor = color;
779
- var shadowMap = this.shadowMap;
780
- shadowMap._customColor.invisibly = color;
781
- }
782
- },
783
- visiblyColor: {
784
- get: function () {
785
- return this._visiblyColor;
786
- },
787
- set: function (color) {
788
- this._visiblyColor = color;
789
- var shadowMap = this.shadowMap;
790
- shadowMap._customColor.visibly = color;
791
- }
792
- },
793
- viewerPosition: {
794
- get: function () {
795
- return this._viewerPosition;
796
- },
797
- set: function (position) {
798
- this._viewerPosition = position, this._cameraUpdated = !1;
799
- }
800
- },
801
- direction: {
802
- get: function () {
803
- return this._direction;
804
- },
805
- set: function (direction) {
806
- this._direction = direction, this._cameraUpdated = !1;
807
- }
808
- },
809
- pitch: {
810
- get: function () {
811
- return this._pitch;
812
- },
813
- set: function (pitch) {
814
- this._pitch = pitch, this._cameraUpdated = !1;
815
- }
816
- },
817
- horizontalFov: {
818
- get: function () {
819
- return this._horizontalFov;
820
- },
821
- set: function (e) {
822
- this._horizontalFov = e, this._cameraUpdated = !1, this._hintLineUpdated = !1;
823
- }
824
- },
825
- verticalFov: {
826
- get: function () {
827
- return this._verticalFov;
828
- },
829
- set: function (e) {
830
- this._verticalFov = e, this._cameraUpdated = !1, this._hintLineUpdated = !1;
831
- }
832
- },
833
- distance: {
834
- get: function () {
835
- return this._distance;
836
- },
837
- set: function (distance) {
838
- this._distance = distance, this._cameraUpdated = !1;
839
- }
840
- }
841
- });
842
- ShadowPrimitive.prototype.setDebugFrustumEffect = function (bShowPlane, bShowOutline) {
843
- if (!this.debugShow) return;
844
- var planesPrimitives = this.debugLightFrustum._planesPrimitives;
845
- var outlinePrimitives = this.debugLightFrustum._outlinePrimitives;
846
- planesPrimitives.forEach(function (plane) {
847
- plane.show = bShowPlane;
848
- });
849
- outlinePrimitives.forEach(function (outline) {
850
- outline.show = bShowOutline;
851
- });
852
- };
853
- ShadowPrimitive.prototype.update = function (frameState) {
854
- if (!this.show) return;
855
- if (!this._cameraUpdated) {
856
- this._updateCamera();
857
- //更新debug
858
- if (this.debugShow) {
859
- if (!Cesium.defined(this.debugLightFrustum)) {
860
- this._debugLightFrustum = new Cesium.DebugCameraPrimitive({
861
- camera: this._depthCamera,
862
- color: Cesium.Color.YELLOW,
863
- updateOnChange: true
864
- });
865
- this.setDebugFrustumEffect(false, true);
866
- }
867
- }
868
- }
869
- var frustumSplitsFar = frameState.frustumSplits[1];
870
- frameState.frustumSplits[1] = this._distance;
871
- this._debugLightFrustum.update(frameState);
872
- frameState.frustumSplits[1] = frustumSplitsFar;
873
- //if(!this._initialized)this._initialize();
874
- //if (!this._hintLineUpdated)this._updateHintLine(frameState);
875
- frameState.shadowMaps.push(this.shadowMap);
876
- };
877
- ShadowPrimitive.prototype._updateCamera = function () {
878
- this._depthCamera.frustum.near = .001 * this._distance, this._depthCamera.frustum.far = this._distance, this._depthCamera.frustum.fov = Cesium.Math.toRadians(Math.max(this._horizontalFov, this._verticalFov)), this._depthCamera.frustum.aspectRatio = this._horizontalFov / this._verticalFov, this._depthCamera.setView({
879
- destination: this._viewerPosition,
880
- orientation: {
881
- heading: Cesium.Math.toRadians(this._direction),
882
- pitch: Cesium.Math.toRadians(this._pitch)
883
- }
884
- }), this._modelMatrix = this._depthCamera.inverseViewMatrix;
885
- this._cameraUpdated = !0;
886
- };
887
- ShadowPrimitive.prototype.setPoseByTargetPoint = function (point) {
888
- this.distance = Cesium.Cartesian3.distance(this._viewerPosition, point);
889
- var t = new Cesium.Cartesian3(),
890
- matrix_ENU = Cesium.Transforms.eastNorthUpToFixedFrame(this._viewerPosition);
891
- Cesium.Matrix4.inverse(matrix_ENU, matrix_ENU), Cesium.Matrix4.multiplyByPoint(matrix_ENU, point, t), Cesium.Cartesian3.normalize(t, t), this.direction = Cesium.Math.toDegrees(Math.atan2(t.x, t.y)), this.pitch = Cesium.Math.toDegrees(Math.asin(t.z));
892
- };
893
- ShadowPrimitive.prototype._updateHintLine = function (frameState) {
894
- var i,
895
- a,
896
- s,
897
- d,
898
- p = this._positions,
899
- m = Cesium.Math.toRadians(this._horizontalFov),
900
- v = Cesium.Math.toRadians(this._verticalFov),
901
- b = Math.tan(.5 * m),
902
- S = Math.tan(.5 * v);
903
- a = this._distance * b, d = this._distance * S, i = -a, s = -d;
904
- var w = new Cesium.Cartesian3(i, s, -this._distance),
905
- x = new Cesium.Cartesian3(a, d, 0);
906
- Cesium.Matrix4.multiplyByPoint(this._modelMatrix, w, w), Cesium.Matrix4.multiplyByPoint(this._modelMatrix, x, x);
907
- var boundingSphere = Cesium.BoundingSphere.fromCornerPoints(w, x);
908
- if (frameState.cullingVolume.computeVisibility(boundingSphere) === Intersect.OUTSIDE) return void (this._valid = !1);
909
- this._valid = !0;
910
- var P = 0;
911
- p[P++] = 0, p[P++] = 0, p[P++] = 0;
912
- for (var D, I, M = Math.PI - .5 * m, R = m / 4, L = 0; L < 5; ++L) {
913
- D = M + L * R;
914
- for (var B = d / (this._distance / Math.cos(D)), F = Math.atan(B), U = -F, V = F / 10, z = 0; z < 21; ++z) I = U + z * V, p[P++] = this._distance * Math.cos(I) * Math.sin(D), p[P++] = this._distance * Math.sin(I), p[P++] = this._distance * Math.cos(I) * Math.cos(D);
915
- }
916
- R = m / 20;
917
- for (var G = 0; G < 21; ++G) {
918
- D = M + G * R;
919
- for (var B = d / (this._distance / Math.cos(D)), F = Math.atan(B), U = -F, V = F / 2, H = 0; H < 5; ++H) I = U + H * V, p[P++] = this._distance * Math.cos(I) * Math.sin(D), p[P++] = this._distance * Math.sin(I), p[P++] = this._distance * Math.cos(I) * Math.cos(D);
920
- }
921
- var context = frameState.context,
922
- indexBuffer = Cesium.Buffer.createIndexBuffer({
923
- context: context,
924
- typedArray: new Uint32Array(this._indices),
925
- usage: Cesium.BufferUsage.STATIC_DRAW,
926
- indexDatatype: IndexDatatype.UNSIGNED_INT
927
- }),
928
- vertexBuffer = Cesium.Buffer.createVertexBuffer({
929
- context: context,
930
- typedArray: Cesium.ComponentDatatype.createTypedArray(Cesium.ComponentDatatype.FLOAT, this._positions),
931
- usage: Cesium.BufferUsage.STATIC_DRAW
932
- }),
933
- attributes = [];
934
- attributes.push({
935
- index: 0,
936
- vertexBuffer: vertexBuffer,
937
- componentDatatype: Cesium.ComponentDatatype.FLOAT,
938
- componentsPerAttribute: 3,
939
- normalize: !1
940
- });
941
- var vertexArray = new Cesium.VertexArray({
942
- context: context,
943
- attributes: attributes,
944
- indexBuffer: indexBuffer
945
- });
946
- if (Cesium.defined(this._drawLineCommand)) this._drawLineCommand.vertexArray.destroy(), this._drawLineCommand.vertexArray = vertexArray, this._drawLineCommand.modelMatrix = this._modelMatrix, this._drawLineCommand.boundingVolume = boundingSphere;else {
947
- var shaderProgram = Cesium.ShaderProgram.fromCache({
948
- context: context,
949
- vertexShaderSource: ViewshedLineVS,
950
- fragmentShaderSource: ViewshedLineFS
951
- }),
952
- renderState = Cesium.RenderState.fromCache({
953
- depthTest: {
954
- enabled: !0
955
- }
956
- }),
957
- _this = this,
958
- uniformMap = {
959
- u_bgColor: function () {
960
- return _this._lineColor;
961
- },
962
- u_modelViewMatrix: function () {
963
- return context.uniformState.modelView;
964
- }
965
- };
966
- this._drawLineCommand = new Cesium.DrawCommand({
967
- boundingVolume: boundingSphere,
968
- modelMatrix: _this._modelMatrix,
969
- primitiveType: Cesium.PrimitiveType.LINES,
970
- vertexArray: vertexArray,
971
- shaderProgram: shaderProgram,
972
- castShadows: !1,
973
- receiveShadows: !1,
974
- uniformMap: uniformMap,
975
- renderState: renderState,
976
- pass: Cesium.Pass.OPAQUE
977
- });
978
- }
979
- this._hintLineUpdated = true;
980
- };
981
- Cesium.Scene.ShadowPrimitive = ShadowPrimitive;
982
- },
983
- /**
984
- * 图元点
985
- */
986
- _installPointsPrimitive: function () {
987
- /**
988
- *
989
- * @param {*} options
990
- */
991
- function PointsPrimitive(options) {
992
- if (options && options.viewer && options.Cartesians) {
993
- this._vertexShader = this.getVSPolylie();
994
- this._fragmentShader = this.getFSPolyline();
995
- this._geometry = null;
996
- this._appearance = null;
997
- this._viewer = options.viewer;
998
- this.build(options);
999
- }
1000
- }
1001
- PointsPrimitive.prototype = {
1002
- build: function (options) {
1003
- if (options.Cartesians && options.Cartesians.length >= 2) {
1004
- var postionsTemp = [];
1005
- var colorsTemp = [];
1006
- var indicesTesm = [];
1007
- if (options.Colors && options.Colors.length === options.Cartesians.length * 4) {
1008
- for (var i = 0; i < options.Cartesians.length; i++) {
1009
- postionsTemp.push(options.Cartesians[i].x);
1010
- postionsTemp.push(options.Cartesians[i].y);
1011
- postionsTemp.push(options.Cartesians[i].z);
1012
- }
1013
- colorsTemp = options.Colors;
1014
- } else {
1015
- for (var j = 0; j < options.Cartesians.length; j++) {
1016
- postionsTemp.push(options.Cartesians[j].x);
1017
- postionsTemp.push(options.Cartesians[j].y);
1018
- postionsTemp.push(options.Cartesians[j].z);
1019
- //
1020
- colorsTemp.push(0.0);
1021
- colorsTemp.push(0.0);
1022
- colorsTemp.push(1.0);
1023
- colorsTemp.push(1.0);
1024
- }
1025
- }
1026
- for (var e = 0; e < options.Cartesians.length; e++) {
1027
- indicesTesm.push(e);
1028
- }
1029
- this.positionArr = new Float64Array(postionsTemp);
1030
- this.colorArr = new Float32Array(colorsTemp);
1031
- this.indiceArr = new Uint16Array(indicesTesm);
1032
- } else {
1033
- var p1 = Cesium.Cartesian3.fromDegrees(0, 0, -10);
1034
- var p2 = Cesium.Cartesian3.fromDegrees(0, 0.001, -10);
1035
- this.positionArr = new Float64Array([p1.x, p1.y, p1.z, p2.x, p2.y, p2.z]);
1036
- //默认蓝色
1037
- this.colorArr = new Float32Array([0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0]);
1038
- this.indiceArr = new Uint16Array([0, 1]);
1039
- }
1040
- this._geometry = this.createGeometry(this.positionArr, this.colorArr, this.indiceArr);
1041
- this._appearance = this.createAppearence(this._fragmentShader, this._vertexShader);
1042
- this.primitive = this._viewer.scene.primitives.add(new Cesium.Primitive({
1043
- geometryInstances: new Cesium.GeometryInstance({
1044
- geometry: this._geometry
1045
- }),
1046
- appearance: this._appearance,
1047
- asynchronous: false
1048
- }));
1049
- },
1050
- getVSPolylie: function () {
1051
- return 'attribute vec3 position3DHigh;\
1052
- attribute vec3 position3DLow;\
1053
- attribute vec4 color;\
1054
- varying vec4 v_color;\
1055
- attribute float batchId;\
1056
- void main()\
1057
- {\
1058
- vec4 p = czm_computePosition();\
1059
- v_color =color;\
1060
- p = czm_modelViewProjectionRelativeToEye * p;\
1061
- gl_Position = p;\
1062
- gl_PointSize=4.0;\
1063
- }\
1064
- ';
1065
- },
1066
- getFSPolyline: function () {
1067
- return 'varying vec4 v_color;\
1068
- void main()\
1069
- {\
1070
- float d = distance(gl_PointCoord, vec2(0.5,0.5));\
1071
- if(d < 0.5){\
1072
- gl_FragColor = v_color;\
1073
- }else{\
1074
- discard;\
1075
- }\
1076
- }\
1077
- ';
1078
- },
1079
- createAppearence: function (fs, vs) {
1080
- return new Cesium.Appearance({
1081
- renderState: {
1082
- blending: Cesium.BlendingState.PRE_MULTIPLIED_ALPHA_BLEND,
1083
- depthTest: {
1084
- enabled: true
1085
- },
1086
- depthMask: true
1087
- },
1088
- fragmentShaderSource: fs,
1089
- vertexShaderSource: vs
1090
- });
1091
- },
1092
- createGeometry: function (positions, colors, indices) {
1093
- return new Cesium.Geometry({
1094
- attributes: {
1095
- position: new Cesium.GeometryAttribute({
1096
- componentDatatype: Cesium.ComponentDatatype.DOUBLE,
1097
- componentsPerAttribute: 3,
1098
- values: positions
1099
- }),
1100
- color: new Cesium.GeometryAttribute({
1101
- componentDatatype: Cesium.ComponentDatatype.FLOAT,
1102
- componentsPerAttribute: 4,
1103
- values: colors
1104
- })
1105
- },
1106
- indices: indices,
1107
- primitiveType: Cesium.PrimitiveType.POINTS,
1108
- boundingSphere: Cesium.BoundingSphere.fromVertices(positions)
1109
- });
1110
- },
1111
- remove: function () {
1112
- if (this.primitive != null) {
1113
- this._viewer.scene.primitives.remove(this.primitive);
1114
- this.primitive = null;
1115
- }
1116
- },
1117
- updateCartesianPosition: function (cartesians) {
1118
- if (this.primitive != null) {
1119
- this._viewer.scene.primitives.remove(this.primitive);
1120
- if (cartesians && cartesians.length < 2) {
1121
- return;
1122
- }
1123
- if (cartesians.length === this.positionArr.length / 3) {
1124
- var p1 = cartesians[0];
1125
- var p2 = cartesians[1];
1126
- this.positionArr = new Float64Array([p1.x, p1.y, p1.z, p2.x, p2.y, p2.z]);
1127
- this._geometry = this.createGeometry(this.positionArr, this.colorArr, this.indiceArr);
1128
- } else {
1129
- //默认蓝色
1130
- var postionsTemp = [];
1131
- var colorsTemp = [];
1132
- var indicesTesm = [];
1133
- for (var i = 0; i < cartesians.length; i++) {
1134
- postionsTemp.push(cartesians[i].x);
1135
- postionsTemp.push(cartesians[i].y);
1136
- postionsTemp.push(cartesians[i].z);
1137
- colorsTemp.push(0.0);
1138
- colorsTemp.push(0.0);
1139
- colorsTemp.push(1.0);
1140
- colorsTemp.push(1.0);
1141
- }
1142
- for (var j = 0; j < cartesians.length; j++) {
1143
- indicesTesm.push(j);
1144
- }
1145
- this.positionArr = new Float64Array(postionsTemp);
1146
- this.colorArr = new Float32Array(colorsTemp);
1147
- this.indiceArr = new Uint16Array(indicesTesm);
1148
- geometry = this.createGeometry(this.positionArr, this.colorArr, this.indiceArr);
1149
- appearance = this.createAppearence(this._fragmentShader, this._vertexShader);
1150
- }
1151
- this.primitive = this._viewer.scene.primitives.add(new Cesium.Primitive({
1152
- geometryInstances: new Cesium.GeometryInstance({
1153
- geometry: this._geometry
1154
- }),
1155
- appearance: this._appearance,
1156
- asynchronous: false
1157
- }));
1158
- } else {
1159
- return;
1160
- }
1161
- },
1162
- updateCartesianPositionColor: function (cartesians, colors) {
1163
- if (colors.length !== cartesians.length * 4) {
1164
- return;
1165
- }
1166
- if (this.primitive != null) {
1167
- viewer.scene.primitives.remove(this.primitive);
1168
- if (cartesians && cartesians.length < 2) {
1169
- return;
1170
- }
1171
- if (cartesians.length === this.positionArr.length / 3) {
1172
- var p1 = cartesians[0];
1173
- var p2 = cartesians[1];
1174
- this.positionArr = new Float64Array([p1.x, p1.y, p1.z, p2.x, p2.y, p2.z]);
1175
- this.colorArr = new Float32Array(colors);
1176
- geometry = CreateGeometry(this.positionArr, this.colorArr, this.indiceArr);
1177
- } else {
1178
- var postionsTemp = [];
1179
- var indicesTesm = [];
1180
- for (var i = 0; i < cartesians.length; i++) {
1181
- postionsTemp.push(cartesians[i].x);
1182
- postionsTemp.push(cartesians[i].y);
1183
- postionsTemp.push(cartesians[i].z);
1184
- }
1185
- for (var i = 0; i < cartesians.length; i++) {
1186
- indicesTesm.push(i);
1187
- }
1188
- this.positionArr = new Float64Array(postionsTemp);
1189
- this.colorArr = new Float32Array(colors);
1190
- this.indiceArr = new Uint16Array(indicesTesm);
1191
- this._geometry = this.createGeometry(this.positionArr, this.colorArr, this.indiceArr);
1192
- this._appearance = this.createAppearence(this._fragmentShader, this._vertexShader);
1193
- }
1194
- this.primitive = viewer.scene.primitives.add(new Cesium.Primitive({
1195
- geometryInstances: new Cesium.GeometryInstance({
1196
- geometry: this._geometry
1197
- }),
1198
- appearance: this._appearance,
1199
- asynchronous: false
1200
- }));
1201
- } else {
1202
- return;
1203
- }
1204
- }
1205
- };
1206
- Cesium.Scene.PointsPrimitive = PointsPrimitive;
1207
- },
1208
- /**
1209
- * 水面效果
1210
- */
1211
- _installWaterPrimitive: function () {
1212
- /**
1213
- *
1214
- * @param {*} options
1215
- */
1216
- const WaterPrimitive = function (opt) {
1217
- this._positions = opt.positions;
1218
- this._url = opt.normalMapUrl || 'static/data/images/Textures/waterNormals.jpg';
1219
- this._frequency = opt.frequency || 1000.0;
1220
- this._animationSpeed = opt.animationSpeed || 0.01;
1221
- this._amplitude = opt.amplitude || 10.0;
1222
- this._extrudedHeight = opt.extrudedHeight || 0;
1223
- this._fs = this.getFS();
1224
- };
1225
- WaterPrimitive.prototype.build = function () {
1226
- this._geometry = this._createGeometry();
1227
- this._appearance = this._createAppearence();
1228
- this.primitive = this._viewer.scene.primitives.add(new Cesium.Primitive({
1229
- allowPicking: false,
1230
- geometryInstances: new Cesium.GeometryInstance({
1231
- geometry: this._geometry
1232
- }),
1233
- appearance: this._appearance,
1234
- asynchronous: false
1235
- }));
1236
- };
1237
- WaterPrimitive.prototype._createAppearence = function () {
1238
- return new Cesium.EllipsoidSurfaceAppearance({
1239
- material: new Cesium.Material({
1240
- fabric: {
1241
- type: 'Water',
1242
- uniforms: {
1243
- normalMap: this._url,
1244
- frequency: this._frequency,
1245
- animationSpeed: this._animationSpeed,
1246
- amplitude: this._amplitude
1247
- }
1248
- }
1249
- }),
1250
- fragmentShaderSource: this._fs
1251
- });
1252
- };
1253
- WaterPrimitive.prototype._createGeometry = function () {
1254
- return new Cesium.PolygonGeometry({
1255
- polygonHierarchy: new Cesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArrayHeights(this._positions)),
1256
- extrudedHeight: this._extrudedHeight,
1257
- perPositionHeight: true
1258
- });
1259
- };
1260
- WaterPrimitive.prototype.getFS = function () {
1261
- return 'varying vec3 v_positionMC;\n\
1262
- varying vec3 v_positionEC;\n\
1263
- varying vec2 v_st;\n\
1264
- \n\
1265
- void main()\n\
1266
- {\n\
1267
- czm_materialInput materialInput;\n\
1268
- vec3 normalEC = normalize(czm_normal3D * czm_geodeticSurfaceNormal(v_positionMC, vec3(0.0), vec3(1.0)));\n\
1269
- #ifdef FACE_FORWARD\n\
1270
- normalEC = faceforward(normalEC, vec3(0.0, 0.0, 1.0), -normalEC);\n\
1271
- #endif\n\
1272
- materialInput.s = v_st.s;\n\
1273
- materialInput.st = v_st;\n\
1274
- materialInput.str = vec3(v_st, 0.0);\n\
1275
- materialInput.normalEC = normalEC;\n\
1276
- materialInput.tangentToEyeMatrix = czm_eastNorthUpToEyeCoordinates(v_positionMC, materialInput.normalEC);\n\
1277
- vec3 positionToEyeEC = -v_positionEC;\n\
1278
- materialInput.positionToEyeEC = positionToEyeEC;\n\
1279
- czm_material material = czm_getMaterial(materialInput);\n\
1280
- #ifdef FLAT\n\
1281
- gl_FragColor = vec4(material.diffuse + material.emission, material.alpha);\n\
1282
- #else\n\
1283
- gl_FragColor = czm_phong(normalize(positionToEyeEC), material);\n\
1284
- gl_FragColor.a = 0.5;\n\
1285
- #endif\n\
1286
- }\n\
1287
- ';
1288
- };
1289
- Primitive.prototype.updateDegreesPosition = function (_degreesArrayHeights, _extrudedHeight) {
1290
- if (this.primitive != null) {
1291
- this._viewer.scene.primitives.remove(this.primitive);
1292
- if (_degreesArrayHeights && _degreesArrayHeights.length < 3) {
1293
- return;
1294
- }
1295
- var geometry = this._createGeometry(_degreesArrayHeights, _extrudedHeight ? _extrudedHeight : 0);
1296
- this.primitive = this._viewer.scene.primitives.add(new Cesium.Primitive({
1297
- allowPicking: false,
1298
- geometryInstances: new Cesium.GeometryInstance({
1299
- geometry: geometry
1300
- }),
1301
- appearance: this._appearance,
1302
- asynchronous: false
1303
- }));
1304
- } else {
1305
- return;
1306
- }
1307
- };
1308
- Cesium.Scene.WaterPrimitive = WaterPrimitive;
1309
- },
1310
- /**
1311
- * 纹理图 视频图像
1312
- */
1313
- _installTexturePrimitive: function () {
1314
- /**
1315
- *
1316
- * @param {*} options
1317
- */
1318
- function TexturePrimitive(options) {
1319
- this._vertexShader = this.getVS();
1320
- this._fragmentShader = this.getFS();
1321
- this._materialShader = this.getMS();
1322
- this._url = options.url;
1323
- this._cartesians = options.cartesians;
1324
- this._id = options.id || '';
1325
- }
1326
-
1327
- // 构建
1328
- TexturePrimitive.prototype.build = function () {
1329
- var postionsTemp = [],
1330
- stsTemp = [0, 0, 1, 0, 1, 1, 0, 1] //纹理坐标,调整纹理坐标顺序即可完成贴图的旋转
1331
- // var stsTemp = [1,1,0,1,0,0,1,0];
1332
- ,
1333
- indicesTesm = [0, 1, 2, 0, 2, 3]; //索引数组
1334
-
1335
- for (var i = 0; i < this._cartesians.length; i++) {
1336
- postionsTemp.push(this._cartesians[i].x);
1337
- postionsTemp.push(this._cartesians[i].y);
1338
- postionsTemp.push(this._cartesians[i].z);
1339
- }
1340
- this._positionArr = new Float32Array(postionsTemp);
1341
- this._sts = new Uint8Array(stsTemp);
1342
- this._indiceArr = new Uint16Array(indicesTesm);
1343
- //通过坐标数组,索引数组,纹理坐标数组创建多边形
1344
- this._geometry = this._createGeometry();
1345
- this._appearance = this._createAppearence();
1346
- this.primitive = this._viewer.scene.primitives.add(new Cesium.Primitive({
1347
- geometryInstances: new Cesium.GeometryInstance({
1348
- geometry: this._geometry,
1349
- id: this._id
1350
- }),
1351
- appearance: this._appearance,
1352
- asynchronous: false
1353
- }));
1354
- };
1355
- // 生成几何
1356
- TexturePrimitive.prototype._createGeometry = function () {
1357
- var sess = new Cesium.GeometryAttribute({
1358
- componentDatatype: Cesium.ComponentDatatype.FLOAT,
1359
- componentsPerAttribute: 2,
1360
- values: this._sts
1361
- });
1362
- return new Cesium.Geometry({
1363
- attributes: {
1364
- position: new Cesium.GeometryAttribute({
1365
- componentDatatype: Cesium.ComponentDatatype.DOUBLE,
1366
- componentsPerAttribute: 3,
1367
- values: this._positions
1368
- }),
1369
- st: sess
1370
- },
1371
- indices: this._indices,
1372
- //索引指标,指示创建三角形的顺序
1373
- primitiveType: Cesium.PrimitiveType.TRIANGLES,
1374
- boundingSphere: Cesium.BoundingSphere.fromVertices(positions)
1375
- });
1376
- };
1377
- //生成外观
1378
- TexturePrimitive.prototype._createAppearence = function () {
1379
- return new Cesium.Appearance({
1380
- material: new Cesium.Material({
1381
- fabric: {
1382
- uniforms: {
1383
- image: this._url
1384
- },
1385
- source: this._materialShader
1386
- }
1387
- }),
1388
- aboveGround: true,
1389
- faceForward: true,
1390
- flat: true,
1391
- translucent: false,
1392
- renderState: {
1393
- blending: Cesium.BlendingState.PRE_MULTIPLIED_ALPHA_BLEND,
1394
- depthTest: {
1395
- enabled: true
1396
- },
1397
- depthMask: true
1398
- },
1399
- fragmentShaderSource: this._fragmentShader,
1400
- vertexShaderSource: this._vertexShader
1401
- });
1402
- };
1403
- TexturePrimitive.prototype.getVS = function () {
1404
- return 'attribute vec3 position3DHigh;\
1405
- attribute vec3 position3DLow;\
1406
- attribute vec2 st;\
1407
- attribute float batchId;\
1408
- varying vec2 v_st;\
1409
- void main()\
1410
- {\
1411
- vec4 p = czm_computePosition();\
1412
- v_st=st;\
1413
- p = czm_modelViewProjectionRelativeToEye * p;\
1414
- gl_Position = p;\
1415
- }\
1416
- ';
1417
- };
1418
- TexturePrimitive.prototype.getFS = function () {
1419
- return 'varying vec2 v_st;\
1420
- void main()\
1421
- {\
1422
- czm_materialInput materialInput;\
1423
- czm_material material=czm_getMaterial(materialInput,v_st);\
1424
- vec4 color=vec4(material.diffuse + material.emission,material.alpha);\
1425
- if(color.x==1.0&&color.y==1.0&&color.z==1.0&&color.w==1.0) color=vec4(vec3(0.0,0.0,0.0),0.0);\
1426
- gl_FragColor =color;\
1427
- }\
1428
- ';
1429
- };
1430
- TexturePrimitive.prototype.getMS = function () {
1431
- return 'czm_material czm_getMaterial(czm_materialInput materialInput,vec2 v_st)\
1432
- {\
1433
- vec4 color = texture2D(image, v_st);\
1434
- czm_material material = czm_getDefaultMaterial(materialInput);\
1435
- material.diffuse= color.rgb;\
1436
- material.alpha=color.a;\
1437
- return material;\
1438
- }\
1439
- ';
1440
- };
1441
- Cesium.Scene.TexturePrimitive = TexturePrimitive;
1442
- },
1443
- /**
1444
- * 卫星雷达波
1445
- */
1446
- _installProbingPrimitive: function () {
1447
- var DEF_OPT = {
1448
- color: new Cesium.Color(1.0, 0.0, 1.0, 0.8),
1449
- repeat: 30.0,
1450
- offset: 0.0,
1451
- thickness: 0.3,
1452
- center: Cesium.Cartesian3.fromDegrees(116.39, 39.9),
1453
- length: 400000.0,
1454
- bottom: 1000,
1455
- top: 0.0
1456
- };
1457
- var viewer = this._viewer;
1458
-
1459
- /**
1460
- *
1461
- * @param {*} option
1462
- */
1463
- function ProbingPrimitive(option) {
1464
- this._viewer = viewer;
1465
- this._length = option.length || DEF_OPT.length;
1466
- this._center = option.center || DEF_OPT.center;
1467
- this._color = option.color || DEF_OPT.color;
1468
- this._repeat = option.repeat || DEF_OPT.repeat;
1469
- this._offset = option.offset || DEF_OPT.offset;
1470
- this._thickness = option.thickness || DEF_OPT.thickness;
1471
- this._bottom = option.bottom || DEF_OPT.bottom;
1472
- this._top = option.top || DEF_OPT.top;
1473
- this._radar = undefined;
1474
- this.build();
1475
- }
1476
- ProbingPrimitive.prototype.build = function () {
1477
- var cylinderGeometry = new Cesium.CylinderGeometry({
1478
- length: this._length,
1479
- topRadius: this._top,
1480
- bottomRadius: this._bottom,
1481
- vertexFormat: Cesium.MaterialAppearance.MaterialSupport.TEXTURED.vertexFormat
1482
- }),
1483
- redCone = new Cesium.GeometryInstance({
1484
- geometry: cylinderGeometry,
1485
- modelMatrix: this.getModelMatrix()
1486
- }),
1487
- appearance = new Cesium.MaterialAppearance({
1488
- material: this.getMaterial(),
1489
- faceForward: false,
1490
- closed: true
1491
- }),
1492
- $this = this;
1493
- this._radar = this._viewer.scene.primitives.add(new Cesium.Primitive({
1494
- geometryInstances: [redCone],
1495
- appearance: appearance
1496
- }));
1497
- //监听渲染事件 动态修改雷达材质中的offset变量 从而实现动态效果
1498
- this._viewer.scene.preUpdate.addEventListener(function () {
1499
- var offset = $this._radar.appearance.material.uniforms.offset;
1500
- offset -= 0.001;
1501
- if (offset > 1.0) {
1502
- offset = 0.0;
1503
- }
1504
- $this._radar.appearance.material.uniforms.offset = offset;
1505
- });
1506
- };
1507
- ProbingPrimitive.prototype.getModelMatrix = function () {
1508
- return Cesium.Matrix4.multiplyByTranslation(
1509
- //转换矩阵
1510
- Cesium.Transforms.eastNorthUpToFixedFrame(this._center),
1511
- //矩阵
1512
- new Cesium.Cartesian3(0.0, 0.0, this._length * 0.5),
1513
- //要转换的笛卡尔坐标
1514
- new Cesium.Matrix4() //返回新的矩阵
1515
- );
1516
- };
1517
-
1518
- ProbingPrimitive.prototype.updateModelMatrix = function () {};
1519
- ProbingPrimitive.prototype.getMaterial = function () {
1520
- var materialSource = `uniform vec4 color;
1521
- uniform float repeat;
1522
- uniform float offset;
1523
- uniform float thickness;
1524
-
1525
- czm_material czm_getMaterial(czm_materialInput materialInput){
1526
- czm_material material = czm_getDefaultMaterial(materialInput);
1527
- float sp = 1.0/repeat;
1528
- vec2 st = materialInput.st;
1529
- float dis = distance(st, vec2(0.5));
1530
- float m = mod(dis + offset, sp);
1531
- float a = step(sp*(1.0-thickness), m);
1532
-
1533
- material.diffuse = color.rgb;
1534
- material.alpha = a * color.a;
1535
- return material;
1536
- }`;
1537
- return new Cesium.Material({
1538
- fabric: {
1539
- type: 'radarPrimitive',
1540
- uniforms: {
1541
- //动态传递参数
1542
- color: this._color,
1543
- repeat: this._repeat,
1544
- offset: this._offset,
1545
- thickness: this._thickness
1546
- },
1547
- source: materialSource
1548
- },
1549
- translucent: false
1550
- });
1551
- };
1552
- ProbingPrimitive.prototype.remove = function () {
1553
- if (this._radar) {
1554
- this._viewer.scene.primitives.remove(this._radar);
1555
- }
1556
- };
1557
- Cesium.Scene.ProbingPrimitive = ProbingPrimitive;
1558
- }
1559
- };