您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

DiffuseWallPrimitive.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * @Author: Caven
  3. * @Date: 2021-06-04 20:38:39
  4. */
  5. import { Cesium } from '@dc-modules/namespace'
  6. import State from '@dc-modules/state/State'
  7. import Parse from '@dc-modules/parse/Parse'
  8. import { Util } from '@dc-modules/utils'
  9. import { Transform } from '@dc-modules/transform'
  10. import Overlay from '../Overlay'
  11. const DEF_STYLE = {
  12. minRadius: 10,
  13. minHeight: 30,
  14. color: Cesium.Color.RED,
  15. slices: 128,
  16. speed: 10
  17. }
  18. class DiffuseWallPrimitive extends Overlay {
  19. constructor(center, radius, height) {
  20. super()
  21. this._center = Parse.parsePosition(center)
  22. this._delegate = undefined
  23. this._height = height
  24. this._radius = radius
  25. this._currentHeight = height || 0
  26. this._currentRadius = 10
  27. this._style = { ...DEF_STYLE }
  28. this._state = State.INITIALIZED
  29. }
  30. get type() {
  31. return Overlay.getOverlayType('diffuse_wall_primitive')
  32. }
  33. set center(position) {
  34. this._center = Parse.parsePosition(position)
  35. return this
  36. }
  37. get center() {
  38. return this._center
  39. }
  40. set radius(radius) {
  41. this._radius = radius
  42. return this
  43. }
  44. get radius() {
  45. return this._radius
  46. }
  47. set height(height) {
  48. this._height = height
  49. return this
  50. }
  51. get height() {
  52. return this._height
  53. }
  54. /**
  55. *
  56. * @returns {*}
  57. * @private
  58. */
  59. _getPositions() {
  60. let pnts = []
  61. let modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(
  62. Transform.transformWGS84ToCartesian(this._center)
  63. )
  64. for (let i = 0; i < this._style.slices; i++) {
  65. let angle = (i / this._style.slices) * Cesium.Math.TWO_PI
  66. let x = Math.cos(angle)
  67. let y = Math.sin(angle)
  68. let point = new Cesium.Cartesian3(
  69. x * this._currentRadius,
  70. y * this._currentRadius,
  71. 0.0
  72. )
  73. pnts.push(
  74. Cesium.Matrix4.multiplyByPoint(
  75. modelMatrix,
  76. point,
  77. new Cesium.Cartesian3()
  78. )
  79. )
  80. }
  81. pnts.push(pnts[0])
  82. return pnts
  83. }
  84. /**
  85. *
  86. * @param length
  87. * @param hegiht
  88. * @returns {*[]}
  89. * @private
  90. */
  91. _getHeights(length, hegiht) {
  92. let heights = []
  93. for (let i = 0; i < length; i++) {
  94. heights.push(hegiht)
  95. }
  96. return heights
  97. }
  98. /**
  99. *
  100. * @param frameState
  101. * @returns {boolean}
  102. */
  103. update(frameState) {
  104. this._delegate = this._delegate && this._delegate.destroy()
  105. this._currentRadius += this._radius / this._style.speed / 20
  106. this._currentHeight -= this._height / this._style.speed / 20
  107. if (
  108. this._currentRadius > this._radius ||
  109. this._currentHeight < this._style.minHeight
  110. ) {
  111. this._currentRadius = this._style.minRadius
  112. this._currentHeight = this._height
  113. }
  114. if (!this._style.slices || this._style.slices < 3) {
  115. return false
  116. }
  117. let positions = this._getPositions()
  118. if (!positions || !positions.length) {
  119. return false
  120. }
  121. let geometry = new Cesium.WallGeometry({
  122. positions: positions,
  123. minimumHeights: this._getHeights(positions.length, 0),
  124. maximumHeights: this._getHeights(positions.length, this._currentHeight)
  125. })
  126. this._delegate = new Cesium.Primitive({
  127. geometryInstances: new Cesium.GeometryInstance({
  128. geometry
  129. }),
  130. appearance: new Cesium.MaterialAppearance({
  131. material: Cesium.Material.fromType('WallDiffuse', {
  132. color: this._style?.color
  133. }),
  134. flat: true
  135. }),
  136. asynchronous: false
  137. })
  138. this._delegate.update(frameState)
  139. }
  140. /**
  141. *
  142. * @param style
  143. * @returns {DiffuseWallPrimitive}
  144. */
  145. setStyle(style) {
  146. if (!style || Object.keys(style).length === 0) {
  147. return this
  148. }
  149. Util.merge(this._style, style)
  150. return this
  151. }
  152. /**
  153. * @return {*}
  154. */
  155. destroy() {
  156. return Cesium.destroyObject(this)
  157. }
  158. }
  159. Overlay.registerType('diffuse_wall_primitive')
  160. export default DiffuseWallPrimitive