You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LightCylinderPrimitive.js 7.6KB

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