|
| 1 | +<!doctype html> |
| 2 | +<html> |
| 3 | + <head> |
| 4 | + <meta charset="utf-8" /> |
| 5 | + <title>MuJoCo Studio!</title> |
| 6 | + </head> |
| 7 | + <body style="margin: 0; overflow: hidden"> |
| 8 | + <div style="position: absolute; top: 3px; right: 3px; z-index: 1;"> |
| 9 | + <button id="uploadButton">Upload Model</button> |
| 10 | + <input type="file" id="fileInput" accept=".xml,.mjb" style="display: none;"> |
| 11 | + </div> |
| 12 | + <canvas |
| 13 | + class="emscripten" |
| 14 | + id="canvas" |
| 15 | + oncontextmenu="event.preventDefault()" |
| 16 | + style="width: 100vw; height: 100vh; display: block" |
| 17 | + ></canvas> |
| 18 | + <script> |
| 19 | + var Module = { |
| 20 | + preRun: [], |
| 21 | + postRun: [], |
| 22 | + locateFile: function(path) { |
| 23 | + const baseURL = window.location.origin + window.location.pathname.substring(0, window.location.pathname.lastIndexOf("/")); |
| 24 | + return baseURL + "/bin/" + path; |
| 25 | + }, |
| 26 | + print: console.log, |
| 27 | + printErr: text => { |
| 28 | + console.error(text + "\n" + new Error().stack); |
| 29 | + }, |
| 30 | + canvas: (() => { |
| 31 | + const canvas = document.getElementById("canvas"); |
| 32 | + // As a default initial behavior, pop up an alert when webgl context is lost. To make your |
| 33 | + // application robust, you may want to override this behavior before shipping! |
| 34 | + // See http://www.khronos.org/registry/webgl/specs/latest/1.0/#5.15.2 |
| 35 | + canvas.addEventListener( |
| 36 | + "webglcontextlost", |
| 37 | + e => { |
| 38 | + alert("WebGL context lost. You will need to reload the page."); |
| 39 | + e.preventDefault(); |
| 40 | + }, |
| 41 | + false, |
| 42 | + ); |
| 43 | + return canvas; |
| 44 | + })(), |
| 45 | + setStatus(text) {}, |
| 46 | + totalDependencies: 0, |
| 47 | + monitorRunDependencies(left) {}, |
| 48 | + onRuntimeInitialized: () => { |
| 49 | + // Define assets to prefetch. These paths are relative to the wasm_binary/ directory. |
| 50 | + const assetsToPrefetch = [ |
| 51 | + "ibl.ktx", |
| 52 | + "pbr.filamat", |
| 53 | + "pbr_packed.filamat", |
| 54 | + "phong_2d.filamat", |
| 55 | + "phong_2d_uv.filamat", |
| 56 | + "phong_color.filamat", |
| 57 | + "phong_cube.filamat", |
| 58 | + "unlit_line.filamat", |
| 59 | + "unlit_ui.filamat", |
| 60 | + "phong_2d_fade.filamat", |
| 61 | + "phong_2d_uv_fade.filamat", |
| 62 | + "phong_color_fade.filamat", |
| 63 | + "phong_cube_fade.filamat", |
| 64 | + "unlit_depth.filamat", |
| 65 | + "unlit_segmentation.filamat", |
| 66 | + "OpenSans-Regular.ttf", |
| 67 | + "fontawesome-webfont.ttf", |
| 68 | + ]; |
| 69 | + |
| 70 | + const assetPromises = assetsToPrefetch.map(async (relativePath) => { |
| 71 | + const assetUrl = Module.locateFile(relativePath); |
| 72 | + try { |
| 73 | + const response = await fetch(assetUrl); |
| 74 | + if (!response.ok) { |
| 75 | + throw new Error(`Failed to fetch ${assetUrl}: ${response.statusText}`); |
| 76 | + } |
| 77 | + const buffer = await response.arrayBuffer(); |
| 78 | + const filename = relativePath.substring(relativePath.lastIndexOf('/') + 1); |
| 79 | + Module.registerAsset(filename, new Uint8Array(buffer)); |
| 80 | + console.log(`Registered asset: ${filename}`); |
| 81 | + } catch (error) { |
| 82 | + console.error(`Error prefetching asset ${assetUrl}:`, error); |
| 83 | + throw error; // Re-throw to be caught by Promise.all |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + Promise.all(assetPromises) |
| 88 | + .then(() => { |
| 89 | + try { |
| 90 | + Module.init(); |
| 91 | + requestAnimationFrame(Module.animate); |
| 92 | + } catch (error) { |
| 93 | + console.error('Failed to initialize app.', error); |
| 94 | + } |
| 95 | + }) |
| 96 | + .catch((error) => { |
| 97 | + console.error('Failed to prefetch one or more assets.', error); |
| 98 | + }); |
| 99 | + }, |
| 100 | + animate: () => { |
| 101 | + try { |
| 102 | + Module.renderFrame(); |
| 103 | + } catch (error) { |
| 104 | + console.error('Update error:', error); |
| 105 | + } |
| 106 | + requestAnimationFrame(Module.animate); |
| 107 | + }, |
| 108 | + }; |
| 109 | + // Ensure the canvas is resized when the window is resized. |
| 110 | + window.addEventListener("resize", function () { |
| 111 | + Module.canvas.style.width = window.innerWidth + "px"; |
| 112 | + Module.canvas.style.height = window.innerHeight + "px"; |
| 113 | + }); |
| 114 | + |
| 115 | + document.addEventListener('DOMContentLoaded', () => { |
| 116 | + const uploadButton = document.getElementById('uploadButton'); |
| 117 | + const fileInput = document.getElementById('fileInput'); |
| 118 | + |
| 119 | + uploadButton.addEventListener('click', () => { |
| 120 | + fileInput.click(); |
| 121 | + }); |
| 122 | + |
| 123 | + fileInput.addEventListener('change', (event) => { |
| 124 | + const file = event.target.files[0]; |
| 125 | + if (!file) { |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + const reader = new FileReader(); |
| 130 | + reader.onload = (e) => { |
| 131 | + const buffer = e.target.result; |
| 132 | + try { |
| 133 | + if (file.name.endsWith('.mjb')) { |
| 134 | + Module.loadMjb(buffer); |
| 135 | + } else if (file.name.endsWith('.xml')) { |
| 136 | + Module.loadXml(buffer); |
| 137 | + } else { |
| 138 | + console.error('Unsupported file type:', file.name); |
| 139 | + } |
| 140 | + console.log('Model loaded from file:', file.name); |
| 141 | + } catch (error) { |
| 142 | + console.error('Failed to load model from file:', error); |
| 143 | + } |
| 144 | + }; |
| 145 | + reader.onerror = (e) => { |
| 146 | + console.error('Error reading file:', e); |
| 147 | + }; |
| 148 | + reader.readAsArrayBuffer(file); // Reads as text, suitable for XML. |
| 149 | + }); |
| 150 | + }); |
| 151 | + </script> |
| 152 | + <script async src="/bin/wasm.js"></script> |
| 153 | + </body> |
| 154 | +</html> |
0 commit comments