Skip to content

Instantly share code, notes, and snippets.

@mzgoddard
Created August 20, 2012 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mzgoddard/3405120 to your computer and use it in GitHub Desktop.
Save mzgoddard/3405120 to your computer and use it in GitHub Desktop.
// packing a float in glsl with multiplication and mod
vec4 packFloat( 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 );
// combination of mod and multiplication and division works better
vec4 res = mod(
depth * bit_shift * vec4( 255 ),
vec4( 256 ) ) / vec4( 255 );
res -= res.xxyz * bit_mask;
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment