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.

Model.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * @Author: Caven
  3. * @Date: 2020-01-06 20:03:25
  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. class Model extends Overlay {
  12. constructor(position, modelUrl) {
  13. super()
  14. this._delegate = new Cesium.Entity({ model: {} })
  15. this._position = Parse.parsePosition(position)
  16. this._modelUrl = modelUrl
  17. this._rotateAmount = 0
  18. this.type = Overlay.getOverlayType('model')
  19. this._state = State.INITIALIZED
  20. }
  21. set position(position) {
  22. this._position = Parse.parsePosition(position)
  23. this._delegate.position = Transform.transformWGS84ToCartesian(
  24. this._position
  25. )
  26. if (this._rotateAmount === 0) {
  27. this._delegate.orientation = Cesium.Transforms.headingPitchRollQuaternion(
  28. Transform.transformWGS84ToCartesian(this._position),
  29. new Cesium.HeadingPitchRoll(
  30. Cesium.Math.toRadians(this._position.heading),
  31. Cesium.Math.toRadians(this._position.pitch),
  32. Cesium.Math.toRadians(this._position.roll)
  33. )
  34. )
  35. }
  36. return this
  37. }
  38. get position() {
  39. return this._position
  40. }
  41. set modelUrl(modelUrl) {
  42. this._modelUrl = modelUrl
  43. this._delegate.model.uri = this._modelUrl
  44. return this
  45. }
  46. get modelUrl() {
  47. return this._modelUrl
  48. }
  49. set rotateAmount(amount) {
  50. this._rotateAmount = +amount
  51. this._delegate.orientation = new Cesium.CallbackProperty(time => {
  52. this._position.heading += this._rotateAmount
  53. if (this._position.heading >= 360 || this._position.heading <= -360) {
  54. this._position.heading = 0
  55. }
  56. return Cesium.Transforms.headingPitchRollQuaternion(
  57. Transform.transformWGS84ToCartesian(this._position),
  58. new Cesium.HeadingPitchRoll(
  59. Cesium.Math.toRadians(this._position.heading),
  60. Cesium.Math.toRadians(this._position.pitch),
  61. Cesium.Math.toRadians(this._position.roll)
  62. )
  63. )
  64. })
  65. return this
  66. }
  67. get rotateAmount() {
  68. return this._rotateAmount
  69. }
  70. _mountedHook() {
  71. /**
  72. * set the location
  73. */
  74. this.position = this._position
  75. /**
  76. * initialize the Overlay parameter
  77. */
  78. this.modelUrl = this._modelUrl
  79. }
  80. /**
  81. * Sets style
  82. * @param style
  83. * @returns {Model}
  84. */
  85. setStyle(style) {
  86. if (!style || Object.keys(style).length === 0) {
  87. return this
  88. }
  89. delete style['uri']
  90. this._style = style
  91. Util.merge(this._delegate.model, this._style)
  92. return this
  93. }
  94. /**
  95. * Parse from entity
  96. * @param entity
  97. * @param modelUrl
  98. * @returns {Model}
  99. */
  100. static fromEntity(entity, modelUrl) {
  101. let now = Cesium.JulianDate.now()
  102. let position = Transform.transformCartesianToWGS84(
  103. entity.position.getValue(now)
  104. )
  105. let model = new Model(position, modelUrl)
  106. model.attr = {
  107. ...entity.properties.getValue(now)
  108. }
  109. return model
  110. }
  111. }
  112. Overlay.registerType('model')
  113. export default Model