-
Notifications
You must be signed in to change notification settings - Fork 0
/
Loader.js
134 lines (118 loc) · 3.12 KB
/
Loader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
Copyright 2017 Marcel Greter
https://www.github.com/mgreter
*/
// private scope
(function (THREE, THREEAPP)
{
"use strict";
// implement as void function
function loaded ( resource, data )
{
var L = resource.cbs.length;
// store result on resources
for (var i = 0; i < L; i++) {
// store received data on asset
resource.cbs[i].assets[2] = resource;
// resolve optional promise
if (resource.cbs[i].resolve)
{ resource.cbs[i].resolve(); }
// mark resource as complete
resource.complete = true;
// attach the result
resource.data = data;
}
}
function loadError()
{
this.invoke(
'fetch.error', {}
);
}
function loadStart()
{
var loader = this.loader;
// only init once
if (++ loader.completed == 1) {
// trigger an event on the viewport
this.trigger('prefetch.complete', this.loader);
this.trigger('fetch.complete', this.loader);
}
else {
// trigger an event on the viewport
this.trigger('fetch.complete', this.loader);
}
}
function loadStep(args) {
var loader = this.loader;
loader.filesTotal = args[2];
loader.filesLoaded = args[1];
this.trigger(
'fetch.progress', {
filesTotal: loader.filesTotal,
filesLoaded: loader.filesLoaded,
bytesTotal: loader.bytesTotal,
bytesLoaded: loader.bytesLoaded
}
);
}
// progress updates asset info
function progress (obj, asset, evt)
{
var loader = this.loader;
// add total file size once
if (asset.total == 0) {
asset.total = evt.total;
loader.bytesTotal += evt.total;
loader.filesTotal += 1;
}
// increment loaded stats by delta of asset
loader.bytesLoaded += evt.loaded - asset.loaded;
// update singleton values
asset.loaded = evt.loaded;
// trigger the download progress
// ToDo: throttle for better fps?
this.trigger(
'fetch.progress', {
filesTotal: loader.filesTotal,
filesLoaded: loader.filesLoaded,
bytesTotal: loader.bytesTotal,
bytesLoaded: loader.bytesLoaded
}
);
}
var Loader = THREEAPP.Class.create('Loader', null, ['Plugin'])
.proto('provides', 'loader')
.ctor(function (app) {
// scope for closures
var loader = this;
app.loader = loader;
// ready status
loader.completed = 0;
// init statistics
loader.filesTotal = 0;
loader.filesLoaded = 0;
loader.bytesTotal = 0;
loader.bytesLoaded = 0;
// register event handlers
app.listen('loader.step', loadStep);
app.listen('loader.start', loadStart);
app.listen('loader.error', loadError);
app.listen('loader.complete', loaded);
app.listen('loader.progress', progress);
})
.init(function (app) {
// create shared loading manager instance
app.manager = new THREE.LoadingManager(
function () { app.invoke('loader.start', arguments); },
function () { app.invoke('loader.step', arguments); },
function () { app.invoke('loader.error', arguments); }
);
// create back reference
app.manager.app = app;
})
;
// assign class to global namespace
THREEAPP('Plugin.Loader', Loader);
// EO private scope
})(THREE, THREEAPP);