UNPKG

@openhps/core

Version:

Open Hybrid Positioning System - Core component

52 lines (42 loc) 2.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _TSLBase = require("../../tsl/TSLBase.js"); // See: https://casual-effects.com/research/Wyman2017Hashed/index.html const ALPHA_HASH_SCALE = 0.05; // Derived from trials only, and may be changed. const hash2D = /*@__PURE__*/(0, _TSLBase.Fn)(([value]) => { return (0, _TSLBase.fract)((0, _TSLBase.mul)(1.0e4, (0, _TSLBase.sin)((0, _TSLBase.mul)(17.0, value.x).add((0, _TSLBase.mul)(0.1, value.y)))).mul((0, _TSLBase.add)(0.1, (0, _TSLBase.abs)((0, _TSLBase.sin)((0, _TSLBase.mul)(13.0, value.y).add(value.x)))))); }); const hash3D = /*@__PURE__*/(0, _TSLBase.Fn)(([value]) => { return hash2D((0, _TSLBase.vec2)(hash2D(value.xy), value.z)); }); const getAlphaHashThreshold = /*@__PURE__*/(0, _TSLBase.Fn)(([position]) => { // Find the discretized derivatives of our coordinates const maxDeriv = (0, _TSLBase.max)((0, _TSLBase.length)((0, _TSLBase.dFdx)(position.xyz)), (0, _TSLBase.length)((0, _TSLBase.dFdy)(position.xyz))); const pixScale = (0, _TSLBase.float)(1).div((0, _TSLBase.float)(ALPHA_HASH_SCALE).mul(maxDeriv)).toVar('pixScale'); // Find two nearest log-discretized noise scales const pixScales = (0, _TSLBase.vec2)((0, _TSLBase.exp2)((0, _TSLBase.floor)((0, _TSLBase.log2)(pixScale))), (0, _TSLBase.exp2)((0, _TSLBase.ceil)((0, _TSLBase.log2)(pixScale)))); // Compute alpha thresholds at our two noise scales const alpha = (0, _TSLBase.vec2)(hash3D((0, _TSLBase.floor)(pixScales.x.mul(position.xyz))), hash3D((0, _TSLBase.floor)(pixScales.y.mul(position.xyz)))); // Factor to interpolate lerp with const lerpFactor = (0, _TSLBase.fract)((0, _TSLBase.log2)(pixScale)); // Interpolate alpha threshold from noise at two scales const x = (0, _TSLBase.add)((0, _TSLBase.mul)(lerpFactor.oneMinus(), alpha.x), (0, _TSLBase.mul)(lerpFactor, alpha.y)); // Pass into CDF to compute uniformly distrib threshold const a = (0, _TSLBase.min)(lerpFactor, lerpFactor.oneMinus()); const cases = (0, _TSLBase.vec3)(x.mul(x).div((0, _TSLBase.mul)(2.0, a).mul((0, _TSLBase.sub)(1.0, a))), x.sub((0, _TSLBase.mul)(0.5, a)).div((0, _TSLBase.sub)(1.0, a)), (0, _TSLBase.sub)(1.0, (0, _TSLBase.sub)(1.0, x).mul((0, _TSLBase.sub)(1.0, x)).div((0, _TSLBase.mul)(2.0, a).mul((0, _TSLBase.sub)(1.0, a))))); // Find our final, uniformly distributed alpha threshold (ατ) const threshold = x.lessThan(a.oneMinus()).select(x.lessThan(a).select(cases.x, cases.y), cases.z); // Avoids ατ == 0. Could also do ατ =1-ατ return (0, _TSLBase.clamp)(threshold, 1.0e-6, 1.0); }).setLayout({ name: 'getAlphaHashThreshold', type: 'float', inputs: [{ name: 'position', type: 'vec3' }] }); var _default = exports.default = getAlphaHashThreshold;