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.

ModelPrimitive.js 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * @Author: Caven
  3. * @Date: 2021-05-14 00:33:27
  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 ModelPrimitive extends Overlay {
  12. constructor(position, modelUrl) {
  13. super()
  14. this._position = Parse.parsePosition(position)
  15. this._modelUrl = modelUrl
  16. this._delegate = Cesium.Model.fromGltf({ url: modelUrl })
  17. this.type = Overlay.getOverlayType('model_primitive')
  18. this._state = State.INITIALIZED
  19. }
  20. set position(position) {
  21. this._position = Parse.parsePosition(position)
  22. let origin = Transform.transformWGS84ToCartesian(this._position)
  23. let modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(origin)
  24. let hprMatrix = Cesium.Transforms.headingPitchRollToFixedFrame(
  25. origin,
  26. new Cesium.HeadingPitchRoll(
  27. Cesium.Math.toRadians(this._position.heading),
  28. Cesium.Math.toRadians(this._position.pitch),
  29. Cesium.Math.toRadians(this._position.roll)
  30. )
  31. )
  32. Cesium.Matrix4.multiply(modelMatrix, hprMatrix, modelMatrix)
  33. this._delegate.modelMatrix = modelMatrix
  34. return this
  35. }
  36. get position() {
  37. return this._position
  38. }
  39. set modelUrl(modelUrl) {
  40. this._modelUrl = modelUrl
  41. this._delegate = Cesium.Model.fromGltf({ url: modelUrl })
  42. this.position = this._position
  43. return this
  44. }
  45. get modelUrl() {
  46. return this._modelUrl
  47. }
  48. /**
  49. *
  50. * @private
  51. */
  52. _mountedHook() {
  53. /**
  54. * set the location
  55. */
  56. this.position = this._position
  57. }
  58. /**
  59. * Sets style
  60. * @param style
  61. * @returns {ModelPrimitive}
  62. */
  63. setStyle(style) {
  64. if (!style || Object.keys(style).length === 0) {
  65. return this
  66. }
  67. this._style = style
  68. Util.merge(this._delegate, this._style)
  69. return this
  70. }
  71. }
  72. Overlay.registerType('model_primitive')
  73. export default ModelPrimitive