Guest User

Unity WebGL Frame Rate Limit

a guest
Aug 28th, 2023
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 5.47 KB | Source Code | 0 0
  1. function _emscripten_set_main_loop_timing(mode, value) {
  2.     console.log("_emscripten_set_main_loop_timing called with " + mode + ", " + value); // 1: _emscripten_set_main_loop_timing called with 0, 33.333333333333336
  3.     Browser.mainLoop.timingMode = mode;
  4.     Browser.mainLoop.timingValue = value;
  5.     if (!Browser.mainLoop.func) {
  6.         return 1;
  7.     }
  8.     if (!Browser.mainLoop.running) {
  9.         Browser.mainLoop.running = true;
  10.     }
  11.     if (mode == 0) {
  12.         console.log("setting timeout"); // 2: called next
  13.         Browser.mainLoop.scheduler = function Browser_mainLoop_scheduler_setTimeout() {
  14.             var timeUntilNextTick = Math.max(0, Browser.mainLoop.tickStartTime + value - _emscripten_get_now()) | 0;
  15.             console.log("timeUntilNextTick: " + timeUntilNextTick); // 3: called regularly with "timeUntilNextTick: 32" or "...33"
  16.             setTimeout(Browser.mainLoop.runner, timeUntilNextTick);
  17.         };
  18.         Browser.mainLoop.method = "timeout";
  19.     } else if (mode == 1) {
  20.         Browser.mainLoop.scheduler = function Browser_mainLoop_scheduler_rAF() {
  21.             Browser.requestAnimationFrame(Browser.mainLoop.runner);
  22.         };
  23.         Browser.mainLoop.method = "rAF";
  24.     } else if (mode == 2) {
  25.         if (typeof setImmediate === "undefined") {
  26.             var setImmediates = [];
  27.             var emscriptenMainLoopMessageId = "setimmediate";
  28.             var Browser_setImmediate_messageHandler = function (event) {
  29.                 if (event.data === emscriptenMainLoopMessageId || event.data.target === emscriptenMainLoopMessageId) {
  30.                     event.stopPropagation();
  31.                     setImmediates.shift()();
  32.                 }
  33.             };
  34.             addEventListener("message", Browser_setImmediate_messageHandler, true);
  35.             setImmediate = function Browser_emulated_setImmediate(func) {
  36.                 setImmediates.push(func);
  37.                 if (ENVIRONMENT_IS_WORKER) {
  38.                     if (Module["setImmediates"] === undefined) Module["setImmediates"] = [];
  39.                     Module["setImmediates"].push(func);
  40.                     postMessage({ target: emscriptenMainLoopMessageId });
  41.                 } else postMessage(emscriptenMainLoopMessageId, "*");
  42.             };
  43.         }
  44.         Browser.mainLoop.scheduler = function Browser_mainLoop_scheduler_setImmediate() {
  45.             setImmediate(Browser.mainLoop.runner);
  46.         };
  47.         Browser.mainLoop.method = "immediate";
  48.     }
  49.     return 0;
  50. }
  51.  
  52. function setMainLoop(browserIterationFunc, fps, simulateInfiniteLoop, arg, noSetTiming) {
  53.     assert(!Browser.mainLoop.func, "emscripten_set_main_loop: there can only be one main loop function at once: call emscripten_cancel_main_loop to cancel the previous one before setting a new one with different parameters.");
  54.     Browser.mainLoop.func = browserIterationFunc;
  55.     Browser.mainLoop.arg = arg;
  56.     var thisMainLoopId = Browser.mainLoop.currentlyRunningMainloop;
  57.     function checkIsRunning() {
  58.         if (thisMainLoopId < Browser.mainLoop.currentlyRunningMainloop) {
  59.             maybeExit();
  60.             return false;
  61.         }
  62.         return true;
  63.     }
  64.     Browser.mainLoop.running = false;
  65.     Browser.mainLoop.runner = function Browser_mainLoop_runner() {
  66.         if (ABORT) return;
  67.         if (Browser.mainLoop.queue.length > 0) {
  68.             var start = Date.now();
  69.             var blocker = Browser.mainLoop.queue.shift();
  70.             blocker.func(blocker.arg);
  71.             if (Browser.mainLoop.remainingBlockers) {
  72.                 var remaining = Browser.mainLoop.remainingBlockers;
  73.                 var next = remaining % 1 == 0 ? remaining - 1 : Math.floor(remaining);
  74.                 if (blocker.counted) {
  75.                     Browser.mainLoop.remainingBlockers = next;
  76.                 } else {
  77.                     next = next + 0.5;
  78.                     Browser.mainLoop.remainingBlockers = (8 * remaining + next) / 9;
  79.                 }
  80.             }
  81.             console.log('main loop blocker "' + blocker.name + '" took ' + (Date.now() - start) + " ms");
  82.             Browser.mainLoop.updateStatus();
  83.             if (!checkIsRunning()) return;
  84.             console.log("Setting timeout to 0");
  85.             setTimeout(Browser.mainLoop.runner, 0);
  86.             return;
  87.         }
  88.         if (!checkIsRunning()) return;
  89.         Browser.mainLoop.currentFrameNumber = (Browser.mainLoop.currentFrameNumber + 1) | 0;
  90.         if (Browser.mainLoop.timingMode == 1 && Browser.mainLoop.timingValue > 1 && Browser.mainLoop.currentFrameNumber % Browser.mainLoop.timingValue != 0) {
  91.             console.log("Browser.mainLoop.timingMode == 1");
  92.             Browser.mainLoop.scheduler();
  93.             return;
  94.         } else if (Browser.mainLoop.timingMode == 0) {
  95.             console.log("Browser.mainLoop.timingMode == 0");
  96.             Browser.mainLoop.tickStartTime = _emscripten_get_now();
  97.         }
  98.         GL.newRenderingFrameStarted();
  99.         Browser.mainLoop.runIter(browserIterationFunc);
  100.         if (!checkIsRunning()) return;
  101.         if (typeof SDL === "object" && SDL.audio && SDL.audio.queueNewAudioData) SDL.audio.queueNewAudioData();
  102.         Browser.mainLoop.scheduler();
  103.     };
  104.     if (!noSetTiming) {
  105.         console.log("fps: " + fps);
  106.         fps = 30;
  107.         if (fps && fps > 0) _emscripten_set_main_loop_timing(0, 1e3 / fps);
  108.         else _emscripten_set_main_loop_timing(1, 1);
  109.         Browser.mainLoop.scheduler();
  110.     }
  111.     if (simulateInfiniteLoop) {
  112.         throw "unwind";
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment