babylonjs-loaders 9.27.0 → 9.28.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/babylon.objFileLoader.js +1 -1
- package/babylon.objFileLoader.js.map +1 -1
- package/babylon.objFileLoader.min.js +1 -1
- package/babylon.objFileLoader.min.js.map +1 -1
- package/babylonjs.loaders.d.ts +19 -1
- package/babylonjs.loaders.es5.js +1261 -1161
- package/babylonjs.loaders.js +1 -1
- package/babylonjs.loaders.js.map +1 -1
- package/babylonjs.loaders.min.js +1 -1
- package/babylonjs.loaders.min.js.map +1 -1
- package/babylonjs.loaders.module.d.ts +38 -2
- package/package.json +3 -3
package/babylon.objFileLoader.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("babylonjs")):"function"==typeof define&&define.amd?define(["exports","babylonjs"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).BABYLON=e.BABYLON||{},e.BABYLON)}(this,function(e,t){"use strict";class s{constructor(){this.materials=[]}parseMTL(e,i,o,r){if(i instanceof ArrayBuffer)return;const n=i.split("\n"),a=/\s+/;let l,h=null;for(let i=0;i<n.length;i++){const _=n[i].trim();if(0===_.length||"#"===_.charAt(0))continue;const p=_.indexOf(" ");let d=p>=0?_.substring(0,p):_;d=d.toLowerCase();const m=p>=0?_.substring(p+1).trim():"";if("newmtl"===d)h&&this.materials.push(h),e._blockEntityCollection=!!r,h=new t.StandardMaterial(m,e),h._parentContainer=r,e._blockEntityCollection=!1;else if("kd"===d&&h)l=m.split(a,3).map(parseFloat),h.diffuseColor=t.Color3.FromArray(l);else if("ka"===d&&h)l=m.split(a,3).map(parseFloat),h.ambientColor=t.Color3.FromArray(l);else if("ks"===d&&h)l=m.split(a,3).map(parseFloat),h.specularColor=t.Color3.FromArray(l);else if("ke"===d&&h)l=m.split(a,3).map(parseFloat),h.emissiveColor=t.Color3.FromArray(l);else if("ns"===d&&h)h.specularPower=parseFloat(m);else if("d"===d&&h)h.alpha=parseFloat(m);else if("map_ka"===d&&h)h.ambientTexture=s._GetTexture(o,m,e);else if("map_kd"===d&&h)h.diffuseTexture=s._GetTexture(o,m,e);else if("map_ks"===d&&h)h.specularTexture=s._GetTexture(o,m,e);else if("map_ns"===d);else if("map_bump"===d&&h){const t=m.split(a),i=t.indexOf("-bm");let r=null;i>=0&&(r=t[i+1],t.splice(i,2)),h.bumpTexture=s._GetTexture(o,t.join(" "),e),h.bumpTexture&&null!==r&&(h.bumpTexture.level=parseFloat(r))}else"map_d"===d&&h&&(h.opacityTexture=s._GetTexture(o,m,e))}h&&this.materials.push(h)}static _GetTexture(e,i,o){if(!i)return null;let r=e;if("file:"===e){let e=i.lastIndexOf("\\");-1===e&&(e=i.lastIndexOf("/")),r+=e>-1?i.substring(e+1):i}else r+=i;return new t.Texture(r,o,!1,s.INVERT_TEXTURE_Y)}}s.INVERT_TEXTURE_Y=!0;class i{constructor(e,s,i){this._positions=[],this._normals=[],this._uvs=[],this._colors=[],this._extColors=[],this._meshesFromObj=[],this._indicesForBabylon=[],this._wrappedPositionForBabylon=[],this._wrappedUvsForBabylon=[],this._wrappedColorsForBabylon=[],this._wrappedNormalsForBabylon=[],this._tuplePosNorm=[],this._curPositionInIndices=0,this._hasMeshes=!1,this._unwrappedPositionsForBabylon=[],this._unwrappedColorsForBabylon=[],this._unwrappedNormalsForBabylon=[],this._unwrappedUVForBabylon=[],this._triangles=[],this._materialNameFromObj="",this._objMeshName="",this._increment=1,this._isFirstMaterial=!0,this._grayColor=new t.Color4(.5,.5,.5,1),this._hasLineData=!1,this._materialToUse=e,this._babylonMeshesArray=s,this._loadingOptions=i}_isInArray(e,t){e[t[0]]||(e[t[0]]={normals:[],idx:[]});const s=e[t[0]].normals.indexOf(t[1]);return-1===s?-1:e[t[0]].idx[s]}_isInArrayUV(e,t){e[t[0]]||(e[t[0]]={normals:[],idx:[],uv:[]});const s=e[t[0]].normals.indexOf(t[1]);return 1!=s&&t[2]===e[t[0]].uv[s]?e[t[0]].idx[s]:-1}_setData(e){var t,s;let i;null!==(t=e.indiceUvsFromObj)&&void 0!==t||(e.indiceUvsFromObj=-1),null!==(s=e.indiceNormalFromObj)&&void 0!==s||(e.indiceNormalFromObj=-1),i=this._loadingOptions.optimizeWithUV?this._isInArrayUV(this._tuplePosNorm,[e.indicePositionFromObj,e.indiceNormalFromObj,e.indiceUvsFromObj]):this._isInArray(this._tuplePosNorm,[e.indicePositionFromObj,e.indiceNormalFromObj]),-1===i?(this._indicesForBabylon.push(this._wrappedPositionForBabylon.length),this._wrappedPositionForBabylon.push(e.positionVectorFromOBJ),void 0!==e.textureVectorFromOBJ&&this._wrappedUvsForBabylon.push(e.textureVectorFromOBJ),void 0!==e.normalsVectorFromOBJ&&this._wrappedNormalsForBabylon.push(e.normalsVectorFromOBJ),void 0!==e.positionColorsFromOBJ&&this._wrappedColorsForBabylon.push(e.positionColorsFromOBJ),this._tuplePosNorm[e.indicePositionFromObj].normals.push(e.indiceNormalFromObj),this._tuplePosNorm[e.indicePositionFromObj].idx.push(this._curPositionInIndices++),this._loadingOptions.optimizeWithUV&&this._tuplePosNorm[e.indicePositionFromObj].uv.push(e.indiceUvsFromObj)):this._indicesForBabylon.push(i)}_unwrapData(){try{for(let e=0;e<this._wrappedPositionForBabylon.length;e++)this._unwrappedPositionsForBabylon.push(this._wrappedPositionForBabylon[e].x*this._handednessSign,this._wrappedPositionForBabylon[e].y,this._wrappedPositionForBabylon[e].z),this._wrappedNormalsForBabylon.length&&this._unwrappedNormalsForBabylon.push(this._wrappedNormalsForBabylon[e].x*this._handednessSign,this._wrappedNormalsForBabylon[e].y,this._wrappedNormalsForBabylon[e].z),this._wrappedUvsForBabylon.length&&this._unwrappedUVForBabylon.push(this._wrappedUvsForBabylon[e].x,this._wrappedUvsForBabylon[e].y),this._wrappedColorsForBabylon.length&&this._unwrappedColorsForBabylon.push(this._wrappedColorsForBabylon[e].r,this._wrappedColorsForBabylon[e].g,this._wrappedColorsForBabylon[e].b,this._wrappedColorsForBabylon[e].a);this._wrappedPositionForBabylon.length=0,this._wrappedNormalsForBabylon.length=0,this._wrappedUvsForBabylon.length=0,this._wrappedColorsForBabylon.length=0,this._tuplePosNorm.length=0,this._curPositionInIndices=0}catch(e){throw new Error("Unable to unwrap data while parsing OBJ data.",{cause:e})}}_getTriangles(e,t){for(let s=t;s<e.length-1;s++)this._pushTriangle(e,s)}_getColor(e){var t;return this._loadingOptions.importVertexColors?null!==(t=this._extColors[e])&&void 0!==t?t:this._colors[e]:void 0}_setDataForCurrentFaceWithPattern1(e,t){this._getTriangles(e,t);for(let e=0;e<this._triangles.length;e++){const t=parseInt(this._triangles[e])-1;this._setData({indicePositionFromObj:t,positionVectorFromOBJ:this._positions[t],positionColorsFromOBJ:this._getColor(t)})}this._triangles.length=0}_setDataForCurrentFaceWithPattern2(e,t){this._getTriangles(e,t);for(let e=0;e<this._triangles.length;e++){const t=this._triangles[e].split("/"),s=parseInt(t[0])-1,i=parseInt(t[1])-1;this._setData({indicePositionFromObj:s,indiceUvsFromObj:i,positionVectorFromOBJ:this._positions[s],textureVectorFromOBJ:this._uvs[i],positionColorsFromOBJ:this._getColor(s)})}this._triangles.length=0}_setDataForCurrentFaceWithPattern3(e,t){this._getTriangles(e,t);for(let e=0;e<this._triangles.length;e++){const t=this._triangles[e].split("/"),s=parseInt(t[0])-1,i=parseInt(t[1])-1,o=parseInt(t[2])-1;this._setData({indicePositionFromObj:s,indiceUvsFromObj:i,indiceNormalFromObj:o,positionVectorFromOBJ:this._positions[s],textureVectorFromOBJ:this._uvs[i],normalsVectorFromOBJ:this._normals[o]})}this._triangles.length=0}_setDataForCurrentFaceWithPattern4(e,t){this._getTriangles(e,t);for(let e=0;e<this._triangles.length;e++){const t=this._triangles[e].split("//"),s=parseInt(t[0])-1,i=parseInt(t[1])-1;this._setData({indicePositionFromObj:s,indiceNormalFromObj:i,positionVectorFromOBJ:this._positions[s],normalsVectorFromOBJ:this._normals[i],positionColorsFromOBJ:this._getColor(s)})}this._triangles.length=0}_setDataForCurrentFaceWithPattern5(e,t){this._getTriangles(e,t);for(let e=0;e<this._triangles.length;e++){const t=this._triangles[e].split("/"),s=this._positions.length+parseInt(t[0]),i=this._uvs.length+parseInt(t[1]),o=this._normals.length+parseInt(t[2]);this._setData({indicePositionFromObj:s,indiceUvsFromObj:i,indiceNormalFromObj:o,positionVectorFromOBJ:this._positions[s],textureVectorFromOBJ:this._uvs[i],normalsVectorFromOBJ:this._normals[o],positionColorsFromOBJ:this._getColor(s)})}this._triangles.length=0}_addPreviousObjMesh(){this._meshesFromObj.length>0&&(this._handledMesh=this._meshesFromObj[this._meshesFromObj.length-1],this._unwrapData(),this._loadingOptions.useLegacyBehavior&&this._indicesForBabylon.reverse(),this._handledMesh.indices=this._indicesForBabylon.slice(),this._handledMesh.positions=this._unwrappedPositionsForBabylon.slice(),this._unwrappedNormalsForBabylon.length&&(this._handledMesh.normals=this._unwrappedNormalsForBabylon.slice()),this._unwrappedUVForBabylon.length&&(this._handledMesh.uvs=this._unwrappedUVForBabylon.slice()),this._unwrappedColorsForBabylon.length&&(this._handledMesh.colors=this._unwrappedColorsForBabylon.slice()),this._handledMesh.hasLines=this._hasLineData,this._indicesForBabylon.length=0,this._unwrappedPositionsForBabylon.length=0,this._unwrappedColorsForBabylon.length=0,this._unwrappedNormalsForBabylon.length=0,this._unwrappedUVForBabylon.length=0,this._hasLineData=!1)}_optimizeNormals(e){const s=e.getVerticesData(t.VertexBuffer.PositionKind),i=e.getVerticesData(t.VertexBuffer.NormalKind),o={};if(!s||!i)return;for(let e=0;e<s.length/3;e++){const t=s[3*e+0]+"_"+s[3*e+1]+"_"+s[3*e+2];let i=o[t];i||(i=[],o[t]=i),i.push(e)}const r=new t.Vector3;for(const e in o){const t=o[e];if(t.length<2)continue;const s=t[0];for(let e=1;e<t.length;++e){const o=t[e];i[3*s+0]+=i[3*o+0],i[3*s+1]+=i[3*o+1],i[3*s+2]+=i[3*o+2]}r.copyFromFloats(i[3*s+0],i[3*s+1],i[3*s+2]),r.normalize();for(let e=0;e<t.length;++e){const s=t[e];i[3*s+0]=r.x,i[3*s+1]=r.y,i[3*s+2]=r.z}}e.setVerticesData(t.VertexBuffer.NormalKind,i)}static _IsLineElement(e){return e.startsWith("l")}static _IsObjectElement(e){return e.startsWith("o")}static _IsGroupElement(e){return e.startsWith("g")}static _GetZbrushMRGB(e,s){if(!e.startsWith("mrgb"))return null;if(e=e.replace("mrgb","").trim(),s)return[];const i=e.match(/[a-z0-9]/g);if(!i||i.length%8!=0)return[];const o=[];for(let e=0;e<i.length/8;e++){const s=i[8*e+2]+i[8*e+3],r=i[8*e+4]+i[8*e+5],n=i[8*e+6]+i[8*e+7];o.push(new t.Color4(parseInt(s,16)/255,parseInt(r,16)/255,parseInt(n,16)/255,1))}return o}parse(e,s,o,r,n){var a,l,h,_,p;s=(s=s.replace(/#MRGB/g,"mrgb")).replace(/#.*$/gm,"").trim(),this._loadingOptions.useLegacyBehavior?(this._pushTriangle=(e,t)=>this._triangles.push(e[0],e[t],e[t+1]),this._handednessSign=1):o.useRightHandedSystem?(this._pushTriangle=(e,t)=>this._triangles.push(e[0],e[t+1],e[t]),this._handednessSign=1):(this._pushTriangle=(e,t)=>this._triangles.push(e[0],e[t],e[t+1]),this._handednessSign=-1);const d=s.split("\n"),m=[];let c=[];m.push(c);for(let e=0;e<d.length;e++){const t=d[e].trim().replace(/\s\s/g," ");if(0!==t.length&&"#"!==t.charAt(0))if((i._IsGroupElement(t)||i._IsObjectElement(t))&&(c=[],m.push(c)),i._IsLineElement(t)){const e=t.split(" ");for(let t=1;t<e.length-1;t++)c.push(`l ${e[t]} ${e[t+1]}`)}else c.push(t)}const u=m.flat();for(let e=0;e<u.length;e++){const s=u[e].trim().replace(/\s\s/g," ");let o;if(0!==s.length&&"#"!==s.charAt(0))if(i.VertexPattern.test(s)){if(o=s.match(/[^ ]+/g),this._positions.push(new t.Vector3(parseFloat(o[1]),parseFloat(o[2]),parseFloat(o[3]))),this._loadingOptions.importVertexColors)if(o.length>=7){const e=parseFloat(o[4]),s=parseFloat(o[5]),i=parseFloat(o[6]);this._colors.push(new t.Color4(e>1?e/255:e,s>1?s/255:s,i>1?i/255:i,7===o.length||void 0===o[7]?1:parseFloat(o[7])))}else this._colors.push(this._grayColor)}else if(null!==(o=i.NormalPattern.exec(s)))this._normals.push(new t.Vector3(parseFloat(o[1]),parseFloat(o[2]),parseFloat(o[3])));else if(null!==(o=i.UVPattern.exec(s)))this._uvs.push(new t.Vector2(parseFloat(o[1])*this._loadingOptions.UVScaling.x,parseFloat(o[2])*this._loadingOptions.UVScaling.y));else if(null!==(o=i.FacePattern3.exec(s)))this._setDataForCurrentFaceWithPattern3(o[1].trim().split(" "),1);else if(null!==(o=i.FacePattern4.exec(s)))this._setDataForCurrentFaceWithPattern4(o[1].trim().split(" "),1);else if(null!==(o=i.FacePattern5.exec(s)))this._setDataForCurrentFaceWithPattern5(o[1].trim().split(" "),1);else if(null!==(o=i.FacePattern2.exec(s)))this._setDataForCurrentFaceWithPattern2(o[1].trim().split(" "),1);else if(null!==(o=i.FacePattern1.exec(s)))this._setDataForCurrentFaceWithPattern1(o[1].trim().split(" "),1);else if(null!==(o=i.LinePattern1.exec(s)))this._setDataForCurrentFaceWithPattern1(o[1].trim().split(" "),0),this._hasLineData=!0;else if(null!==(o=i.LinePattern2.exec(s)))this._setDataForCurrentFaceWithPattern2(o[1].trim().split(" "),0),this._hasLineData=!0;else if(o=i._GetZbrushMRGB(s,!this._loadingOptions.importVertexColors))for(const e of o)this._extColors.push(e);else if(null!==(o=i.LinePattern3.exec(s)))this._setDataForCurrentFaceWithPattern3(o[1].trim().split(" "),0),this._hasLineData=!0;else if(i.GroupDescriptor.test(s)||i.ObjectDescriptor.test(s)){const e={name:s.substring(2).trim(),indices:null,positions:null,normals:null,uvs:null,colors:null,materialName:this._materialNameFromObj,isObject:i.ObjectDescriptor.test(s)};this._addPreviousObjMesh(),this._meshesFromObj.push(e),this._hasMeshes=!0,this._isFirstMaterial=!0,this._increment=1}else if(i.UseMtlDescriptor.test(s)){if(this._materialNameFromObj=s.substring(7).trim(),!this._isFirstMaterial||!this._hasMeshes){this._addPreviousObjMesh();const e={name:(this._objMeshName||"mesh")+"_mm"+this._increment.toString(),indices:null,positions:null,normals:null,uvs:null,colors:null,materialName:this._materialNameFromObj,isObject:!1};this._increment++,this._meshesFromObj.push(e),this._hasMeshes=!0}this._hasMeshes&&this._isFirstMaterial&&(this._meshesFromObj[this._meshesFromObj.length-1].materialName=this._materialNameFromObj,this._isFirstMaterial=!1)}else i.MtlLibGroupDescriptor.test(s)?n(s.substring(7).trim()):i.SmoothDescriptor.test(s)||t.Logger.Log("Unhandled expression at line : "+s)}if(this._hasMeshes&&(this._handledMesh=this._meshesFromObj[this._meshesFromObj.length-1],this._loadingOptions.useLegacyBehavior&&this._indicesForBabylon.reverse(),this._unwrapData(),this._handledMesh.indices=this._indicesForBabylon,this._handledMesh.positions=this._unwrappedPositionsForBabylon,this._unwrappedNormalsForBabylon.length&&(this._handledMesh.normals=this._unwrappedNormalsForBabylon),this._unwrappedUVForBabylon.length&&(this._handledMesh.uvs=this._unwrappedUVForBabylon),this._unwrappedColorsForBabylon.length&&(this._handledMesh.colors=this._unwrappedColorsForBabylon),this._handledMesh.hasLines=this._hasLineData),!this._hasMeshes){let e=null;if(this._indicesForBabylon.length)this._loadingOptions.useLegacyBehavior&&this._indicesForBabylon.reverse(),this._unwrapData();else{for(const e of this._positions)this._unwrappedPositionsForBabylon.push(e.x,e.y,e.z);if(this._normals.length)for(const e of this._normals)this._unwrappedNormalsForBabylon.push(e.x,e.y,e.z);if(this._uvs.length)for(const e of this._uvs)this._unwrappedUVForBabylon.push(e.x,e.y);if(this._extColors.length)for(const e of this._extColors)this._unwrappedColorsForBabylon.push(e.r,e.g,e.b,e.a);else if(this._colors.length)for(const e of this._colors)this._unwrappedColorsForBabylon.push(e.r,e.g,e.b,e.a);this._materialNameFromObj||(e=new t.StandardMaterial(t.Geometry.RandomId(),o),e.pointsCloud=!0,this._materialNameFromObj=e.name,this._normals.length||(e.disableLighting=!0,e.emissiveColor=t.Color3.White()))}this._meshesFromObj.push({name:t.Geometry.RandomId(),indices:this._indicesForBabylon,positions:this._unwrappedPositionsForBabylon,colors:this._unwrappedColorsForBabylon,normals:this._unwrappedNormalsForBabylon,uvs:this._unwrappedUVForBabylon,materialName:this._materialNameFromObj,directMaterial:e,isObject:!0,hasLines:this._hasLineData})}for(let s=0;s<this._meshesFromObj.length;s++){if(e&&this._meshesFromObj[s].name)if(e instanceof Array){if(-1===e.indexOf(this._meshesFromObj[s].name))continue}else if(this._meshesFromObj[s].name!==e)continue;this._handledMesh=this._meshesFromObj[s],o._blockEntityCollection=!!r;const i=new t.Mesh(this._meshesFromObj[s].name,o);if(i._parentContainer=r,o._blockEntityCollection=!1,this._handledMesh._babylonMesh=i,!this._handledMesh.isObject)for(let e=s-1;e>=0;--e)if(this._meshesFromObj[e].isObject&&this._meshesFromObj[e]._babylonMesh){i.parent=this._meshesFromObj[e]._babylonMesh;break}if(this._materialToUse.push(this._meshesFromObj[s].materialName),this._handledMesh.hasLines&&(null!==(a=i._internalMetadata)&&void 0!==a||(i._internalMetadata={}),i._internalMetadata._isLine=!0),0===(null===(l=this._handledMesh.positions)||void 0===l?void 0:l.length)){this._babylonMeshesArray.push(i);continue}const n=new t.VertexData;if(n.indices=this._handledMesh.indices,n.positions=this._handledMesh.positions,this._loadingOptions.computeNormals||!(null===(h=this._handledMesh.normals)||void 0===h?void 0:h.length)){const e=new Array;t.VertexData.ComputeNormals(this._handledMesh.positions,this._handledMesh.indices,e),n.normals=e}else n.normals=this._handledMesh.normals;(null===(_=this._handledMesh.uvs)||void 0===_?void 0:_.length)&&(n.uvs=this._handledMesh.uvs),(null===(p=this._handledMesh.colors)||void 0===p?void 0:p.length)&&(n.colors=this._handledMesh.colors),n.applyToMesh(i),this._loadingOptions.invertY&&(i.scaling.y*=-1),this._loadingOptions.optimizeNormals&&this._optimizeNormals(i),this._babylonMeshesArray.push(i),this._handledMesh.directMaterial&&(i.material=this._handledMesh.directMaterial)}}}i.ObjectDescriptor=/^o/,i.GroupDescriptor=/^g/,i.MtlLibGroupDescriptor=/^mtllib /,i.UseMtlDescriptor=/^usemtl /,i.SmoothDescriptor=/^s /,i.VertexPattern=/^v(\s+[\d|.|+|\-|e|E]+){3,7}/,i.NormalPattern=/^vn(\s+[\d|.|+|\-|e|E]+)( +[\d|.|+|\-|e|E]+)( +[\d|.|+|\-|e|E]+)/,i.UVPattern=/^vt(\s+[\d|.|+|\-|e|E]+)( +[\d|.|+|\-|e|E]+)/,i.FacePattern1=/^f\s+(([\d]{1,}[\s]?){3,})+/,i.FacePattern2=/^f\s+((([\d]{1,}\/[\d]{1,}[\s]?){3,})+)/,i.FacePattern3=/^f\s+((([\d]{1,}\/[\d]{1,}\/[\d]{1,}[\s]?){3,})+)/,i.FacePattern4=/^f\s+((([\d]{1,}\/\/[\d]{1,}[\s]?){3,})+)/,i.FacePattern5=/^f\s+(((-[\d]{1,}\/-[\d]{1,}\/-[\d]{1,}[\s]?){3,})+)/,i.LinePattern1=/^l\s+(([\d]{1,}[\s]?){2,})+/,i.LinePattern2=/^l\s+((([\d]{1,}\/[\d]{1,}[\s]?){2,})+)/,i.LinePattern3=/^l\s+((([\d]{1,}\/[\d]{1,}\/[\d]{1,}[\s]?){2,})+)/;const o={".obj":{isBinary:!0}};class r{static get INVERT_TEXTURE_Y(){return s.INVERT_TEXTURE_Y}static set INVERT_TEXTURE_Y(e){s.INVERT_TEXTURE_Y=e}constructor(e){this.name="obj",this.extensions=o,this._assetContainer=null,this._loadingOptions=Object.assign(Object.assign({},r._DefaultLoadingOptions),null!=e?e:{})}static get _DefaultLoadingOptions(){return{encoding:r.ENCODING,computeNormals:r.COMPUTE_NORMALS,optimizeNormals:r.OPTIMIZE_NORMALS,importVertexColors:r.IMPORT_VERTEX_COLORS,invertY:r.INVERT_Y,invertTextureY:r.INVERT_TEXTURE_Y,UVScaling:r.UV_SCALING,materialLoadingFailsSilently:r.MATERIAL_LOADING_FAILS_SILENTLY,optimizeWithUV:r.OPTIMIZE_WITH_UV,skipMaterials:r.SKIP_MATERIALS,useLegacyBehavior:r.USE_LEGACY_BEHAVIOR}}_loadMTL(e,s,i,o){const r=s+e;t.Tools.LoadFile(r,i,void 0,void 0,!0,(e,t)=>{o(r,t)})}_decode(e){var t;if("string"==typeof e)return e;const s=new Uint8Array(e),i=null!==(t=this._loadingOptions.encoding)&&void 0!==t?t:"auto";if("auto"!==i)return new TextDecoder(i).decode(s);if(255===s[0]&&254===s[1])return new TextDecoder("utf-16le").decode(s);if(254===s[0]&&255===s[1])return new TextDecoder("utf-16be").decode(s);try{return new TextDecoder("utf-8",{fatal:!0}).decode(s)}catch(e){return new TextDecoder("gb18030").decode(s)}}createPlugin(e){return new r(e.obj)}canDirectLoad(){return!1}importMeshAsync(e,t,s,i){return this._parseSolidAsync(e,t,s,i).then(e=>({meshes:e,particleSystems:[],skeletons:[],animationGroups:[],transformNodes:[],geometries:[],lights:[],spriteManagers:[]}))}loadAsync(e,t,s){return this.importMeshAsync(null,e,t,s).then(()=>{})}loadAssetContainerAsync(e,s,i){const o=new t.AssetContainer(e);return this._assetContainer=o,this.importMeshAsync(null,e,s,i).then(e=>(e.meshes.forEach(e=>o.meshes.push(e)),e.meshes.forEach(e=>{const t=e.material;t&&-1==o.materials.indexOf(t)&&(o.materials.push(t),t.getActiveTextures().forEach(e=>{-1==o.textures.indexOf(e)&&o.textures.push(e)}))}),this._assetContainer=null,o)).catch(e=>{throw this._assetContainer=null,e})}_parseSolidAsync(e,o,r,n){let a="";const l=new s,h=[],_=[];r=(r=this._decode(r)).replace(/#.*$/gm,"").trim(),new i(h,_,this._loadingOptions).parse(e,r,o,this._assetContainer,e=>{a=e});const p=[];return""===a||this._loadingOptions.skipMaterials||p.push(new Promise((e,s)=>{this._loadMTL(a,n,i=>{try{l.parseMTL(o,this._decode(i),n,this._assetContainer);for(let e=0;e<l.materials.length;e++){let t=0;const s=[];let i;for(;(i=h.indexOf(l.materials[e].name,t))>-1;)s.push(i),t=i+1;if(-1===i&&0===s.length)l.materials[e].dispose();else for(let t=0;t<s.length;t++){const i=_[s[t]],o=l.materials[e];i.material=o,i.getTotalIndices()||(o.pointsCloud=!0)}}e()}catch(i){t.Tools.Warn(`Error processing MTL file: '${a}'`),this._loadingOptions.materialLoadingFailsSilently?e():s(i)}},(i,o)=>{t.Tools.Warn(`Error downloading MTL file: '${a}'`),this._loadingOptions.materialLoadingFailsSilently?e():s(o)})})),Promise.all(p).then(()=>{const e=e=>{var t,s;return Boolean(null!==(s=null===(t=e._internalMetadata)||void 0===t?void 0:t._isLine)&&void 0!==s&&s)};return _.forEach(s=>{var i,r;if(e(s)){let n=null!==(i=s.material)&&void 0!==i?i:new t.StandardMaterial(s.name+"_line",o);n.getBindedMeshes().filter(t=>!e(t)).length>0&&(n=null!==(r=n.clone(n.name+"_line"))&&void 0!==r?r:n),n.wireframe=!0,s.material=n,s._internalMetadata&&(s._internalMetadata._isLine=void 0)}}),_})}}r.ENCODING="auto",r.OPTIMIZE_WITH_UV=!0,r.INVERT_Y=!1,r.IMPORT_VERTEX_COLORS=!1,r.COMPUTE_NORMALS=!1,r.OPTIMIZE_NORMALS=!1,r.UV_SCALING=new t.Vector2(1,1),r.SKIP_MATERIALS=!1,r.MATERIAL_LOADING_FAILS_SILENTLY=!0,r.USE_LEGACY_BEHAVIOR=!1;let n=!1;function a(){n||(n=!0,t.RegisterSceneLoaderPlugin(new r))}a();var l={__proto__:null,MTLFileLoader:s,OBJFileLoader:r,RegisterOBJFileLoader:a,SolidParser:i};const h="undefined"!=typeof global?global:"undefined"!=typeof window?window:void 0;if(void 0!==h)for(const e in l)h.BABYLON[e]||(h.BABYLON[e]=l[e]);e.MTLFileLoader=s,e.OBJFileLoader=r,e.RegisterOBJFileLoader=a,e.SolidParser=i});
|
|
1
|
+
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("babylonjs")):"function"==typeof define&&define.amd?define(["exports","babylonjs"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).BABYLON=t.BABYLON||{},t.BABYLON)}(this,function(t,e){"use strict";class s{constructor(){this.materials=[]}parseMTL(t,i,o,n,r=s.INVERT_TEXTURE_Y,a,l=!1){const h=l?[]:null;if(i instanceof ArrayBuffer)return null!=h?h:[];const _=i.split("\n"),p=/\s+/;let d,c=null;for(let i=0;i<_.length;i++){const l=_[i].trim();if(0===l.length||"#"===l.charAt(0))continue;const u=l.indexOf(" ");let m=u>=0?l.substring(0,u):l;m=m.toLowerCase();const F=u>=0?l.substring(u+1).trim():"";if("newmtl"===m){if(c&&this.materials.push(c),a&&!a.has(F)){c=null;continue}const s=t._blockEntityCollection;t._blockEntityCollection=!!n;try{c=new e.StandardMaterial(F,t),c._parentContainer=n,null==n||n.materials.push(c)}finally{t._blockEntityCollection=s}}else if("kd"===m&&c)d=F.split(p,3).map(parseFloat),c.diffuseColor=e.Color3.FromArray(d);else if("ka"===m&&c)d=F.split(p,3).map(parseFloat),c.ambientColor=e.Color3.FromArray(d);else if("ks"===m&&c)d=F.split(p,3).map(parseFloat),c.specularColor=e.Color3.FromArray(d);else if("ke"===m&&c)d=F.split(p,3).map(parseFloat),c.emissiveColor=e.Color3.FromArray(d);else if("ns"===m&&c)c.specularPower=parseFloat(F);else if("d"===m&&c)c.alpha=parseFloat(F);else if("map_ka"===m&&c)c.ambientTexture=s._GetTexture(o,F,t,n,r,h);else if("map_kd"===m&&c)c.diffuseTexture=s._GetTexture(o,F,t,n,r,h);else if("map_ks"===m&&c)c.specularTexture=s._GetTexture(o,F,t,n,r,h);else if("map_ns"===m);else if("map_bump"===m&&c){const e=F.split(p),i=e.indexOf("-bm");let a=null;i>=0&&(a=e[i+1],e.splice(i,2)),c.bumpTexture=s._GetTexture(o,e.join(" "),t,n,r,h),c.bumpTexture&&null!==a&&(c.bumpTexture.level=parseFloat(a))}else"map_d"===m&&c&&(c.opacityTexture=s._GetTexture(o,F,t,n,r,h))}return c&&this.materials.push(c),null!=h?h:[]}static _GetTexture(t,s,i,o,n,r){if(!s)return null;let a=t;if("file:"===t){let t=s.lastIndexOf("\\");-1===t&&(t=s.lastIndexOf("/")),a+=t>-1?s.substring(t+1):s}else a+=s;const l=r?new e.Deferred:null;let h;l&&(l.promise.catch(()=>{}),r.push(l.promise));const _=()=>{const t=null==h?void 0:h.getInternalTexture();if(!t)return;const e=i.getEngine().getLoadedTexturesCache(),s=e.indexOf(t);-1!==s&&e.splice(s,1)},p=i._blockEntityCollection;i._blockEntityCollection=!!o;try{h=new e.Texture(a,i,{invertY:n,onLoad:null==l?void 0:l.resolve,onError:(t,e)=>{h?_():queueMicrotask(_),l&&l.reject(new Error(`${a}: ${(null==e?void 0:e.message)||t||"Failed to load texture"}`))}}),h._parentContainer=o,null==o||o.textures.push(h)}finally{i._blockEntityCollection=p}return l&&h.delayLoadState===e.Constants.DELAYLOADSTATE_NOTLOADED&&h.delayLoad(),h}}s.INVERT_TEXTURE_Y=!0;class i{constructor(t,s,i){this._positions=[],this._normals=[],this._uvs=[],this._colors=[],this._extColors=[],this._meshesFromObj=[],this._indicesForBabylon=[],this._wrappedPositionForBabylon=[],this._wrappedUvsForBabylon=[],this._wrappedColorsForBabylon=[],this._wrappedNormalsForBabylon=[],this._tuplePosNorm=[],this._curPositionInIndices=0,this._hasMeshes=!1,this._unwrappedPositionsForBabylon=[],this._unwrappedColorsForBabylon=[],this._unwrappedNormalsForBabylon=[],this._unwrappedUVForBabylon=[],this._triangles=[],this._materialNameFromObj="",this._objMeshName="",this._increment=1,this._isFirstMaterial=!0,this._grayColor=new e.Color4(.5,.5,.5,1),this._hasLineData=!1,this._materialToUse=t,this._babylonMeshesArray=s,this._loadingOptions=i}_isInArray(t,e){t[e[0]]||(t[e[0]]={normals:[],idx:[]});const s=t[e[0]].normals.indexOf(e[1]);return-1===s?-1:t[e[0]].idx[s]}_isInArrayUV(t,e){t[e[0]]||(t[e[0]]={normals:[],idx:[],uv:[]});const s=t[e[0]].normals.indexOf(e[1]);return 1!=s&&e[2]===t[e[0]].uv[s]?t[e[0]].idx[s]:-1}_setData(t){var e,s;let i;null!==(e=t.indiceUvsFromObj)&&void 0!==e||(t.indiceUvsFromObj=-1),null!==(s=t.indiceNormalFromObj)&&void 0!==s||(t.indiceNormalFromObj=-1),i=this._loadingOptions.optimizeWithUV?this._isInArrayUV(this._tuplePosNorm,[t.indicePositionFromObj,t.indiceNormalFromObj,t.indiceUvsFromObj]):this._isInArray(this._tuplePosNorm,[t.indicePositionFromObj,t.indiceNormalFromObj]),-1===i?(this._indicesForBabylon.push(this._wrappedPositionForBabylon.length),this._wrappedPositionForBabylon.push(t.positionVectorFromOBJ),void 0!==t.textureVectorFromOBJ&&this._wrappedUvsForBabylon.push(t.textureVectorFromOBJ),void 0!==t.normalsVectorFromOBJ&&this._wrappedNormalsForBabylon.push(t.normalsVectorFromOBJ),void 0!==t.positionColorsFromOBJ&&this._wrappedColorsForBabylon.push(t.positionColorsFromOBJ),this._tuplePosNorm[t.indicePositionFromObj].normals.push(t.indiceNormalFromObj),this._tuplePosNorm[t.indicePositionFromObj].idx.push(this._curPositionInIndices++),this._loadingOptions.optimizeWithUV&&this._tuplePosNorm[t.indicePositionFromObj].uv.push(t.indiceUvsFromObj)):this._indicesForBabylon.push(i)}_unwrapData(){try{for(let t=0;t<this._wrappedPositionForBabylon.length;t++)this._unwrappedPositionsForBabylon.push(this._wrappedPositionForBabylon[t].x*this._handednessSign,this._wrappedPositionForBabylon[t].y,this._wrappedPositionForBabylon[t].z),this._wrappedNormalsForBabylon.length&&this._unwrappedNormalsForBabylon.push(this._wrappedNormalsForBabylon[t].x*this._handednessSign,this._wrappedNormalsForBabylon[t].y,this._wrappedNormalsForBabylon[t].z),this._wrappedUvsForBabylon.length&&this._unwrappedUVForBabylon.push(this._wrappedUvsForBabylon[t].x,this._wrappedUvsForBabylon[t].y),this._wrappedColorsForBabylon.length&&this._unwrappedColorsForBabylon.push(this._wrappedColorsForBabylon[t].r,this._wrappedColorsForBabylon[t].g,this._wrappedColorsForBabylon[t].b,this._wrappedColorsForBabylon[t].a);this._wrappedPositionForBabylon.length=0,this._wrappedNormalsForBabylon.length=0,this._wrappedUvsForBabylon.length=0,this._wrappedColorsForBabylon.length=0,this._tuplePosNorm.length=0,this._curPositionInIndices=0}catch(t){throw new Error("Unable to unwrap data while parsing OBJ data.",{cause:t})}}_getTriangles(t,e){for(let s=e;s<t.length-1;s++)this._pushTriangle(t,s)}_getColor(t){var e;return this._loadingOptions.importVertexColors?null!==(e=this._extColors[t])&&void 0!==e?e:this._colors[t]:void 0}_setDataForCurrentFaceWithPattern1(t,e){this._getTriangles(t,e);for(let t=0;t<this._triangles.length;t++){const e=parseInt(this._triangles[t])-1;this._setData({indicePositionFromObj:e,positionVectorFromOBJ:this._positions[e],positionColorsFromOBJ:this._getColor(e)})}this._triangles.length=0}_setDataForCurrentFaceWithPattern2(t,e){this._getTriangles(t,e);for(let t=0;t<this._triangles.length;t++){const e=this._triangles[t].split("/"),s=parseInt(e[0])-1,i=parseInt(e[1])-1;this._setData({indicePositionFromObj:s,indiceUvsFromObj:i,positionVectorFromOBJ:this._positions[s],textureVectorFromOBJ:this._uvs[i],positionColorsFromOBJ:this._getColor(s)})}this._triangles.length=0}_setDataForCurrentFaceWithPattern3(t,e){this._getTriangles(t,e);for(let t=0;t<this._triangles.length;t++){const e=this._triangles[t].split("/"),s=parseInt(e[0])-1,i=parseInt(e[1])-1,o=parseInt(e[2])-1;this._setData({indicePositionFromObj:s,indiceUvsFromObj:i,indiceNormalFromObj:o,positionVectorFromOBJ:this._positions[s],textureVectorFromOBJ:this._uvs[i],normalsVectorFromOBJ:this._normals[o]})}this._triangles.length=0}_setDataForCurrentFaceWithPattern4(t,e){this._getTriangles(t,e);for(let t=0;t<this._triangles.length;t++){const e=this._triangles[t].split("//"),s=parseInt(e[0])-1,i=parseInt(e[1])-1;this._setData({indicePositionFromObj:s,indiceNormalFromObj:i,positionVectorFromOBJ:this._positions[s],normalsVectorFromOBJ:this._normals[i],positionColorsFromOBJ:this._getColor(s)})}this._triangles.length=0}_setDataForCurrentFaceWithPattern5(t,e){this._getTriangles(t,e);for(let t=0;t<this._triangles.length;t++){const e=this._triangles[t].split("/"),s=this._positions.length+parseInt(e[0]),i=this._uvs.length+parseInt(e[1]),o=this._normals.length+parseInt(e[2]);this._setData({indicePositionFromObj:s,indiceUvsFromObj:i,indiceNormalFromObj:o,positionVectorFromOBJ:this._positions[s],textureVectorFromOBJ:this._uvs[i],normalsVectorFromOBJ:this._normals[o],positionColorsFromOBJ:this._getColor(s)})}this._triangles.length=0}_addPreviousObjMesh(){this._meshesFromObj.length>0&&(this._handledMesh=this._meshesFromObj[this._meshesFromObj.length-1],this._unwrapData(),this._loadingOptions.useLegacyBehavior&&this._indicesForBabylon.reverse(),this._handledMesh.indices=this._indicesForBabylon.slice(),this._handledMesh.positions=this._unwrappedPositionsForBabylon.slice(),this._unwrappedNormalsForBabylon.length&&(this._handledMesh.normals=this._unwrappedNormalsForBabylon.slice()),this._unwrappedUVForBabylon.length&&(this._handledMesh.uvs=this._unwrappedUVForBabylon.slice()),this._unwrappedColorsForBabylon.length&&(this._handledMesh.colors=this._unwrappedColorsForBabylon.slice()),this._handledMesh.hasLines=this._hasLineData,this._indicesForBabylon.length=0,this._unwrappedPositionsForBabylon.length=0,this._unwrappedColorsForBabylon.length=0,this._unwrappedNormalsForBabylon.length=0,this._unwrappedUVForBabylon.length=0,this._hasLineData=!1)}_optimizeNormals(t){const s=t.getVerticesData(e.VertexBuffer.PositionKind),i=t.getVerticesData(e.VertexBuffer.NormalKind),o={};if(!s||!i)return;for(let t=0;t<s.length/3;t++){const e=s[3*t+0]+"_"+s[3*t+1]+"_"+s[3*t+2];let i=o[e];i||(i=[],o[e]=i),i.push(t)}const n=new e.Vector3;for(const t in o){const e=o[t];if(e.length<2)continue;const s=e[0];for(let t=1;t<e.length;++t){const o=e[t];i[3*s+0]+=i[3*o+0],i[3*s+1]+=i[3*o+1],i[3*s+2]+=i[3*o+2]}n.copyFromFloats(i[3*s+0],i[3*s+1],i[3*s+2]),n.normalize();for(let t=0;t<e.length;++t){const s=e[t];i[3*s+0]=n.x,i[3*s+1]=n.y,i[3*s+2]=n.z}}t.setVerticesData(e.VertexBuffer.NormalKind,i)}static _IsLineElement(t){return t.startsWith("l")}static _IsObjectElement(t){return t.startsWith("o")}static _IsGroupElement(t){return t.startsWith("g")}static _GetZbrushMRGB(t,s){if(!t.startsWith("mrgb"))return null;if(t=t.replace("mrgb","").trim(),s)return[];const i=t.match(/[a-z0-9]/g);if(!i||i.length%8!=0)return[];const o=[];for(let t=0;t<i.length/8;t++){const s=i[8*t+2]+i[8*t+3],n=i[8*t+4]+i[8*t+5],r=i[8*t+6]+i[8*t+7];o.push(new e.Color4(parseInt(s,16)/255,parseInt(n,16)/255,parseInt(r,16)/255,1))}return o}parse(t,s,o,n,r){var a,l,h,_,p;s=(s=s.replace(/#MRGB/g,"mrgb")).replace(/#.*$/gm,"").trim(),this._loadingOptions.useLegacyBehavior?(this._pushTriangle=(t,e)=>this._triangles.push(t[0],t[e],t[e+1]),this._handednessSign=1):o.useRightHandedSystem?(this._pushTriangle=(t,e)=>this._triangles.push(t[0],t[e+1],t[e]),this._handednessSign=1):(this._pushTriangle=(t,e)=>this._triangles.push(t[0],t[e],t[e+1]),this._handednessSign=-1);const d=s.split("\n"),c=[];let u=[];c.push(u);for(let t=0;t<d.length;t++){const e=d[t].trim().replace(/\s\s/g," ");if(0!==e.length&&"#"!==e.charAt(0))if((i._IsGroupElement(e)||i._IsObjectElement(e))&&(u=[],c.push(u)),i._IsLineElement(e)){const t=e.split(" ");for(let e=1;e<t.length-1;e++)u.push(`l ${t[e]} ${t[e+1]}`)}else u.push(e)}const m=c.flat();for(let t=0;t<m.length;t++){const s=m[t].trim().replace(/\s\s/g," ");let o;if(0!==s.length&&"#"!==s.charAt(0))if(i.VertexPattern.test(s)){if(o=s.match(/[^ ]+/g),this._positions.push(new e.Vector3(parseFloat(o[1]),parseFloat(o[2]),parseFloat(o[3]))),this._loadingOptions.importVertexColors)if(o.length>=7){const t=parseFloat(o[4]),s=parseFloat(o[5]),i=parseFloat(o[6]);this._colors.push(new e.Color4(t>1?t/255:t,s>1?s/255:s,i>1?i/255:i,7===o.length||void 0===o[7]?1:parseFloat(o[7])))}else this._colors.push(this._grayColor)}else if(null!==(o=i.NormalPattern.exec(s)))this._normals.push(new e.Vector3(parseFloat(o[1]),parseFloat(o[2]),parseFloat(o[3])));else if(null!==(o=i.UVPattern.exec(s)))this._uvs.push(new e.Vector2(parseFloat(o[1])*this._loadingOptions.UVScaling.x,parseFloat(o[2])*this._loadingOptions.UVScaling.y));else if(null!==(o=i.FacePattern3.exec(s)))this._setDataForCurrentFaceWithPattern3(o[1].trim().split(" "),1);else if(null!==(o=i.FacePattern4.exec(s)))this._setDataForCurrentFaceWithPattern4(o[1].trim().split(" "),1);else if(null!==(o=i.FacePattern5.exec(s)))this._setDataForCurrentFaceWithPattern5(o[1].trim().split(" "),1);else if(null!==(o=i.FacePattern2.exec(s)))this._setDataForCurrentFaceWithPattern2(o[1].trim().split(" "),1);else if(null!==(o=i.FacePattern1.exec(s)))this._setDataForCurrentFaceWithPattern1(o[1].trim().split(" "),1);else if(null!==(o=i.LinePattern1.exec(s)))this._setDataForCurrentFaceWithPattern1(o[1].trim().split(" "),0),this._hasLineData=!0;else if(null!==(o=i.LinePattern2.exec(s)))this._setDataForCurrentFaceWithPattern2(o[1].trim().split(" "),0),this._hasLineData=!0;else if(o=i._GetZbrushMRGB(s,!this._loadingOptions.importVertexColors))for(const t of o)this._extColors.push(t);else if(null!==(o=i.LinePattern3.exec(s)))this._setDataForCurrentFaceWithPattern3(o[1].trim().split(" "),0),this._hasLineData=!0;else if(i.GroupDescriptor.test(s)||i.ObjectDescriptor.test(s)){const t={name:s.substring(2).trim(),indices:null,positions:null,normals:null,uvs:null,colors:null,materialName:this._materialNameFromObj,isObject:i.ObjectDescriptor.test(s)};this._addPreviousObjMesh(),this._meshesFromObj.push(t),this._hasMeshes=!0,this._isFirstMaterial=!0,this._increment=1}else if(i.UseMtlDescriptor.test(s)){if(this._materialNameFromObj=s.substring(7).trim(),!this._isFirstMaterial||!this._hasMeshes){this._addPreviousObjMesh();const t={name:(this._objMeshName||"mesh")+"_mm"+this._increment.toString(),indices:null,positions:null,normals:null,uvs:null,colors:null,materialName:this._materialNameFromObj,isObject:!1};this._increment++,this._meshesFromObj.push(t),this._hasMeshes=!0}this._hasMeshes&&this._isFirstMaterial&&(this._meshesFromObj[this._meshesFromObj.length-1].materialName=this._materialNameFromObj,this._isFirstMaterial=!1)}else i.MtlLibGroupDescriptor.test(s)?r(s.substring(7).trim()):i.SmoothDescriptor.test(s)||e.Logger.Log("Unhandled expression at line : "+s)}if(this._hasMeshes&&(this._handledMesh=this._meshesFromObj[this._meshesFromObj.length-1],this._loadingOptions.useLegacyBehavior&&this._indicesForBabylon.reverse(),this._unwrapData(),this._handledMesh.indices=this._indicesForBabylon,this._handledMesh.positions=this._unwrappedPositionsForBabylon,this._unwrappedNormalsForBabylon.length&&(this._handledMesh.normals=this._unwrappedNormalsForBabylon),this._unwrappedUVForBabylon.length&&(this._handledMesh.uvs=this._unwrappedUVForBabylon),this._unwrappedColorsForBabylon.length&&(this._handledMesh.colors=this._unwrappedColorsForBabylon),this._handledMesh.hasLines=this._hasLineData),!this._hasMeshes){let t=null;if(this._indicesForBabylon.length)this._loadingOptions.useLegacyBehavior&&this._indicesForBabylon.reverse(),this._unwrapData();else{for(const t of this._positions)this._unwrappedPositionsForBabylon.push(t.x,t.y,t.z);if(this._normals.length)for(const t of this._normals)this._unwrappedNormalsForBabylon.push(t.x,t.y,t.z);if(this._uvs.length)for(const t of this._uvs)this._unwrappedUVForBabylon.push(t.x,t.y);if(this._extColors.length)for(const t of this._extColors)this._unwrappedColorsForBabylon.push(t.r,t.g,t.b,t.a);else if(this._colors.length)for(const t of this._colors)this._unwrappedColorsForBabylon.push(t.r,t.g,t.b,t.a);if(!this._materialNameFromObj){const s=o._blockEntityCollection;o._blockEntityCollection=!!n;try{t=new e.StandardMaterial(e.Geometry.RandomId(),o),t._parentContainer=n,null==n||n.materials.push(t)}finally{o._blockEntityCollection=s}t.pointsCloud=!0,this._materialNameFromObj=t.name,this._normals.length||(t.disableLighting=!0,t.emissiveColor=e.Color3.White())}}this._meshesFromObj.push({name:e.Geometry.RandomId(),indices:this._indicesForBabylon,positions:this._unwrappedPositionsForBabylon,colors:this._unwrappedColorsForBabylon,normals:this._unwrappedNormalsForBabylon,uvs:this._unwrappedUVForBabylon,materialName:this._materialNameFromObj,directMaterial:t,isObject:!0,hasLines:this._hasLineData})}for(let s=0;s<this._meshesFromObj.length;s++){if(t&&this._meshesFromObj[s].name)if(t instanceof Array){if(-1===t.indexOf(this._meshesFromObj[s].name))continue}else if(this._meshesFromObj[s].name!==t)continue;this._handledMesh=this._meshesFromObj[s];const i=o._blockEntityCollection;let r;o._blockEntityCollection=!!n;try{r=new e.Mesh(this._meshesFromObj[s].name,o),r._parentContainer=n,null==n||n.meshes.push(r)}finally{o._blockEntityCollection=i}if(this._handledMesh._babylonMesh=r,!this._handledMesh.isObject)for(let t=s-1;t>=0;--t)if(this._meshesFromObj[t].isObject&&this._meshesFromObj[t]._babylonMesh){r.parent=this._meshesFromObj[t]._babylonMesh;break}if(this._materialToUse.push(this._meshesFromObj[s].materialName),this._handledMesh.hasLines&&(null!==(a=r._internalMetadata)&&void 0!==a||(r._internalMetadata={}),r._internalMetadata._isLine=!0),0===(null===(l=this._handledMesh.positions)||void 0===l?void 0:l.length)){this._babylonMeshesArray.push(r);continue}const d=new e.VertexData;if(d.indices=this._handledMesh.indices,d.positions=this._handledMesh.positions,this._loadingOptions.computeNormals||!(null===(h=this._handledMesh.normals)||void 0===h?void 0:h.length)){const t=new Array;e.VertexData.ComputeNormals(this._handledMesh.positions,this._handledMesh.indices,t),d.normals=t}else d.normals=this._handledMesh.normals;(null===(_=this._handledMesh.uvs)||void 0===_?void 0:_.length)&&(d.uvs=this._handledMesh.uvs),(null===(p=this._handledMesh.colors)||void 0===p?void 0:p.length)&&(d.colors=this._handledMesh.colors);const c=o._blockEntityCollection;o._blockEntityCollection=!!n;try{d.applyToMesh(r),r.geometry._parentContainer=n,null==n||n.geometries.push(r.geometry)}finally{o._blockEntityCollection=c}this._loadingOptions.invertY&&(r.scaling.y*=-1),this._loadingOptions.optimizeNormals&&this._optimizeNormals(r),this._babylonMeshesArray.push(r),this._handledMesh.directMaterial&&(r.material=this._handledMesh.directMaterial)}}}function o(t,e,s,i){return new(s||(s=Promise))(function(o,n){function r(t){try{l(i.next(t))}catch(t){n(t)}}function a(t){try{l(i.throw(t))}catch(t){n(t)}}function l(t){var e;t.done?o(t.value):(e=t.value,e instanceof s?e:new s(function(t){t(e)})).then(r,a)}l((i=i.apply(t,e||[])).next())})}i.ObjectDescriptor=/^o/,i.GroupDescriptor=/^g/,i.MtlLibGroupDescriptor=/^mtllib /,i.UseMtlDescriptor=/^usemtl /,i.SmoothDescriptor=/^s /,i.VertexPattern=/^v(\s+[\d|.|+|\-|e|E]+){3,7}/,i.NormalPattern=/^vn(\s+[\d|.|+|\-|e|E]+)( +[\d|.|+|\-|e|E]+)( +[\d|.|+|\-|e|E]+)/,i.UVPattern=/^vt(\s+[\d|.|+|\-|e|E]+)( +[\d|.|+|\-|e|E]+)/,i.FacePattern1=/^f\s+(([\d]{1,}[\s]?){3,})+/,i.FacePattern2=/^f\s+((([\d]{1,}\/[\d]{1,}[\s]?){3,})+)/,i.FacePattern3=/^f\s+((([\d]{1,}\/[\d]{1,}\/[\d]{1,}[\s]?){3,})+)/,i.FacePattern4=/^f\s+((([\d]{1,}\/\/[\d]{1,}[\s]?){3,})+)/,i.FacePattern5=/^f\s+(((-[\d]{1,}\/-[\d]{1,}\/-[\d]{1,}[\s]?){3,})+)/,i.LinePattern1=/^l\s+(([\d]{1,}[\s]?){2,})+/,i.LinePattern2=/^l\s+((([\d]{1,}\/[\d]{1,}[\s]?){2,})+)/,i.LinePattern3=/^l\s+((([\d]{1,}\/[\d]{1,}\/[\d]{1,}[\s]?){2,})+)/,Object.create,Object.create,"function"==typeof SuppressedError&&SuppressedError;const n={".obj":{isBinary:!0}};class r{static get INVERT_TEXTURE_Y(){return s.INVERT_TEXTURE_Y}static set INVERT_TEXTURE_Y(t){s.INVERT_TEXTURE_Y=t}constructor(t){this.name="obj",this.extensions=n,this._assetContainer=null,this._loadingOptions=Object.assign(Object.assign({},r._DefaultLoadingOptions),null!=t?t:{})}static get _DefaultLoadingOptions(){return{encoding:r.ENCODING,computeNormals:r.COMPUTE_NORMALS,optimizeNormals:r.OPTIMIZE_NORMALS,importVertexColors:r.IMPORT_VERTEX_COLORS,invertY:r.INVERT_Y,invertTextureY:r.INVERT_TEXTURE_Y,waitForTextures:r.WAIT_FOR_TEXTURES,UVScaling:r.UV_SCALING,materialLoadingFailsSilently:r.MATERIAL_LOADING_FAILS_SILENTLY,optimizeWithUV:r.OPTIMIZE_WITH_UV,skipMaterials:r.SKIP_MATERIALS,useLegacyBehavior:r.USE_LEGACY_BEHAVIOR}}_loadMTL(t,s,i,o){const n=s+t;e.Tools.LoadFile(n,i,void 0,void 0,!0,(t,e)=>{o(n,e)})}_decode(t){var e;if("string"==typeof t)return t;const s=new Uint8Array(t),i=null!==(e=this._loadingOptions.encoding)&&void 0!==e?e:"auto";if("auto"!==i)return new TextDecoder(i).decode(s);if(255===s[0]&&254===s[1])return new TextDecoder("utf-16le").decode(s);if(254===s[0]&&255===s[1])return new TextDecoder("utf-16be").decode(s);try{return new TextDecoder("utf-8",{fatal:!0}).decode(s)}catch(t){return new TextDecoder("gb18030").decode(s)}}createPlugin(t){return new r(t.obj)}canDirectLoad(){return!1}importMeshAsync(t,e,s,i){return this._parseSolidAsync(t,e,s,i).then(t=>{const e=[];for(let s=0;s<t.length;++s){const i=t[s].geometry;i&&e.push(i)}return{meshes:t,particleSystems:[],skeletons:[],animationGroups:[],transformNodes:[],geometries:e,lights:[],spriteManagers:[]}})}loadAsync(t,e,s){return this.importMeshAsync(null,t,e,s).then(()=>{})}loadAssetContainerAsync(t,s,i){return o(this,void 0,void 0,function*(){const o=new e.AssetContainer(t);this._assetContainer=o;try{return yield this.importMeshAsync(null,t,s,i),o}catch(t){throw o.dispose(),t}finally{this._assetContainer=null}})}_parseSolidAsync(t,n,r,a){let l="";const h=new s,_=[],p=[];r=(r=this._decode(r)).replace(/#.*$/gm,"").trim(),new i(_,p,this._loadingOptions).parse(t,r,n,this._assetContainer,t=>{l=t});const d=[];return""===l||this._loadingOptions.skipMaterials||d.push(new Promise((t,s)=>{this._loadMTL(l,a,i=>o(this,void 0,void 0,function*(){try{const e=h.parseMTL(n,this._decode(i),a,this._assetContainer,this._loadingOptions.invertTextureY,new Set(_),this._loadingOptions.waitForTextures);for(let t=0;t<h.materials.length;++t){const e=h.materials[t];for(let t=_.indexOf(e.name);-1!==t;t=_.indexOf(e.name,t+1)){const s=p[t];s.material=e,s.getTotalIndices()||(e.pointsCloud=!0)}}if(this._loadingOptions.waitForTextures){const t=(yield Promise.allSettled(e)).find(t=>"rejected"===t.status);if(t)throw t.reason}t()}catch(i){e.Tools.Warn(`Error processing MTL file: '${l}'`),this._loadingOptions.materialLoadingFailsSilently?t():s(i)}}),(i,o)=>{e.Tools.Warn(`Error downloading MTL file: '${l}'`),this._loadingOptions.materialLoadingFailsSilently?t():s(o)})})),Promise.all(d).then(()=>{const t=t=>{var e,s;return Boolean(null!==(s=null===(e=t._internalMetadata)||void 0===e?void 0:e._isLine)&&void 0!==s&&s)};return p.forEach(s=>{var i,o;if(t(s)){let r=s.material;if(!r){const t=n._blockEntityCollection;n._blockEntityCollection=!!this._assetContainer;try{r=new e.StandardMaterial(s.name+"_line",n),r._parentContainer=this._assetContainer,null===(i=this._assetContainer)||void 0===i||i.materials.push(r)}finally{n._blockEntityCollection=t}}if(r.getBindedMeshes().filter(e=>!t(e)).length>0){const t=r,{ambientTexture:e,diffuseTexture:s,specularTexture:i,bumpTexture:a,opacityTexture:l}=t,h=n._blockEntityCollection;let _;try{t.ambientTexture=null,t.diffuseTexture=null,t.specularTexture=null,t.bumpTexture=null,t.opacityTexture=null,n._blockEntityCollection=!!this._assetContainer,_=t.clone(t.name+"_line"),_._parentContainer=this._assetContainer,null===(o=this._assetContainer)||void 0===o||o.materials.push(_)}finally{n._blockEntityCollection=h,t.ambientTexture=e,t.diffuseTexture=s,t.specularTexture=i,t.bumpTexture=a,t.opacityTexture=l}_.ambientTexture=e,_.diffuseTexture=s,_.specularTexture=i,_.bumpTexture=a,_.opacityTexture=l,r=_}r.wireframe=!0,s.material=r}}),p.forEach(t=>{t._internalMetadata&&(t._internalMetadata._isLine=void 0)}),p})}}r.ENCODING="auto",r.OPTIMIZE_WITH_UV=!0,r.INVERT_Y=!1,r.WAIT_FOR_TEXTURES=!1,r.IMPORT_VERTEX_COLORS=!1,r.COMPUTE_NORMALS=!1,r.OPTIMIZE_NORMALS=!1,r.UV_SCALING=new e.Vector2(1,1),r.SKIP_MATERIALS=!1,r.MATERIAL_LOADING_FAILS_SILENTLY=!0,r.USE_LEGACY_BEHAVIOR=!1;let a=!1;function l(){a||(a=!0,e.RegisterSceneLoaderPlugin(new r))}l();var h={__proto__:null,MTLFileLoader:s,OBJFileLoader:r,RegisterOBJFileLoader:l,SolidParser:i};const _="undefined"!=typeof global?global:"undefined"!=typeof window?window:void 0;if(void 0!==_)for(const t in h)_.BABYLON[t]||(_.BABYLON[t]=h[t]);t.MTLFileLoader=s,t.OBJFileLoader=r,t.RegisterOBJFileLoader=l,t.SolidParser=i});
|
|
2
2
|
//# sourceMappingURL=babylon.objFileLoader.min.js.map
|