【发布时间】:2015-12-29 14:11:27
【问题描述】:
我有来自 threejs 示例 [http://threejs.org/examples/#webgl_animation_cloth] 的代码示例,其中浮点值转换为 vec4。我在其他几个论坛上看到过这个逻辑,但没有解释。
- 有人能解释一下这个逻辑在做什么以及 256 的用途吗?我了解按位掩码和移位。
我也看过这个链接Packing float into vec4 - how does this code work?
它表示 vec4 最终将存储在 32 位 RGBA8 缓冲区中。
由于我们将深度值传递给颜色缓冲区,opengl 将如何知道如何处理它?-
此外,由于 vec4 有 4 个组件,每个 4 个字节使其成为 16 个字节,这使其成为 16 * 8 位,这如何适合 32 位 RGBA8?
vec4 pack_depth( const in float depth ) { const vec4 bit_shift = vec4( 256.0 * 256.0 * 256.0, 256.0 * 256.0,256.0,1.0); const vec4 bit_mask = vec4( 0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0); vec4 res = fract( depth * bit_shift ); res -= res.xxyz * bit_mask; return res; } void main() { vec4 pixel = texture2D( texture, vUV ); if ( pixel.a < 0.5 ) discard; gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z ); }
【问题讨论】: