WallObject.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* 引入Cesium */
  2. // import * as Cesium from 'Cesium';
  3. import {
  4. setSessionid
  5. } from "./common/common.js";
  6. /**
  7. *流动纹理
  8. */
  9. import DynamicWallMaterialProperty from "./WallObject/DynamicWallMaterialProperty.js";
  10. import WallDiffuseMaterialProperty from "./WallObject/WallDiffuseMaterialProperty.js";
  11. import WallMaterialProperty from "./WallObject/WallMaterialProperty.js";
  12. /**
  13. * 墙体对象
  14. */
  15. class WallObject {
  16. /**
  17. * 默认初始化
  18. */
  19. constructor(viewer) {
  20. if (!viewer) throw new Cesium.DeveloperError('no viewer object!');
  21. this._viewer = viewer;
  22. }
  23. }
  24. /**
  25. * 通用对外公开函数
  26. */
  27. Object.assign(WallObject.prototype, /** @lends WallObject.prototype */ {
  28. /**
  29. * @description 根据GeoJson绘制墙体对象
  30. * @param {String} geoJsonUrl geoJson文件路径
  31. * @param {Object} [options] 线的样式,具有以下属性:
  32. * @param {Number} [options.id] 用于移除
  33. * @param {Number} [options.clampToGround=true] 是否贴地
  34. * @param {Number} [options.minimunHeights=0] 最低高度
  35. * @param {Number} [options.maximumHeights=100] 最高高度
  36. * @param {Number} [options.imgUrl] 动态墙图片
  37. * @param {String} [options.color="#FF0000"] 指定墙的颜色
  38. * @param {Number} [options.duration=3000] 持续时间 毫秒,越小越快
  39. * @param {Number} [options.count] 重复次数
  40. * @param {String} [options.direction='horizontal'] 方向 vertical纵,垂直方向,horizontal横,水平方向
  41. * @param {String} [options.order] 方向正负
  42. * vertical 纵:'-'(由下到上) , '+"(由上到下)
  43. * horizontal 横:'-'(顺时针) , '+'(逆时针)
  44. */
  45. drawWallByGeoJson: function(geoJsonUrl, options) {
  46. return new Promise((resolve, reject) => {
  47. let _self = this;
  48. let viewer = this._viewer;
  49. if (!Cesium.defined(geoJsonUrl)) {
  50. throw new Cesium.DeveloperError("geoJsonUrl is required.");
  51. }
  52. options = options || {};
  53. options.id = options.id || setSessionid();
  54. options.clampToGround = Cesium.defaultValue(options.clampToGround, true);
  55. options.minimunHeights = options.minimunHeights !== undefined && typeof options.minimunHeights === 'number' ? options.minimunHeights : 0;
  56. options.maximumHeights = options.maximumHeights !== undefined && typeof options.maximumHeights === 'number' ? options.maximumHeights : 1000;
  57. if (options.color) {
  58. if (options.color instanceof Array) {
  59. options.color = new Cesium.Color(options.color[0] / 255, options.color[1] / 255, options.color[2] / 255, options.color[3]);
  60. } else if (typeof(options.color) === 'string') {
  61. options.color = new Cesium.Color.fromCssColorString(options.color);
  62. } else {
  63. options.color = new Cesium.Color.fromCssColorString("#FFFF00");
  64. }
  65. }
  66. options.trailImage = Cesium.defaultValue(options.trailImage, 'jt3dSDK/imgs/wallmaterial/wl.png');
  67. options.duration = Cesium.defaultValue(options.duration, 3000);
  68. options.count = Cesium.defaultValue(options.count, 1);
  69. options.direction = Cesium.defaultValue(options.direction, 'vertical');
  70. options.order = Cesium.defaultValue(options.order, '-');
  71. fetch(geoJsonUrl).then(res => {
  72. return res.json();
  73. }).then(res => {
  74. for (var i = 0; i < res.features.length; i++) {
  75. let coordinates = res.features[i].geometry.coordinates;
  76. let positions = coordinates.map(point => {
  77. return Cesium.Cartesian3.fromDegrees(point[0], point[1], point[2] || 0);
  78. });
  79. //先创建一个CustomDataSource源,然后把entity存入这里面
  80. let wall = new Cesium.CustomDataSource(options.id);
  81. viewer.dataSources.add(wall);
  82. let entity = new Cesium.Entity({
  83. name: "立体墙效果",
  84. wall: {
  85. positions: positions,
  86. // 设置高度
  87. maximumHeights: new Array(positions.length).fill(options.maximumHeights),
  88. minimunHeights: new Array(positions.length).fill(options.minimunHeights),
  89. // 扩散墙材质
  90. // material: new Cesium.WallDiffuseMaterialProperty({
  91. // color: new Cesium.Color(1.0, 1.0, 0.0, 1.0)
  92. // }),
  93. material: new WallMaterialProperty(viewer, {
  94. trailImage: options.trailImage,
  95. color: options.color,
  96. duration: options.duration,
  97. param: {
  98. count: options.count,
  99. direction: options.direction,
  100. order: options.order,
  101. },
  102. }),
  103. // material: new Cesium.DynamicWallMaterialProperty({
  104. // trailImage: 'jt3dSDK/imgs/wallmaterial/wl.png',
  105. // color: Cesium.Color.CYAN,
  106. // duration: 1500
  107. // })
  108. }
  109. });
  110. // 绘制墙体
  111. wall.entities.add(entity)
  112. }
  113. resolve(options.id);
  114. });
  115. });
  116. },
  117. });
  118. export default WallObject;