Browse Source

added CPU unpackRGBAToDepth

Alexander Rose 4 years ago
parent
commit
4cfbccc5d6
1 changed files with 16 additions and 0 deletions
  1. 16 0
      src/mol-util/float-packing.ts

+ 16 - 0
src/mol-util/float-packing.ts

@@ -6,6 +6,7 @@
 
 import { clamp } from '../mol-math/interpolate';
 import { fasterExp, fasterLog } from '../mol-math/approx';
+import { Vec3, Vec4 } from '../mol-math/linear-algebra';
 
 const maxFloat = 10000.0; // NOTE same constant is set in shaders
 const floatLogFactor = fasterLog(maxFloat + 1.0);
@@ -30,4 +31,19 @@ export function encodeFloatRGB(value: number) {
 /** decode float encoded as rgb triplet */
 export function decodeFloatRGB(r: number, g: number, b: number) {
     return (Math.floor(r) * 256 * 256 + Math.floor(g) * 256 + Math.floor(b)) - 1;
+}
+
+const UnpackDownscale = 255 / 256; // 0..1 -> fraction (excluding 1)
+const PackFactors = Vec3.create(256 * 256 * 256, 256 * 256,  256);
+const UnpackFactors = Vec4.create(
+    UnpackDownscale / PackFactors[0],
+    UnpackDownscale / PackFactors[1],
+    UnpackDownscale / PackFactors[2],
+    UnpackDownscale / 1
+);
+
+const tmpDepthRGBA = Vec4();
+export function unpackRGBAToDepth(r: number, g: number, b: number, a: number) {
+    Vec4.set(tmpDepthRGBA, r / 255, g / 255, b / 255, a / 255);
+    return Vec4.dot(tmpDepthRGBA, UnpackFactors);
 }