Skip to content

Instantly share code, notes, and snippets.

@cvan
Created February 20, 2019 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cvan/3d61a3e698add063c53e387b5de83743 to your computer and use it in GitHub Desktop.
Save cvan/3d61a3e698add063c53e387b5de83743 to your computer and use it in GitHub Desktop.
a helpful function for retrieving debug details for a user's capabilities, libraries used, and perf stats for WebGL in a WebVR / WebXR web page
function WebGameDebugInfo () {
let canvas;
const stats = {};
const info = {
supports: {
webxr: 'hasNativeWebXRImplementation' in window ? window.hasNativeWebXRImplementation : !!navigator.xr,
webvr: 'hasNativeWebVRImplementation' in window ? window.hasNativeWebVRImplementation : !!navigator.vr
},
libs: {
aframe: null,
three: null,
playcanvas: null,
polyfill: null
}
};
if (window.THREE) {
info.libs.three = {
revision: window.THREE.revision
};
}
if (window.AFRAME) {
canvas = window.AFRAME.scenes ? AFRAME.scenes[0].canvas : document.querySelector('a-scene').canvas;
info.supports.webxr = 'hasNativeWebXRImplementation' in window ? window.hasNativeWebXRImplementation : !!navigator.xr;
info.supports.webvr = 'hasNativeWebVRImplementation' in window ? window.hasNativeWebVRImplementation : !!navigator.vr;
info.libs.aframe = {
version: window.AFRAME.version
};
}
const polyfill = window.webvrpolyfill && window.webvrpolyfill.constructor.version ? window.webvrpolyfill : window.WebVRPolyfill;
if (polyfill) {
if ('hasNative' in polyfill) {
info.supports.webvr = polyfill.hasNative;
}
info.libs.polyfill = {
version: polyfill.version || (polyfill.constructor && polyfill.constructor.version) || null,
config: polyfill.config || null,
hasNative: polyfill.hasNative || null,
enabled: polyfill.enabled || null
};
}
try {
stats.aframe = window.aframeStats();
} catch (err) {
}
try {
stats.browser = window.BrowserStats();
} catch (err) {
}
try {
stats.gl = window.glStats();
} catch (err) {
}
try {
stats.three = window.threeStats();
} catch (err) {
}
if (window.pc) {
info.libs.playcanvas = {
version: window.pc.version,
revision: window.pc.revision
};
if (pc.VrManager && 'usesPolyfill' in pc.VrManager) {
if (!info.libs.polyfill) {
info.libs.polyfill = {};
}
info.libs.polyfill.enabled = pc.VrManager.usesPolyfill;
}
}
if (window.BABYLON) {
info.libs.babylon = {
version: (window.BABYLON.Engine && BABYLON.Engine.Version) || null
};
}
canvas = canvas || document.querySelector('canvas');
try {
const gl = canvas.getContext('webgl');
const debugInfo = gl.getExtension('WEBGL_debug_renderer_info');
info.glRenderer = {
vendor: gl.getParameter(debugInfo.UNMASKED_VENDOR_WEBGL),
render: gl.getParameter(debugInfo.UNMASKED_RENDERER_WEBGL)
};
} catch (err) {
// Reading the WebGL context for `WEBGL_debug_renderer_info` in Firefox can fail if a
// user has set in `about:config` the `privacy.resistFingerprinting` pref to `true`.
// Details: https://developer.mozilla.org/en-US/docs/Web/API/WEBGL_debug_renderer_info#wikiArticle
}
info.stats = stats;
return info;
}
if (window) {
window.WebGameDebugInfo = WebGameDebugInfo;
window.webGameDebugInfo = new window.WebGameDebugInfo();
} else if (module && module.exports) {
module.exports.WebGameDebugInfo = WebGameDebugInfo;
module.exports.webGameDebugInfo = new module.exports.WebGameDebugInfo();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment