@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
46 lines (40 loc) • 1.36 kB
text/typescript
import {BufferGeometry} from 'three';
import {Poly} from '../../../engine/Poly';
import {CoreBaseLoader} from '../_Base';
import {JSONDataParser} from './JSONDataParser';
import {BaseNodeType} from '../../../engine/nodes/_Base';
export interface JsonDataLoaderOptions {
dataKeysPrefix?: string;
skipEntries?: string;
doConvert?: boolean;
convertToNumeric?: string;
}
export class JsonDataLoader extends CoreBaseLoader<string> {
private _parser: JSONDataParser;
constructor(url: string, options: JsonDataLoaderOptions = {}, protected override _node?: BaseNodeType) {
super(url, _node);
this._parser = new JSONDataParser(options);
}
async load(
success_callback: (geometry: BufferGeometry) => void,
progress_callback: (() => void) | undefined,
error_callback: (error: ErrorEvent) => void | undefined
) {
const url = this._urlToLoad();
fetch(url)
.then(async (response) => {
let json = await response.json();
const dataKeysPrefix = this._parser.dataKeysPrefix();
if (dataKeysPrefix != null && dataKeysPrefix != '') {
json = this._parser.get_prefixed_json(json, dataKeysPrefix.split('.'));
}
this._parser.setJSON(json);
const object = this._parser.createObject();
success_callback(object);
})
.catch((error: ErrorEvent) => {
Poly.error('error', error);
error_callback(error);
});
}
}