Cesium杂谈一:Cesium中的remove

Cesium中的remove

在Cesium中,清除实体Entity的方法主要是在Cesium.viewer.entities实体集合中调用remove()removeById(id)removeAll()

一、remove()方法

1、用法

从集合中删除一个实体。

viewer.entities.remove(entity)

2、源码

EntityCollection.prototype.remove = function (entity) {
  // 如果实体未定义,则返回 false
  if (!defined(entity)) {
    return false;
  }
  // 调用 removeById 方法移除实体
  return this.removeById(entity.id);
};

二、removeById(id)方法

1、用法

移除指定ID的Entity

viewer.entities.removeById(entityId)

2、源码

EntityCollection.prototype.removeById = function (id) {
  // 如果 id 未定义,则返回 false
  if (!defined(id)) {
    return false;
  }
    
  // 从实体集合中移除具有指定 id 的实体
  const entities = this._entities;
  const entity = entities.get(id);
  if (!this._entities.remove(id)) {
    return false;
  }

  // 从 _addedEntities 中移除实体,如果不存在则将实体添加到 _removedEntities
  if (!this._addedEntities.remove(id)) {
    this._removedEntities.set(id, entity);
    this._changedEntities.remove(id);
  }
  this._entities.remove(id);
  // 移除监听实体定义更改的事件处理函数
  entity.definitionChanged.removeEventListener(
    EntityCollection.prototype._onEntityDefinitionChanged,
    this
  );
  // 触发集合变更事件
  fireChangedEvent(this);

  return true;
};

三、removeAll()

1、用法

移除所有实体

viewer.entities.removeAll();

2、源码

EntityCollection.prototype.removeAll = function () {
  // 从 _entities 中移除所有实体
  // 只包含在事件暂停前添加的项目和集合的内容。
  const entities = this._entities;
  const entitiesLength = entities.length;
  const array = entities.values;

  const addedEntities = this._addedEntities;
  const removed = this._removedEntities;

  for (let i = 0; i < entitiesLength; i++) {
    // 对于每个实体,如果它是在添加实体集合中不存在的实体,则将其从集合中移除
    const existingItem = array[i];
    const existingItemId = existingItem.id;
    const addedItem = addedEntities.get(existingItemId);
    if (!defined(addedItem)) {
      existingItem.definitionChanged.removeEventListener(
        EntityCollection.prototype._onEntityDefinitionChanged,
        this
      );
      removed.set(existingItemId, existingItem);
    }
  }

  // 移除所有实体
  entities.removeAll();
  addedEntities.removeAll();
  this._changedEntities.removeAll();
  // 触发集合变更事件
  fireChangedEvent(this);
};

在Cesium中,图元Primitive是最基本单位,所以可以从webGL层面销毁Primitive,也可以从集合层面移除所有Primitive

四、destroy()—Primitive层面

1、用法

销毁该对象持有的 WebGL 资源。销毁对象可以确定性地释放 WebGL 资源,而不是依赖垃圾收集器来销毁该对象。

一旦对象被销毁,就不应再使用;调用除此之外的任何函数 isDestroyed都会导致DeveloperError异常。因此,将返回值 ( undefined) 分配给该对象,

primitiveTest = new Cesium.Primitive()
primitiveTest.destroy()

2、源码

Primitive.prototype.destroy = function () {
  let length;
  let i;

  // 销毁渲染时的着色器程序和深度测试失败时的着色器程序
  this._sp = this._sp && this._sp.destroy();
  this._spDepthFail = this._spDepthFail && this._spDepthFail.destroy();

  // 销毁顶点数组
  const va = this._va;
  length = va.length;
  for (i = 0; i < length; ++i) {
    va[i].destroy();
  }
  this._va = undefined;

  // 销毁拾取 ID
  const pickIds = this._pickIds;
  length = pickIds.length;
  for (i = 0; i < length; ++i) {
    pickIds[i].destroy();
  }
  this._pickIds = undefined;

  // 销毁批处理表和实例 ID
  this._batchTable = this._batchTable && this._batchTable.destroy();
  this._instanceIds = undefined;
  this._perInstanceAttributeCache = undefined;
  this._attributeLocations = undefined;

  // 最后销毁 Primitive 对象本身
  return destroyObject(this);
};

五、remove()

1、用法

从集合中删除一个Primitive

const billboards = scene.primitives.add(new Cesium.BillboardCollection());
scene.primitives.remove(billboards);  // Returns true

2、源码

PrimitiveCollection.prototype.remove = function (primitive) {
  // 如果 PrimitiveCollection 包含指定的 primitive
  if (this.contains(primitive)) {
    // 获取 primitive 在 _primitives 数组中的索引
    const index = this._primitives.indexOf(primitive);
    if (index !== -1) {
      // 从 _primitives 数组中移除指定的 primitive
      this._primitives.splice(index, 1);

      // 删除 primitive 在 _external._composites 中的引用
      delete primitive._external._composites[this._guid];

      // 如果设置了 destroyPrimitives 属性,销毁 primitive
      if (this.destroyPrimitives) {
        primitive.destroy();
      }

      // 触发 primitiveRemoved 事件
      this._primitiveRemoved.raiseEvent(primitive);

      return true;
    }
  }

  return false;
};

六、removeAll()

1、用法

移除集合中所有Primitive

scene.primitives.removeAll(); 

2、源码

PrimitiveCollection.prototype.removeAll = function () {
  const primitives = this._primitives;
  const length = primitives.length;
  // 遍历所有的 Primitive 对象
  for (let i = 0; i < length; ++i) {
    // 删除 Primitive 对象在 _external._composites 中的引用
    delete primitives[i]._external._composites[this._guid];

    // 如果设置了 destroyPrimitives 属性,销毁 Primitive 对象
    if (this.destroyPrimitives) {
      primitives[i].destroy();
    }

    // 触发 primitiveRemoved 事件
    this._primitiveRemoved.raiseEvent(primitives[i]);
  }
  // 清空 _primitives 数组
  this._primitives = [];
};

七、destroy()—PrimitiveCollection层面

1、用法

销毁该集合中每个图元所持有的 WebGL 资源。显式销毁此集合可以确定性地释放 WebGL 资源,而不是依赖垃圾收集器来销毁此集合。

由于销毁集合会销毁所有包含的基元,因此只有在确定没有其他代码仍在使用任何包含的基元时才销毁集合。

一旦该集合被销毁,则不应再使用;调用除此之外的任何函数 isDestroyed都会导致DeveloperError异常。

primitives = primitives && primitives.destroy();

2、源码

PrimitiveCollection.prototype.destroy = function () {
  this.removeAll();
  return destroyObject(this);
};

注意:

不要轻易使用PrimitiveCollectionremoveAll()destroy(),因为Entity也是会解析转换为Primitive,不正确使用会导致移除其他单元的Primitive

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CESIUM EPSG::900913 是指Cesium常用的投影坐标系,也被称为Web墨卡托投影(Web Mercator)。该投影使用了EPSG:3857的代码。它是一种等距投影,用于在Web地图显示经纬度坐标。Web墨卡托投影将地球表面投影到一个平面上,使得在地图上的距离与实际距离近似相等,从而方便进行地理数据的可视化和分析。 要在Cesium使用EPSG::900913投影,您可以在代码使用相应的坐标系转换库(例如proj4)来将其他坐标系转换为EPSG::900913。您还可以查找提供的地理数据文件(如geojson或json文件)的"crs"字段,查找与EPSG::900913或EPSG::3857相关的字段名称。 请注意,虽然EPSG::900913常用于Web地图应用程序,但它实际上是一个非正式的投影代码,因此更推荐使用EPSG::3857来表示Web墨卡托投影。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Cesium出现Unknown crs name: urn:ogc:def:crs:EPSG:xxxx](https://blog.csdn.net/weixin_43972992/article/details/127066824)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [ol-cesium:OpenLayers-Cesium集成](https://download.csdn.net/download/weixin_42121086/14949890)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值