@xeokit/xeokit-sdk 2.5.2-beta-8 → 2.5.2-beta-10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,5 @@
1
1
  import {Map} from "./scene/utils/Map.js";
2
+ import {core} from "./scene/core";
2
3
 
3
4
  /**
4
5
  @desc Base class for {@link Viewer} plugin classes.
@@ -38,6 +39,14 @@ class Plugin {
38
39
  viewer.addPlugin(this);
39
40
  }
40
41
 
42
+ /**
43
+ * Schedule a task to perform on the next browser interval
44
+ * @param task
45
+ */
46
+ scheduleTask(task) {
47
+ core.scheduleTask(task, null);
48
+ }
49
+
41
50
  /**
42
51
  * Fires an event on this Plugin.
43
52
  *
@@ -814,6 +814,14 @@ class Component {
814
814
  }
815
815
  }
816
816
 
817
+ /**
818
+ * Schedule a task to perform on the next browser interval
819
+ * @param task
820
+ */
821
+ scheduleTask(task) {
822
+ core.scheduleTask(task, null);
823
+ }
824
+
817
825
  /**
818
826
  * Protected virtual template method, optionally implemented
819
827
  * by sub-classes to perform a scheduled task.
@@ -15,6 +15,7 @@ let lastTime = 0;
15
15
  let elapsedTime;
16
16
  let totalFPS = 0;
17
17
 
18
+
18
19
  /**
19
20
  * @private
20
21
  */
@@ -100,7 +101,7 @@ function Core() {
100
101
  * @param {Function} callback Callback that runs the task.
101
102
  * @param {Object} [scope] Scope for the callback.
102
103
  */
103
- this.scheduleTask = function (callback, scope) {
104
+ this.scheduleTask = function (callback, scope = null) {
104
105
  taskQueue.push(callback);
105
106
  taskQueue.push(scope);
106
107
  };
@@ -135,8 +136,43 @@ function Core() {
135
136
  */
136
137
  const core = new Core();
137
138
 
138
-
139
139
  const frame = function () {
140
+ let time = Date.now();
141
+ elapsedTime = time - lastTime;
142
+ if (lastTime > 0 && elapsedTime > 0) { // Log FPS stats
143
+ var newFPS = 1000 / elapsedTime; // Moving average of FPS
144
+ totalFPS += newFPS;
145
+ fpsSamples.push(newFPS);
146
+ if (fpsSamples.length >= numFPSSamples) {
147
+ totalFPS -= fpsSamples.shift();
148
+ }
149
+ stats.frame.fps = Math.round(totalFPS / fpsSamples.length);
150
+ }
151
+ for (let id in core.scenes) {
152
+ core.scenes[id].compile();
153
+ }
154
+ runTasks(time);
155
+ lastTime = time;
156
+ };
157
+
158
+ class WorkerInterval {
159
+ worker = null;
160
+
161
+ constructor(callback, interval) {
162
+ const blob = new Blob([`setInterval(() => postMessage(0), ${interval});`]);
163
+ const workerScript = URL.createObjectURL(blob);
164
+ this.worker = new Worker(workerScript);
165
+ this.worker.onmessage = callback;
166
+ }
167
+
168
+ stop() {
169
+ this.worker.terminate();
170
+ }
171
+ }
172
+
173
+ const interval = new WorkerInterval(frame, 100);
174
+
175
+ const renderFrame = function () {
140
176
  let time = Date.now();
141
177
  elapsedTime = time - lastTime;
142
178
  if (lastTime > 0 && elapsedTime > 0) { // Log FPS stats
@@ -151,10 +187,11 @@ const frame = function () {
151
187
  runTasks(time);
152
188
  fireTickEvents(time);
153
189
  renderScenes();
154
- lastTime = time;
155
- (window.requestPostAnimationFrame !== undefined) ? window.requestPostAnimationFrame(frame) : requestAnimationFrame(frame);
190
+ (window.requestPostAnimationFrame !== undefined) ? window.requestPostAnimationFrame(frame) : requestAnimationFrame(renderFrame);
156
191
  };
157
192
 
193
+ renderFrame();
194
+
158
195
  function runTasks(time) { // Process as many enqueued tasks as we can within the per-frame task budget
159
196
  const tasksRun = core.runTasks(time + taskBudget);
160
197
  const tasksScheduled = core.getNumTasks();
@@ -228,15 +265,4 @@ function renderScenes() {
228
265
  }
229
266
  }
230
267
 
231
- (window.requestPostAnimationFrame !== undefined) ? window.requestPostAnimationFrame(frame) : requestAnimationFrame(frame);
232
-
233
- function compileScenes() {
234
-
235
- for (let id in core.scenes) {
236
- core.scenes[id].compile();
237
- }
238
- }
239
-
240
- window.setInterval(compileScenes, 1000 / 60);
241
-
242
268
  export {core};
@@ -1,5 +1,6 @@
1
1
  import {Cache} from './Cache.js';
2
2
  import {Loader} from './Loader.js';
3
+ import {core} from "../core";
3
4
 
4
5
  const loading = {};
5
6
 
@@ -20,7 +21,7 @@ class FileLoader extends Loader {
20
21
  const cached = Cache.get(url);
21
22
  if (cached !== undefined) {
22
23
  this.manager.itemStart(url);
23
- setTimeout(() => {
24
+ core.scheduleTask(() => {
24
25
  if (onLoad) {
25
26
  onLoad(cached);
26
27
  }
@@ -1,6 +1,8 @@
1
1
  /**
2
2
  * @private
3
3
  */
4
+ import {core} from "./core";
5
+
4
6
  function xmlToJson(node, attributeRenamer) {
5
7
  if (node.nodeType === node.TEXT_NODE) {
6
8
  var v = node.nodeValue;
@@ -206,13 +208,13 @@ function loadArraybuffer(url, ok, err) {
206
208
  for (var i = 0; i < data.length; i++) {
207
209
  view[i] = data.charCodeAt(i);
208
210
  }
209
- window.setTimeout(function () {
211
+ core.scheduleTask(() => {
210
212
  ok(buffer);
211
- }, 0);
213
+ });
212
214
  } catch (error) {
213
- window.setTimeout(function () {
215
+ core.scheduleTask(() => {
214
216
  err(error);
215
- }, 0);
217
+ });
216
218
  }
217
219
  } else {
218
220
  const request = new XMLHttpRequest();