Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

DiffuseWallPrimitive.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.type = Overlay.getOverlayType('diffuse_wall_primitive')
  29. this._state = State.INITIALIZED
  30. }
  31. set center(position) {
  32. this._center = Parse.parsePosition(position)
  33. return this
  34. }
  35. get center() {
  36. return this._center
  37. }
  38. set radius(radius) {
  39. this._radius = radius
  40. return this
  41. }
  42. get radius() {
  43. return this._radius
  44. }
  45. set height(height) {
  46. this._height = height
  47. return this
  48. }
  49. get height() {
  50. return this._height
  51. }
  52. /**
  53. *
  54. * @returns {*}
  55. * @private
  56. */
  57. _getPositions() {
  58. let pnts = []
  59. let modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(
  60. Transform.transformWGS84ToCartesian(this._center)
  61. )
  62. for (let i = 0; i < this._style.slices; i++) {
  63. let angle = (i / this._style.slices) * Cesium.Math.TWO_PI
  64. let x = Math.cos(angle)
  65. let y = Math.sin(angle)
  66. let point = new Cesium.Cartesian3(
  67. x * this._currentRadius,
  68. y * this._currentRadius,
  69. 0.0
  70. )
  71. pnts.push(
  72. Cesium.Matrix4.multiplyByPoint(
  73. modelMatrix,
  74. point,
  75. new Cesium.Cartesian3()
  76. )
  77. )
  78. }
  79. pnts.push(pnts[0])
  80. return pnts
  81. }
  82. /**
  83. *
  84. * @param length
  85. * @param hegiht
  86. * @returns {*[]}
  87. * @private
  88. */
  89. _getHeights(length, hegiht) {
  90. let heights = []
  91. for (let i = 0; i < length; i++) {
  92. heights.push(hegiht)
  93. }
  94. return heights
  95. }
  96. /**
  97. *
  98. * @param layer
  99. * @private
  100. */
  101. _onAdd(layer) {
  102. if (!layer) {
  103. return
  104. }
  105. this._layer = layer
  106. if (this._layer?.delegate?.add) {
  107. this._layer.delegate.add(this)
  108. }
  109. this._addedHook && this._addedHook()
  110. this._state = State.ADDED
  111. }
  112. /**
  113. *
  114. * @private
  115. */
  116. _onRemove() {
  117. if (!this._layer) {
  118. return
  119. }
  120. if (this._layer?.delegate?.remove) {
  121. this._layer.delegate.remove(this)
  122. }
  123. this._state = State.REMOVED
  124. }
  125. /**
  126. *
  127. * @param frameState
  128. * @returns {boolean}
  129. */
  130. update(frameState) {
  131. this._delegate = this._delegate && this._delegate.destroy()
  132. this._currentRadius += this._radius / this._style.speed / 20
  133. this._currentHeight -= this._height / this._style.speed / 20
  134. if (
  135. this._currentRadius > this._radius ||
  136. this._currentHeight < this._style.minHeight
  137. ) {
  138. this._currentRadius = this._style.minRadius
  139. this._currentHeight = this._height
  140. }
  141. if (!this._style.slices || this._style.slices < 3) {
  142. return false
  143. }
  144. let positions = this._getPositions()
  145. if (!positions || !positions.length) {
  146. return false
  147. }
  148. let geometry = new Cesium.WallGeometry({
  149. positions: positions,
  150. minimumHeights: this._getHeights(positions.length, 0),
  151. maximumHeights: this._getHeights(positions.length, this._currentHeight)
  152. })
  153. this._delegate = new Cesium.Primitive({
  154. geometryInstances: new Cesium.GeometryInstance({
  155. geometry
  156. }),
  157. appearance: new Cesium.MaterialAppearance({
  158. material: Cesium.Material.fromType('WallDiffuse', {
  159. color: this._style?.color
  160. }),
  161. flat: true
  162. }),
  163. asynchronous: false
  164. })
  165. this._delegate.update(frameState)
  166. }
  167. /**
  168. *
  169. * @param style
  170. * @returns {DiffuseWallPrimitive}
  171. */
  172. setStyle(style) {
  173. if (!style || Object.keys(style).length === 0) {
  174. return this
  175. }
  176. Util.merge(this._style, style)
  177. return this
  178. }
  179. }
  180. Overlay.registerType('diffuse_wall_primitive')
  181. export default DiffuseWallPrimitive