babylon.glTF2FileLoader.min.js 28 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 n=(function(){function e(){this.coordinateSystemMode=t.AUTO,this.name="gltf",this.extensions={".gltf":{isBinary:!1},".glb":{isBinary:!0}}}return e.prototype.importMeshAsync=function(t,n,r,a,o,i,s){var l=e._parse(r,s);if(l){var u=this._getLoader(l,s);u&&u.importMeshAsync(t,n,l,a,o,i,s)}},e.prototype.loadAsync=function(t,n,r,a,o,i){var s=e._parse(n,i);if(s){var l=this._getLoader(s,i);if(l)return l.loadAsync(t,s,r,a,o,i)}},e.prototype.canDirectLoad=function(e){return-1!==e.indexOf("scene")&&-1!==e.indexOf("node")},e._parse=function(t,n){if(t instanceof ArrayBuffer)return e._parseBinary(t,n);try{return{json:JSON.parse(t),bin:null}}catch(e){return n(e.message),null}},e.prototype._getLoader=function(t,n){var r={major:2,minor:0},a=t.json.asset||{},o=e._parseVersion(a.version);if(!o)return n("Invalid version: "+a.version),null;if(void 0!==a.minVersion){var i=e._parseVersion(a.minVersion);if(!i)return n("Invalid minimum version: "+a.minVersion),null;if(e._compareVersion(i,r)>0)return n("Incompatible minimum version: "+a.minVersion),null}var s={1:e.CreateGLTFLoaderV1,2:e.CreateGLTFLoaderV2},l=s[o.major];return l?l(this):(n("Unsupported version: "+a.version),null)},e._parseBinary=function(t,n){var a={Magic:1179937895},o=new r(t),i=o.readUint32();if(i!==a.Magic)return n("Unexpected magic: "+i),null;var s=o.readUint32();switch(s){case 1:return e._parseV1(o,n);case 2:return e._parseV2(o,n)}return n("Unsupported version: "+s),null},e._parseV1=function(t,n){var r={JSON:0},a=t.readUint32();if(a!=t.getLength())return n("Length in header does not match actual data length: "+a+" != "+t.getLength()),null;var o,i=t.readUint32(),s=t.readUint32();switch(s){case r.JSON:o=JSON.parse(e._decodeBufferToText(t.readUint8Array(i)));break;default:return n("Unexpected content format: "+s),null}var l=t.getLength()-t.getPosition();return{json:o,bin:t.readUint8Array(l)}},e._parseV2=function(t,n){var r={JSON:1313821514,BIN:5130562},a=t.readUint32();if(a!==t.getLength())return n("Length in header does not match actual data length: "+a+" != "+t.getLength()),null;var o=t.readUint32(),i=t.readUint32();if(i!==r.JSON)return n("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 r.JSON:return n("Unexpected JSON chunk"),null;case r.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 n=+t[0];if(isNaN(n))return null;var r=+t[1];return isNaN(r)?null:{major:n,minor:r}},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="",n=e.byteLength,r=0;r<n;++r)t+=String.fromCharCode(e[r]);return t},e})();n.HomogeneousCoordinates=!1,n.IncrementalLoading=!0,e.GLTFFileLoader=n;var r=(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 n)})(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 n=(function(){function n(t){this._renderReady=!1,this._disposed=!1,this._objectURLs=new Array,this._renderReadyObservable=new e.Observable,this._renderPendingCount=0,this._loaderPendingCount=0,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)},Object.defineProperty(n.prototype,"gltf",{get:function(){return this._gltf},enumerable:!0,configurable:!0}),Object.defineProperty(n.prototype,"babylonScene",{get:function(){return this._babylonScene},enumerable:!0,configurable:!0}),n.prototype.executeWhenRenderReady=function(e){this._renderReady?e():this._renderReadyObservable.add(e)},n.prototype.dispose=function(){this._disposed||(this._disposed=!0,this._objectURLs.forEach((function(e){return URL.revokeObjectURL(e)})),this._objectURLs.length=0,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,n,r,a,o,i){var s=this;this._loadAsync(e,t,n,r,(function(){a(s._getMeshes(),null,s._getSkeletons())}),o,i)},n.prototype.loadAsync=function(e,t,n,r,a,o){this._loadAsync(null,e,t,n,r,a,o)},n.prototype._loadAsync=function(e,t,n,r,a,o,i){this._loadData(n),this._babylonScene=t,this._rootUrl=r,this._successCallback=a,this._progressCallback=o,this._errorCallback=i,this.addPendingData(this),this._loadScene(e),this._loadAnimations(),this.removePendingData(this)},n.prototype._onError=function(e){this._errorCallback(e),this.dispose()},n.prototype._onProgress=function(e){this._progressCallback(e)},n.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)},n.prototype._onLoaderComplete=function(){this.dispose(),this._parent.onComplete&&this._parent.onComplete()},n.prototype._loadData=function(t){this._gltf=t.json;var n,r=this._gltf.buffers;r.length>0&&void 0===r[0].uri&&(n=r[0]),t.bin&&(n?n.byteLength!=t.bin.byteLength&&e.Tools.Warn("Binary buffer length ("+n.byteLength+") from JSON does not match chunk length ("+t.bin.byteLength+")"):e.Tools.Warn("Unexpected BIN chunk"),n.loadedData=t.bin)},n.prototype._addRightHandToLeftHandRootTransform=function(){var t=new e.Mesh("root",this._babylonScene);t.scaling=new e.Vector3(1,1,-1),t.rotation.y=Math.PI;for(var n=this._gltf.nodes,r=0;r<n.length;r++){var a=n[r].babylonMesh;a&&!a.parent&&(a.parent=t)}},n.prototype._getMeshes=function(){var e=[],t=this._gltf.nodes;return t&&t.forEach((function(t){t.babylonMesh&&e.push(t.babylonMesh)})),e},n.prototype._getSkeletons=function(){var t=[],n=this._gltf.skins;return n&&n.forEach((function(n){n.babylonSkeleton instanceof e.Skeleton&&t.push(n.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._showMeshes=function(){this._getMeshes().forEach((function(e){return e.isVisible=!0}))},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(e){var t=this,n=this._gltf.scenes[this._gltf.scene||0],r=n.nodes;this._traverseNodes(r,(function(e,t,n){return e.index=t,e.parent=n,!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(r,(function(t){return-1===e.indexOf(t.name)||(o.push(t.index),!1)})),r=o}this._traverseNodes(r,(function(e){return t._loadSkin(e)})),this._traverseNodes(r,(function(e){return t._loadMesh(e)}))},n.prototype._loadSkin=function(t){var n=this;if(void 0!==t.skin){var r=this._gltf.skins[t.skin],a="skeleton"+t.skin;r.babylonSkeleton=new e.Skeleton(r.name||a,a,this._babylonScene),r.index=t.skin;for(var o=0;o<r.joints.length;o++)this._createBone(this._gltf.nodes[r.joints[o]],r);if(void 0===r.skeleton)throw new Error("Not implemented");if(void 0===r.inverseBindMatrices)throw new Error("Not implemented");var i=this._gltf.accessors[r.inverseBindMatrices];this._loadAccessorAsync(i,(function(e){n._traverseNode(r.skeleton,(function(t,a,o){return n._updateBone(t,o,r,e)}))}))}return!0},n.prototype._updateBone=function(t,n,r,a){var o=r.joints.indexOf(t.index);-1===o&&this._createBone(t,r);var i=t.babylonSkinToBones[r.index],s=-1===o?e.Matrix.Identity():e.Matrix.FromArray(a,16*o);return s.invertToRef(s),n&&(i.setParent(n.babylonSkinToBones[r.index],!1),s.multiplyToRef(i.getParent().getInvertedAbsoluteTransform(),s)),i.updateMatrix(s),!0},n.prototype._createBone=function(t,n){var r=new e.Bone(t.name||"bone"+t.index,n.babylonSkeleton);return t.babylonSkinToBones=t.babylonSkinToBones||{},t.babylonSkinToBones[n.index]=r,t.babylonAnimationTargets=t.babylonAnimationTargets||[],t.babylonAnimationTargets.push(r),r},n.prototype._loadMesh=function(t){var n=new e.Mesh(t.name||"mesh"+t.index,this._babylonScene);if(n.isVisible=!1,this._loadTransform(t,n),void 0!==t.mesh){var r=this._gltf.meshes[t.mesh];this._loadMeshData(t,r,n)}if(n.parent=t.parent?t.parent.babylonMesh:null,t.babylonMesh=n,t.babylonAnimationTargets=t.babylonAnimationTargets||[],t.babylonAnimationTargets.push(t.babylonMesh),void 0!==t.skin){var a=this._gltf.skins[t.skin];n.skeleton=a.babylonSkeleton}return t.camera,!0},n.prototype._loadMeshData=function(n,r,a){var o=this;a.name=r.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=[],c=0,d=r.primitives.length,f=this,h=0;h<d;h++)!(function(h){var T=r.primitives[h];if(T.mode&&T.mode!==t.EMeshPrimitiveMode.TRIANGLES)throw new Error("Not implemented");f._createMorphTargets(n,r,T,a),f._loadVertexDataAsync(T,(function(t){if(o._loadMorphTargetsData(r,T,t,a),u.push({materialIndex:h,verticesStart:l.positions.length,verticesCount:t.positions.length,indicesStart:l.indices.length,indicesCount:t.indices.length}),l.merge(t),void 0===T.material)i.subMaterials[h]=o._getDefaultMaterial();else{var n=o._gltf.materials[T.material];o.loadMaterial(n,(function(e,t){t&&o._parent.onMaterialLoaded&&o._parent.onMaterialLoaded(e),o.addPendingData(n),e.forceCompilation(a,(function(e){i.subMaterials[h]=e,o.removePendingData(n)}))}))}++c===d&&(s.setAllVerticesData(l,!1),a.subMeshes=[],u.forEach((function(t){return new e.SubMesh(t.materialIndex,t.verticesStart,t.verticesCount,t.indicesStart,t.indicesCount,a)})))}))})(h)},n.prototype._loadVertexDataAsync=function(t,n){var r=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 c in a)!(function(c){o=u._gltf.accessors[a[c]],u._loadAccessorAsync(o,(function(a){switch(c){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 '"+c+"'")}if(++s===l){var o=r._gltf.accessors[t.indices];o?r._loadAccessorAsync(o,(function(e){i.indices=e,n(i)})):(i.indices=new Uint32Array(i.positions.length/3),i.indices.forEach((function(e,t){return i.indices[t]=t})),n(i))}}))})(c)},n.prototype._createMorphTargets=function(t,n,r,a){var o=r.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]:n.weights?n.weights[i]:0;a.morphTargetManager.addTarget(new e.MorphTarget("morphTarget"+i,s))}}},n.prototype._loadMorphTargetsData=function(n,r,a,o){var i=r.targets;if(i)for(var s,l,u=this,c=0;c<i.length;c++)!(function(){var n=o.morphTargetManager.getTarget(c);s=i[c];for(var r in s)!(function(r){l=u._gltf.accessors[s[r]],u._loadAccessorAsync(l,(function(o){l.name&&(n.name=l.name);var i=o;switch(r){case"NORMAL":t.GLTFUtils.ForEach(i,(function(e,t){return i[t]+=a.normals[t]})),n.setNormals(i);break;case"POSITION":t.GLTFUtils.ForEach(i,(function(e,t){return i[t]+=a.positions[t]})),n.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++;n.setTangents(i);break;default:e.Tools.Warn("Ignoring unrecognized semantic '"+r+"'")}}))})(r)})()},n.prototype._loadTransform=function(t,n){var r=e.Vector3.Zero(),a=e.Quaternion.Identity(),o=e.Vector3.One();if(t.matrix){e.Matrix.FromArray(t.matrix).decompose(o,a,r)}else t.translation&&(r=e.Vector3.FromArray(t.translation)),t.rotation&&(a=e.Quaternion.FromArray(t.rotation)),t.scale&&(o=e.Vector3.FromArray(t.scale));n.position=r,n.rotationQuaternion=a,n.scaling=o},n.prototype._traverseNodes=function(e,t,n){void 0===n&&(n=null);for(var r=0;r<e.length;r++)this._traverseNode(e[r],t,n)},n.prototype._traverseNode=function(e,t,n){void 0===n&&(n=null);var r=this._gltf.nodes[e];if(t(r,e,n)&&r.children)for(var a=0;a<r.children.length;a++)this._traverseNode(r.children[a],t,r)},n.prototype._loadAnimations=function(){var e=this._gltf.animations;if(e&&0!==e.length)for(var t=0;t<e.length;t++)for(var n=e[t],r=0;r<n.channels.length;r++)this._loadAnimationChannel(n,t,r)},n.prototype._loadAnimationChannel=function(t,n,r){var a=t.channels[r],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,c,d={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&&c){for(var r=0,a={position:function(){var t=e.Vector3.FromArray(c,r);return r+=3,t},rotationQuaternion:function(){var t=e.Quaternion.FromArray(c,r);return r+=4,t},scaling:function(){var t=e.Vector3.FromArray(c,r);return r+=3,t},influence:function(){for(var e=s.babylonMesh.morphTargetManager.numTargets,t=new Array(e),n=0;n<e;n++)t[n]=c[r++];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 T=s.babylonMesh.morphTargetManager,p=0;p<T.numTargets;p++){var _=T.getTarget(p),E=(t.name||"anim"+n)+"_"+p,A=new e.Animation(E,l,1,d);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}}))),_.animations.push(A),t.targets.push(_)}else{var E=t.name||"anim"+n,A=new e.Animation(E,l,1,d);A.setKeys(f);for(var y=0;y<s.babylonAnimationTargets.length;y++){var g=s.babylonAnimationTargets[y];g.animations.push(A.clone()),t.targets.push(g)}}}};this._loadAccessorAsync(this._gltf.accessors[i.input],(function(e){u=e,f()})),this._loadAccessorAsync(this._gltf.accessors[i.output],(function(e){c=e,f()}))},n.prototype._loadBufferAsync=function(n,r){var a=this,o=this._gltf.buffers[n];if(this.addPendingData(o),o.loadedData)setTimeout((function(){r(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(){r(o.loadedData),a.removePendingData(o)}))}else o.loadedObservable?o.loadedObservable.add((function(e){r(e.loadedData),a.removePendingData(e)})):(o.loadedObservable=new e.Observable,o.loadedObservable.add((function(e){r(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))})))},n.prototype._loadBufferViewAsync=function(e,n,r,a,o){var i=this;n+=e.byteOffset||0,this._loadBufferAsync(e.buffer,(function(e){if(n+r>e.byteLength)return void i._onError("Buffer access is out of range");var s=e.buffer;n+=e.byteOffset;var l;switch(a){case t.EComponentType.BYTE:l=new Int8Array(s,n,r);break;case t.EComponentType.UNSIGNED_BYTE:l=new Uint8Array(s,n,r);break;case t.EComponentType.SHORT:l=new Int16Array(s,n,r);break;case t.EComponentType.UNSIGNED_SHORT:l=new Uint16Array(s,n,r);break;case t.EComponentType.UNSIGNED_INT:l=new Uint32Array(s,n,r);break;case t.EComponentType.FLOAT:l=new Float32Array(s,n,r);break;default:return void i._onError("Invalid component type ("+a+")")}o(l)}))},n.prototype._loadAccessorAsync=function(e,t){var n=this._gltf.bufferViews[e.bufferView],r=e.byteOffset||0,a=e.count*this._getByteStrideFromType(e);this._loadBufferViewAsync(n,r,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++},n.prototype.removeLoaderPendingData=function(e){0==--this._loaderPendingCount&&this._onLoaderComplete()},n.prototype._getDefaultMaterial=function(){if(!this._defaultMaterial){var t="__gltf_default",n=this._babylonScene.getMaterialByName(t);n||(n=new e.PBRMaterial(t,this._babylonScene),n.sideOrientation=e.Material.CounterClockWiseSideOrientation,n.metallic=1,n.roughness=1),this._defaultMaterial=n}return this._defaultMaterial},n.prototype._loadMaterialMetallicRoughnessProperties=function(t){var n=t.babylonMaterial;n.metallic=1,n.roughness=1;var r=t.pbrMetallicRoughness;r&&(n.albedoColor=r.baseColorFactor?e.Color3.FromArray(r.baseColorFactor):new e.Color3(1,1,1),n.metallic=void 0===r.metallicFactor?1:r.metallicFactor,n.roughness=void 0===r.roughnessFactor?1:r.roughnessFactor,r.baseColorTexture&&(n.albedoTexture=this.loadTexture(r.baseColorTexture)),r.metallicRoughnessTexture&&(n.metallicTexture=this.loadTexture(r.metallicRoughnessTexture),n.useMetallnessFromMetallicTextureBlue=!0,n.useRoughnessFromMetallicTextureGreen=!0,n.useRoughnessFromMetallicTextureAlpha=!1),this.loadMaterialAlphaProperties(t,r.baseColorFactor))},n.prototype.loadMaterial=function(e,n){if(e.babylonMaterial)return void n(e.babylonMaterial,!1);t.GLTFLoaderExtension.LoadMaterial(this,e,n)||(this.createPbrMaterial(e),this.loadMaterialBaseProperties(e),this._loadMaterialMetallicRoughnessProperties(e),n(e.babylonMaterial,!0))},n.prototype.createPbrMaterial=function(t){var n=new e.PBRMaterial(t.name||"mat"+t.index,this._babylonScene);n.sideOrientation=e.Material.CounterClockWiseSideOrientation,t.babylonMaterial=n},n.prototype.loadMaterialBaseProperties=function(t){var n=t.babylonMaterial;n.emissiveColor=t.emissiveFactor?e.Color3.FromArray(t.emissiveFactor):new e.Color3(0,0,0),t.doubleSided&&(n.backFaceCulling=!1,n.twoSidedLighting=!0),t.normalTexture&&(n.bumpTexture=this.loadTexture(t.normalTexture),n.invertNormalMapX=!0,n.invertNormalMapY=!0,void 0!==t.normalTexture.scale&&(n.bumpTexture.level=t.normalTexture.scale)),t.occlusionTexture&&(n.ambientTexture=this.loadTexture(t.occlusionTexture),n.useAmbientInGrayScale=!0,void 0!==t.occlusionTexture.strength&&(n.ambientTextureStrength=t.occlusionTexture.strength)),t.emissiveTexture&&(n.emissiveTexture=this.loadTexture(t.emissiveTexture))},n.prototype.loadMaterialAlphaProperties=function(t,n){var r=t.babylonMaterial,a=t.alphaMode||"OPAQUE";switch(a){case"OPAQUE":break;case"MASK":case"BLEND":n&&(r.alpha=n[3]),r.albedoTexture&&(r.albedoTexture.hasAlpha=!0,r.useAlphaFromAlbedoTexture="BLEND"===a);break;default:e.Tools.Warn("Invalid alpha mode '"+t.alphaMode+"'")}},n.prototype.loadTexture=function(n){var r=this,a=this._gltf.textures[n.index],o=n.texCoord||0;if(!a||void 0===a.source)return null;var i;if(a.babylonTextures){if(!(i=a.babylonTextures[o]))for(var s=0;s<a.babylonTextures.length;s++)if(i=a.babylonTextures[s]){i=i.clone(),i.coordinatesIndex=o;break}return i}var l=this._gltf.images[a.source],u=void 0===a.sampler?{}:this._gltf.samplers[a.sampler],c=u.minFilter===t.ETextureMinFilter.NEAREST||u.minFilter===t.ETextureMinFilter.LINEAR,d=t.GLTFUtils.GetTextureSamplingMode(u.magFilter,u.minFilter);this.addPendingData(a),i=new e.Texture(null,this._babylonScene,c,!1,d,function(){r._disposed||r.removePendingData(a)},function(){r._disposed||(r._onError("Failed to load texture '"+l.uri+"'"),r.removePendingData(a))});var f=function(e){var t=URL.createObjectURL(new Blob([e],{type:l.mimeType}));r._objectURLs.push(t),i.updateURL(t)};if(l.uri)t.GLTFUtils.IsBase64(l.uri)?f(new Uint8Array(t.GLTFUtils.DecodeBase64(l.uri))):e.Tools.LoadFile(this._rootUrl+l.uri,f,(function(e){r._disposed||r._onProgress(e)}),this._babylonScene.database,!0,(function(e){r._onError("Failed to load file '"+l.uri+"': "+e.status+" "+e.statusText)}));else{var h=this._gltf.bufferViews[l.bufferView];this._loadBufferViewAsync(h,0,h.byteLength,t.EComponentType.UNSIGNED_BYTE,f)}return i.coordinatesIndex=o,i.wrapU=t.GLTFUtils.GetTextureWrapMode(u.wrapS),i.wrapV=t.GLTFUtils.GetTextureWrapMode(u.wrapT),i.name=a.name||"texture"+n.index,a.babylonTextures=a.babylonTextures||[],a.babylonTextures[o]=i,this._parent.onTextureLoaded&&this._parent.onTextureLoaded(i),i},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 n=(function(){function n(){}return n.IsBase64=function(e){return!(e.length<5)&&"data:"===e.substr(0,5)},n.DecodeBase64=function(e){for(var t=atob(e.split(",")[1]),n=t.length,r=new Uint8Array(new ArrayBuffer(n)),a=0;a<n;a++)r[a]=t.charCodeAt(a);return r.buffer},n.ForEach=function(e,t){for(var n=0;n<e.length;n++)t(e[n],n)},n.GetTextureWrapMode=function(n){switch(n=void 0===n?t.ETextureWrapMode.REPEAT:n){case t.ETextureWrapMode.CLAMP_TO_EDGE: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 ("+n+")"),e.Texture.WRAP_ADDRESSMODE}},n.GetTextureSamplingMode=function(n,r){if(n=void 0===n?t.ETextureMagFilter.LINEAR:n,r=void 0===r?t.ETextureMinFilter.LINEAR_MIPMAP_NEAREST:r,n===t.ETextureMagFilter.LINEAR)switch(r){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 ("+r+")"),e.Texture.LINEAR_LINEAR_MIPLINEAR}else switch(n!==t.ETextureMagFilter.NEAREST&&e.Tools.Warn("Invalid texture magnification filter ("+n+")"),r){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 ("+r+")"),e.Texture.NEAREST_NEAREST_MIPNEAREST}},n.DecodeBufferToText=function(e){for(var t="",n=e.byteLength,r=0;r<n;++r)t+=String.fromCharCode(e[r]);return t},n})();t.GLTFUtils=n})(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,n){return!1},e.LoadMaterial=function(e,t,n){return this._ApplyExtensions((function(r){return r.loadMaterial(e,t,n)}))},e._ApplyExtensions=function(t){var n=e._Extensions;if(n){for(var r=0;r<n.length;r++){var a=n[r];if(a.enabled&&t(a))return!0}return!1}},e})();t._Extensions=[],e.GLTFLoaderExtension=t})(e.GLTF2||(e.GLTF2={}))})(BABYLON||(BABYLON={}));var BABYLON;!(function(e){!(function(t){!(function(n){var r=(function(t){function n(){return null!==t&&t.apply(this,arguments)||this}return __extends(n,t),Object.defineProperty(n.prototype,"name",{get:function(){return"MSFT_lod"},enumerable:!0,configurable:!0}),n.prototype.loadMaterial=function(e,t,n){if(!t.extensions)return!1;var r=t.extensions[this.name];if(!r)return!1;t.extensions[this.name]=void 0,e.addLoaderPendingData(t);var a=[t.index].concat(r.ids);return this.loadMaterialLOD(e,t,a,a.length-1,n),!0},n.prototype.loadMaterialLOD=function(t,n,r,a,o){var i=this,s=t.gltf.materials[r[a]];t.loadMaterial(s,(function(s,l){if(o(s,l),0===a)return void t.removeLoaderPendingData(n);t.executeWhenRenderReady((function(){e.BaseTexture.WhenAllReady(s.getActiveTextures(),(function(){i.loadMaterialLOD(t,n,r,a-1,o)}))}))}))},n})(t.GLTFLoaderExtension);n.MSFTLOD=r,t.GLTFLoader.RegisterExtension(new r)})(t.Extensions||(t.Extensions={}))})(e.GLTF2||(e.GLTF2={}))})(BABYLON||(BABYLON={}));var BABYLON;!(function(e){!(function(t){!(function(n){var r=(function(t){function n(){return null!==t&&t.apply(this,arguments)||this}return __extends(n,t),Object.defineProperty(n.prototype,"name",{get:function(){return"KHR_materials_pbrSpecularGlossiness"},enumerable:!0,configurable:!0}),n.prototype.loadMaterial=function(e,t,n){if(!t.extensions)return!1;var r=t.extensions[this.name];return!!r&&(e.createPbrMaterial(t),e.loadMaterialBaseProperties(t),this._loadSpecularGlossinessProperties(e,t,r),n(t.babylonMaterial,!0),!0)},n.prototype._loadSpecularGlossinessProperties=function(t,n,r){var a=n.babylonMaterial;a.albedoColor=r.diffuseFactor?e.Color3.FromArray(r.diffuseFactor):new e.Color3(1,1,1),a.reflectivityColor=r.specularFactor?e.Color3.FromArray(r.specularFactor):new e.Color3(1,1,1),a.microSurface=void 0===r.glossinessFactor?1:r.glossinessFactor,r.diffuseTexture&&(a.albedoTexture=t.loadTexture(r.diffuseTexture),t.loadMaterialAlphaProperties(n)),r.specularGlossinessTexture&&(a.reflectivityTexture=t.loadTexture(r.specularGlossinessTexture),a.reflectivityTexture.hasAlpha=!0,a.useMicroSurfaceFromReflectivityMapAlpha=!0),t.loadMaterialAlphaProperties(n,r.diffuseFactor)},n})(t.GLTFLoaderExtension);n.KHRMaterialsPbrSpecularGlossiness=r,t.GLTFLoader.RegisterExtension(new r)})(t.Extensions||(t.Extensions={}))})(e.GLTF2||(e.GLTF2={}))})(BABYLON||(BABYLON={}));