Packing a float value in RGBA
Here’s the way to pack a float value in a RGBA8 texture. You should try to use float textures instead but if that’s not possible then this might be a good work around.
// Packing a [0-1] float value into a 4D vector where each component will be a 8-bits integer vec4 packFloatToVec4i(const float value) { const vec4 bitSh = vec4(256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0); const vec4 bitMsk = vec4(0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0); vec4 res = fract(value * bitSh); res -= res.xxyz * bitMsk; return res; } // Unpacking a [0-1] float value from a 4D vector where each component was a 8-bits integer float unpackFloatFromVec4i(const vec4 value) { const vec4 bitSh = vec4(1.0 / (256.0 * 256.0 * 256.0), 1.0 / (256.0 * 256.0), 1.0 / 256.0, 1.0); return(dot(value, bitSh)); } |
The source was posted on the GameDev forum:
http://www.gamedev.net/community/forums/topic.asp?topic_id=463075&whichpage=1�
3 Comments »
RSS feed for comments on this post. TrackBack URI





















[...] Repost gamerendering blog [...]
are you sure that 256 is the correct base for packing/unpacking?
I have not verified it mathematically. But I’ve used it in some implementations successfully.