@needle-tools/materialx
Version:
MaterialX material support for three.js and Needle Engine – render physically based MaterialX shaders in the browser via WebAssembly
65 lines (52 loc) • 1.81 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
}
}
export function isDevEnvironment() {
// check if we're in localhost or using an ip address
return window.location.hostname === "localhost" || /^\d{1,3}(\.\d{1,3}){3}$/.test(window.location.hostname);
}