简体   繁体   English

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

[英]How to check if a Cesium Entity is visible / occluded

Given an Entity, I'm trying to figure out if at a particular moment/frame, if one of the entities is viewable on the screen.给定一个实体,我试图确定是否在特定时刻/帧,如果其中一个实体在屏幕上可见。

The closest I could find was Entity.isVisible, but it seems to just tally the explicit show properties.我能找到的最接近的是 Entity.isVisible,但它似乎只是计算显式show属性。 It returns true even if...它返回true,即使...

  • Transparency is 0%透明度为 0%
  • The Entity is out of frame (eg. above the viewable area)实体超出框架(例如,在可视区域上方)
  • The Entity is occluded (eg. on the other side of the Earth)实体被遮挡(例如在地球的另一边)

I also can't find any functions to convert the entity position to viewport coordinates to test if it is at least within the camera view cone.我也找不到任何函数将实体 position 转换为视口坐标以测试它是否至少在相机视锥内。

I had one idea to measure the distance between the entity and the camera kinda like this Cesium.Cartesian3.distance(this.viewer.selectedEntity.position.getValue(Cesium.JulianDate.now()), this.viewer.camera.position);我有一个想法来测量实体和相机之间的距离,有点像这个Cesium.Cartesian3.distance(this.viewer.selectedEntity.position.getValue(Cesium.JulianDate.now()), this.viewer.camera.position); But obviously the acceptable distance needs to be based on the camera height, FOV and if the camera is even looking in that direction.但显然,可接受的距离需要基于摄像机高度、FOV 以及摄像机是否朝那个方向看。 I haven't yet been able to get the math to work for this solution.我还不能让数学为这个解决方案工作。

How can I tell if an Entity is currently visible to the user?如何判断实体当前是否对用户可见?

One of the ways is to use BoundingSphere in the frustum culling.其中一种方法是在截锥体剔除中使用BoundingSphere Then check if the bounding sphere of the entity is visible in the culling volume.然后检查实体的边界球是否在剔除体积中可见。

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

I have also made a Cesium Sandcastle example .我还做了一个铯沙堡的例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM