-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.ts
executable file
·187 lines (159 loc) · 5.42 KB
/
loader.ts
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* convenience functions for loading shaders, and loading meshes in a simple JSON format.
*
* loadFile/loadFiles from http://stackoverflow.com/questions/4878145/javascript-and-webgl-external-scripts
* loadMesh adapted from various loaders in http://threejs.org
*/
function loadFile(url: string, data: any, callback: (response:string, data:any)=>void, errorCallback: (url:string)=>void) {
// Set up an asynchronous request
var request = new XMLHttpRequest();
request.open('GET', url, true);
// Hook the event that gets called as the request progresses
request.onreadystatechange = function () {
// If the request is "DONE" (completed or failed)
if (request.readyState == 4) {
// If we got HTTP status 200 (OK)
if (request.status == 200) {
callback(request.responseText, data)
} else { // Failed
errorCallback(url);
}
}
};
request.send(null);
}
export function loadFiles(urls: string[], callback: (response:string[])=>void, errorCallback: (url:string)=>void) {
var numUrls = urls.length;
var numComplete = 0;
var result: string[] = [];
// Callback for a single file
function partialCallback(text: string, urlIndex: number) {
result[urlIndex] = text;
numComplete++;
// When all files have downloaded
if (numComplete == numUrls) {
callback(result);
}
}
for (var i = 0; i < numUrls; i++) {
loadFile(urls[i], i, partialCallback, errorCallback);
}
}
/*
* Load a Mesh file asynchronously from a file stored on the web.
* The results will be provided to the "onLoad" callback, and are a Mesh object
* with an array of vertices and an array of triangles as members.
*
* For example:
* var onLoad = function (mesh: loader.Mesh) {
* console.log("got a mesh: " + mesh);
* }
* var onProgress = function (progress: ProgressEvent) {
* console.log("loading: " + progress.loaded + " of " + progress.total + "...");
* }
* var onError = function (error: ErrorEvent) {
* console.log("error! " + error);
* }
*
* loader.loadMesh("models/venus.json", onLoad, onProgress, onError);
*
*/
export type Vertex = [number, number, number];
export type Triangle = [number, number, number];
export interface Mesh {
v: Array<Vertex>;
t: Array<Triangle>;
}
// if there is a current request outstanding, this will be set to it
var currentRequest: XMLHttpRequest | undefined = undefined;
export function loadMesh ( url: string,
onLoad: (data: any) => void,
onProgress?: (progress: ProgressEvent) => void,
onError?: (error: string) => void ): XMLHttpRequest {
// if there is a request in progress, abort it.
if (currentRequest !== undefined) {
currentRequest.abort();
currentRequest = undefined;
}
// set up the new request
var request = new XMLHttpRequest();
request.open( 'GET', url, true );
currentRequest = request; // save it, so we can abort if another request is made by the user
request.addEventListener( 'load', function ( event ) {
// finished with the current request now
currentRequest = undefined;
//var json = this.response; /// already in JSON format, don't need:
var json = JSON.parse( this.response );
// we'll put a metadata field in the object, just to be sure it's one of ours
var metadata = json.metadata;
if ( metadata !== undefined ) {
if ( metadata.type !== 'triangles' ) {
console.error( 'Loader: ' + url + ' should be a "triangles" files.' );
return;
}
} else {
console.error( 'Loader: ' + url + ' does not have a metadata field.' );
return;
}
var object = validate( json, url );
if (object !== undefined) {
console.log("Loader: " + url + " contains " + object.v.length + " vertices " +
" and " + object.t.length + " triangles.")
}
onLoad( object );
}, false );
if ( onProgress !== undefined ) {
request.addEventListener( 'progress', function ( event: ProgressEvent ) {
onProgress( event );
}, false );
}
if ( onError !== undefined) {
request.addEventListener( 'error', function ( event: ProgressEvent ) {
currentRequest = undefined; // request failed, clear the current request field
if ( onError ) onError( "file couldn't load" );
}, false );
}
// ask for a "json" file
//request.responseType = "json";
request.send( null );
return request;
}
// validate the received JSON, just to make sure it's what we are expecting (and thus avoid
// bugs down the road in our code)
function validate(json: any, url: string): Mesh | undefined {
if (json instanceof Object &&
json.hasOwnProperty('t') &&
json.t instanceof Array &&
json.hasOwnProperty('v') &&
json.v instanceof Array) {
var numV = json.v.length;
for (var i in json.t) {
if (!(json.t[i] instanceof Array &&
json.t[i].length == 3 &&
typeof json.t[i][0] == "number" &&
typeof json.t[i][1] == "number" &&
typeof json.t[i][2] == "number" &&
json.t[i][0] < numV &&
json.t[i][1] < numV &&
json.t[i][2] < numV)) {
console.log("Loader: json file " + url + ", invalid t[" + i + "].");
return undefined;
}
}
for (var i in json.v) {
if (!(json.v[i] instanceof Array &&
json.v[i].length == 3 &&
typeof json.v[i][0] == "number" &&
typeof json.v[i][1] == "number" &&
typeof json.v[i][2] == "number")) {
console.log("Loader: json file " + url + ", invalid v[" + i + "].");
return undefined;
}
// i++;
}
return <Mesh>json;
} else {
console.log("Loader: json file " + url + " does not have .t and .v members.");
return undefined;
}
}