PackedRGBALib top-level constant

String const PackedRGBALib

Implementation

const String PackedRGBALib = """
// r,g,b,a  are in the range of [0, 254]
// float = r / 255^1 + g / 255^2 + b / 255^3 + a / 255^4
// float is assumed to be in [0, 1]
// Not that the conversion from bytes to floats introduces a 1/255 factor
// Inspired by http://spidergl.org/example.php?id=6

// 256.0 does not work quite as well.
const float _b = 255.0;
const vec4 _shift = vec4(1.0, _b, _b * _b, _b * _b * _b);
const vec4 _shiftInv = vec4(1.0, 1.0 / _b, 1.0 / (_b * _b), 1.0 / (_b * _b * _b));

vec4 packDepth(float depth) {
	  vec4 res = fract(depth * _shift);
    // the next three correction terms can probably be omitted if we
    // know for sure that we are dealing with 8 bits per color component
    res.r -= res.g / _b;
    res.g -= res.b / _b;
    res.b -= res.a / _b;
	  return res;
}


float unpackDepth(vec4 rgba_depth) {
	  return dot(rgba_depth, _shiftInv);
}

""";