babylon.glTF2FileLoader.min.js 34 KB

12
  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){this.onParsed&&this.onParsed(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){this.onParsed&&this.onParsed(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){try{return t instanceof ArrayBuffer?e._parseBinary(t,r):{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})();r.HomogeneousCoordinates=!1,r.IncrementalLoading=!0,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 e(e){this._pendingCount=0,this._callback=e}return e.prototype._addPendingData=function(e){this._pendingCount++},e.prototype._removePendingData=function(e){0==--this._pendingCount&&this._callback()},e})(),n=(function(){function n(t){this._renderReady=!1,this._disposed=!1,this._renderReadyObservable=new e.Observable,this._renderPendingCount=0,this._loaderPendingCount=0,this._loaderTrackers=new Array,this._parent=t}return n.RegisterExtension=function(r){if(n.Extensions[r.name])return void e.Tools.Error("Extension with the same name '"+r.name+"' already exists");n.Extensions[r.name]=r,t.GLTFLoaderExtension._Extensions.push(r)},n.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)},n.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)},n.prototype.loadAsync=function(e,t,r,n,a,o){this._loadAsync(null,e,t,r,n,a,o)},n.prototype._loadAsync=function(e,t,r,n,a,o,i){try{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)}catch(e){this._onError(e.message)}},n.prototype._onError=function(t){this._disposed||(e.Tools.Error("glTF Loader Error: "+t),this._errorCallback&&this._errorCallback(t),this.dispose())},n.prototype._onProgress=function(e){this._disposed||this._progressCallback&&this._progressCallback(e)},n.prototype._executeWhenRenderReady=function(e){this._renderReady?e():this._renderReadyObservable.add(e)},n.prototype._onRenderReady=function(){this._rootNode.babylonMesh.setEnabled(!0),this._startAnimations(),this._successCallback(),this._renderReadyObservable.notifyObservers(this),this._parent.onReady&&this._parent.onReady()},n.prototype._onComplete=function(){this._parent.onComplete&&this._parent.onComplete(),this.dispose()},n.prototype._loadData=function(t){if(this._gltf=t.json,t.bin){var r=this._gltf.buffers;if(r&&r[0]&&!r[0].uri){var n=r[0];n.byteLength!=t.bin.byteLength&&e.Tools.Warn("Binary buffer length ("+n.byteLength+") from JSON does not match chunk length ("+t.bin.byteLength+")"),n.loadedData=t.bin}else e.Tools.Warn("Unexpected BIN chunk")}},n.prototype._getMeshes=function(){var e=[this._rootNode.babylonMesh],t=this._gltf.nodes;return t&&t.forEach((function(t){t.babylonMesh&&e.push(t.babylonMesh)})),e},n.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},n.prototype._getAnimationTargets=function(){var e=[],t=this._gltf.animations;return t&&t.forEach((function(t){e.push.apply(e,t.targets)})),e},n.prototype._startAnimations=function(){var e=this;this._getAnimationTargets().forEach((function(t){return e._babylonScene.beginAnimation(t,0,Number.MAX_VALUE,!0)}))},n.prototype._loadScene=function(t){var r=this._getArrayItem(this._gltf.scenes,this._gltf.scene||0,"Scene");if(r){switch(this._rootNode={name:"__root__"},this._parent.coordinateSystemMode){case e.GLTFLoaderCoordinateSystemMode.AUTO:this._babylonScene.useRightHandedSystem||(this._rootNode.rotation=[0,1,0,0],this._rootNode.scale=[1,1,-1]);break;case e.GLTFLoaderCoordinateSystemMode.PASS_THROUGH:break;case e.GLTFLoaderCoordinateSystemMode.FORCE_RIGHT_HANDED:this._babylonScene.useRightHandedSystem=!0;break;default:return void e.Tools.Error("Invalid coordinate system mode ("+this._parent.coordinateSystemMode+")")}this._loadNode(this._rootNode);var n=r.nodes;this._traverseNodes(n,(function(e,t,r){return e.index=t,e.parent=r,!0}),this._rootNode);var a=this._gltf.materials;a&&a.forEach((function(e,t){return e.index=t}));var o=this._gltf.skins;if(o&&o.forEach((function(e,t){return e.index=t})),t){t instanceof Array||(t=[t]);var i=new Array;this._traverseNodes(n,(function(e){return-1===t.indexOf(e.name)||(i.push(e.index),!1)}),this._rootNode),n=i}for(var s=0;s<n.length;s++){var l=this._getArrayItem(this._gltf.nodes,n[s],"Node");if(!l)return;this._loadNode(l)}this._rootNode.babylonMesh.setEnabled(!1)}},n.prototype._loadNode=function(r){if(!t.GLTFLoaderExtension.LoadNode(this,r)){if(r.babylonMesh=new e.Mesh(r.name||"mesh"+r.index,this._babylonScene),this._loadTransform(r),void 0!==r.mesh){var n=this._getArrayItem(this._gltf.meshes,r.mesh,"Mesh");if(!n)return;this._loadMesh(r,n)}if(r.babylonMesh.parent=r.parent?r.parent.babylonMesh:null,r.babylonAnimationTargets=r.babylonAnimationTargets||[],r.babylonAnimationTargets.push(r.babylonMesh),void 0!==r.skin){var a=this._getArrayItem(this._gltf.skins,r.skin,"Skin");if(!a)return;r.babylonMesh.skeleton=this._loadSkin(a)}if(r.camera,r.children)for(var o=0;o<r.children.length;o++){var i=this._getArrayItem(this._gltf.nodes,r.children[o],"Node");if(!i)return;this._loadNode(i)}}},n.prototype._loadMesh=function(r,n){var a=this;r.babylonMesh.name=n.name||r.babylonMesh.name;var o=new e.MultiMaterial(r.babylonMesh.name,this._babylonScene);r.babylonMesh.material=o;var i=new e.Geometry(r.babylonMesh.name,this._babylonScene,null,!1,r.babylonMesh),s=new e.VertexData;s.positions=[],s.indices=[];for(var l=[],u=0,d=n.primitives.length,c=this,f=0;f<d;f++)!(function(f){var _=n.primitives[f];if(_.mode&&_.mode!==t.EMeshPrimitiveMode.TRIANGLES)throw new Error("Not implemented");c._createMorphTargets(r,n,_,r.babylonMesh),c._loadVertexDataAsync(_,(function(t){a._loadMorphTargetsData(n,_,t,r.babylonMesh),l.push({materialIndex:f,verticesStart:s.positions.length,verticesCount:t.positions.length,indicesStart:s.indices.length,indicesCount:t.indices.length,loadMaterial:function(){if(null==_.material)return void(o.subMaterials[f]=a._getDefaultMaterial());var e=a._getArrayItem(a._gltf.materials,_.material,"Material");e&&a._loadMaterial(e,(function(t,n){n&&a._parent.onMaterialLoaded&&a._parent.onMaterialLoaded(t),a._parent.onBeforeMaterialReadyAsync?(a._addLoaderPendingData(e),a._parent.onBeforeMaterialReadyAsync(t,r.babylonMesh,null!=o.subMaterials[f],(function(){o.subMaterials[f]=t,a._removeLoaderPendingData(e)}))):o.subMaterials[f]=t}))}}),s.merge(t),++u===d&&(i.setAllVerticesData(s,!1),l.forEach((function(e){return e.loadMaterial()})),r.babylonMesh.subMeshes=[],l.forEach((function(t){return new e.SubMesh(t.materialIndex,t.verticesStart,t.verticesCount,t.indicesStart,t.indicesCount,r.babylonMesh)})))}))})(f)},n.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){var c=(function(d){if(!(o=u._getArrayItem(u._gltf.accessors,a[d],"Mesh primitive attribute '"+d+"' accessor")))return{value:void 0};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 attribute '"+d+"'")}if(++s===l)if(null==t.indices)i.indices=new Uint32Array(i.positions.length/3),i.indices.forEach((function(e,t){return i.indices[t]=t})),r(i);else{var o=n._getArrayItem(n._gltf.accessors,t.indices,"Mesh primitive 'indices' accessor");if(!o)return;n._loadAccessorAsync(o,(function(e){i.indices=e,r(i)}))}}))})(d);if("object"==typeof c)return c.value}},n.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))}}},n.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++){var c=(function(){var r=o.morphTargetManager.getTarget(d);s=i[d];for(var n in s){var c=(function(n){if(!(l=u._getArrayItem(u._gltf.accessors,s[n],"Mesh primitive morph target attribute '"+n+"' accessor")))return{value:void 0};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 attribute '"+n+"'")}}))})(n);if("object"==typeof c)return c}})();if("object"==typeof c)return c.value}},n.prototype._loadTransform=function(t){var r=e.Vector3.Zero(),n=e.Quaternion.Identity(),a=e.Vector3.One();if(t.matrix){e.Matrix.FromArray(t.matrix).decompose(a,n,r)}else t.translation&&(r=e.Vector3.FromArray(t.translation)),t.rotation&&(n=e.Quaternion.FromArray(t.rotation)),t.scale&&(a=e.Vector3.FromArray(t.scale));t.babylonMesh.position=r,t.babylonMesh.rotationQuaternion=n,t.babylonMesh.scaling=a},n.prototype._loadSkin=function(t){var r=this,n="skeleton"+t.index;if(t.babylonSkeleton=new e.Skeleton(t.name||n,n,this._babylonScene),null==t.inverseBindMatrices)this._loadBones(t,null);else{var a=this._getArrayItem(this._gltf.accessors,t.inverseBindMatrices,"Skin ("+t.index+") inverse bind matrices attribute accessor");if(!a)return;this._loadAccessorAsync(a,(function(e){r._loadBones(t,e)}))}return t.babylonSkeleton},n.prototype._createBone=function(t,r,n,a,o,i){var s=new e.Bone(t.name||"bone"+t.index,r.babylonSkeleton,n,a,null,o,i);return t.babylonBones=t.babylonBones||{},t.babylonBones[r.index]=s,t.babylonAnimationTargets=t.babylonAnimationTargets||[],t.babylonAnimationTargets.push(s),s},n.prototype._loadBones=function(e,t){for(var r={},n=0;n<e.joints.length;n++){var a=this._getArrayItem(this._gltf.nodes,e.joints[n],"Skin ("+e.index+") joint");if(!a)return;this._loadBone(a,e,t,r)}},n.prototype._loadBone=function(t,r,n,a){var o=a[t.index];if(o)return o;var i=r.joints.indexOf(t.index),s=e.Matrix.Identity();n&&-1!==i&&(s=e.Matrix.FromArray(n,16*i),s.invertToRef(s));var l;return t.index!=r.skeleton&&t.parent&&(l=this._loadBone(t.parent,r,n,a),s.multiplyToRef(l.getInvertedAbsoluteTransform(),s)),o=this._createBone(t,r,l,this._getNodeMatrix(t),s,i),a[t.index]=o,o},n.prototype._getNodeMatrix=function(t){return t.matrix?e.Matrix.FromArray(t.matrix):e.Matrix.Compose(t.scale?e.Vector3.FromArray(t.scale):e.Vector3.One(),t.rotation?e.Quaternion.FromArray(t.rotation):e.Quaternion.Identity(),t.translation?e.Vector3.FromArray(t.translation):e.Vector3.Zero())},n.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)},n.prototype._traverseNode=function(e,r,n){if(void 0===n&&(n=null),!t.GLTFLoaderExtension.TraverseNode(this,e,r,n)){var a=this._getArrayItem(this._gltf.nodes,e,"Node");a&&r(a,e,n)&&a.children&&this._traverseNodes(a.children,r,a)}},n.prototype._loadAnimations=function(){var e=this._gltf.animations;if(e)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)},n.prototype._loadAnimationChannel=function(t,r,n){var a=t.channels[n],o=a.sampler,i=t.samplers[o],s=this._getArrayItem(this._gltf.nodes,a.target.node,"Animation channel target");if(s){var l={translation:"position",rotation:"rotationQuaternion",scale:"scaling",weights:"influence"}[a.target.path];if(!l)return void this._onError("Invalid animation channel target path '"+a.target.path+"'");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),_=0;_<u.length;_++)f[_]=o(_);if(t.targets=t.targets||[],"influence"===l)for(var h=s.babylonMesh.morphTargetManager,p=0;p<h.numTargets;p++){var y=h.getTarget(p),A=(t.name||"anim"+r)+"_"+p,T=new e.Animation(A,l,1,c);T.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}}))),y.animations.push(T),t.targets.push(y)}else{var A=t.name||"anim"+r,T=new e.Animation(A,l,1,c);T.setKeys(f);for(var b=0;b<s.babylonAnimationTargets.length;b++){var g=s.babylonAnimationTargets[b];g.animations.push(T.clone()),t.targets.push(g)}}}},_=this._getArrayItem(this._gltf.accessors,i.input,"Animation sampler input accessor");if(_){this._loadAccessorAsync(_,(function(e){u=e,f()}));var h=this._getArrayItem(this._gltf.accessors,i.output,"Animation sampler output accessor");h&&this._loadAccessorAsync(h,(function(e){d=e,f()}))}}},n.prototype._validateUri=function(e){return!!e||(this._onError("Uri is missing"),!1)},n.prototype._loadBufferAsync=function(r,n){var a=this;if(this._addPendingData(r),r.loadedData)setTimeout((function(){n(r.loadedData),a._removePendingData(r)}));else if(r.loadedObservable)r.loadedObservable.add((function(e){n(e.loadedData),a._removePendingData(e)}));else if(this._validateUri(r.uri))if(t.GLTFUtils.IsBase64(r.uri)){var o=t.GLTFUtils.DecodeBase64(r.uri);r.loadedData=new Uint8Array(o),setTimeout((function(){n(r.loadedData),a._removePendingData(r)}))}else r.loadedObservable=new e.Observable,r.loadedObservable.add((function(e){n(e.loadedData),a._removePendingData(e)})),e.Tools.LoadFile(this._rootUrl+r.uri,(function(e){r.loadedData=new Uint8Array(e),r.loadedObservable.notifyObservers(r),r.loadedObservable=null}),(function(e){a._onProgress(e)}),this._babylonScene.database,!0,(function(e){a._onError("Failed to load file '"+r.uri+"'"+(e?": "+e.status+" "+e.statusText:""))}))},n.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},n.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},n.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},n.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},n.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},n.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},n.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}},n.prototype._loadBufferViewAsync=function(e,r,n,a,o,i){var s=this;r+=e.byteOffset||0;var l=this._getArrayItem(this._gltf.buffers,e.buffer,"Buffer");l&&this._loadBufferAsync(l,(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)}))},n.prototype._loadAccessorAsync=function(e,t){var r=this._getArrayItem(this._gltf.bufferViews,e.bufferView,"Buffer view");if(r){var n=e.byteOffset||0,a=this._getByteStrideFromType(e),o=e.count*a;this._loadBufferViewAsync(r,n,o,a,e.componentType,t)}},n.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}},n.prototype._addPendingData=function(e){this._renderReady||this._renderPendingCount++,this._addLoaderPendingData(e)},n.prototype._removePendingData=function(e){this._renderReady||0==--this._renderPendingCount&&(this._renderReady=!0,this._onRenderReady()),this._removeLoaderPendingData(e)},n.prototype._addLoaderPendingData=function(e){this._loaderPendingCount++,this._loaderTrackers.forEach((function(t){return t._addPendingData(e)}))},n.prototype._removeLoaderPendingData=function(e){this._loaderTrackers.forEach((function(t){return t._removePendingData(e)})),0==--this._loaderPendingCount&&this._onComplete()},n.prototype._whenAction=function(e,t){var n=this,a=new r(function(){n._loaderTrackers.splice(n._loaderTrackers.indexOf(a)),t()});this._loaderTrackers.push(a),this._addLoaderPendingData(a),e(),this._removeLoaderPendingData(a)},n.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},n.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=null==n.metallicFactor?1:n.metallicFactor,r.roughness=null==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))},n.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))},n.prototype._createPbrMaterial=function(t){var r=new e.PBRMaterial(t.name||"mat"+t.index,this._babylonScene);r.sideOrientation=e.Material.CounterClockWiseSideOrientation,t.babylonMaterial=r},n.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=!this._babylonScene.useRightHandedSystem,r.invertNormalMapY=this._babylonScene.useRightHandedSystem,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))},n.prototype._loadMaterialAlphaProperties=function(e,t){var r=e.babylonMaterial,n=e.alphaMode||"OPAQUE";switch(n){case"OPAQUE":break;case"MASK":case"BLEND":t&&(r.alpha=t[3]),r.albedoTexture&&(r.albedoTexture.hasAlpha=!0,r.useAlphaFromAlbedoTexture="BLEND"===n);break;default:return void this._onError("Invalid alpha mode '"+e.alphaMode+"'")}r.alphaCutOff=null==e.alphaCutoff?.5:e.alphaCutoff},n.prototype._loadTexture=function(r){var n=this,a=this._getArrayItem(this._gltf.textures,r.index,"Texture");if(!a)return null;var o=r.texCoord||0,i=this._getArrayItem(this._gltf.images,a.source,"Texture ("+r.index+") source");if(!i)return null;var s=null==a.sampler?{}:this._getArrayItem(this._gltf.samplers,a.sampler,"Texture ("+r.index+") sampler");if(s){var 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._onProgress(e)}),this._babylonScene.database,!0,(function(e){n._onError("Failed to load file '"+i.uri+"': "+e.status+" "+e.statusText)}));else{var f=this._getArrayItem(this._gltf.bufferViews,i.bufferView,"Texture ("+r.index+") source ("+a.source+") buffer view");if(!f)return;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}},n.prototype._getArrayItem=function(e,t,r){return e&&e[t]?e[t]:(this._onError(r+" index ("+t+") was not found"),null)},n})();n.Extensions={},t.GLTFLoader=n,e.GLTFFileLoader.CreateGLTFLoaderV2=function(e){return new n(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._traverseNode=function(e,t,r,n){return!1},e.prototype._loadNode=function(e,t){return!1},e.prototype._loadMaterial=function(e,t,r){return!1},e.prototype._loadExtension=function(e,t){var r=this;if(!e.extensions)return!1;var n=e.extensions[this.name];return!!n&&(e.extensions[this.name]=void 0,t(n,(function(){e.extensions[r.name]=n})),!0)},e.TraverseNode=function(e,t,r,n){return this._ApplyExtensions((function(a){return a._traverseNode(e,t,r,n)}))},e.LoadNode=function(e,t){return this._ApplyExtensions((function(r){return r._loadNode(e,t)}))},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})();t._Extensions=[],e.GLTFLoaderExtension=t})(e.GLTF2||(e.GLTF2={}))})(BABYLON||(BABYLON={}));var __extends=this&&this.__extends||(function(){var e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)t.hasOwnProperty(r)&&(e[r]=t[r])};return function(t,r){function n(){this.constructor=t}e(t,r),t.prototype=null===r?Object.create(r):(n.prototype=r.prototype,new n)}})(),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._traverseNode=function(e,t,r,n){var a=e._getArrayItem(e._gltf.nodes,t,"Node");return!a||this._loadExtension(a,(function(a,o){
  2. for(var i=a.ids.length-1;i>=0;i--)e._traverseNode(a.ids[i],r,n);e._traverseNode(t,r,n),o()}))},r.prototype._loadNode=function(e,t){var r=this;return this._loadExtension(t,(function(n,a){var o=[t.index].concat(n.ids).map((function(t){return e._gltf.nodes[t]}));e._addLoaderPendingData(t),r._loadNodeLOD(e,o,o.length-1,(function(){e._removeLoaderPendingData(t),a()}))}))},r.prototype._loadNodeLOD=function(e,t,n,a){var o=this;e._whenAction((function(){e._loadNode(t[n])}),(function(){if(n!==t.length-1){t[n+1].babylonMesh.setEnabled(!1)}if(0===n)return void a();setTimeout((function(){o._loadNodeLOD(e,t,n-1,a)}),r.MinimalLODDelay)}))},r.prototype._loadMaterial=function(e,t,r){var n=this;return this._loadExtension(t,(function(a,o){var i=[t.index].concat(a.ids).map((function(t){return e._gltf.materials[t]}));e._addLoaderPendingData(t),n._loadMaterialLOD(e,i,i.length-1,r,(function(){t.extensions[n.name]=a,e._removeLoaderPendingData(t),o()}))}))},r.prototype._loadMaterialLOD=function(t,n,a,o,i){var s=this;t._loadMaterial(n[a],(function(l,u){if(o(l,u),0===a)return void i();t._executeWhenRenderReady((function(){e.BaseTexture.WhenAllReady(l.getActiveTextures(),(function(){setTimeout((function(){s._loadMaterialLOD(t,n,a-1,o,i)}),r.MinimalLODDelay)}))}))}))},r})(t.GLTFLoaderExtension);n.MinimalLODDelay=250,r.MSFTLOD=n,t.GLTFLoader.RegisterExtension(new n)})(t.Extensions||(t.Extensions={}))})(e.GLTF2||(e.GLTF2={}))})(BABYLON||(BABYLON={}));var __extends=this&&this.__extends||(function(){var e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)t.hasOwnProperty(r)&&(e[r]=t[r])};return function(t,r){function n(){this.constructor=t}e(t,r),t.prototype=null===r?Object.create(r):(n.prototype=r.prototype,new n)}})(),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){var n=this;return this._loadExtension(t,(function(a,o){e._createPbrMaterial(t),e._loadMaterialBaseProperties(t),n._loadSpecularGlossinessProperties(e,t,a),r(t.babylonMaterial,!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={}));