1、float深度值转rgba纹素值
cesium中将float转换为rgba主要是为了解决显卡不支持float类型纹理的问题,例如将像素着色器中的深度提取出来,由于在opengl的规范当中将pixel shader中的数据提取出来的方法是保存成像素值,而低端的显卡硬件都支持将像素保存成rgba格式的[0~1]范围的值,这就需要将float转换成rgba的方式进行渲染,而后在将这个渲染出的结果中还原原来的float数据。
cesium中glsl相关的代码是:
/**
* Unpacks a vec4 depth value to a float in [0, 1) range.
*
* @name czm_unpackDepth
* @glslFunction
*
* @param {vec4} packedDepth The packed depth.
*
* @returns {float} The floating-point depth in [0, 1) range.
*/
float czm_unpackDepth(vec4 packedDepth)
{
// See Aras Pranckevičius' post Encoding Floats to RGBA
// http://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/
return dot(packedDepth, vec4(1.0, 1.0 / 255.0, 1.0 / 65025.0, 1.0 / 16581375.0));
}
/**
* Packs a depth value into a vec3 that can be represented by unsigned bytes.
*
* @name czm_packDepth
* @glslFunction
*
* @param {float} depth The floating-point depth.
* @returns {vec3} The packed depth.
*/
vec4 czm_packDepth(float depth)
{
// See Aras Pranckevičius' post Encoding Floats to RGBA
// http://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/
vec4 enc = vec4(1.0, 255.0, 65025.0, 16581375.0) * depth;
enc = fract(enc);
enc -= enc.yzww * vec4(1.0 / 255.0, 1.0 / 255.0, 1.0 / 255.0, 0.0);
return enc;
}
整个计算过程如下:
1 将float转换为rbga的过程vec4 czm_packDepth(float depth)如下:
1.1 假设depth为深度值,这个深度值是介于(0~1)开区间之间的,对于不在这个范围内的值可

文章讲述了在Cesium中如何将浮点深度值转换为RGBA格式来适应GPU渲染,以及对UV坐标的压缩方法。通过编码和解码函数,浮点深度值在[0,1]范围内被转换并存储在RGBA纹理中,而UV坐标则被压缩到单个浮点数,保留12位精度。这些技术用于优化地形数据的存储和传输。
最低0.47元/天 解锁文章
2546

被折叠的 条评论
为什么被折叠?



