Skip to content

Commit

Permalink
Global configs management #758
Browse files Browse the repository at this point in the history
  • Loading branch information
xeolabs committed Nov 30, 2021
1 parent a6a9eb5 commit 218362a
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@
<label for="info-button" class="info-button"><i class="far fa-3x fa-question-circle"></i></label>
<canvas id="myCanvas"></canvas>
<div class="slideout-sidebar">
<h1>Configs - disabling double precision and RAF</h1><br>
<h1>Configs - disabling double precision</h1><br>
<p>In this example we use a <a href="../docs/class/src/viewer/Configs.js~Configs.html"
target="_other">Configs</a> to disable xeokit's double-precision support, which gives a performance and memory boost
on low-power devices. This however also means that we can no longer render double-precision models without jittering.</p>

<p>We're also switching xeokit from using window.requestAnimationFrame to window.setInterval for its main render loop, which can also
give a performance boost for some cases.</p>
<h3>Stats</h3>
<ul>
<li>
Expand Down Expand Up @@ -60,7 +58,6 @@ <h3>Components Used</h3>
//------------------------------------------------------------------------------------------------------------------

configs.doublePrecisionEnabled = false; // Disable 64-bit precision for extra speed. Set this once, before you create any Viewers.
configs.rafEnabled = false; // Disable window.requestAnimationFrame (RAF) and use window.setInterval for Viewer render loops. Can be switched dynamically.

//------------------------------------------------------------------------------------------------------------------
// Create a Viewer, arrange the camera
Expand Down
2 changes: 1 addition & 1 deletion examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@

"Configs": [
"#Configuring the xeokit-sdk",
["Configs_disableDoublePrecisionAndRAF", "Disable double precision (and RAF) for extra performance"]
["Configs_disableDoublePrecision", "Disable double precision for extra performance"]
],

"Viewer": [
Expand Down
37 changes: 0 additions & 37 deletions src/viewer/Configs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {math} from "./scene/math/math.js";
import {core} from "./scene/core";

/**
* Manages global configurations for all {@link Viewer}s.
Expand All @@ -11,9 +10,6 @@ import {core} from "./scene/core";
*
* That's OK if we know that we're not going to view models that are geographically vast, or offset far from the World coordinate origin.
*
* We'll also switch xeokit from using ````window.requestAnimationFrame```` to ````window.setInterval```` for its render loop,
* which may give a smoother experience in some cases.
*
* [[Run this example](http://xeokit.github.io/xeokit-sdk/examples/#Configs_disableDoublePrecisionAndRAF)]
*
* ````javascript
Expand All @@ -27,11 +23,6 @@ import {core} from "./scene/core";
* // Only set this config once, before you create any Viewers.
* configs.doublePrecisionEnabled = false;
*
* // Disable window.requestAnimationFrame (RAF) and use
* // window.setInterval for Viewer render loops.
* // This config can be switched dynamically, if needed.
* configs.rafEnabled = false;
*
* // Create a Viewer, to which our configs apply
* const viewer = new Viewer({
* canvasId: "myCanvas"
Expand Down Expand Up @@ -83,34 +74,6 @@ class Configs {
get doublePrecisionEnabled() {
return math.getDoublePrecisionEnabled();
}

/**
* Sets whether Viewers currently use ````window.requestAnimationFrame```` (RAF) or ````window.setInterval```` for animations.
*
* With RAF, the render loop is suspended whenever we switch away from the browser tab that
* contains our application. With setInterval, the render loop will continue running. Since a Viewer only
* renders frames when the view has actually updated, disabling RAF can actually give a performance boost.
*
* This is ````true```` by default, to use RAF.
*
* This can be dynamically set at any time.
*
* @returns {boolean}
*/
set rafEnabled(rafEnabled) {
core.setRAFEnabled(rafEnabled);
}

/**
* Gets whether Viewers currently use ````window.requestAnimationFrame```` (RAF) or ````window.setInterval```` for animations.
*
* @returns {boolean}
*/
get rafEnabled() {
return core.getRAFEnabled();
}


}

export {Configs};
39 changes: 2 additions & 37 deletions src/viewer/scene/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ import {Map} from './utils/Map.js';
import {stats} from './stats.js';
import {utils} from './utils.js';

let rafEnabled = true;
let interval = null;

const scenesRenderInfo = {}; // Used for throttling FPS for each Scene
const sceneIDMap = new Map(); // Ensures unique scene IDs
const taskQueue = new Queue(); // Task queue, which is pumped on each frame; tasks are pushed to it with calls to xeokit.schedule
Expand Down Expand Up @@ -73,32 +70,6 @@ function Core() {
});
};

/**
* @private
*/
this.setRAFEnabled = function (value) {
if (value === rafEnabled) {
return;
}
rafEnabled = value;
if (rafEnabled) {
if (interval !== null) {
clearInterval(interval);
interval = null;
}
(window.requestPostAnimationFrame !== undefined) ? window.requestPostAnimationFrame(frame) : requestAnimationFrame(frame);
} else {
interval = setInterval(frame, 16);
}
};

/**
* @private
*/
this.getRAFEnabled = function () {
return rafEnabled;
}

/**
* @private
*/
Expand Down Expand Up @@ -181,9 +152,7 @@ const frame = function () {
fireTickEvents(time);
renderScenes();
lastTime = time;
if (rafEnabled) {
(window.requestPostAnimationFrame !== undefined) ? window.requestPostAnimationFrame(frame) : requestAnimationFrame(frame);
}
(window.requestPostAnimationFrame !== undefined) ? window.requestPostAnimationFrame(frame) : requestAnimationFrame(frame);
};

function runTasks(time) { // Process as many enqueued tasks as we can within the per-frame task budget
Expand Down Expand Up @@ -259,10 +228,6 @@ function renderScenes() {
}
}

if (rafEnabled) {
(window.requestPostAnimationFrame !== undefined) ? window.requestPostAnimationFrame(frame) : requestAnimationFrame(frame);
} else {
interval = setInterval(frame, 16);
}
(window.requestPostAnimationFrame !== undefined) ? window.requestPostAnimationFrame(frame) : requestAnimationFrame(frame);

export {core};

0 comments on commit 218362a

Please sign in to comment.