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 r=(function(){function r(){this.coordinateSystemMode=t.AUTO,this.extensions={".gltf":{isBinary:!1},".glb":{isBinary:!0}}}return r.prototype.importMeshAsync=function(e,t,n,a,o,i){var s=r._parse(n),l=this._getLoader(s);if(!l)return void i();l.importMeshAsync(e,t,s,a,o,i)},r.prototype.loadAsync=function(e,t,n,a,o){var i=r._parse(t),s=this._getLoader(i);return s?s.loadAsync(e,i,n,a,o):void o()},r.prototype.canDirectLoad=function(e){return-1!==e.indexOf("scene")&&-1!==e.indexOf("node")},r._parse=function(e){return e instanceof ArrayBuffer?r._parseBinary(e):{json:JSON.parse(e),bin:null}},r.prototype._getLoader=function(t){var n={major:2,minor:0},a=t.json.asset||{},o=r._parseVersion(a.version);if(!o)return e.Tools.Error("Invalid version"),null;var i=r._parseVersion(a.minVersion);if(i&&r._compareVersion(i,n)>0)return e.Tools.Error("Incompatible version"),null;var s={1:r.CreateGLTFLoaderV1,2:r.CreateGLTFLoaderV2},l=s[o.major](this);return null===l?(e.Tools.Error("Unsupported version"),null):l},r._parseBinary=function(t){var a={Magic:1179937895},o=new n(t),i=o.readUint32();if(i!==a.Magic)return e.Tools.Error("Unexpected magic: "+i),null;var s=o.readUint32();switch(s){case 1:return r._parseV1(o);case 2:return r._parseV2(o)}return e.Tools.Error("Unsupported version: "+s),null},r._parseV1=function(t){var n={JSON:0},a=t.readUint32();if(a!=t.getLength())return e.Tools.Error("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(r._decodeBufferToText(t.readUint8Array(i)));break;default:return e.Tools.Error("Unexpected content format: "+s),null}var l=t.getLength()-t.getPosition();return{json:o,bin:t.readUint8Array(l)}},r._parseV2=function(t){var n={JSON:1313821514,BIN:5130562},a=t.readUint32();if(a!==t.getLength())return e.Tools.Error("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 e.Tools.Error("First chunk format is not JSON"),null;for(var s=JSON.parse(r._decodeBufferToText(t.readUint8Array(o))),l=null;t.getPosition()<t.getLength();)switch(o=t.readUint32(),i=t.readUint32()){case n.JSON:return e.Tools.Error("Unexpected JSON chunk"),null;case n.BIN:l=t.readUint8Array(o);break;default:t.skipBytes(o)}return{json:s,bin:l}},r._parseVersion=function(e){if(!e)return null;var t=e.split(".");if(0===t.length)return null;var r=parseInt(t[0]);if(r>1&&2!=t.length)return null;parseInt(t[1]);return{major:r,minor:parseInt(t[0])}},r._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},r._decodeBufferToText=function(e){for(var t="",r=e.byteLength,n=0;n<r;++n)t+=String.fromCharCode(e[n]);return t},r})();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 r(t){this._renderReadyObservable=new e.Observable,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._succeeded):this._renderReadyObservable.add(e)},r.prototype.importMeshAsync=function(t,r,n,a,o,i){var s=this;this._loadAsync(t,r,n,a,(function(){var t=[];if(s._gltf.nodes)for(var r=0;r<s._gltf.nodes.length;r++){var n=s._gltf.nodes[r];n.babylonMesh&&t.push(n.babylonMesh)}var a=[];if(s._gltf.skins)for(var r=0;r<s._gltf.skins.length;r++){var i=s._gltf.skins[r];i.babylonSkeleton instanceof e.Skeleton&&a.push(i.babylonSkeleton)}o(t,null,a)}),i)},r.prototype.loadAsync=function(e,t,r,n,a){this._loadAsync(null,e,t,r,n,a)},r.prototype._loadAsync=function(e,t,r,n,a,o){this._clear(),this._loadData(r),this._babylonScene=t,this._rootUrl=n,this._onSuccess=a,this._onError=o,this.addPendingData(this),this._loadScene(e),this._loadAnimations(),this.removePendingData(this)},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._succeeded=0===this._errors.length,this._succeeded?(this._showMeshes(),this._startAnimations(),this._onSuccess()):(this._errors.forEach((function(t){return e.Tools.Error(t)})),this._errors=[],this._onError()),this._renderReadyObservable.notifyObservers(this._succeeded)},r.prototype._onLoaderComplete=function(){this._errors.forEach((function(t){return e.Tools.Error(t)})),this._errors=[],this._clear(),this._parent.onComplete&&this._parent.onComplete()},r.prototype._loadData=function(t){this._gltf=t.json;var r,n=this._gltf.buffers;n.length>0&&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(){var t=new e.Mesh("root",this._babylonScene);t.scaling=new e.Vector3(1,1,-1),t.rotationQuaternion=new e.Quaternion(0,1,0,0);for(var r=this._gltf.nodes,n=0;n<r.length;n++){var a=r[n].babylonMesh;a&&!a.parent&&(a.parent=t)}},r.prototype._showMeshes=function(){for(var e=this._gltf.nodes,t=0;t<e.length;t++){var r=e[t];r.babylonMesh&&(r.babylonMesh.isVisible=!0)}},r.prototype._startAnimations=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.targets.length;n++)this._babylonScene.beginAnimation(r.targets[n],0,Number.MAX_VALUE,!0)},r.prototype._clear=function(){if(this._gltf&&this._gltf.textures)for(var e=0;e<this._gltf.textures.length;e++){var t=this._gltf.textures[e];t.blobURL&&URL.revokeObjectURL(t.blobURL)}this._gltf=void 0,this._errors=[],this._babylonScene=void 0,this._rootUrl=void 0,this._defaultMaterial=void 0,this._onSuccess=void 0,this._onError=void 0,this._succeeded=!1,this._renderReady=!1,this._renderReadyObservable.clear(),this._renderPendingCount=0,this._loaderPendingCount=0},r.prototype._loadScene=function(e){var t=this,r=this._gltf.scenes[this._gltf.scene||0],n=r.nodes;if(this._traverseNodes(n,(function(e,t,r){return e.index=t,e.parent=r,!0})),e){e instanceof Array||(e=[e]);var a=new Array;this._traverseNodes(n,(function(t){return-1===e.indexOf(t.name)||(a.push(t.index),!1)})),n=a}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=[],c=0,d=n.primitives.length,f=this,h=0;h<d;h++)!(function(h){var T=n.primitives[h];if(T.mode&&T.mode!==t.EMeshPrimitiveMode.TRIANGLES)throw new Error("Not implemented");f._createMorphTargets(r,n,T,a),f._loadVertexDataAsync(T,(function(t){o._loadMorphTargetsData(n,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():o.loadMaterial(T.material,(function(e){o._renderReady?e.forceCompilation(a,(function(e){o._assignMaterial(i,h,e)})):o._assignMaterial(i,h,e)})),++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)},r.prototype._assignMaterial=function(e,t,r){e.subMaterials[t]=r,this._parent.onMaterialLoaded&&this._parent.onMaterialLoaded(r)},r.prototype._loadVertexDataAsync=function(t,r){var n=this,a=t.attributes;if(!a)return void this._errors.push("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=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))}}))})(c)},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,c=0;c<i.length;c++)!(function(){var r=o.morphTargetManager.getTarget(c);s=i[c];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,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 n=0,a={position:function(){var t=e.Vector3.FromArray(c,n);return n+=3,t},rotationQuaternion:function(){var t=e.Quaternion.FromArray(c,n);return n+=4,t},scaling:function(){var t=e.Vector3.FromArray(c,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]=c[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 T=s.babylonMesh.morphTargetManager,_=0;_<T.numTargets;_++){var p=T.getTarget(_),E=(t.name||"anim"+r)+"_"+_,A=new e.Animation(E,l,1,d);A.setKeys(f.map((function(e){return{frame:e.frame,inTangent:e.inTangent?e.inTangent[_]:void 0,value:e.value[_],outTangent:e.outTangent?e.outTangent[_]:void 0}}))),p.animations.push(A),t.targets.push(p)}else{var E=t.name||"anim"+r,A=new e.Animation(E,l,1,d);A.setKeys(f);for(var b=0;b<s.babylonAnimationTargets.length;b++){var g=s.babylonAnimationTargets[b];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()}))},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}),null,null,!0,(function(e){a._errors.push("Failed to load file '"+o.uri+"': "+e.statusText+"("+e.status+")"),a.removePendingData(o)})))},r.prototype._loadBufferViewAsync=function(e,r,n,a,o){var i=this;r+=e.byteOffset||0,this._loadBufferAsync(e.buffer,(function(e){if(r+n>e.byteLength)return void i._errors.push("Buffer access is out of range");var s=e.buffer;r+=e.byteOffset;var l;switch(a){case t.EComponentType.BYTE:l=new Int8Array(s,r,n);break;case t.EComponentType.UNSIGNED_BYTE:l=new Uint8Array(s,r,n);break;case t.EComponentType.SHORT:l=new Int16Array(s,r,n);break;case t.EComponentType.UNSIGNED_SHORT:l=new Uint16Array(s,r,n);break;case t.EComponentType.UNSIGNED_INT:l=new Uint32Array(s,r,n);break;case t.EComponentType.FLOAT:l=new Float32Array(s,r,n);break;default:return void i._errors.push("Invalid component type ("+a+")")}o(l)}))},r.prototype._loadAccessorAsync=function(e,r){var n=this._gltf.bufferViews[e.bufferView],a=e.byteOffset||0,o=e.count*t.GLTFUtils.GetByteStrideFromType(e);this._loadBufferViewAsync(n,a,o,e.componentType,r)},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.addLoaderPendingData=function(e){this._loaderPendingCount++},r.prototype.removeLoaderPendingData=function(e){0==--this._loaderPendingCount&&this._onLoaderComplete()},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),this.loadMaterialAlphaProperties(t)),n.metallicRoughnessTexture&&(r.metallicTexture=this.loadTexture(n.metallicRoughnessTexture),r.useMetallnessFromMetallicTextureBlue=!0,r.useRoughnessFromMetallicTextureGreen=!0,r.useRoughnessFromMetallicTextureAlpha=!1))},r.prototype.loadMaterial=function(e,r){var n=this._gltf.materials[e];if(n.index=e,n.babylonMaterial)return void r(n.babylonMaterial);t.GLTFLoaderExtension.LoadMaterial(this,n,r)||(this.createPbrMaterial(n),this.loadMaterialBaseProperties(n),this._loadMaterialMetallicRoughnessProperties(n),r(n.babylonMaterial))},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),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){var r=t.babylonMaterial;switch(t.alphaMode||"OPAQUE"){case"OPAQUE":break;case"MASK":r.albedoTexture.hasAlpha=!0,r.useAlphaFromAlbedoTexture=!1;break;case"BLEND":r.albedoTexture.hasAlpha=!0,r.useAlphaFromAlbedoTexture=!0;break;default:e.Tools.Warn("Invalid alpha mode '"+t.alphaMode+"'")}},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;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,u=this._gltf.images[a.source];if(u.uri)if(t.GLTFUtils.IsBase64(u.uri)){var c=new Uint8Array(t.GLTFUtils.DecodeBase64(u.uri));a.blobURL=URL.createObjectURL(new Blob([c],{type:u.mimeType})),l=a.blobURL}else l=this._rootUrl+u.uri;else{var d=this._gltf.bufferViews[u.bufferView];this._loadBufferViewAsync(d,0,d.byteLength,t.EComponentType.UNSIGNED_BYTE,(function(e){a.blobURL=URL.createObjectURL(new Blob([e],{type:u.mimeType})),a.babylonTextures[o].updateURL(a.blobURL)}))}var f=void 0===a.sampler?{}:this._gltf.samplers[a.sampler],h=f.minFilter===t.ETextureMinFilter.NEAREST||f.minFilter===t.ETextureMinFilter.LINEAR,T=t.GLTFUtils.GetTextureSamplingMode(f.magFilter,f.minFilter);this.addPendingData(a);var i=new e.Texture(l,this._babylonScene,h,!1,T,function(){n.removePendingData(a)},function(){n._errors.push("Failed to load texture '"+u.uri+"'"),n.removePendingData(a)});return i.coordinatesIndex=o,i.wrapU=t.GLTFUtils.GetTextureWrapMode(f.wrapS),i.wrapV=t.GLTFUtils.GetTextureWrapMode(f.wrapT),i.name=a.name||"texture"+r.index,a.babylonTextures=a.babylonTextures||[],a.babylonTextures[o]=i,this._parent.onTextureLoaded&&this._parent.onTextureLoaded(i),i},r})();r.Extensions={},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: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.GetByteStrideFromType=function(e){switch(e.type){case"VEC2":return 2;case"VEC3":return 3;case"VEC4":case"MAT2":return 4;case"MAT3":return 9;case"MAT4":return 16;default:return 1}},r.GetTextureSamplingMode=function(r,n){if(r=void 0===r?t.ETextureMagFilter.LINEAR:r,n=void 0===n?t.ETextureMinFilter.LINEAR_MIPMAP_NEAREST: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})();t._Extensions=[],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,e.addLoaderPendingData(t);var a=[t.index].concat(n.ids);return this.loadMaterialLOD(e,t,a,a.length-1,r),!0},r.prototype.loadMaterialLOD=function(t,r,n,a,o){var i=this;t.loadMaterial(n[a],(function(s){if(o(s),0===a)return void t.removeLoaderPendingData(r);t.executeWhenRenderReady((function(l){l&&e.BaseTexture.WhenAllReady(s.getActiveTextures(),(function(){i.loadMaterialLOD(t,r,n,a-1,o)}))}))}))},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)},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),t.loadMaterialAlphaProperties(r)),n.specularGlossinessTexture&&(a.reflectivityTexture=t.loadTexture(n.specularGlossinessTexture),a.useMicroSurfaceFromReflectivityMapAlpha=!0)},r})(t.GLTFLoaderExtension);r.KHRMaterialsPbrSpecularGlossiness=n,t.GLTFLoader.RegisterExtension(new n)})(t.Extensions||(t.Extensions={}))})(e.GLTF2||(e.GLTF2={}))})(BABYLON||(BABYLON={}));