Cesium 1.107+ 自定义类支持 readyPromise

由于cesium 1.107+ 的图元(Primitive) 已经不支持 readyPromise。

Cesium 1.107+ 自定义类支持 readyPromise_第1张图片

但是个人感觉比较好用,于是用了一个插件来实现。

用法:

// 定义图元并添加,和之前一样
const boxGreen = new BoxPrimitive({
  color: "#ff0000"
})
viewer.scene.primitives.add(boxGreen.primitive)

// 这个是新增的,创建一个 readyPromise 辅助器
const readyHelper = new ReadyPromiseHelper(viewer)
readyHelper.readyPromise(boxGreen.primitive).then(() => {
  console.log("primitive is ready");
})

ReadyPromiseHelper 实现

export class ReadyPromiseHelper {
  constructor(viewer) {
    this.viewer = viewer;

    this._primitives = new Map();

    viewer.scene.postRender.addEventListener(() => {
      const needDelete = []
      this._primitives.forEach((readyCallback, primitive) => {
        if (primitive.ready) {
          readyCallback()
          needDelete.push(primitive)
        }
      })

      needDelete.forEach(primitive => {
        this._primitives.delete(primitive)
      })
    })
  }

  readyPromise(primitive) {
    let readyCallback;

    const promise = new Promise((resolve, reject) => {
      readyCallback = resolve;
    });
    this._primitives.set(primitive, readyCallback)
    return promise;
  }
}

你可能感兴趣的:(cesium原理,前端,javascript,开发语言,cesium.js)