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,o,a,i){var s=t._parse(n),l=this._getLoader(s);if(!l)return void i();l.importMeshAsync(e,r,s,o,a,i)},t.prototype.loadAsync=function(e,r,n,o,a){var i=t._parse(r),s=this._getLoader(i);return s?s.loadAsync(e,i,n,o,a):void a()},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},o=r.json.asset||{},a=t._parseVersion(o.version);if(!a)return e.Tools.Error("Invalid version"),null;var i=t._parseVersion(o.minVersion);if(i&&t._compareVersion(i,n)>0)return e.Tools.Error("Incompatible version"),null;var s={1:t.CreateGLTFLoaderV1,2:t.CreateGLTFLoaderV2},l=s[a.major](this);return null===l?(e.Tools.Error("Unsupported version"),null):l},t._parseBinary=function(n){var o={Magic:1179937895},a=new r(n),i=a.readUint32();if(i!==o.Magic)return e.Tools.Error("Unexpected magic: "+i),null;var s=a.readUint32();switch(s){case 1:return t._parseV1(a);case 2:return t._parseV2(a)}return e.Tools.Error("Unsupported version: "+s),null},t._parseV1=function(r){var n={JSON:0},o=r.readUint32();if(o!=r.getLength())return e.Tools.Error("Length in header does not match actual data length: "+o+" != "+r.getLength()),null;var a,i=r.readUint32(),s=r.readUint32();switch(s){case n.JSON:a=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:a,bin:r.readUint8Array(l)}},t._parseV2=function(r){var n={JSON:1313821514,BIN:5130562},o=r.readUint32();if(o!==r.getLength())return e.Tools.Error("Length in header does not match actual data length: "+o+" != "+r.getLength()),null;var a=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(a))),l=null;r.getPosition()<r.getLength();)switch(a=r.readUint32(),i=r.readUint32()){case n.JSON:return e.Tools.Error("Unexpected JSON chunk"),null;case n.BIN:l=r.readUint8Array(a);break;default:r.skipBytes(a)}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.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&&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(e){this._parent=e}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,o,a,i){var s=this;this._loadAsync(t,r,n,o,(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 o=[];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&&o.push(i.babylonSkeleton)}a(t,null,o)}),i)},r.prototype.loadAsync=function(e,t,r,n,o){this._loadAsync(null,e,t,r,n,o)},r.prototype._loadAsync=function(e,t,r,n,o,a){t.useRightHandedSystem=!0,this._clear(),this._loadData(r),this._babylonScene=t,this._rootUrl=n,this._onSuccess=o,this._onError=a,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(),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._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 o=new Array;this._traverseNodes(n,(function(t){return-1===e.indexOf(t.name)||(o.push(t.index),!1)})),n=o}this._traverseNodes(n,(function(e){return t._loadSkin(e)})),this._traverseNodes(n,(function(e){return t._loadMesh(e)}))},r.prototype._loadSkin=function(t){var r=this;if(void 0!==t.skin){var n=this._gltf.skins[t.skin],o="skeleton"+t.skin;n.babylonSkeleton=new e.Skeleton(n.name||o,o,this._babylonScene),n.index=t.skin;for(var a=0;a<n.joints.length;a++)this._createBone(this._gltf.nodes[n.joints[a]],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,o,a){return r._updateBone(t,a,n,e)}))}))}return!0},r.prototype._updateBone=function(t,r,n,o){var a=n.joints.indexOf(t.index);-1===a&&this._createBone(t,n);var i=t.babylonSkinToBones[n.index],s=-1===a?e.Matrix.Identity():e.Matrix.FromArray(o,16*a);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 o=this._gltf.skins[t.skin];r.skeleton=o.babylonSkeleton}return t.camera,!0},r.prototype._loadMeshData=function(r,n,o){var a=this;o.name=n.name||o.name;var i=new e.MultiMaterial(o.name,this._babylonScene);o.material=i;var s=new e.Geometry(o.name,this._babylonScene,null,!1,o),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 p=n.primitives[h];if(p.mode&&p.mode!==t.EMeshPrimitiveMode.TRIANGLES)throw new Error("Not implemented");f._createMorphTargets(r,n,p,o),f._loadVertexDataAsync(p,(function(t){a._loadMorphTargetsData(n,p,t,o),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===p.material?i.subMaterials[h]=a._getDefaultMaterial():a.loadMaterial(p.material,(function(e){a._renderReady?e.forceCompilation(o,(function(e){a._assignMaterial(i,h,e)})):a._assignMaterial(i,h,e)})),++c===d&&(s.setAllVerticesData(l,!1),o.subMeshes=[],u.forEach((function(t){return new e.SubMesh(t.materialIndex,t.verticesStart,t.verticesCount,t.indicesStart,t.indicesCount,o)})))}))})(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,o=t.attributes;if(!o)return void this._errors.push("Primitive has no attributes");var a,i=new e.VertexData,s=0,l=Object.keys(o).length,u=this;for(var c in o)!(function(c){a=u._gltf.accessors[o[c]],u._loadAccessorAsync(a,(function(o){switch(c){case"NORMAL":i.normals=o;break;case"POSITION":i.positions=o;break;case"TANGENT":i.tangents=o;break;case"TEXCOORD_0":i.uvs=o;break;case"TEXCOORD_1":i.uvs2=o;break;case"JOINTS_0":i.matricesIndices=new Float32Array(Array.prototype.slice.apply(o));break;case"WEIGHTS_0":i.matricesWeights=o;break;case"COLOR_0":i.colors=o;break;default:e.Tools.Warn("Ignoring unrecognized semantic '"+c+"'")}if(++s===l){var a=n._gltf.accessors[t.indices];a?n._loadAccessorAsync(a,(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,o){var a=n.targets;if(a){o.morphTargetManager||(o.morphTargetManager=new e.MorphTargetManager);for(var i=0;i<a.length;i++){var s=t.weights?t.weights[i]:r.weights?r.weights[i]:0;o.morphTargetManager.addTarget(new e.MorphTarget("morphTarget"+i,s))}}},r.prototype._loadMorphTargetsData=function(t,r,n,o){var a=r.targets;if(a)for(var i,s,l=this,u=0;u<a.length;u++)!(function(){var t=o.morphTargetManager.getTarget(u);i=a[u];for(var r in i)!(function(r){s=l._gltf.accessors[i[r]],l._loadAccessorAsync(s,(function(o){s.name&&(t.name=s.name);var a=o;switch(r){case"NORMAL":a.forEach((function(e,t){return a[t]+=n.normals[t]})),t.setNormals(a);break;case"POSITION":a.forEach((function(e,t){return a[t]+=n.positions[t]})),t.setPositions(a);break;case"TANGENT":for(var i=0,l=0;i<a.length;i++,l++)a[i]+=n.tangents[l],(i+1)%3==0&&l++;t.setTangents(a);break;default:e.Tools.Warn("Ignoring unrecognized semantic '"+r+"'")}}))})(r)})()},r.prototype._loadTransform=function(t,r){var n=e.Vector3.Zero(),o=e.Quaternion.Identity(),a=e.Vector3.One();if(t.matrix){e.Matrix.FromArray(t.matrix).decompose(a,o,n)}else t.translation&&(n=e.Vector3.FromArray(t.translation)),t.rotation&&(o=e.Quaternion.FromArray(t.rotation)),t.scale&&(a=e.Vector3.FromArray(t.scale));r.position=n,r.rotationQuaternion=o,r.scaling=a},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 o=0;o<n.children.length;o++)this._traverseNode(n.children[o],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 o=t.channels[n],a=o.sampler,i=t.samplers[a],s=this._gltf.nodes[o.target.node];if(!s)return void e.Tools.Warn("Animation channel target node ("+o.target.node+") does not exist");var l={translation:"position",rotation:"rotationQuaternion",scale:"scaling",weights:"influence"}[o.target.path];if(!l)return void e.Tools.Warn("Animation channel target path '"+o.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,o={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],a={LINEAR:function(e){return{frame:u[e],value:o()}},CUBICSPLINE:function(e){return{frame:u[e],inTangent:o(),value:o(),outTangent:o()}}}[i.interpolation],f=new Array(u.length),h=0;h<u.length;h++)f[h]=a(h);if(t.targets=t.targets||[],"influence"===l)for(var p=s.babylonMesh.morphTargetManager,T=0;T<p.numTargets;T++){var _=p.getTarget(T),g=(t.name||"anim"+r)+"_"+T,b=new e.Animation(g,l,1,d);b.setKeys(f.map((function(e){return{frame:e.frame,inTangent:e.inTangent?e.inTangent[T]:void 0,value:e.value[T],outTangent:e.outTangent?e.outTangent[T]:void 0}}))),_.animations.push(b),t.targets.push(_)}else{var g=t.name||"anim"+r,b=new e.Animation(g,l,1,d);b.setKeys(f);for(var y=0;y<s.babylonAnimationTargets.length;y++){var m=s.babylonAnimationTargets[y];m.animations.push(b.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 o=this,a=this._gltf.buffers[r];if(this.addPendingData(a),a.loadedData)setTimeout((function(){n(a.loadedData),o.removePendingData(a)}));else if(t.GLTFUtils.IsBase64(a.uri)){var i=t.GLTFUtils.DecodeBase64(a.uri);a.loadedData=new Uint8Array(i),setTimeout((function(){n(a.loadedData),o.removePendingData(a)}))}else a.loadedObservable?a.loadedObservable.add((function(e){n(e.loadedData),o.removePendingData(e)})):(a.loadedObservable=new e.Observable,a.loadedObservable.add((function(e){n(e.loadedData),o.removePendingData(e)})),e.Tools.LoadFile(this._rootUrl+a.uri,(function(e){a.loadedData=new Uint8Array(e),a.loadedObservable.notifyObservers(a),a.loadedObservable=null}),null,null,!0,(function(e){o._errors.push("Failed to load file '"+a.uri+"': "+e.statusText+"("+e.status+")"),o.removePendingData(a)})))},r.prototype._loadBufferViewAsync=function(e,r,n,o,a){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(o){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 ("+o+")")}a(l)}))},r.prototype._loadAccessorAsync=function(e,r){var n=this._gltf.bufferViews[e.bufferView],o=e.byteOffset||0,a=e.count*t.GLTFUtils.GetByteStrideFromType(e);this._loadBufferViewAsync(n,o,a,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,o=this._gltf.textures[r.index],a=r.texCoord||0;if(!o||void 0===o.source)return null;var i;if(o.babylonTextures){if(!(i=o.babylonTextures[a]))for(var s=0;s<o.babylonTextures.length;s++)if(i=o.babylonTextures[s]){i=i.clone(),i.coordinatesIndex=a;break}return i}var l,u=this._gltf.images[o.source];if(u.uri)if(t.GLTFUtils.IsBase64(u.uri)){var c=new Uint8Array(t.GLTFUtils.DecodeBase64(u.uri));o.blobURL=URL.createObjectURL(new Blob([c],{type:u.mimeType})),l=o.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){o.blobURL=URL.createObjectURL(new Blob([e],{type:u.mimeType})),o.babylonTextures[a].updateURL(o.blobURL)}))}var f=void 0===o.sampler?{}:this._gltf.samplers[o.sampler],h=f.minFilter===t.ETextureMinFilter.NEAREST||f.minFilter===t.ETextureMinFilter.LINEAR,p=t.GLTFUtils.GetTextureFilterMode(f.minFilter);this.addPendingData(o);var i=new e.Texture(l,this._babylonScene,h,!1,p,function(){n.removePendingData(o)},function(){n._errors.push("Failed to load texture '"+u.uri+"'"),n.removePendingData(o)});return i.coordinatesIndex=a,i.wrapU=t.GLTFUtils.GetWrapMode(f.wrapS),i.wrapV=t.GLTFUtils.GetWrapMode(f.wrapT),i.name=o.name||"texture"+r.index,o.babylonTextures=o.babylonTextures||[],o.babylonTextures[a]=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)),o=0;o<r;o++)n[o]=t.charCodeAt(o);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 o=r[n];if(o.enabled&&t(o))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 o=[t.index].concat(n.ids);return this.loadMaterialLOD(e,t,o,o.length-1,r),!0},r.prototype.loadMaterialLOD=function(t,r,n,o,a){var i=this;t.loadMaterial(n[o],(function(s){if(s.name+=".LOD"+o,a(s),0===o)return void t.removeLoaderPendingData(r);e.BaseTexture.WhenAllReady(s.getActiveTextures(),(function(){i.loadMaterialLOD(t,r,n,o-1,a)}))}))},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 o=r.babylonMaterial;o.albedoColor=n.diffuseFactor?e.Color3.FromArray(n.diffuseFactor):new e.Color3(1,1,1),o.reflectivityColor=n.specularFactor?e.Color3.FromArray(n.specularFactor):new e.Color3(1,1,1),o.microSurface=void 0===n.glossinessFactor?1:n.glossinessFactor,n.diffuseTexture&&(o.albedoTexture=t.loadTexture(n.diffuseTexture),t.loadMaterialAlphaProperties(r)),n.specularGlossinessTexture&&(o.reflectivityTexture=t.loadTexture(n.specularGlossinessTexture),o.useMicroSurfaceFromReflectivityMapAlpha=!0)},r})(t.GLTFLoaderExtension);r.KHRMaterialsPbrSpecularGlossiness=n,t.GLTFLoader.RegisterExtension(new n)})(t.Extensions||(t.Extensions={}))})(e.GLTF2||(e.GLTF2={}))})(BABYLON||(BABYLON={}));