babylon.glTF2FileLoader.min.js 25 KB

1
  1. var BABYLON;!(function(e){var t=(function(){function t(){this.extensions={".gltf":{isBinary:!1},".glb":{isBinary:!0}}}return t.prototype.importMeshAsync=function(e,r,n,a,o,i){var s=t._parse(n),l=this._getLoader(s);if(!l)return void i();l.importMeshAsync(e,r,s,a,o,i)},t.prototype.loadAsync=function(e,r,n,a,o){var i=t._parse(r),s=this._getLoader(i);return s?s.loadAsync(e,i,n,a,o):void o()},t.prototype.canDirectLoad=function(e){return-1!==e.indexOf("scene")&&-1!==e.indexOf("node")},t._parse=function(e){return e instanceof ArrayBuffer?t._parseBinary(e):{json:JSON.parse(e),bin:null}},t.prototype._getLoader=function(r){var n={major:2,minor:0},a=r.json.asset||{},o=t._parseVersion(a.version);if(!o)return e.Tools.Error("Invalid version"),null;var i=t._parseVersion(a.minVersion);if(i&&t._compareVersion(i,n)>0)return e.Tools.Error("Incompatible version"),null;var s={1:t.GLTFLoaderV1,2:t.GLTFLoaderV2},l=s[o.major];return void 0===l?(e.Tools.Error("Unsupported version"),null):null===l?(e.Tools.Error("v"+o.major+" loader is not available"),null):l},t._parseBinary=function(n){var a={Magic:1179937895},o=new r(n),i=o.readUint32();if(i!==a.Magic)return e.Tools.Error("Unexpected magic: "+i),null;var s=o.readUint32();switch(s){case 1:return t._parseV1(o);case 2:return t._parseV2(o)}return e.Tools.Error("Unsupported version: "+s),null},t._parseV1=function(r){var n={JSON:0},a=r.readUint32();if(a!=r.getLength())return e.Tools.Error("Length in header does not match actual data length: "+a+" != "+r.getLength()),null;var o,i=r.readUint32(),s=r.readUint32();switch(s){case n.JSON:o=JSON.parse(t._decodeBufferToText(r.readUint8Array(i)));break;default:return e.Tools.Error("Unexpected content format: "+s),null}var l=r.getLength()-r.getPosition();return{json:o,bin:r.readUint8Array(l)}},t._parseV2=function(r){var n={JSON:1313821514,BIN:5130562},a=r.readUint32();if(a!==r.getLength())return e.Tools.Error("Length in header does not match actual data length: "+a+" != "+r.getLength()),null;var o=r.readUint32(),i=r.readUint32();if(i!==n.JSON)return e.Tools.Error("First chunk format is not JSON"),null;for(var s=JSON.parse(t._decodeBufferToText(r.readUint8Array(o))),l=null;r.getPosition()<r.getLength();)switch(o=r.readUint32(),i=r.readUint32()){case n.JSON:return e.Tools.Error("Unexpected JSON chunk"),null;case n.BIN:l=r.readUint8Array(o);break;default:r.skipBytes(o)}return{json:s,bin:l}},t._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])}},t._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},t._decodeBufferToText=function(e){for(var t="",r=e.byteLength,n=0;n<r;++n)t+=String.fromCharCode(e[n]);return t},t})();t.GLTFLoaderV1=null,t.GLTFLoaderV2=null,t.HomogeneousCoordinates=!1,t.IncrementalLoading=!0,e.GLTFFileLoader=t;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.RegisterPlugin(new t)})(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(){}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.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){t.useRightHandedSystem=!0,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(){this._showMeshes(),this._startAnimations(),0===this._errors.length?this._onSuccess():(this._errors.forEach((function(t){return e.Tools.Error(t)})),this._errors=[],this._onError())},r.prototype._onLoaderComplete=function(){this._errors.forEach((function(t){return e.Tools.Error(t)})),this._errors=[],this._clear()},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._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._renderReady=!1,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 b=n.primitives[h];if(b.mode&&b.mode!==t.EMeshPrimitiveMode.TRIANGLES)throw new Error("Not implemented");f._createMorphTargets(r,n,b,a),f._loadVertexDataAsync(b,(function(t){o._loadMorphTargetsData(n,b,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===b.material?i.subMaterials[h]=o._getDefaultMaterial():o.loadMaterial(b.material,(function(e){o._renderReady?e.forceCompilation(a,(function(e){i.subMaterials[h]=e})):i.subMaterials[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._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(t,r,n,a){var o=r.targets;if(o)for(var i,s,l=this,u=0;u<o.length;u++)!(function(){var t=a.morphTargetManager.getTarget(u);i=o[u];for(var r in i)!(function(r){s=l._gltf.accessors[i[r]],l._loadAccessorAsync(s,(function(a){s.name&&(t.name=s.name);var o=a;switch(r){case"NORMAL":o.forEach((function(e,t){return o[t]+=n.normals[t]})),t.setNormals(o);break;case"POSITION":o.forEach((function(e,t){return o[t]+=n.positions[t]})),t.setPositions(o);break;case"TANGENT":for(var i=0,l=0;i<o.length;i++,l++)o[i]+=n.tangents[l],(i+1)%3==0&&l++;t.setTangents(o);break;default:e.Tools.Warn("Ignoring unrecognized semantic '"+r+"'")}}))})(r)})()},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 b=s.babylonMesh.morphTargetManager,p=0;p<b.numTargets;p++){var T=b.getTarget(p),y=(t.name||"anim"+r)+"_"+p,g=new e.Animation(y,l,1,d);g.setKeys(f.map((function(e){return{frame:e.frame,inTangent:e.inTangent?e.inTangent[p]:void 0,value:e.value[p],outTangent:e.outTangent?e.outTangent[p]:void 0}}))),T.animations.push(g),t.targets.push(T)}else{var y=t.name||"anim"+r,g=new e.Animation(y,l,1,d);g.setKeys(f);for(var _=0;_<s.babylonAnimationTargets.length;_++){var m=s.babylonAnimationTargets[_];m.animations.push(g.clone()),t.targets.push(m)}}}};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){t.babylonMaterial.metallic=1,t.babylonMaterial.roughness=1;var r=t.pbrMetallicRoughness;r&&(t.babylonMaterial.albedoColor=r.baseColorFactor?e.Color3.FromArray(r.baseColorFactor):new e.Color3(1,1,1),t.babylonMaterial.metallic=void 0===r.metallicFactor?1:r.metallicFactor,t.babylonMaterial.roughness=void 0===r.roughnessFactor?1:r.roughnessFactor,r.baseColorTexture&&(t.babylonMaterial.albedoTexture=this.loadTexture(r.baseColorTexture),this.loadMaterialAlphaProperties(t)),r.metallicRoughnessTexture&&(t.babylonMaterial.metallicTexture=this.loadTexture(r.metallicRoughnessTexture),t.babylonMaterial.useMetallnessFromMetallicTextureBlue=!0,t.babylonMaterial.useRoughnessFromMetallicTextureGreen=!0,t.babylonMaterial.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){t.babylonMaterial=new e.PBRMaterial(t.name||"mat"+t.index,this._babylonScene),t.babylonMaterial.sideOrientation=e.Material.CounterClockWiseSideOrientation,t.babylonMaterial.useScalarInLinearSpace=!0},r.prototype.loadMaterialBaseProperties=function(t){t.babylonMaterial.useEmissiveAsIllumination=!(!t.emissiveFactor&&!t.emissiveTexture),t.babylonMaterial.emissiveColor=t.emissiveFactor?e.Color3.FromArray(t.emissiveFactor):new e.Color3(0,0,0),t.doubleSided&&(t.babylonMaterial.backFaceCulling=!1,t.babylonMaterial.twoSidedLighting=!0),t.normalTexture&&(t.babylonMaterial.bumpTexture=this.loadTexture(t.normalTexture),void 0!==t.normalTexture.scale&&(t.babylonMaterial.bumpTexture.level=t.normalTexture.scale)),t.occlusionTexture&&(t.babylonMaterial.ambientTexture=this.loadTexture(t.occlusionTexture),t.babylonMaterial.useAmbientInGrayScale=!0,void 0!==t.occlusionTexture.strength&&(t.babylonMaterial.ambientTextureStrength=t.occlusionTexture.strength)),t.emissiveTexture&&(t.babylonMaterial.emissiveTexture=this.loadTexture(t.emissiveTexture))},r.prototype.loadMaterialAlphaProperties=function(t){switch(t.alphaMode||"OPAQUE"){case"OPAQUE":break;case"MASK":t.babylonMaterial.albedoTexture.hasAlpha=!0,t.babylonMaterial.useAlphaFromAlbedoTexture=!1;break;case"BLEND":t.babylonMaterial.albedoTexture.hasAlpha=!0,t.babylonMaterial.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,b=t.GLTFUtils.GetTextureFilterMode(f.minFilter);this.addPendingData(a);var i=new e.Texture(l,this._babylonScene,h,!1,b,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.GetWrapMode(f.wrapS),i.wrapV=t.GLTFUtils.GetWrapMode(f.wrapT),i.name=a.name,a.babylonTextures=a.babylonTextures||[],a.babylonTextures[o]=i,i},r})();r.Extensions={},t.GLTFLoader=r,e.GLTFFileLoader.GLTFLoaderV2=new r})(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.GetWrapMode=function(r){switch(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:default:return 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.GetTextureFilterMode=function(r){switch(r){case t.ETextureMinFilter.LINEAR:case t.ETextureMinFilter.LINEAR_MIPMAP_NEAREST:case t.ETextureMinFilter.LINEAR_MIPMAP_LINEAR:return e.Texture.TRILINEAR_SAMPLINGMODE;case t.ETextureMinFilter.NEAREST:case t.ETextureMinFilter.NEAREST_MIPMAP_NEAREST:return e.Texture.NEAREST_SAMPLINGMODE;default:return e.Texture.BILINEAR_SAMPLINGMODE}},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(s.name+=".LOD"+a,o(s),0===a)return void t.removeLoaderPendingData(r);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(t,r,n){if(!r.extensions)return!1;var a=r.extensions[this.name];return!!a&&(t.createPbrMaterial(r),t.loadMaterialBaseProperties(r),r.babylonMaterial.albedoColor=a.diffuseFactor?e.Color3.FromArray(a.diffuseFactor):new e.Color3(1,1,1),r.babylonMaterial.reflectivityColor=a.specularFactor?e.Color3.FromArray(a.specularFactor):new e.Color3(1,1,1),r.babylonMaterial.microSurface=void 0===a.glossinessFactor?1:a.glossinessFactor,a.diffuseTexture&&(r.babylonMaterial.albedoTexture=t.loadTexture(a.diffuseTexture),t.loadMaterialAlphaProperties(r)),a.specularGlossinessTexture&&(r.babylonMaterial.reflectivityTexture=t.loadTexture(a.specularGlossinessTexture),r.babylonMaterial.useMicroSurfaceFromReflectivityMapAlpha=!0),n(r.babylonMaterial),!0)},r})(t.GLTFLoaderExtension);r.KHRMaterialsPbrSpecularGlossiness=n,t.GLTFLoader.RegisterExtension(new n)})(t.Extensions||(t.Extensions={}))})(e.GLTF2||(e.GLTF2={}))})(BABYLON||(BABYLON={}));