const float PackUpscale = 256. / 255.;
const float UnpackDownscale = 255. / 256.;
const vec3 PackFactors = vec3( 256. * 256. * 256..256. * 256..256. );
const vec4 UnpackFactors = UnpackDownscale / vec4( PackFactors, 1. );
const float ShiftRight8 = 1. / 256.;
vec4 packDepthToRGBA( const in float v ) {
    vec4 r = vec4( fract( v * PackFactors ), v );
    r.yzw -= r.xyz * ShiftRight8;
    return r * PackUpscale;
}
float unpackRGBAToDepth( const in vec4 v ) {
    return dot( v, UnpackFactors );
}
vec4 pack2HalfToRGBA( vec2 v ) {
    vec4 r = vec4( v.x, fract( v.x * 255.0 ), v.y, fract( v.y * 255.0 ));
    return vec4( r.x - r.y / 255.0, r.y, r.z - r.w / 255.0, r.w);
}
vec2 unpackRGBATo2Half( vec4 v ) {
    return vec2( v.x + ( v.y / 255.0 ), v.z + ( v.w / 255.0)); }Copy the code

To obtain the original depth value in the Display Fragment Shader, use the following methods:

  • The above operation can be passeddot()GLSL built-in functions complete:
vec4 packDepth(float depth) {
    const vec4 bitShift = vec4(1.0.255.0.255.0 * 255.0.255.0 * 255.0 * 255.0);
    const vec4 bitMask = vec4(1.0/255.0.1.0/255.0.1.0/255.0.0.0);
    vec4 rgbaDepth = fract(depth * bitShift);
    rgbaDepth -= rgbaDepth.gbaa * bitMask;
	return rgbaDepth;
}

float unpackDepth(const in vec4 rgbaDepth) {
    const vec4 bitShift = vec4(1.0.1.0/255.0.1.0/ (255.0 * 255.0), 1.0/ (255.0*255.0*255.0));
    float depth = dot(rgbaDepth, bitShift);
    return depth;
}
Copy the code