UNPKG

molstar

Version:

A comprehensive macromolecular library.

45 lines 2.35 kB
"use strict"; /** * Copyright (c) 2019-2020 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author Alexander Rose <alexander.rose@weirdbyte.de> */ Object.defineProperty(exports, "__esModule", { value: true }); exports.unpackRGBAToDepth = exports.decodeFloatRGB = exports.encodeFloatRGBtoArray = exports.decodeFloatLog = exports.encodeFloatLog = void 0; var interpolate_1 = require("../mol-math/interpolate"); var approx_1 = require("../mol-math/approx"); var linear_algebra_1 = require("../mol-math/linear-algebra"); var maxFloat = 10000.0; // NOTE same constant is set in shaders var floatLogFactor = (0, approx_1.fasterLog)(maxFloat + 1); /** encode float logarithmically */ function encodeFloatLog(value) { return (0, approx_1.fasterLog)(value + 1) / floatLogFactor; } exports.encodeFloatLog = encodeFloatLog; /** decode logarithmically encoded float */ function decodeFloatLog(value) { return (0, approx_1.fasterExp)(value * floatLogFactor) - 1; } exports.decodeFloatLog = decodeFloatLog; /** encode float as rgb triplet into array at offset */ function encodeFloatRGBtoArray(value, array, offset) { value = (0, interpolate_1.clamp)(value, 0, 16777216 - 1) + 1; array[offset + 2] = value % 256; value = Math.floor(value / 256); array[offset + 1] = value % 256; value = Math.floor(value / 256); array[offset] = value % 256; return array; } exports.encodeFloatRGBtoArray = encodeFloatRGBtoArray; /** decode float encoded as rgb triplet */ function decodeFloatRGB(r, g, b) { return (Math.floor(r) * 256 * 256 + Math.floor(g) * 256 + Math.floor(b)) - 1; } exports.decodeFloatRGB = decodeFloatRGB; var UnpackDownscale = 255 / 256; // 0..1 -> fraction (excluding 1) var PackFactors = linear_algebra_1.Vec3.create(256 * 256 * 256, 256 * 256, 256); var UnpackFactors = linear_algebra_1.Vec4.create(UnpackDownscale / PackFactors[0], UnpackDownscale / PackFactors[1], UnpackDownscale / PackFactors[2], UnpackDownscale / 1); var tmpDepthRGBA = (0, linear_algebra_1.Vec4)(); function unpackRGBAToDepth(r, g, b, a) { linear_algebra_1.Vec4.set(tmpDepthRGBA, r / 255, g / 255, b / 255, a / 255); return linear_algebra_1.Vec4.dot(tmpDepthRGBA, UnpackFactors); } exports.unpackRGBAToDepth = unpackRGBAToDepth; //# sourceMappingURL=float-packing.js.map