197 lines
6.1 KiB
JavaScript
197 lines
6.1 KiB
JavaScript
/* StemEngine runtime bootstrap injected into Muon build at image build-time. */
|
|
|
|
(function stemengineBootstrap() {
|
|
const REQUIRED_EXTENSIONS = ['OES_texture_float_linear', 'EXT_color_buffer_float']
|
|
const OVERLAY_SELECTORS = [
|
|
'.dg',
|
|
'.dg.ac',
|
|
'.lil-gui',
|
|
'#stats',
|
|
'.stats',
|
|
'.fps',
|
|
'.fps-counter',
|
|
'.epok-info',
|
|
'.epok-main-controls',
|
|
'.epok-audio-controls',
|
|
'.epok-captured-overlay',
|
|
'.epok-edit-controls',
|
|
'.epok-controls',
|
|
'.epok-editor',
|
|
'.epok-ui',
|
|
'.epok-overlay',
|
|
'.epok-button',
|
|
'.epok-main-control',
|
|
]
|
|
|
|
function installOverlaySuppressionStyle() {
|
|
if (document.getElementById('stemengine-overlay-suppression-style')) return
|
|
const style = document.createElement('style')
|
|
style.id = 'stemengine-overlay-suppression-style'
|
|
style.textContent =
|
|
OVERLAY_SELECTORS.join(',\n') +
|
|
' { display: none !important; visibility: hidden !important; pointer-events: none !important; }'
|
|
|
|
const parent = document.head || document.documentElement
|
|
if (parent) parent.appendChild(style)
|
|
}
|
|
|
|
function isProtectedNode(node) {
|
|
if (!node || node.nodeType !== Node.ELEMENT_NODE) return true
|
|
const tag = node.tagName ? node.tagName.toLowerCase() : ''
|
|
return (
|
|
tag === 'html' ||
|
|
tag === 'head' ||
|
|
tag === 'body' ||
|
|
tag === 'canvas' ||
|
|
tag === 'script' ||
|
|
tag === 'style' ||
|
|
tag === 'link'
|
|
)
|
|
}
|
|
|
|
function removeMatchingOverlayNodes(root) {
|
|
const scope = root && root.querySelectorAll ? root : document
|
|
OVERLAY_SELECTORS.forEach((selector) => {
|
|
const nodes = scope.querySelectorAll(selector)
|
|
nodes.forEach((node) => {
|
|
if (isProtectedNode(node)) return
|
|
node.remove()
|
|
})
|
|
})
|
|
}
|
|
|
|
function installOverlaySuppressionObserver() {
|
|
if (window.__STEMENGINE_OVERLAY_OBSERVER__) return
|
|
const observer = new MutationObserver((mutations) => {
|
|
for (let i = 0; i < mutations.length; i += 1) {
|
|
const m = mutations[i]
|
|
if (!m || !m.addedNodes || m.addedNodes.length === 0) continue
|
|
for (let j = 0; j < m.addedNodes.length; j += 1) {
|
|
const node = m.addedNodes[j]
|
|
if (!node || node.nodeType !== Node.ELEMENT_NODE) continue
|
|
if (isProtectedNode(node)) {
|
|
removeMatchingOverlayNodes(node)
|
|
continue
|
|
}
|
|
if (node.matches && OVERLAY_SELECTORS.some((selector) => node.matches(selector))) {
|
|
node.remove()
|
|
continue
|
|
}
|
|
removeMatchingOverlayNodes(node)
|
|
}
|
|
}
|
|
})
|
|
|
|
observer.observe(document.documentElement, { childList: true, subtree: true })
|
|
window.__STEMENGINE_OVERLAY_OBSERVER__ = observer
|
|
}
|
|
|
|
function installOverlaySuppression() {
|
|
installOverlaySuppressionStyle()
|
|
removeMatchingOverlayNodes(document)
|
|
if (document.documentElement) {
|
|
installOverlaySuppressionObserver()
|
|
}
|
|
}
|
|
|
|
function installContextCaptureHook() {
|
|
const originalGetContext = HTMLCanvasElement.prototype.getContext
|
|
if (typeof originalGetContext !== 'function') return
|
|
if (HTMLCanvasElement.prototype.__stemengineContextHookInstalled__) return
|
|
|
|
HTMLCanvasElement.prototype.getContext = function patchedGetContext(type, attrs) {
|
|
const ctx = originalGetContext.call(this, type, attrs)
|
|
if ((type === 'webgl2' || type === 'webgl') && ctx && this.isConnected) {
|
|
if (!window.__STEMENGINE_MUON_GL_CTX__) {
|
|
window.__STEMENGINE_MUON_GL_CTX__ = ctx
|
|
}
|
|
if (!window.__STEMENGINE_VIZ_GL_CTX__) {
|
|
window.__STEMENGINE_VIZ_GL_CTX__ = ctx
|
|
}
|
|
if (!window.__STEMENGINE_MUON_CANVAS__) {
|
|
window.__STEMENGINE_MUON_CANVAS__ = this
|
|
}
|
|
if (!window.__STEMENGINE_VIZ_CANVAS__) {
|
|
window.__STEMENGINE_VIZ_CANVAS__ = this
|
|
}
|
|
}
|
|
return ctx
|
|
}
|
|
|
|
Object.defineProperty(HTMLCanvasElement.prototype, '__stemengineContextHookInstalled__', {
|
|
value: true,
|
|
configurable: true,
|
|
writable: false,
|
|
enumerable: false,
|
|
})
|
|
}
|
|
|
|
function checkBaseline() {
|
|
const canvas = document.createElement('canvas')
|
|
const gl = canvas.getContext('webgl2') || canvas.getContext('webgl')
|
|
if (!gl) {
|
|
console.warn('[StemEngine][Muon] WebGL unavailable; fallback mode should be used.')
|
|
return {
|
|
ok: false,
|
|
missing: ['webgl'],
|
|
}
|
|
}
|
|
|
|
const missing = REQUIRED_EXTENSIONS.filter((name) => !gl.getExtension(name))
|
|
if (missing.length > 0) {
|
|
console.warn('[StemEngine][Muon] Missing required WebGL extensions:', missing.join(', '))
|
|
return { ok: false, missing }
|
|
}
|
|
|
|
return { ok: true, missing: [] }
|
|
}
|
|
|
|
function installContextLifecycleHooks() {
|
|
let retries = 0
|
|
const maxRetries = 120
|
|
|
|
const attach = () => {
|
|
const canvas = document.querySelector('canvas.webgl') || document.querySelector('canvas')
|
|
if (!canvas) {
|
|
retries += 1
|
|
if (retries <= maxRetries) requestAnimationFrame(attach)
|
|
return
|
|
}
|
|
|
|
canvas.addEventListener('webglcontextlost', (event) => {
|
|
event.preventDefault()
|
|
console.warn('[StemEngine][Muon] webglcontextlost')
|
|
window.__STEMENGINE_MUON_CONTEXT_LOST__ = true
|
|
window.__STEMENGINE_VIZ_CONTEXT_LOST__ = true
|
|
})
|
|
|
|
canvas.addEventListener('webglcontextrestored', () => {
|
|
console.info('[StemEngine][Muon] webglcontextrestored')
|
|
window.__STEMENGINE_MUON_CONTEXT_LOST__ = false
|
|
window.__STEMENGINE_VIZ_CONTEXT_LOST__ = false
|
|
})
|
|
}
|
|
|
|
attach()
|
|
}
|
|
|
|
if (typeof window.__STEMENGINE_MUON_STOP_FN__ !== 'function') {
|
|
window.__STEMENGINE_MUON_STOP_FN__ = function stemengineNoopStop() {}
|
|
}
|
|
if (typeof window.__STEMENGINE_MUON_RESUME_FN__ !== 'function') {
|
|
window.__STEMENGINE_MUON_RESUME_FN__ = function stemengineNoopResume() {}
|
|
}
|
|
|
|
installContextCaptureHook()
|
|
installOverlaySuppression()
|
|
|
|
const baseline = checkBaseline()
|
|
window.__STEMENGINE_MUON_WEBGL_BASELINE__ = baseline
|
|
window.__STEMENGINE_VIZ_WEBGL_BASELINE__ = baseline
|
|
|
|
if (!baseline.ok) {
|
|
document.documentElement.classList.add('stemengine-muon-degraded')
|
|
}
|
|
|
|
installContextLifecycleHooks()
|
|
})() |