flyline.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * @Description: 飞线效果(参考开源代码)
  3. * @Version: 1.0
  4. * @Author: Julian
  5. * @Date: 2022-03-05 16:13:21
  6. * @LastEditors: Julian
  7. * @LastEditTime: 2022-03-05 17:39:38
  8. */
  9. class LineFlowMaterialProperty {
  10. constructor(options) {
  11. this._definitionChanged = new Cesium.Event();
  12. this._color = undefined;
  13. this._speed = undefined;
  14. this._percent = undefined;
  15. this._gradient = undefined;
  16. this.color = options.color;
  17. this.speed = options.speed;
  18. this.percent = options.percent;
  19. this.gradient = options.gradient;
  20. };
  21. get isConstant() {
  22. return false;
  23. }
  24. get definitionChanged() {
  25. return this._definitionChanged;
  26. }
  27. getType(time) {
  28. return Cesium.Material.LineFlowMaterialType;
  29. }
  30. getValue(time, result) {
  31. if (!Cesium.defined(result)) {
  32. result = {};
  33. }
  34. result.color = Cesium.Property.getValueOrDefault(this._color, time, Cesium.Color.RED, result.color);
  35. result.speed = Cesium.Property.getValueOrDefault(this._speed, time, 5.0, result.speed);
  36. result.percent = Cesium.Property.getValueOrDefault(this._percent, time, 0.1, result.percent);
  37. result.gradient = Cesium.Property.getValueOrDefault(this._gradient, time, 0.01, result.gradient);
  38. return result
  39. }
  40. equals(other) {
  41. return (this === other ||
  42. (other instanceof LineFlowMaterialProperty &&
  43. Cesium.Property.equals(this._color, other._color) &&
  44. Cesium.Property.equals(this._speed, other._speed) &&
  45. Cesium.Property.equals(this._percent, other._percent) &&
  46. Cesium.Property.equals(this._gradient, other._gradient))
  47. )
  48. }
  49. }
  50. Object.defineProperties(LineFlowMaterialProperty.prototype, {
  51. color: Cesium.createPropertyDescriptor('color'),
  52. speed: Cesium.createPropertyDescriptor('speed'),
  53. percent: Cesium.createPropertyDescriptor('percent'),
  54. gradient: Cesium.createPropertyDescriptor('gradient'),
  55. })
  56. Cesium.LineFlowMaterialProperty = LineFlowMaterialProperty;
  57. Cesium.Material.LineFlowMaterialProperty = 'LineFlowMaterialProperty';
  58. Cesium.Material.LineFlowMaterialType = 'LineFlowMaterialType';
  59. Cesium.Material.LineFlowMaterialSource =
  60. `
  61. uniform vec4 color;
  62. uniform float speed;
  63. uniform float percent;
  64. uniform float gradient;
  65. czm_material czm_getMaterial(czm_materialInput materialInput){
  66. czm_material material = czm_getDefaultMaterial(materialInput);
  67. vec2 st = materialInput.st;
  68. float t =fract(czm_frameNumber * speed / 1000.0);
  69. t *= (1.0 + percent);
  70. float alpha = smoothstep(t- percent, t, st.s) * step(-t, -st.s);
  71. alpha += gradient;
  72. material.diffuse = color.rgb;
  73. material.alpha = alpha;
  74. return material;
  75. }
  76. `
  77. Cesium.Material._materialCache.addMaterial(Cesium.Material.LineFlowMaterialType, {
  78. fabric: {
  79. type: Cesium.Material.LineFlowMaterialType,
  80. uniforms: {
  81. color: new Cesium.Color(1.0, 0.0, 0.0, 1.0),
  82. speed: 10.0,
  83. percent: 0.1,
  84. gradient: 0.01
  85. },
  86. source: Cesium.Material.LineFlowMaterialSource
  87. },
  88. translucent: function (material) {
  89. return true;
  90. }
  91. })
  92. /**
  93. * @description: 产生随机点
  94. * @param {*} position:中心点坐标
  95. * @param {*} num:随机点数量
  96. * @return {*}
  97. */
  98. function generateRandomPosition(position, num) {
  99. let list = []
  100. for (let i = 0; i < num; i++) {
  101. // random产生的随机数范围是0-1,需要加上正负模拟
  102. let lon = position[0] + Math.random() * 0.04 * (i % 2 == 0 ? 1 : -1);
  103. let lat = position[1] + Math.random() * 0.04 * (i % 2 == 0 ? 1 : -1);
  104. list.push([lon, lat])
  105. }
  106. return list
  107. }
  108. /**
  109. * @description: 竖直随机飞线初始化
  110. * @param {*} _viewer
  111. * @param {*} _center :中心点
  112. * @param {*} _num :数量
  113. * @return {*}
  114. */
  115. function lineFlowInit(_viewer, options) {
  116. let _positions = generateRandomPosition(options.center, options.num);
  117. _positions.forEach(item => {
  118. // 经纬度
  119. let start_lon = item[0];
  120. let start_lat = item[1];
  121. let startPoint = new Cesium.Cartesian3.fromDegrees(start_lon, start_lat, 0);
  122. // 随机高度
  123. let height = 5000 * Math.random();
  124. let endPoint = new Cesium.Cartesian3.fromDegrees(start_lon, start_lat, height);
  125. let linePositions = [];
  126. linePositions.push(startPoint);
  127. linePositions.push(endPoint);
  128. _viewer.entities.add({
  129. polyline: {
  130. positions: linePositions,
  131. material: new Cesium.LineFlowMaterialProperty({
  132. // color: Cesium.Color.fromCssColorString(options.color),
  133. color: Cesium.Color.fromCssColorString(getRandomColor()),
  134. speed: 15 * Math.random(),
  135. percent: 0.1,
  136. gradient: 0.01
  137. })
  138. }
  139. })
  140. })
  141. }
  142. function getRandomColor() {
  143. var str = '#';
  144. var data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c', 'd', 'e', 'f']
  145. for (var i = 0; i < 6; i++) {
  146. // 需要生成的随机数范围是 0-15
  147. var rdnum = Math.floor(Math.random() * 16);
  148. // console.log(data[rdnum]);
  149. str += data[rdnum];
  150. }
  151. return str;
  152. }