Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

LightCylinderPrimitive.js 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /**
  2. * @Author: Caven
  3. * @Date: 2022-05-28 10:25:24
  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 { IMG_PARTICLES } from '@dc-modules/images/base64'
  11. import Overlay from '../Overlay'
  12. const DEF_STYLE = {
  13. color: Cesium.Color.ORANGE
  14. }
  15. class LightCylinderPrimitive extends Overlay {
  16. constructor(center, length, topRadius, bottomRadius) {
  17. super()
  18. this._center = Parse.parsePosition(center)
  19. this._length = length || 100
  20. this._topRadius = topRadius || 1
  21. this._bottomRadius = bottomRadius || 50
  22. this._delegate = new Cesium.PrimitiveCollection()
  23. this._style = { ...DEF_STYLE }
  24. this._state = State.INITIALIZED
  25. }
  26. get type() {
  27. return Overlay.getOverlayType('light-cylinder-primitive')
  28. }
  29. set center(position) {
  30. this._center = Parse.parsePosition(position)
  31. this._updatePrimitives()
  32. return this
  33. }
  34. get center() {
  35. return this._center
  36. }
  37. set length(length) {
  38. this._length = length
  39. this._updatePrimitives()
  40. return this
  41. }
  42. get length() {
  43. return this._length
  44. }
  45. set topRadius(topRadius) {
  46. this._topRadius = topRadius
  47. this._updatePrimitives()
  48. return this
  49. }
  50. get topRadius() {
  51. return this._topRadius
  52. }
  53. set bottomRadius(bottomRadius) {
  54. this._bottomRadius = bottomRadius
  55. this._updatePrimitives()
  56. return this
  57. }
  58. get bottomRadius() {
  59. return this._bottomRadius
  60. }
  61. /**
  62. *
  63. * @param center
  64. * @param radius
  65. * @return {[]}
  66. * @private
  67. */
  68. _computeEllipsePositions(center, radius) {
  69. let cep = Cesium.EllipseGeometryLibrary.computeEllipsePositions(
  70. {
  71. center: Transform.transformWGS84ToCartesian(center),
  72. semiMajorAxis: radius,
  73. semiMinorAxis: radius,
  74. rotation: 0,
  75. granularity: 0.005
  76. },
  77. false,
  78. true
  79. )
  80. let pnts = Cesium.Cartesian3.unpackArray(cep.outerPositions)
  81. pnts.push(pnts[0])
  82. return pnts
  83. }
  84. /**
  85. *
  86. * @param topPts
  87. * @param bottomPts
  88. * @param height
  89. * @return {Cesium.GeometryInstance}
  90. */
  91. _createCylinderInstance(topPts, bottomPts, height) {
  92. let newpts = bottomPts.slice()
  93. let length = bottomPts.length
  94. let len_2 = 2 * length
  95. let sts = []
  96. let st_interval = 1.0 / (length - 1)
  97. let define_indices = []
  98. let ep = []
  99. const addHeight = (p, alt = 0) => {
  100. let c = Cesium.Cartographic.fromCartesian(p)
  101. c.height += alt
  102. return Cesium.Cartographic.toCartesian(c)
  103. }
  104. for (let i = 0; i < length; i++) {
  105. ep.push(addHeight(topPts[i], height))
  106. sts.push(i * st_interval, 0)
  107. let i_1 = i + 1
  108. let i_11 = (i + 1) % length
  109. let len_2_i_1 = len_2 - i_1
  110. define_indices.push(...[len_2_i_1 - 1, len_2_i_1, i])
  111. define_indices.push(...[i, i_11, len_2_i_1 - 1])
  112. }
  113. for (let i in ep) {
  114. newpts.push(ep[length - i - 1])
  115. sts.push(1 - i * st_interval, 1)
  116. }
  117. let polygon = Cesium.PolygonGeometry.createGeometry(
  118. new Cesium.PolygonGeometry({
  119. polygonHierarchy: new Cesium.PolygonHierarchy(newpts),
  120. perPositionHeight: true
  121. })
  122. )
  123. polygon.indices = define_indices
  124. polygon.attributes.st.values = sts
  125. return new Cesium.GeometryInstance({
  126. geometry: polygon
  127. })
  128. }
  129. /**
  130. *
  131. * @return {HTMLCanvasElement}
  132. * @private
  133. */
  134. _getCircleImage() {
  135. let canvas = document.createElement('canvas')
  136. canvas.width = 512
  137. canvas.height = 512
  138. let ctx = canvas.getContext('2d')
  139. ctx.fillStyle = 'rgba(255,255,255,0)'
  140. ctx.strokeStyle = 'rgba(255, 255, 255,255)'
  141. ctx.setLineDash([50, 50])
  142. ctx.lineWidth = 30
  143. ctx.beginPath()
  144. ctx.arc(256, 256, 150, 0, Math.PI * 2, true)
  145. ctx.stroke()
  146. ctx.restore()
  147. return canvas
  148. }
  149. /**
  150. *
  151. * @param image
  152. * @return {HTMLCanvasElement}
  153. * @private
  154. */
  155. _getParticlesImage(image) {
  156. let canvas = document.createElement('canvas')
  157. canvas.width = 64
  158. canvas.height = 256
  159. let ctx = canvas.getContext('2d')
  160. ctx.clearRect(0, 0, 64, 256)
  161. ctx.drawImage(image, 0, 0)
  162. ctx.drawImage(image, 33, 0)
  163. return canvas
  164. }
  165. /**
  166. *
  167. * @private
  168. */
  169. _updatePrimitives() {
  170. this._delegate.removeAll()
  171. const isGroud = this._center.alt === 0
  172. let topPositions = this._computeEllipsePositions(
  173. this._center,
  174. this._topRadius
  175. )
  176. let innerBottomPostions = this._computeEllipsePositions(
  177. this._center,
  178. this._bottomRadius * 0.7
  179. )
  180. let bottomPositions = this._computeEllipsePositions(
  181. this._center,
  182. this._bottomRadius
  183. )
  184. // update buttom circle
  185. const circleOpt = {
  186. geometryInstances: new Cesium.GeometryInstance({
  187. geometry: new Cesium.PolygonGeometry({
  188. polygonHierarchy: new Cesium.PolygonHierarchy(
  189. this._computeEllipsePositions(this._center, this._bottomRadius * 2)
  190. ),
  191. perPositionHeight: !isGroud
  192. }),
  193. asynchronous: false
  194. })
  195. }
  196. // ring
  197. let ring = isGroud
  198. ? new Cesium.GroundPrimitive(circleOpt)
  199. : new Cesium.Primitive(circleOpt)
  200. ring.appearance = new Cesium.EllipsoidSurfaceAppearance({
  201. material: Cesium.Material.fromType(Cesium.Material.CircleRingType, {
  202. color: this._style.color
  203. })
  204. })
  205. // circle
  206. let circle = isGroud
  207. ? new Cesium.GroundPrimitive(circleOpt)
  208. : new Cesium.Primitive(circleOpt)
  209. circle.appearance = new Cesium.EllipsoidSurfaceAppearance({
  210. material: Cesium.Material.fromType(Cesium.Material.CircleRotateType, {
  211. color: this._style.color,
  212. image: this._getCircleImage()
  213. })
  214. })
  215. // cylinder
  216. let cylinder = new Cesium.Primitive({
  217. geometryInstances: this._createCylinderInstance(
  218. topPositions,
  219. innerBottomPostions,
  220. this._length
  221. ),
  222. appearance: new Cesium.EllipsoidSurfaceAppearance({
  223. material: Cesium.Material.fromType(Cesium.Material.CylinderFadeType, {
  224. color: this._style.color
  225. })
  226. }),
  227. asynchronous: false
  228. })
  229. if (isGroud) {
  230. Cesium.GroundPrimitive.initializeTerrainHeights().then(() => {
  231. this._delegate.add(ring)
  232. this._delegate.add(circle)
  233. this._delegate.add(cylinder)
  234. })
  235. } else {
  236. this._delegate.add(ring)
  237. this._delegate.add(circle)
  238. this._delegate.add(cylinder)
  239. }
  240. // particles
  241. Cesium.Resource.fetchImage({ url: IMG_PARTICLES }).then(image => {
  242. let particles = new Cesium.Primitive({
  243. geometryInstances: this._createCylinderInstance(
  244. topPositions,
  245. bottomPositions,
  246. this._length
  247. ),
  248. appearance: new Cesium.EllipsoidSurfaceAppearance({
  249. material: Cesium.Material.fromType(
  250. Cesium.Material.CylinderParticlesType,
  251. {
  252. color: this._style.color,
  253. image: this._getParticlesImage(image)
  254. }
  255. )
  256. }),
  257. asynchronous: false
  258. })
  259. this._delegate.add(particles)
  260. })
  261. }
  262. /**
  263. *
  264. * @private
  265. */
  266. _mountedHook() {
  267. /**
  268. * set the positions
  269. */
  270. this.center = this._center
  271. }
  272. /**
  273. *
  274. * @param frameState
  275. */
  276. update(frameState) {
  277. this._delegate.update(frameState)
  278. }
  279. /**
  280. *
  281. * @param style
  282. * @returns {LightCylinderPrimitive}
  283. */
  284. setStyle(style) {
  285. if (!style || Object.keys(style).length === 0) {
  286. return this
  287. }
  288. Util.merge(this._style, style)
  289. return this
  290. }
  291. destroy() {
  292. return Cesium.destroyObject(this)
  293. }
  294. }
  295. Overlay.registerType('light-cylinder-primitive')
  296. export default LightCylinderPrimitive