| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- /**
- * @Author: Caven
- * @Date: 2021-08-02 20:12:04
- */
-
- import { Cesium } from '@dc-modules/namespace'
- import State from '@dc-modules/state/State'
- import Parse from '@dc-modules/parse/Parse'
- import { Transform } from '@dc-modules/transform'
- import Overlay from '../Overlay'
-
- class ModelCollectionPrimitive extends Overlay {
- constructor(positions, modelUrl) {
- super()
- this._positions = Parse.parsePositions(positions)
- this._modelUrl = modelUrl
- this._attrs = []
- this._state = State.INITIALIZED
- }
-
- get type() {
- return Overlay.getOverlayType('model_collection_primitive')
- }
-
- get readyPromise() {
- return this._delegate.readyPromise
- }
-
- set attrs(attrs) {
- this._attrs = attrs
- return this
- }
-
- get attrs() {
- return this._attrs
- }
-
- set positions(positions) {
- this._positions = Parse.parsePositions(positions)
- if (this._layer) {
- this._resetDelegate()
- this._layer.delegate.add(this._delegate)
- }
- return this
- }
-
- set modelUrl(modelUrl) {
- this._modelUrl = modelUrl
- if (this._layer) {
- this._resetDelegate()
- this._layer.delegate.add(this._delegate)
- }
- return this
- }
-
- get modelUrl() {
- return this._modelUrl
- }
-
- _resetDelegate() {
- this._delegate = this._delegate && this._delegate.destroy()
- this._delegate = new Cesium.ModelInstanceCollection({
- url: this._modelUrl,
- instances: this._positions.map(item => {
- let origin = Transform.transformWGS84ToCartesian(item)
- let modelMatrix = Cesium.Transforms.headingPitchRollToFixedFrame(
- origin,
- new Cesium.HeadingPitchRoll(
- Cesium.Math.toRadians(item.heading),
- Cesium.Math.toRadians(item.pitch),
- Cesium.Math.toRadians(item.roll)
- )
- )
- this._style?.scale &&
- Cesium.Matrix4.multiplyByUniformScale(
- modelMatrix,
- this._style?.scale,
- modelMatrix
- )
- return {
- modelMatrix
- }
- }),
- ...this._style
- })
- this._delegate.layerId = this._layer?.layerId
- this._delegate.overlayId = this._id
- }
-
- _mountedHook() {
- this._resetDelegate()
- }
-
- /**
- *
- * @param instanceId
- * @returns {undefined}
- */
- getModelInstance(instanceId) {
- return this._delegate._instances[instanceId] || undefined
- }
-
- /**
- *
- * @param instanceId
- * @returns {*|{}}
- */
- getAttrByInstanceId(instanceId) {
- return this._attrs[instanceId] || {}
- }
-
- /**
- *
- * @param style
- * @returns {ModelCollectionPrimitive}
- */
- setStyle(style) {
- if (!style || Object.keys(style).length === 0) {
- return this
- }
- delete style['instances'] && delete style['url']
- this._style = style
- if (this._layer) {
- this._resetDelegate()
- this._layer.delegate.add(this._delegate)
- }
- return this
- }
- }
-
- Overlay.registerType('model_collection_primitive')
-
- export default ModelCollectionPrimitive
|