/
glTFValidation.ts
149 lines (133 loc) · 6.18 KB
/
glTFValidation.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
import * as GLTF2 from 'babylonjs-gltf2interface';
import { Tools } from 'babylonjs/Misc/tools';
declare var GLTFValidator: GLTF2.IGLTFValidator;
// WorkerGlobalScope
declare function importScripts(...urls: string[]): void;
declare function postMessage(message: any, transfer?: any[]): void;
function validateAsync(data: string | ArrayBuffer, rootUrl: string, fileName: string, getExternalResource: (uri: string) => Promise<ArrayBuffer>): Promise<GLTF2.IGLTFValidationResults> {
const options: GLTF2.IGLTFValidationOptions = {
externalResourceFunction: (uri) => getExternalResource(uri).then((value) => new Uint8Array(value))
};
if (fileName) {
options.uri = (rootUrl === "file:" ? fileName : rootUrl + fileName);
}
return (data instanceof ArrayBuffer)
? GLTFValidator.validateBytes(new Uint8Array(data), options)
: GLTFValidator.validateString(data, options);
}
/**
* The worker function that gets converted to a blob url to pass into a worker.
*/
function workerFunc(): void {
const pendingExternalResources: Array<{ resolve: (data: any) => void, reject: (reason: any) => void }> = [];
onmessage = (message) => {
const data = message.data;
switch (data.id) {
case "init": {
importScripts(data.url);
break;
}
case "validate": {
validateAsync(data.data, data.rootUrl, data.fileName, (uri) => new Promise((resolve, reject) => {
const index = pendingExternalResources.length;
pendingExternalResources.push({ resolve, reject });
postMessage({ id: "getExternalResource", index: index, uri: uri });
})).then((value) => {
postMessage({ id: "validate.resolve", value: value });
}, (reason) => {
postMessage({ id: "validate.reject", reason: reason });
});
break;
}
case "getExternalResource.resolve": {
pendingExternalResources[data.index].resolve(data.value);
break;
}
case "getExternalResource.reject": {
pendingExternalResources[data.index].reject(data.reason);
break;
}
}
};
}
/**
* Configuration for glTF validation
*/
export interface IGLTFValidationConfiguration {
/**
* The url of the glTF validator.
*/
url: string;
}
/**
* glTF validation
*/
export class GLTFValidation {
/**
* The configuration. Defaults to `{ url: "https://preview.babylonjs.com/gltf_validator.js" }`.
*/
public static Configuration: IGLTFValidationConfiguration = {
url: "https://preview.babylonjs.com/gltf_validator.js"
};
private static _LoadScriptPromise: Promise<void>;
/**
* Validate a glTF asset using the glTF-Validator.
* @param data The JSON of a glTF or the array buffer of a binary glTF
* @param rootUrl The root url for the glTF
* @param fileName The file name for the glTF
* @param getExternalResource The callback to get external resources for the glTF validator
* @returns A promise that resolves with the glTF validation results once complete
*/
public static ValidateAsync(data: string | ArrayBuffer, rootUrl: string, fileName: string, getExternalResource: (uri: string) => Promise<ArrayBuffer>): Promise<GLTF2.IGLTFValidationResults> {
if (typeof Worker === "function") {
return new Promise((resolve, reject) => {
const workerContent = `${validateAsync}(${workerFunc})()`;
const workerBlobUrl = URL.createObjectURL(new Blob([workerContent], { type: "application/javascript" }));
const worker = new Worker(workerBlobUrl);
const onError = (error: ErrorEvent) => {
worker.removeEventListener("error", onError);
worker.removeEventListener("message", onMessage);
reject(error);
};
const onMessage = (message: MessageEvent) => {
const data = message.data;
switch (data.id) {
case "getExternalResource": {
getExternalResource(data.uri).then((value) => {
worker.postMessage({ id: "getExternalResource.resolve", index: data.index, value: value }, [value]);
}, (reason) => {
worker.postMessage({ id: "getExternalResource.reject", index: data.index, reason: reason });
});
break;
}
case "validate.resolve": {
worker.removeEventListener("error", onError);
worker.removeEventListener("message", onMessage);
resolve(data.value);
worker.terminate();
break;
}
case "validate.reject": {
worker.removeEventListener("error", onError);
worker.removeEventListener("message", onMessage);
reject(data.reason);
worker.terminate();
}
}
};
worker.addEventListener("error", onError);
worker.addEventListener("message", onMessage);
worker.postMessage({ id: "init", url: this.Configuration.url });
worker.postMessage({ id: "validate", data: data, rootUrl: rootUrl, fileName: fileName });
});
}
else {
if (!this._LoadScriptPromise) {
this._LoadScriptPromise = Tools.LoadScriptAsync(this.Configuration.url);
}
return this._LoadScriptPromise.then(() => {
return validateAsync(data, rootUrl, fileName, getExternalResource);
});
}
}
}