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.

ModelCollectionPrimitive.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * @Author: Caven
  3. * @Date: 2021-08-02 20:12:04
  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 ModelCollectionPrimitive extends Overlay {
  12. constructor(positions, modelUrl) {
  13. super()
  14. this._positions = Parse.parsePositions(positions)
  15. this._modelUrl = modelUrl
  16. this._attrs = []
  17. this._state = State.INITIALIZED
  18. }
  19. get type() {
  20. return Overlay.getOverlayType('model_collection_primitive')
  21. }
  22. get readyPromise() {
  23. return this._delegate.readyPromise
  24. }
  25. set attrs(attrs) {
  26. this._attrs = attrs
  27. return this
  28. }
  29. get attrs() {
  30. return this._attrs
  31. }
  32. set positions(positions) {
  33. this._positions = Parse.parsePositions(positions)
  34. if (this._layer) {
  35. this._layer.delegate.remove(this._delegate)
  36. this._resetDelegate()
  37. this._layer.delegate.add(this._delegate)
  38. }
  39. return this
  40. }
  41. set modelUrl(modelUrl) {
  42. this._modelUrl = modelUrl
  43. if (this._layer) {
  44. this._layer.delegate.remove(this._delegate)
  45. this._resetDelegate()
  46. this._layer.delegate.add(this._delegate)
  47. }
  48. return this
  49. }
  50. get modelUrl() {
  51. return this._modelUrl
  52. }
  53. _resetDelegate() {
  54. this._delegate = new Cesium.ModelInstanceCollection({
  55. url: this._modelUrl,
  56. instances: this._positions.map(item => {
  57. let origin = Transform.transformWGS84ToCartesian(item)
  58. let modelMatrix = Cesium.Transforms.headingPitchRollToFixedFrame(
  59. origin,
  60. new Cesium.HeadingPitchRoll(
  61. Cesium.Math.toRadians(item.heading),
  62. Cesium.Math.toRadians(item.pitch),
  63. Cesium.Math.toRadians(item.roll)
  64. )
  65. )
  66. this._style?.scale &&
  67. Cesium.Matrix4.multiplyByUniformScale(
  68. modelMatrix,
  69. this._style?.scale,
  70. modelMatrix
  71. )
  72. return {
  73. modelMatrix
  74. }
  75. }),
  76. ...this._style
  77. })
  78. this._delegate.layerId = this._layer?.layerId
  79. this._delegate.overlayId = this._id
  80. }
  81. _mountedHook() {
  82. this._resetDelegate()
  83. }
  84. /**
  85. *
  86. * @param instanceId
  87. * @returns {undefined}
  88. */
  89. getModelInstance(instanceId) {
  90. return this._delegate._instances[instanceId] || undefined
  91. }
  92. /**
  93. *
  94. * @param instanceId
  95. * @returns {*|{}}
  96. */
  97. getAttrByInstanceId(instanceId) {
  98. return this._attrs[instanceId] || {}
  99. }
  100. /**
  101. *
  102. * @param style
  103. * @returns {ModelCollectionPrimitive}
  104. */
  105. setStyle(style) {
  106. if (!style || Object.keys(style).length === 0) {
  107. return this
  108. }
  109. delete style['instances'] && delete style['url']
  110. Util.merge(this._style, style)
  111. if (this._layer) {
  112. this._resetDelegate()
  113. this._layer.delegate.add(this._delegate)
  114. }
  115. return this
  116. }
  117. }
  118. Overlay.registerType('model_collection_primitive')
  119. export default ModelCollectionPrimitive