ReaderParser.parseSceneGraph = function(node, options) { if (node.Version !== undefined && node.Version > 0) { utils.time('osgjs.metric:ReaderParser.parseSceneGraph', notify.INFO); var key; for (var prop in node) { if (prop !== 'Generator' && prop !== 'Version') { key = prop; break; } } if (key) { var obj = {}; obj[key] = node[key]; var input = ReaderParser.registry().clone(); input.setJSON(obj); // copy global options and override with user options var opt = utils.objectMix( utils.objectMix({}, ReaderParser.registry().getOptions()), options || {} ); input.setOptions(opt); var object = input.readObject(); utils.timeEnd('osgjs.metric:ReaderParser.parseSceneGraph', notify.INFO); return object; } else { notify.log("can't parse scenegraph " + node, notify.INFO); } } else { utils.time('osgjs.metric:ReaderParser.parseSceneGraphDeprecated', notify.INFO); var nodeOld = ReaderParser.parseSceneGraphDeprecated(node); utils.timeEnd('osgjs.metric:ReaderParser.parseSceneGraphDeprecated', notify.INFO); return nodeOld; } return undefined; };
readNodeURL: function(url, opt) { var options = opt; if (options === undefined) { options = this._defaultOptions; } // hook reader if (options.readNodeURL) { // be carefull if you plan to call hook the call and after // call the original readNodeURL, you will need to remove // from options the readNodeURL if you dont want an infinte // recursion call return options.readNodeURL.call(this, url, options); } url = this.computeURL(url); var that = this; // copy because we are going to modify it to have relative prefix to load assets options = utils.objectMix({}, options); // automatic prefix if non specfied if (!options.prefixURL) { var prefix = that.getPrefixURL(); var index = url.lastIndexOf('/'); if (index !== -1) { prefix = url.substring(0, index + 1); } options.prefixURL = prefix; } var ReaderParser = require('osgDB/readerParser').default; var readSceneGraph = function(data) { return ReaderParser.parseSceneGraph(data, options).then(function(child) { notify.log('loaded ' + url); return child; }); }; var ungzipFile = function(arrayBuffer) { function pad(n) { return n.length < 2 ? '0' + n : n; } function uintToString(uintArray) { var str = ''; for (var i = 0, len = uintArray.length; i < len; ++i) { str += '%' + pad(uintArray[i].toString(16)); } str = decodeURIComponent(str); return str; } var unpacked = arrayBuffer; if (zlib.isGunzipBuffer(arrayBuffer)) { unpacked = zlib.gunzip(arrayBuffer); } var typedArray = new Uint8Array(unpacked); var str = uintToString(typedArray); return str; }; utils.time('osgjs.metric:Input.readNodeURL', notify.INFO); // try to get the file as responseText to parse JSON var fileTextPromise = that.requestFile(url); return fileTextPromise .then(function(str) { var data; try { data = JSON.parse(str); } catch (error) { // can't parse try with ungzip code path notify.error('cant parse url ' + url + ' try to gunzip'); } // we have the json, read it if (data) return readSceneGraph(data); // no data try with gunzip var fileGzipPromise = that.requestFile(url, { responseType: 'arraybuffer' }); return fileGzipPromise .then(function(file) { var strUnzip = ungzipFile(file); data = JSON.parse(strUnzip); return readSceneGraph(data); }) .catch(function(status) { var err = 'cant read file ' + url + ' status ' + status; notify.error(err); return err; }); }) .catch(function(status) { var err = 'cant get file ' + url + ' status ' + status; notify.error(err); return err; }) .finally(function() { // Stop the timer utils.timeEnd('osgjs.metric:Input.readNodeURL'); }); },