tjfjtj.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. function WallRegularDiffuse(viewer, options) {
  2. let _viewer = viewer;
  3. // 扩散中心点
  4. let _center = options.center;
  5. // 扩散半径半径
  6. let _radius = options.radius || 1000.0;
  7. // 扩散正多变形的边数
  8. let _edge = options.edge || 64;
  9. // 扩散速度
  10. let _speed = options.speed || 5.0;
  11. // 扩散高度
  12. let _height = options.height || 100.0;
  13. // 实时高度
  14. let _currentHeight = _height;
  15. // 最小半径
  16. let _minRadius = options.minRadius || 10;
  17. // 实时半径
  18. let _currentRadius = _minRadius;
  19. if (_edge < 3) {
  20. return false;
  21. }
  22. /**
  23. * @description: 获取当前多边形的节点位置和高度
  24. * @param {*} _center:多边形中心
  25. * @param {*} _edge:多边形边数
  26. * @param {*} _currentRadius:当前半径
  27. * @param {*} _currentHeight:当前高度
  28. * @return {*}
  29. */
  30. function _getPositions(_center, _edge, _currentRadius, _currentHeight) {
  31. let positions = [];
  32. let modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(
  33. Cesium.Cartesian3.fromDegrees(_center[0], _center[1], 0)
  34. );
  35. for (let i = 0; i < _edge; i++) {
  36. let angle = (i / _edge) * Cesium.Math.TWO_PI;
  37. let x = Math.cos(angle);
  38. let y = Math.sin(angle);
  39. let point = new Cesium.Cartesian3(
  40. x * _currentRadius,
  41. y * _currentRadius,
  42. _currentHeight
  43. )
  44. positions.push(Cesium.Matrix4.multiplyByPoint(modelMatrix, point, new Cesium.Cartesian3()));
  45. }
  46. // 封闭墙,首节点点需要存两次
  47. positions.push(positions[0]);
  48. return positions;
  49. }
  50. // 添加多边形
  51. _viewer.entities.add({
  52. wall: {
  53. // callbackProperty回调函数,实时更新
  54. positions: new Cesium.CallbackProperty(() => {
  55. let positions = [];
  56. _currentRadius += _radius * _speed / 1000.0;
  57. _currentHeight -= _height * _speed / 1000.0;
  58. // 判断扩散的实际半径和高度是否超出范围
  59. if (_currentRadius > _radius || _currentHeight < 0) {
  60. _currentRadius = _minRadius;
  61. _currentHeight = _height;
  62. }
  63. positions = _getPositions(_center, _edge, _currentRadius, _currentHeight);
  64. return positions;
  65. }, false),
  66. // 设置材质
  67. material: new Cesium.WallDiffuseMaterialProperty({
  68. // color: new Cesium.Color(1.0, 1.0, 0.0, 1.0)
  69. color: Cesium.Color.fromCssColorString(options.color)
  70. })
  71. }
  72. })
  73. }
  74. /*
  75. * @Description: 动态扩散墙的墙体效果(参考开源代码)(不同高度透明度不同)
  76. * @Version: 1.0
  77. * @Author: Julian
  78. * @Date: 2022-03-07 19:50:46
  79. * @LastEditors: Julian
  80. * @LastEditTime: 2022-03-07 19:56:30
  81. */
  82. class WallDiffuseMaterialProperty {
  83. constructor(options) {
  84. this._definitionChanged = new Cesium.Event();
  85. this._color = undefined;
  86. this.color = options.color;
  87. };
  88. get isConstant() {
  89. return false;
  90. }
  91. get definitionChanged() {
  92. return this._definitionChanged;
  93. }
  94. getType(time) {
  95. return Cesium.Material.WallDiffuseMaterialType;
  96. }
  97. getValue(time, result) {
  98. if (!Cesium.defined(result)) {
  99. result = {};
  100. }
  101. result.color = Cesium.Property.getValueOrDefault(this._color, time, Cesium.Color.RED, result.color);
  102. return result
  103. }
  104. equals(other) {
  105. return (this === other ||
  106. (other instanceof WallDiffuseMaterialProperty &&
  107. Cesium.Property.equals(this._color, other._color))
  108. )
  109. }
  110. }
  111. Object.defineProperties(WallDiffuseMaterialProperty.prototype, {
  112. color: Cesium.createPropertyDescriptor('color'),
  113. })
  114. Cesium.WallDiffuseMaterialProperty = WallDiffuseMaterialProperty;
  115. Cesium.Material.WallDiffuseMaterialProperty = 'WallDiffuseMaterialProperty';
  116. Cesium.Material.WallDiffuseMaterialType = 'WallDiffuseMaterialType';
  117. Cesium.Material.WallDiffuseMaterialSource =
  118. `
  119. uniform vec4 color;
  120. czm_material czm_getMaterial(czm_materialInput materialInput){
  121. czm_material material = czm_getDefaultMaterial(materialInput);
  122. vec2 st = materialInput.st;
  123. material.diffuse = color.rgb * 2.0;
  124. material.alpha = color.a * (1.0-fract(st.t)) * 0.8;
  125. return material;
  126. }
  127. `
  128. Cesium.Material._materialCache.addMaterial(Cesium.Material.WallDiffuseMaterialType, {
  129. fabric: {
  130. type: Cesium.Material.WallDiffuseMaterialType,
  131. uniforms: {
  132. color: new Cesium.Color(1.0, 0.0, 0.0, 1.0),
  133. },
  134. source: Cesium.Material.WallDiffuseMaterialSource
  135. },
  136. translucent: function (material) {
  137. return true;
  138. }
  139. })