cesium中有时会用到每个像素代表的米数量这个计算,在metersPerPixel.glsl文件中,例如:当计算裁切时,在裁切面的裁切边缘上想要看到边界,而边界占用几个像素就可以通过这个计算公式得出。
float czm_metersPerPixel(vec4 positionEC, float pixelRatio) {
float width = czm_viewport.z;
float height = czm_viewport.w;
float pixelWidth;
float pixelHeight;
float top = czm_frustumPlanes.x;
float bottom = czm_frustumPlanes.y;
float left = czm_frustumPlanes.z;
float right = czm_frustumPlanes.w;
if (czm_sceneMode == czm_sceneMode2D || czm_orthographicIn3D == 1.0) {
float frustumWidth = right - left;
float frustumHeight = top - bottom;
pixelWidth = frustumWidth / width;
pixelHeight = frustumHeight / height;
}
else {
float distanceToPixel = -positionEC.z;
float inverseNear = 1.0 / czm_currentFrustum.x;
float tanTheta = top * inverseNear;
pixelHeight = 2.0 * distanceToPixel * tanTheta / height;
tanTheta = right * inverseNear;
pixelWidth = 2.0 * distanceToPixel * tanTheta / width;
}
return max(pixelWidth, pixelHeight) * pixelRatio;
}
1、计算视锥中近裁切面中的宽度和高度(一般会设置于视口相同的大小,单位是像素);
2、计算当前像素点距离相机的实际距离(单位是米),并通过视锥的视野(视角范围)计算出当前像素距离处的宽度和高度(单位是米);
3、将步骤1中计算的宽高与步骤2中计算的结果进行比较,取最大的即可得到结果。