当前位置:   article > 正文

Cesium笔记 生成抛物线 流动线_cesium 动态箭头

cesium 动态箭头

效果图

代码思路,根据起点终点坐标以及需要设置的抛物线高度生成抛物线坐标串,然后根据坐标串生成cesium实体线,并修改材质为流动线 

  1. viewer.entities.removeById('pwxline')
  2. viewer.entities.removeById('verticalPwxline')
  3. let startPoint = this.clickPoint
  4. let endPoint = row.geoPositon.split(',')
  5. let height = this.getFlatternDistance(startPoint.lat, startPoint.lng, parseFloat(endPoint[1]), parseFloat(endPoint[0]))//高度是根据起始点距离的一半
  6. let positions = this.parabolaEquation({//生成抛物线代码
  7. startPoint: { lng: parseFloat(endPoint[0]), lat: parseFloat(endPoint[1]) },
  8. endPoint: { lng: startPoint.lng, lat: startPoint.lat },
  9. height: height / 2,
  10. num: 100
  11. })
  12. this.positionsParabola2.push(positions) //获取抛物线坐标串
  13. let postionarry = []
  14. for (let i = 0; i < positions.length; i++) {
  15. postionarry.push(positions[i].lng, positions[i].lat, positions[i].height)
  16. }
  17. //let entity = viewer.entities.getOrCreateEntity(this.PDPointSelectID)
  18. //因为流动线材质代码
  19. require('../../../../assets/plugins/PolylineTrailLinkMaterial')
  20. let model = viewer.entities.add({
  21. id: 'pwxline',
  22. name: 'polyline',
  23. polyline: {
  24. positions: Cesium.Cartesian3.fromDegreesArrayHeights(postionarry),
  25. width: 20,
  26. material: new Cesium.PolylineTrailLinkMaterialProperty(Cesium.Color.AQUA, 5000)//修改抛物线材质
  27. }
  28. })
  29. let verticalPwxlinePosition = []
  30. verticalPwxlinePosition.push(parseFloat(endPoint[0]), parseFloat(endPoint[1]), 0)
  31. verticalPwxlinePosition.push(parseFloat(endPoint[0]), parseFloat(endPoint[1]), 55)
  32. viewer.entities.add({
  33. id: 'verticalPwxline',
  34. name: 'verticalPwxline',
  35. polyline: {
  36. positions: Cesium.Cartesian3.fromDegreesArrayHeights(verticalPwxlinePosition),
  37. width: 5,
  38. material: Cesium.Color.AQUA.withAlpha(0.5)
  39. }
  40. })
  41. viewer.flyTo(model, {
  42. complete: () => {}
  43. })

生成抛物线代码

  1. parabolaEquation(options) {
  2. // 方程 y=-(4h/L^2)*x^2+h h:顶点高度 L:横纵间距较大者
  3. const h = options.height && options.height > 50 ? options.height : 50
  4. const L =
  5. Math.abs(options.startPoint.lng - options.endPoint.lng) > Math.abs(options.startPoint.lat - options.endPoint.lat)
  6. ? Math.abs(options.startPoint.lng - options.endPoint.lng)
  7. : Math.abs(options.startPoint.lat - options.endPoint.lat)
  8. const num = options.num && options.num > 50 ? options.num : 50
  9. const result = []
  10. let dlt = L / num
  11. if (Math.abs(options.startPoint.lng - options.endPoint.lng) > Math.abs(options.startPoint.lat - options.endPoint.lat)) {
  12. //以 lng 为基准
  13. const delLat = (options.endPoint.lat - options.startPoint.lat) / num
  14. if (options.startPoint.lng - options.endPoint.lng > 0) {
  15. dlt = -dlt
  16. }
  17. for (let i = 0; i < num; i++) {
  18. const tempH = h - (Math.pow(-0.5 * L + Math.abs(dlt) * i, 2) * 4 * h) / Math.pow(L, 2)
  19. const lng = options.startPoint.lng + dlt * i
  20. const lat = options.startPoint.lat + delLat * i
  21. result.push({ lng, lat, height: tempH })
  22. }
  23. } else {
  24. //以 lat 为基准
  25. let dellng = (options.endPoint.lng - options.startPoint.lng) / num
  26. if (options.startPoint.lat - options.endPoint.lat > 0) {
  27. dlt = -dlt
  28. }
  29. for (let i = 0; i < num; i++) {
  30. const tempH = h - (Math.pow(-0.5 * L + Math.abs(dlt) * i, 2) * 4 * h) / Math.pow(L, 2)
  31. const lng = options.startPoint.lng + dellng * i
  32. const lat = options.startPoint.lat + dlt * i
  33. result.push({ lng, lat, height: tempH })
  34. }
  35. }
  36. // 落地
  37. result.push({ lng: options.endPoint.lng, lat: options.endPoint.lat, height: options.endPoint.height || 0 })
  38. return result
  39. },

流动线代码

  1. // 流动线纹理
  2. function PolylineTrailLinkMaterialProperty(color, duration) {
  3. this._definitionChanged = new Cesium.Event()
  4. this._color = undefined
  5. this._colorSubscription = undefined
  6. this.color = color
  7. this.duration = duration
  8. this._time = new Date().getTime()
  9. }
  10. Object.defineProperties(PolylineTrailLinkMaterialProperty.prototype, {
  11. isConstant: {
  12. get: function () {
  13. return false
  14. }
  15. },
  16. definitionChanged: {
  17. get: function () {
  18. return this._definitionChanged
  19. }
  20. },
  21. color: Cesium.createPropertyDescriptor('color')
  22. })
  23. PolylineTrailLinkMaterialProperty.prototype.getType = function (time) {
  24. return 'PolylineTrailLink'
  25. }
  26. PolylineTrailLinkMaterialProperty.prototype.getValue = function (time, result) {
  27. if (!Cesium.defined(result)) {
  28. result = {}
  29. }
  30. result.color = Cesium.Property.getValueOrClonedDefault(this._color, time, Cesium.Color.WHITE, result.color)
  31. result.image = Cesium.Material.PolylineTrailLinkImage
  32. result.time = ((new Date().getTime() - this._time) % this.duration) / this.duration
  33. return result
  34. }
  35. PolylineTrailLinkMaterialProperty.prototype.equals = function (other) {
  36. return this === other || (other instanceof PolylineTrailLinkMaterialProperty && Cesium.Property.equals(this._color, other._color))
  37. }
  38. Cesium.PolylineTrailLinkMaterialProperty = PolylineTrailLinkMaterialProperty
  39. Cesium.Material.PolylineTrailLinkType = 'PolylineTrailLink'
  40. Cesium.Material.PolylineTrailLinkImage = '/static/images/a3.png' //图片 图片为箭头
  41. Cesium.Material.PolylineTrailLinkSource =
  42. 'czm_material czm_getMaterial(czm_materialInput materialInput)\n\
  43. {\n\
  44. czm_material material = czm_getDefaultMaterial(materialInput);\n\
  45. vec2 st = materialInput.st;\n\
  46. vec4 colorImage = texture2D(image, vec2(fract(st.s - time), st.t));\n\
  47. material.alpha = colorImage.a * color.a;\n\
  48. material.diffuse = (colorImage.rgb+color.rgb)/2.0;\n\
  49. return material;\n\
  50. }'
  51. Cesium.Material._materialCache.addMaterial(Cesium.Material.PolylineTrailLinkType, {
  52. fabric: {
  53. type: Cesium.Material.PolylineTrailLinkType,
  54. uniforms: {
  55. color: new Cesium.Color(255.0, 255.0, 255.0, 1),
  56. image: Cesium.Material.PolylineTrailLinkImage,
  57. time: 0
  58. },
  59. source: Cesium.Material.PolylineTrailLinkSource
  60. },
  61. translucent: function (material) {
  62. return true
  63. }
  64. })

计算起始点距离代码

  1. //计算两点间距离
  2. getFlatternDistance(lat1, lng1, lat2, lng2) {
  3. var EARTH_RADIUS = 6378137.0 //单位M
  4. var PI = Math.PI
  5. function getRad(d) {
  6. return (d * PI) / 180.0
  7. }
  8. var f = getRad((lat1 + lat2) / 2)
  9. var g = getRad((lat1 - lat2) / 2)
  10. var l = getRad((lng1 - lng2) / 2)
  11. var sg = Math.sin(g)
  12. var sl = Math.sin(l)
  13. var sf = Math.sin(f)
  14. var s, c, w, r, d, h1, h2
  15. var a = EARTH_RADIUS
  16. var fl = 1 / 298.257
  17. sg = sg * sg
  18. sl = sl * sl
  19. sf = sf * sf
  20. s = sg * (1 - sl) + (1 - sf) * sl
  21. c = (1 - sg) * (1 - sl) + sf * sl
  22. w = Math.atan(Math.sqrt(s / c))
  23. r = Math.sqrt(s * c) / w
  24. d = 2 * w * a
  25. h1 = (3 * r - 1) / 2 / c
  26. h2 = (3 * r + 1) / 2 / s
  27. return d * (1 + fl * (h1 * sf * (1 - sg) - h2 * (1 - sf) * sg))
  28. },

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/564063
推荐阅读
相关标签
  

闽ICP备14008679号