如何检查铯实体是否可见/被遮挡

时间:2019-10-02 18:34:11

标签: cesium

给出一个实体,我试图确定在特定时刻/帧是否可以在屏幕上看到一个实体。

我能找到的最接近的是Entity.isVisible,但是它似乎只是显示的show属性。即使...,它也会返回true。

  • 透明度为0%
  • 实体超出框架范围(例如,在可见区域上方)
  • 实体被遮挡(例如,在地球的另一侧)

我也找不到任何将实体位置转换为视口坐标以测试其是否至少在相机视锥内的函数。

我有一个想法可以像这样Cesium.Cartesian3.distance(this.viewer.selectedEntity.position.getValue(Cesium.JulianDate.now()), this.viewer.camera.position);测量实体与相机之间的距离,但是显然可接受的距离需要基于相机的高度,FOV以及相机甚至朝那个方向看。我还没有能够使数学适用于该解决方案。

如何判断用户当前是否可以看到实体?

1 个答案:

答案 0 :(得分:0)

其中一种方法是在视锥体剔除中使用 BoundingSphere。然后检查实体的边界球是否在剔除体积中可见。

const viewer = new Cesium.Viewer('cesiumContainer');
const camera = viewer.camera;

const position = Cesium.Cartesian3.fromDegrees(23.9036, 54.8985);

const frustum = camera.frustum;
const cullingVolume = frustum.computeCullingVolume(
  camera.position,
  camera.direction,
  camera.up
);

const someEntity = viewer.entities.add({
    name: 'Some Point',
    position: position,
    point: {
        pixelSize: 14,
        color: Cesium.Color.GREEN
    }
});

// Bounding sphere of the entity.
let boundingSphere = new Cesium.BoundingSphere();
viewer.dataSourceDisplay.getBoundingSphere(someEntity, false, boundingSphere);

// Check if the entity is visible in the screen.
const intersection = cullingVolume.computeVisibility(boundingSphere);

console.log(intersection)
//  1: Cesium.Intersect.INSIDE
//  0: Cesium.Intersect.INTERSECTING
// -1: Cesium.Intersect.OUTSIDE

我也做了一个 Cesium Sandcastle example