Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ModelCollectionPrimitive.js 2.8KB

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