babylon.glTF2FileLoader.min.js 31 KB

1
  1. var BABYLON;!(function(e){var t;!(function(e){e[e.AUTO=0]="AUTO",e[e.PASS_THROUGH=1]="PASS_THROUGH",e[e.FORCE_RIGHT_HANDED=2]="FORCE_RIGHT_HANDED"})(t=e.GLTFLoaderCoordinateSystemMode||(e.GLTFLoaderCoordinateSystemMode={}));var r=(function(){function e(){this.coordinateSystemMode=t.AUTO,this.name="gltf",this.extensions={".gltf":{isBinary:!1},".glb":{isBinary:!0}}}return e.prototype.importMeshAsync=function(t,r,n,a,o,i,s){var l=e._parse(n,s);if(l){var u=this._getLoader(l,s);u&&u.importMeshAsync(t,r,l,a,o,i,s)}},e.prototype.loadAsync=function(t,r,n,a,o,i){var s=e._parse(r,i);if(s){var l=this._getLoader(s,i);if(l)return l.loadAsync(t,s,n,a,o,i)}},e.prototype.canDirectLoad=function(e){return-1!==e.indexOf("scene")&&-1!==e.indexOf("node")},e._parse=function(t,r){if(t instanceof ArrayBuffer)return e._parseBinary(t,r);try{return{json:JSON.parse(t),bin:null}}catch(e){return r(e.message),null}},e.prototype._getLoader=function(t,r){var n={major:2,minor:0},a=t.json.asset||{},o=e._parseVersion(a.version);if(!o)return r("Invalid version: "+a.version),null;if(void 0!==a.minVersion){var i=e._parseVersion(a.minVersion);if(!i)return r("Invalid minimum version: "+a.minVersion),null;if(e._compareVersion(i,n)>0)return r("Incompatible minimum version: "+a.minVersion),null}var s={1:e.CreateGLTFLoaderV1,2:e.CreateGLTFLoaderV2},l=s[o.major];return l?l(this):(r("Unsupported version: "+a.version),null)},e._parseBinary=function(t,r){var a={Magic:1179937895},o=new n(t),i=o.readUint32();if(i!==a.Magic)return r("Unexpected magic: "+i),null;var s=o.readUint32();switch(s){case 1:return e._parseV1(o,r);case 2:return e._parseV2(o,r)}return r("Unsupported version: "+s),null},e._parseV1=function(t,r){var n={JSON:0},a=t.readUint32();if(a!=t.getLength())return r("Length in header does not match actual data length: "+a+" != "+t.getLength()),null;var o,i=t.readUint32(),s=t.readUint32();switch(s){case n.JSON:o=JSON.parse(e._decodeBufferToText(t.readUint8Array(i)));break;default:return r("Unexpected content format: "+s),null}var l=t.getLength()-t.getPosition();return{json:o,bin:t.readUint8Array(l)}},e._parseV2=function(t,r){var n={JSON:1313821514,BIN:5130562},a=t.readUint32();if(a!==t.getLength())return r("Length in header does not match actual data length: "+a+" != "+t.getLength()),null;var o=t.readUint32(),i=t.readUint32();if(i!==n.JSON)return r("First chunk format is not JSON"),null;for(var s=JSON.parse(e._decodeBufferToText(t.readUint8Array(o))),l=null;t.getPosition()<t.getLength();)switch(o=t.readUint32(),i=t.readUint32()){case n.JSON:return r("Unexpected JSON chunk"),null;case n.BIN:l=t.readUint8Array(o);break;default:t.skipBytes(o)}return{json:s,bin:l}},e._parseVersion=function(e){if(!e)return null;var t=e.split(".");if(2!=t.length)return null;var r=+t[0];if(isNaN(r))return null;var n=+t[1];return isNaN(n)?null:{major:r,minor:n}},e._compareVersion=function(e,t){return e.major>t.major?1:e.major<t.major?-1:e.minor>t.minor?1:e.minor<t.minor?-1:0},e._decodeBufferToText=function(e){for(var t="",r=e.byteLength,n=0;n<r;++n)t+=String.fromCharCode(e[n]);return t},e.HomogeneousCoordinates=!1,e.IncrementalLoading=!0,e})();e.GLTFFileLoader=r;var n=(function(){function e(e){this._arrayBuffer=e,this._dataView=new DataView(e),this._byteOffset=0}return e.prototype.getPosition=function(){return this._byteOffset},e.prototype.getLength=function(){return this._arrayBuffer.byteLength},e.prototype.readUint32=function(){var e=this._dataView.getUint32(this._byteOffset,!0);return this._byteOffset+=4,e},e.prototype.readUint8Array=function(e){var t=new Uint8Array(this._arrayBuffer,this._byteOffset,e);return this._byteOffset+=e,t},e.prototype.skipBytes=function(e){this._byteOffset+=e},e})();e.SceneLoader&&e.SceneLoader.RegisterPlugin(new r)})(BABYLON||(BABYLON={}));var BABYLON;!(function(e){!(function(e){!(function(e){e[e.BYTE=5120]="BYTE",e[e.UNSIGNED_BYTE=5121]="UNSIGNED_BYTE",e[e.SHORT=5122]="SHORT",e[e.UNSIGNED_SHORT=5123]="UNSIGNED_SHORT",e[e.UNSIGNED_INT=5125]="UNSIGNED_INT",e[e.FLOAT=5126]="FLOAT"})(e.EComponentType||(e.EComponentType={}));!(function(e){e[e.POINTS=0]="POINTS",e[e.LINES=1]="LINES",e[e.LINE_LOOP=2]="LINE_LOOP",e[e.LINE_STRIP=3]="LINE_STRIP",e[e.TRIANGLES=4]="TRIANGLES",e[e.TRIANGLE_STRIP=5]="TRIANGLE_STRIP",e[e.TRIANGLE_FAN=6]="TRIANGLE_FAN"})(e.EMeshPrimitiveMode||(e.EMeshPrimitiveMode={}));!(function(e){e[e.NEAREST=9728]="NEAREST",e[e.LINEAR=9729]="LINEAR"})(e.ETextureMagFilter||(e.ETextureMagFilter={}));!(function(e){e[e.NEAREST=9728]="NEAREST",e[e.LINEAR=9729]="LINEAR",e[e.NEAREST_MIPMAP_NEAREST=9984]="NEAREST_MIPMAP_NEAREST",e[e.LINEAR_MIPMAP_NEAREST=9985]="LINEAR_MIPMAP_NEAREST",e[e.NEAREST_MIPMAP_LINEAR=9986]="NEAREST_MIPMAP_LINEAR",e[e.LINEAR_MIPMAP_LINEAR=9987]="LINEAR_MIPMAP_LINEAR"})(e.ETextureMinFilter||(e.ETextureMinFilter={}));!(function(e){e[e.CLAMP_TO_EDGE=33071]="CLAMP_TO_EDGE",e[e.MIRRORED_REPEAT=33648]="MIRRORED_REPEAT",e[e.REPEAT=10497]="REPEAT"})(e.ETextureWrapMode||(e.ETextureWrapMode={}))})(e.GLTF2||(e.GLTF2={}))})(BABYLON||(BABYLON={}));var BABYLON;!(function(e){!(function(t){var r=(function(){function r(t){this._renderReady=!1,this._disposed=!1,this._blockPendingTracking=!1,this._renderReadyObservable=new e.Observable,this._renderPendingCount=0,this._loaderPendingCount=0,this._parent=t}return r.RegisterExtension=function(n){if(r.Extensions[n.name])return void e.Tools.Error("Extension with the same name '"+n.name+"' already exists");r.Extensions[n.name]=n,t.GLTFLoaderExtension._Extensions.push(n)},Object.defineProperty(r.prototype,"gltf",{get:function(){return this._gltf},enumerable:!0,configurable:!0}),Object.defineProperty(r.prototype,"babylonScene",{get:function(){return this._babylonScene},enumerable:!0,configurable:!0}),r.prototype.executeWhenRenderReady=function(e){this._renderReady?e():this._renderReadyObservable.add(e)},r.prototype.dispose=function(){this._disposed||(this._disposed=!0,this._gltf.textures&&this._gltf.textures.forEach((function(e){e.url&&URL.revokeObjectURL(e.url)})),this._gltf=void 0,this._babylonScene=void 0,this._rootUrl=void 0,this._defaultMaterial=void 0,this._successCallback=void 0,this._errorCallback=void 0,this._renderReady=!1,this._renderReadyObservable.clear(),this._renderPendingCount=0,this._loaderPendingCount=0)},r.prototype.importMeshAsync=function(e,t,r,n,a,o,i){var s=this;this._loadAsync(e,t,r,n,(function(){a(s._getMeshes(),null,s._getSkeletons())}),o,i)},r.prototype.loadAsync=function(e,t,r,n,a,o){this._loadAsync(null,e,t,r,n,a,o)},r.prototype._loadAsync=function(e,t,r,n,a,o,i){this._loadData(r),this._babylonScene=t,this._rootUrl=n,this._successCallback=a,this._progressCallback=o,this._errorCallback=i,this.addPendingData(this),this._loadScene(e),this._loadAnimations(),this.removePendingData(this)},r.prototype._onError=function(e){this._errorCallback(e),this.dispose()},r.prototype._onProgress=function(e){this._progressCallback(e)},r.prototype._onRenderReady=function(){switch(this._parent.coordinateSystemMode){case e.GLTFLoaderCoordinateSystemMode.AUTO:this._babylonScene.useRightHandedSystem||this._addRightHandToLeftHandRootTransform();break;case e.GLTFLoaderCoordinateSystemMode.PASS_THROUGH:break;case e.GLTFLoaderCoordinateSystemMode.FORCE_RIGHT_HANDED:this._babylonScene.useRightHandedSystem=!0;break;default:e.Tools.Error("Invalid coordinate system mode ("+this._parent.coordinateSystemMode+")")}this._showMeshes(),this._startAnimations(),this._successCallback(),this._renderReadyObservable.notifyObservers(this)},r.prototype._onLoaderComplete=function(){this._parent.onComplete&&this._parent.onComplete()},r.prototype._onLoaderFirstLODComplete=function(){this._parent.onFirstLODComplete&&this._parent.onFirstLODComplete()},r.prototype._loadData=function(t){this._gltf=t.json;var r,n=this._gltf.buffers;n&&void 0===n[0].uri&&(r=n[0]),t.bin&&(r?r.byteLength!=t.bin.byteLength&&e.Tools.Warn("Binary buffer length ("+r.byteLength+") from JSON does not match chunk length ("+t.bin.byteLength+")"):e.Tools.Warn("Unexpected BIN chunk"),r.loadedData=t.bin)},r.prototype._addRightHandToLeftHandRootTransform=function(){this._rootMesh=new e.Mesh("root",this._babylonScene),this._rootMesh.isVisible=!1,this._rootMesh.scaling=new e.Vector3(1,1,-1),this._rootMesh.rotation.y=Math.PI;var t=this._gltf.nodes;if(t)for(var r=0;r<t.length;r++){var n=t[r].babylonMesh;n&&!n.parent&&(n.parent=this._rootMesh)}},r.prototype._getMeshes=function(){var e=[];this._rootMesh&&e.push(this._rootMesh);var t=this._gltf.nodes;return t&&t.forEach((function(t){t.babylonMesh&&e.push(t.babylonMesh)})),e},r.prototype._getSkeletons=function(){var t=[],r=this._gltf.skins;return r&&r.forEach((function(r){r.babylonSkeleton instanceof e.Skeleton&&t.push(r.babylonSkeleton)})),t},r.prototype._getAnimationTargets=function(){var e=[],t=this._gltf.animations;return t&&t.forEach((function(t){e.push.apply(e,t.targets)})),e},r.prototype._showMeshes=function(){this._getMeshes().forEach((function(e){return e.isVisible=!0}))},r.prototype._startAnimations=function(){var e=this;this._getAnimationTargets().forEach((function(t){return e._babylonScene.beginAnimation(t,0,Number.MAX_VALUE,!0)}))},r.prototype._loadScene=function(e){var t=this,r=this._gltf.scenes[this._gltf.scene||0],n=r.nodes;this._traverseNodes(n,(function(e,t,r){return e.index=t,e.parent=r,!0}));var a=this._gltf.materials;if(a&&a.forEach((function(e,t){return e.index=t})),e){e instanceof Array||(e=[e]);var o=new Array;this._traverseNodes(n,(function(t){return-1===e.indexOf(t.name)||(o.push(t.index),!1)})),n=o}this._traverseNodes(n,(function(e){return t._loadSkin(e)})),this._traverseNodes(n,(function(e){return t._loadMesh(e)}))},r.prototype._loadSkin=function(t){var r=this;if(void 0!==t.skin){var n=this._gltf.skins[t.skin],a="skeleton"+t.skin;n.babylonSkeleton=new e.Skeleton(n.name||a,a,this._babylonScene),n.index=t.skin;for(var o=0;o<n.joints.length;o++)this._createBone(this._gltf.nodes[n.joints[o]],n);if(void 0===n.skeleton)throw new Error("Not implemented");if(void 0===n.inverseBindMatrices)throw new Error("Not implemented");var i=this._gltf.accessors[n.inverseBindMatrices];this._loadAccessorAsync(i,(function(e){r._traverseNode(n.skeleton,(function(t,a,o){return r._updateBone(t,o,n,e)}))}))}return!0},r.prototype._updateBone=function(t,r,n,a){var o=n.joints.indexOf(t.index);-1===o&&this._createBone(t,n);var i=t.babylonSkinToBones[n.index],s=-1===o?e.Matrix.Identity():e.Matrix.FromArray(a,16*o);return s.invertToRef(s),r&&(i.setParent(r.babylonSkinToBones[n.index],!1),s.multiplyToRef(i.getParent().getInvertedAbsoluteTransform(),s)),i.updateMatrix(s),!0},r.prototype._createBone=function(t,r){var n=new e.Bone(t.name||"bone"+t.index,r.babylonSkeleton);return t.babylonSkinToBones=t.babylonSkinToBones||{},t.babylonSkinToBones[r.index]=n,t.babylonAnimationTargets=t.babylonAnimationTargets||[],t.babylonAnimationTargets.push(n),n},r.prototype._loadMesh=function(t){var r=new e.Mesh(t.name||"mesh"+t.index,this._babylonScene);if(r.isVisible=!1,this._loadTransform(t,r),void 0!==t.mesh){var n=this._gltf.meshes[t.mesh];this._loadMeshData(t,n,r)}if(r.parent=t.parent?t.parent.babylonMesh:null,t.babylonMesh=r,t.babylonAnimationTargets=t.babylonAnimationTargets||[],t.babylonAnimationTargets.push(t.babylonMesh),void 0!==t.skin){var a=this._gltf.skins[t.skin];r.skeleton=a.babylonSkeleton}return t.camera,!0},r.prototype._loadMeshData=function(r,n,a){var o=this;a.name=n.name||a.name;var i=new e.MultiMaterial(a.name,this._babylonScene);a.material=i;var s=new e.Geometry(a.name,this._babylonScene,null,!1,a),l=new e.VertexData;l.positions=[],l.indices=[];for(var u=[],d=0,c=n.primitives.length,f=this,h=0;h<c;h++)!(function(h){var _=n.primitives[h];if(_.mode&&_.mode!==t.EMeshPrimitiveMode.TRIANGLES)throw new Error("Not implemented");f._createMorphTargets(r,n,_,a),f._loadVertexDataAsync(_,(function(t){o._loadMorphTargetsData(n,_,t,a),u.push({materialIndex:h,verticesStart:l.positions.length,verticesCount:t.positions.length,indicesStart:l.indices.length,indicesCount:t.indices.length,loadMaterial:function(){if(void 0===_.material)i.subMaterials[h]=o._getDefaultMaterial();else{var e=o._gltf.materials[_.material];o.loadMaterial(e,(function(t,r){r&&o._parent.onMaterialLoaded&&o._parent.onMaterialLoaded(t),o._parent.onBeforeMaterialReadyAsync?(o.addLoaderPendingData(e),o._parent.onBeforeMaterialReadyAsync(t,a,null!=i.subMaterials[h],(function(){i.subMaterials[h]=t,o.removeLoaderPendingData(e)}))):i.subMaterials[h]=t}))}}}),l.merge(t),++d===c&&(s.setAllVerticesData(l,!1),u.forEach((function(e){return e.loadMaterial()})),a.subMeshes=[],u.forEach((function(t){return new e.SubMesh(t.materialIndex,t.verticesStart,t.verticesCount,t.indicesStart,t.indicesCount,a)})))}))})(h)},r.prototype._loadVertexDataAsync=function(t,r){var n=this,a=t.attributes;if(!a)return void this._onError("Primitive has no attributes");var o,i=new e.VertexData,s=0,l=Object.keys(a).length,u=this;for(var d in a)!(function(d){o=u._gltf.accessors[a[d]],u._loadAccessorAsync(o,(function(a){switch(d){case"NORMAL":i.normals=a;break;case"POSITION":i.positions=a;break;case"TANGENT":i.tangents=a;break;case"TEXCOORD_0":i.uvs=a;break;case"TEXCOORD_1":i.uvs2=a;break;case"JOINTS_0":i.matricesIndices=new Float32Array(Array.prototype.slice.apply(a));break;case"WEIGHTS_0":i.matricesWeights=a;break;case"COLOR_0":i.colors=a;break;default:e.Tools.Warn("Ignoring unrecognized semantic '"+d+"'")}if(++s===l){var o=n._gltf.accessors[t.indices];o?n._loadAccessorAsync(o,(function(e){i.indices=e,r(i)})):(i.indices=new Uint32Array(i.positions.length/3),i.indices.forEach((function(e,t){return i.indices[t]=t})),r(i))}}))})(d)},r.prototype._createMorphTargets=function(t,r,n,a){var o=n.targets;if(o){a.morphTargetManager||(a.morphTargetManager=new e.MorphTargetManager);for(var i=0;i<o.length;i++){var s=t.weights?t.weights[i]:r.weights?r.weights[i]:0;a.morphTargetManager.addTarget(new e.MorphTarget("morphTarget"+i,s))}}},r.prototype._loadMorphTargetsData=function(r,n,a,o){var i=n.targets;if(i)for(var s,l,u=this,d=0;d<i.length;d++)!(function(){var r=o.morphTargetManager.getTarget(d);s=i[d];for(var n in s)!(function(n){l=u._gltf.accessors[s[n]],u._loadAccessorAsync(l,(function(o){l.name&&(r.name=l.name);var i=o;switch(n){case"NORMAL":t.GLTFUtils.ForEach(i,(function(e,t){return i[t]+=a.normals[t]})),r.setNormals(i);break;case"POSITION":t.GLTFUtils.ForEach(i,(function(e,t){return i[t]+=a.positions[t]})),r.setPositions(i);break;case"TANGENT":for(var s=0,u=0;s<i.length;s++,u++)i[s]+=a.tangents[u],(s+1)%3==0&&u++;r.setTangents(i);break;default:e.Tools.Warn("Ignoring unrecognized semantic '"+n+"'")}}))})(n)})()},r.prototype._loadTransform=function(t,r){var n=e.Vector3.Zero(),a=e.Quaternion.Identity(),o=e.Vector3.One();if(t.matrix){e.Matrix.FromArray(t.matrix).decompose(o,a,n)}else t.translation&&(n=e.Vector3.FromArray(t.translation)),t.rotation&&(a=e.Quaternion.FromArray(t.rotation)),t.scale&&(o=e.Vector3.FromArray(t.scale));r.position=n,r.rotationQuaternion=a,r.scaling=o},r.prototype._traverseNodes=function(e,t,r){void 0===r&&(r=null);for(var n=0;n<e.length;n++)this._traverseNode(e[n],t,r)},r.prototype._traverseNode=function(e,t,r){void 0===r&&(r=null);var n=this._gltf.nodes[e];if(t(n,e,r)&&n.children)for(var a=0;a<n.children.length;a++)this._traverseNode(n.children[a],t,n)},r.prototype._loadAnimations=function(){var e=this._gltf.animations;if(e&&0!==e.length)for(var t=0;t<e.length;t++)for(var r=e[t],n=0;n<r.channels.length;n++)this._loadAnimationChannel(r,t,n)},r.prototype._loadAnimationChannel=function(t,r,n){var a=t.channels[n],o=a.sampler,i=t.samplers[o],s=this._gltf.nodes[a.target.node];if(!s)return void e.Tools.Warn("Animation channel target node ("+a.target.node+") does not exist");var l={translation:"position",rotation:"rotationQuaternion",scale:"scaling",weights:"influence"}[a.target.path];if(!l)return void e.Tools.Warn("Animation channel target path '"+a.target.path+"' is not valid");var u,d,c={position:e.Animation.ANIMATIONTYPE_VECTOR3,rotationQuaternion:e.Animation.ANIMATIONTYPE_QUATERNION,scaling:e.Animation.ANIMATIONTYPE_VECTOR3,influence:e.Animation.ANIMATIONTYPE_FLOAT}[l],f=function(){if(u&&d){for(var n=0,a={position:function(){var t=e.Vector3.FromArray(d,n);return n+=3,t},rotationQuaternion:function(){var t=e.Quaternion.FromArray(d,n);return n+=4,t},scaling:function(){var t=e.Vector3.FromArray(d,n);return n+=3,t},influence:function(){for(var e=s.babylonMesh.morphTargetManager.numTargets,t=new Array(e),r=0;r<e;r++)t[r]=d[n++];return t}}[l],o={LINEAR:function(e){return{frame:u[e],value:a()}},CUBICSPLINE:function(e){return{frame:u[e],inTangent:a(),value:a(),outTangent:a()}}}[i.interpolation],f=new Array(u.length),h=0;h<u.length;h++)f[h]=o(h);if(t.targets=t.targets||[],"influence"===l)for(var _=s.babylonMesh.morphTargetManager,p=0;p<_.numTargets;p++){var T=_.getTarget(p),y=(t.name||"anim"+r)+"_"+p,A=new e.Animation(y,l,1,c);A.setKeys(f.map((function(e){return{frame:e.frame,inTangent:e.inTangent?e.inTangent[p]:void 0,value:e.value[p],outTangent:e.outTangent?e.outTangent[p]:void 0}}))),T.animations.push(A),t.targets.push(T)}else{var y=t.name||"anim"+r,A=new e.Animation(y,l,1,c);A.setKeys(f);for(var g=0;g<s.babylonAnimationTargets.length;g++){var E=s.babylonAnimationTargets[g];E.animations.push(A.clone()),t.targets.push(E)}}}};this._loadAccessorAsync(this._gltf.accessors[i.input],(function(e){u=e,f()})),this._loadAccessorAsync(this._gltf.accessors[i.output],(function(e){d=e,f()}))},r.prototype._loadBufferAsync=function(r,n){var a=this,o=this._gltf.buffers[r];if(this.addPendingData(o),o.loadedData)setTimeout((function(){n(o.loadedData),a.removePendingData(o)}));else if(t.GLTFUtils.IsBase64(o.uri)){var i=t.GLTFUtils.DecodeBase64(o.uri);o.loadedData=new Uint8Array(i),setTimeout((function(){n(o.loadedData),a.removePendingData(o)}))}else o.loadedObservable?o.loadedObservable.add((function(e){n(e.loadedData),a.removePendingData(e)})):(o.loadedObservable=new e.Observable,o.loadedObservable.add((function(e){n(e.loadedData),a.removePendingData(e)})),e.Tools.LoadFile(this._rootUrl+o.uri,(function(e){o.loadedData=new Uint8Array(e),o.loadedObservable.notifyObservers(o),o.loadedObservable=null}),(function(e){a._disposed||a._onProgress(e)}),this._babylonScene.database,!0,(function(e){a._disposed||(a._onError("Failed to load file '"+o.uri+"': "+e.status+" "+e.statusText),a.removePendingData(o))})))},r.prototype._buildInt8ArrayBuffer=function(e,t,r,n,a){if(!n)return new Int8Array(e,t,r);var o=new Int8Array(e,t),i=new Int8Array(r);return this._extractInterleavedData(o,i,a,n,i.length),i},r.prototype._buildUint8ArrayBuffer=function(e,t,r,n,a){if(!n)return new Uint8Array(e,t,r);var o=new Uint8Array(e,t),i=new Uint8Array(r);return this._extractInterleavedData(o,i,a,n,i.length),i},r.prototype._buildInt16ArrayBuffer=function(e,t,r,n,a){if(!n)return new Int16Array(e,t,r);var o=new Int16Array(e,t),i=new Int16Array(r);return this._extractInterleavedData(o,i,a,n/2,i.length),i},r.prototype._buildUint16ArrayBuffer=function(e,t,r,n,a){if(!n)return new Uint16Array(e,t,r);var o=new Uint16Array(e,t),i=new Uint16Array(r);return this._extractInterleavedData(o,i,a,n/2,i.length),i},r.prototype._buildUint32ArrayBuffer=function(e,t,r,n,a){if(!n)return new Uint32Array(e,t,r);var o=new Uint32Array(e,t),i=new Uint32Array(r);return this._extractInterleavedData(o,i,a,n/4,i.length),i},r.prototype._buildFloat32ArrayBuffer=function(e,t,r,n,a){if(!n)return new Float32Array(e,t,r);var o=new Float32Array(e,t),i=new Float32Array(r);return this._extractInterleavedData(o,i,a,n/4,i.length),i},r.prototype._extractInterleavedData=function(e,t,r,n,a){for(var o=0,i=0,s=r;o<a;){for(var l=0;l<s;l++)t[o]=e[i+l],o++;i+=n}},r.prototype._loadBufferViewAsync=function(e,r,n,a,o,i){var s=this;r+=e.byteOffset||0,this._loadBufferAsync(e.buffer,(function(l){if(r+n>l.byteLength)return void s._onError("Buffer access is out of range");var u=l.buffer;r+=l.byteOffset;var d;switch(o){case t.EComponentType.BYTE:d=s._buildInt8ArrayBuffer(u,r,n,e.byteStride,a);break;case t.EComponentType.UNSIGNED_BYTE:d=s._buildUint8ArrayBuffer(u,r,n,e.byteStride,a);break;case t.EComponentType.SHORT:d=s._buildInt16ArrayBuffer(u,r,n,e.byteStride,a);break;case t.EComponentType.UNSIGNED_SHORT:d=s._buildUint16ArrayBuffer(u,r,n,e.byteStride,a);break;case t.EComponentType.UNSIGNED_INT:d=s._buildUint32ArrayBuffer(u,r,n,e.byteStride,a);break;case t.EComponentType.FLOAT:d=s._buildFloat32ArrayBuffer(u,r,n,e.byteStride,a);break;default:return void s._onError("Invalid component type ("+o+")")}i(d)}))},r.prototype._loadAccessorAsync=function(e,t){var r=this._gltf.bufferViews[e.bufferView],n=e.byteOffset||0,a=this._getByteStrideFromType(e),o=e.count*a;this._loadBufferViewAsync(r,n,o,a,e.componentType,t)},r.prototype._getByteStrideFromType=function(e){switch(e.type){case"SCALAR":return 1;case"VEC2":return 2;case"VEC3":return 3;case"VEC4":case"MAT2":return 4;case"MAT3":return 9;case"MAT4":return 16;default:return this._onError("Invalid accessor type ("+e.type+")"),0}},Object.defineProperty(r.prototype,"blockPendingTracking",{set:function(e){this._blockPendingTracking=e},enumerable:!0,configurable:!0}),r.prototype.addPendingData=function(e){this._renderReady||this._renderPendingCount++,this.addLoaderPendingData(e)},r.prototype.removePendingData=function(e){this._renderReady||0==--this._renderPendingCount&&(this._renderReady=!0,this._onRenderReady()),this.removeLoaderPendingData(e)},r.prototype.addLoaderNonBlockingPendingData=function(e){this._nonBlockingData||(this._nonBlockingData=new Array),this._nonBlockingData.push(e)},r.prototype.addLoaderPendingData=function(e){if(this._blockPendingTracking)return void this.addLoaderNonBlockingPendingData(e);this._loaderPendingCount++},r.prototype.removeLoaderPendingData=function(e){var t=this._nonBlockingData?this._nonBlockingData.indexOf(e):-1;-1!==t?this._nonBlockingData.splice(t,1):0==--this._loaderPendingCount&&this._onLoaderFirstLODComplete(),this._nonBlockingData&&0!==this._nonBlockingData.length||0!==this._loaderPendingCount||(this._onLoaderComplete(),this.dispose())},r.prototype._getDefaultMaterial=function(){if(!this._defaultMaterial){var t="__gltf_default",r=this._babylonScene.getMaterialByName(t);r||(r=new e.PBRMaterial(t,this._babylonScene),r.sideOrientation=e.Material.CounterClockWiseSideOrientation,r.metallic=1,r.roughness=1),this._defaultMaterial=r}return this._defaultMaterial},r.prototype._loadMaterialMetallicRoughnessProperties=function(t){var r=t.babylonMaterial;r.metallic=1,r.roughness=1;var n=t.pbrMetallicRoughness;n&&(r.albedoColor=n.baseColorFactor?e.Color3.FromArray(n.baseColorFactor):new e.Color3(1,1,1),r.metallic=void 0===n.metallicFactor?1:n.metallicFactor,r.roughness=void 0===n.roughnessFactor?1:n.roughnessFactor,n.baseColorTexture&&(r.albedoTexture=this.loadTexture(n.baseColorTexture)),n.metallicRoughnessTexture&&(r.metallicTexture=this.loadTexture(n.metallicRoughnessTexture),r.useMetallnessFromMetallicTextureBlue=!0,r.useRoughnessFromMetallicTextureGreen=!0,r.useRoughnessFromMetallicTextureAlpha=!1),this.loadMaterialAlphaProperties(t,n.baseColorFactor))},r.prototype.loadMaterial=function(e,r){if(e.babylonMaterial)return void r(e.babylonMaterial,!1);t.GLTFLoaderExtension.LoadMaterial(this,e,r)||(this.createPbrMaterial(e),this.loadMaterialBaseProperties(e),this._loadMaterialMetallicRoughnessProperties(e),r(e.babylonMaterial,!0))},r.prototype.createPbrMaterial=function(t){var r=new e.PBRMaterial(t.name||"mat"+t.index,this._babylonScene);r.sideOrientation=e.Material.CounterClockWiseSideOrientation,t.babylonMaterial=r},r.prototype.loadMaterialBaseProperties=function(t){var r=t.babylonMaterial;r.emissiveColor=t.emissiveFactor?e.Color3.FromArray(t.emissiveFactor):new e.Color3(0,0,0),t.doubleSided&&(r.backFaceCulling=!1,r.twoSidedLighting=!0),t.normalTexture&&(r.bumpTexture=this.loadTexture(t.normalTexture),r.invertNormalMapX=!0,r.invertNormalMapY=!1,void 0!==t.normalTexture.scale&&(r.bumpTexture.level=t.normalTexture.scale)),t.occlusionTexture&&(r.ambientTexture=this.loadTexture(t.occlusionTexture),r.useAmbientInGrayScale=!0,void 0!==t.occlusionTexture.strength&&(r.ambientTextureStrength=t.occlusionTexture.strength)),t.emissiveTexture&&(r.emissiveTexture=this.loadTexture(t.emissiveTexture))},r.prototype.loadMaterialAlphaProperties=function(t,r){var n=t.babylonMaterial,a=t.alphaMode||"OPAQUE";switch(a){case"OPAQUE":break;case"MASK":case"BLEND":r&&(n.alpha=r[3]),n.albedoTexture&&(n.albedoTexture.hasAlpha=!0,n.useAlphaFromAlbedoTexture="BLEND"===a);break;default:e.Tools.Warn("Invalid alpha mode '"+t.alphaMode+"'")}n.alphaCutOff=void 0===t.alphaCutoff?.5:t.alphaCutoff},r.prototype.loadTexture=function(r){var n=this,a=this._gltf.textures[r.index],o=r.texCoord||0;if(!a||void 0===a.source)return null;var i=this._gltf.images[a.source],s=void 0===a.sampler?{}:this._gltf.samplers[a.sampler],l=s.minFilter===t.ETextureMinFilter.NEAREST||s.minFilter===t.ETextureMinFilter.LINEAR,u=t.GLTFUtils.GetTextureSamplingMode(s.magFilter,s.minFilter);this.addPendingData(a);var d=new e.Texture(null,this._babylonScene,l,!1,u,function(){n._disposed||n.removePendingData(a)},function(){n._disposed||(n._onError("Failed to load texture '"+i.uri+"'"),n.removePendingData(a))});if(a.url)d.updateURL(a.url);else if(a.dataReadyObservable)a.dataReadyObservable.add((function(e){d.updateURL(e.url)}));else{a.dataReadyObservable=new e.Observable,a.dataReadyObservable.add((function(e){d.updateURL(e.url)}));var c=function(e){a.url=URL.createObjectURL(new Blob([e],{type:i.mimeType})),a.dataReadyObservable.notifyObservers(a)};if(i.uri)t.GLTFUtils.IsBase64(i.uri)?c(new Uint8Array(t.GLTFUtils.DecodeBase64(i.uri))):e.Tools.LoadFile(this._rootUrl+i.uri,c,(function(e){n._disposed||n._onProgress(e)}),this._babylonScene.database,!0,(function(e){n._onError("Failed to load file '"+i.uri+"': "+e.status+" "+e.statusText)}));else{var f=this._gltf.bufferViews[i.bufferView];this._loadBufferViewAsync(f,0,f.byteLength,1,t.EComponentType.UNSIGNED_BYTE,c)}}return d.coordinatesIndex=o,d.wrapU=t.GLTFUtils.GetTextureWrapMode(s.wrapS),d.wrapV=t.GLTFUtils.GetTextureWrapMode(s.wrapT),d.name=a.name||"texture"+r.index,this._parent.onTextureLoaded&&this._parent.onTextureLoaded(d),d},r.Extensions={},r})();t.GLTFLoader=r,e.GLTFFileLoader.CreateGLTFLoaderV2=function(e){return new r(e)}})(e.GLTF2||(e.GLTF2={}))})(BABYLON||(BABYLON={}));var BABYLON;!(function(e){!(function(t){var r=(function(){function r(){}return r.IsBase64=function(e){return!(e.length<5)&&"data:"===e.substr(0,5)},r.DecodeBase64=function(e){for(var t=atob(e.split(",")[1]),r=t.length,n=new Uint8Array(new ArrayBuffer(r)),a=0;a<r;a++)n[a]=t.charCodeAt(a);return n.buffer},r.ForEach=function(e,t){for(var r=0;r<e.length;r++)t(e[r],r)},r.GetTextureWrapMode=function(r){switch(r=void 0===r?t.ETextureWrapMode.REPEAT:r){case t.ETextureWrapMode.CLAMP_TO_EDGE:return e.Texture.CLAMP_ADDRESSMODE;case t.ETextureWrapMode.MIRRORED_REPEAT:return e.Texture.MIRROR_ADDRESSMODE;case t.ETextureWrapMode.REPEAT:return e.Texture.WRAP_ADDRESSMODE;default:return e.Tools.Warn("Invalid texture wrap mode ("+r+")"),e.Texture.WRAP_ADDRESSMODE}},r.GetTextureSamplingMode=function(r,n){if(r=void 0===r?t.ETextureMagFilter.LINEAR:r,n=void 0===n?t.ETextureMinFilter.LINEAR_MIPMAP_LINEAR:n,r===t.ETextureMagFilter.LINEAR)switch(n){case t.ETextureMinFilter.NEAREST:return e.Texture.LINEAR_NEAREST;case t.ETextureMinFilter.LINEAR:return e.Texture.LINEAR_LINEAR;case t.ETextureMinFilter.NEAREST_MIPMAP_NEAREST:return e.Texture.LINEAR_NEAREST_MIPNEAREST;case t.ETextureMinFilter.LINEAR_MIPMAP_NEAREST:return e.Texture.LINEAR_LINEAR_MIPNEAREST;case t.ETextureMinFilter.NEAREST_MIPMAP_LINEAR:return e.Texture.LINEAR_NEAREST_MIPLINEAR;case t.ETextureMinFilter.LINEAR_MIPMAP_LINEAR:return e.Texture.LINEAR_LINEAR_MIPLINEAR;default:return e.Tools.Warn("Invalid texture minification filter ("+n+")"),e.Texture.LINEAR_LINEAR_MIPLINEAR}else switch(r!==t.ETextureMagFilter.NEAREST&&e.Tools.Warn("Invalid texture magnification filter ("+r+")"),n){case t.ETextureMinFilter.NEAREST:return e.Texture.NEAREST_NEAREST;case t.ETextureMinFilter.LINEAR:return e.Texture.NEAREST_LINEAR;case t.ETextureMinFilter.NEAREST_MIPMAP_NEAREST:return e.Texture.NEAREST_NEAREST_MIPNEAREST;case t.ETextureMinFilter.LINEAR_MIPMAP_NEAREST:return e.Texture.NEAREST_LINEAR_MIPNEAREST;case t.ETextureMinFilter.NEAREST_MIPMAP_LINEAR:return e.Texture.NEAREST_NEAREST_MIPLINEAR;case t.ETextureMinFilter.LINEAR_MIPMAP_LINEAR:return e.Texture.NEAREST_LINEAR_MIPLINEAR;default:return e.Tools.Warn("Invalid texture minification filter ("+n+")"),e.Texture.NEAREST_NEAREST_MIPNEAREST}},r.DecodeBufferToText=function(e){for(var t="",r=e.byteLength,n=0;n<r;++n)t+=String.fromCharCode(e[n]);return t},r})();t.GLTFUtils=r})(e.GLTF2||(e.GLTF2={}))})(BABYLON||(BABYLON={}));var BABYLON;!(function(e){!(function(e){var t=(function(){function e(){this.enabled=!0}return e.prototype.loadMaterial=function(e,t,r){return!1},e.LoadMaterial=function(e,t,r){return this._ApplyExtensions((function(n){return n.loadMaterial(e,t,r)}))},e._ApplyExtensions=function(t){var r=e._Extensions;if(r){for(var n=0;n<r.length;n++){var a=r[n];if(a.enabled&&t(a))return!0}return!1}},e._Extensions=[],e})();e.GLTFLoaderExtension=t})(e.GLTF2||(e.GLTF2={}))})(BABYLON||(BABYLON={}));var BABYLON;!(function(e){!(function(t){!(function(r){var n=(function(t){function r(){return null!==t&&t.apply(this,arguments)||this}return __extends(r,t),Object.defineProperty(r.prototype,"name",{get:function(){return"MSFT_lod"},enumerable:!0,configurable:!0}),r.prototype.loadMaterial=function(e,t,r){if(!t.extensions)return!1;var n=t.extensions[this.name];if(!n)return!1;t.extensions[this.name]=void 0;var a=[t.index].concat(n.ids);e.addLoaderPendingData(t);for(var o=0;o<a.length;o++)e.addLoaderNonBlockingPendingData(o);return this.loadMaterialLOD(e,t,a,a.length-1,r),!0},r.prototype.loadMaterialLOD=function(t,n,a,o,i){var s=this,l=t.gltf.materials[a[o]];o!==a.length-1&&(t.blockPendingTracking=!0),t.loadMaterial(l,(function(l,u){if(i(l,u),t.removeLoaderPendingData(o),o===a.length-1&&t.removeLoaderPendingData(n),0===o)return void(t.blockPendingTracking=!1);t.executeWhenRenderReady((function(){e.BaseTexture.WhenAllReady(l.getActiveTextures(),(function(){setTimeout((function(){s.loadMaterialLOD(t,n,a,o-1,i)}),r.MinimalLODDelay)}))}))}))},r.MinimalLODDelay=250,r})(t.GLTFLoaderExtension);r.MSFTLOD=n,t.GLTFLoader.RegisterExtension(new n)})(t.Extensions||(t.Extensions={}))})(e.GLTF2||(e.GLTF2={}))})(BABYLON||(BABYLON={}));var BABYLON;!(function(e){!(function(t){!(function(r){var n=(function(t){function r(){return null!==t&&t.apply(this,arguments)||this}return __extends(r,t),Object.defineProperty(r.prototype,"name",{get:function(){return"KHR_materials_pbrSpecularGlossiness"},enumerable:!0,configurable:!0}),r.prototype.loadMaterial=function(e,t,r){if(!t.extensions)return!1;var n=t.extensions[this.name];return!!n&&(e.createPbrMaterial(t),e.loadMaterialBaseProperties(t),this._loadSpecularGlossinessProperties(e,t,n),r(t.babylonMaterial,!0),!0)},r.prototype._loadSpecularGlossinessProperties=function(t,r,n){var a=r.babylonMaterial;a.albedoColor=n.diffuseFactor?e.Color3.FromArray(n.diffuseFactor):new e.Color3(1,1,1),a.reflectivityColor=n.specularFactor?e.Color3.FromArray(n.specularFactor):new e.Color3(1,1,1),a.microSurface=void 0===n.glossinessFactor?1:n.glossinessFactor,n.diffuseTexture&&(a.albedoTexture=t.loadTexture(n.diffuseTexture)),n.specularGlossinessTexture&&(a.reflectivityTexture=t.loadTexture(n.specularGlossinessTexture),a.reflectivityTexture.hasAlpha=!0,a.useMicroSurfaceFromReflectivityMapAlpha=!0),t.loadMaterialAlphaProperties(r,n.diffuseFactor)},r})(t.GLTFLoaderExtension);r.KHRMaterialsPbrSpecularGlossiness=n,t.GLTFLoader.RegisterExtension(new n)})(t.Extensions||(t.Extensions={}))})(e.GLTF2||(e.GLTF2={}))})(BABYLON||(BABYLON={}));