@needle-tools/materialx
Version:
Web runtime support to load and display MaterialX materials in Needle Engine and three.js via the MaterialX WebAssembly library. glTF files containing the `NEEDLE_materials_mtlx` extension can be loaded with this package. There is also experimental suppor
59 lines (48 loc) • 1.6 kB
JavaScript
// import { BufferGeometry } from 'three';
// import * as BufferGeometryUtils from 'three/examples/jsm/utils/BufferGeometryUtils.js';
/**
* Get URL parameter value
* @param {string} name - Parameter name
* @returns {boolean|string} Parameter value or false if not found
*/
export function getParam(name) {
const urlParams = new URLSearchParams(window.location.search);
const param = urlParams.get(name);
if (param == null || param === "0" || param === "false") return false;
if (param === "") return true;
return param;
}
export const debug = getParam("debugmaterialx");
export const debugUpdate = debug === "update";
let time = 0;
/**
* Get current time in seconds
* @returns {number} Current time
*/
export function getTime() {
return time;
}
let frame = 0;
/**
* Get current frame number
* @returns {number} Current frame
*/
export function getFrame() {
return frame;
}
const performance = window.performance || /** @type {any} */ (window).webkitPerformance || /** @type {any} */ (window).mozPerformance;
function updateTime() {
time = performance.now() / 1000; // Convert to seconds
frame++;
window.requestAnimationFrame(updateTime);
}
window.requestAnimationFrame(updateTime);
export async function waitForNetworkIdle() {
if (typeof requestIdleCallback !== "undefined") {
return new Promise(res => requestIdleCallback(res));
}
else {
console.debug("[MaterialX] Can not wait for network idle, using fallback");
return new Promise(res => setTimeout(res, 100)); // Fallback to a short delay
}
}