1
0
Fork 0
This repository has been archived on 2024-01-06. You can view files and clone it, but cannot push or open issues or pull requests.
NUIT_INFO_2_DECEMBRE_2016/scripts/babylon_obj.js
2016-12-01 23:32:19 +01:00

1 line
5.3 KiB
JavaScript

!function(e){var t=function(){function t(){this.materials=[],this.parseMTL=function(t,s,r){for(var a,n,i=s.split("\n"),o=/\s+/,l=0;l<i.length;l++){var u=i[l];if(u=u.trim(),0!==u.length&&"#"!==u.charAt(0)){var p=u.indexOf(" "),h=p>=0?u.substring(0,p):u;h=h.toLowerCase();var f=p>=0?u.substring(p+1):"";f=f.trim(),"newmtl"===h?(n&&this.materials.push(n),n=new e.StandardMaterial(f,t)):"kd"===h?(a=f.split(o,3),n.diffuseColor=e.Color3.FromArray(a)):"ka"===h?(a=f.split(o,3),n.ambientColor=e.Color3.FromArray(a)):"ks"===h?(a=f.split(o,3),n.specularColor=e.Color3.FromArray(a)):"ns"===h?n.specularPower=f:"d"===h?n.alpha=f:"map_ka"===h?n.ambientTexture=new e.Texture(r+f,t):"map_kd"===h?n.diffuseTexture=new e.Texture(r+f,t):"map_ks"===h?n.specularTexture=new e.Texture(r+f,t):"map_ns"===h||("map_bump"===h?n.bumpTexture=new e.Texture(r+f,t):"map_d"===h&&(n.opacityTexture=new e.Texture(r+f,t)))}}this.materials.push(n)}}return t}();e.MTLFileLoader=t;var s=function(){function s(){this.extensions=".obj",this.obj=/^o/,this.group=/^g/,this.mtllib=/^mtllib /,this.usemtl=/^usemtl /,this.smooth=/^s /,this.vertexPattern=/v( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)/,this.normalPattern=/vn( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)/,this.uvPattern=/vt( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)/,this.facePattern1=/f\s(([\d]{1,}[\s]?){3,})+/,this.facePattern2=/f\s((([\d]{1,}\/[\d]{1,}[\s]?){3,})+)/,this.facePattern3=/f\s((([\d]{1,}\/[\d]{1,}\/[\d]{1,}[\s]?){3,})+)/,this.facePattern4=/f\s((([\d]{1,}\/\/[\d]{1,}[\s]?){3,})+)/}return s.prototype._loadMTL=function(t,s,r){var a=new XMLHttpRequest,n=e.Tools.BaseUrl+s+t;a.open("GET",n,!1),a.onreadystatechange=function(){4===a.readyState&&(200===a.status||e.Tools.ValidateXHRData(a,1)?r(a.responseText):console.warn("Error status: "+a.status+" - Unable to load "+n))},a.send(null)},s.prototype.importMesh=function(e,t,s,r,a){var n=this._parseSolid(e,t,s,r);return a&&n.forEach(function(e){a.push(e)}),!0},s.prototype.load=function(e,t,s){return this.importMesh(null,e,t,s,null,null,null)},s.prototype._parseSolid=function(s,r,a,n){for(var i,o=[],l=[],u=[],p=[],h=[],f=[],c=[],m=[],v=[],d=!1,x=[],g=[],w=[],T=[],y="",b="",P=new t,F=function(e,t){for(var s=-1,r=0;r<e.length;r++){var a=e[r];a.x===t.x&&a.y===t.y&&(s=r)}return s},L=function(t,s,r,a,n){var i=new e.Vector2(t,s),o=F(v,i);-1==o?(h.push(f.length),f.push(r),c.push(a),m.push(n),v.push(i)):h.push(o)},E=function(){for(var e=0;e<f.length;e++)x.push(f[e].x,f[e].y,f[e].z),g.push(m[e].x,m[e].y,m[e].z),w.push(c[e].x,c[e].y)},M=function(e,t){t+1<e.length&&(T.push(e[0],e[t],e[t+1]),t+=1,M(e,t))},V=function(t,s){M(t,s);for(var r=0;r<T.length;r++){var a=parseInt(T[r])-1,n=0,i=o[a],l=new e.Vector2(0,0),u=new e.Vector3(0,1,0);L(a,n,i,l,u)}T=[]},_=function(t,s){M(t,s);for(var r=0;r<T.length;r++){var a=T[r].split("/"),n=parseInt(a[0])-1,i=parseInt(a[1])-1,l=0,p=o[n],h=u[i],f=new e.Vector3(0,1,0);L(n,l,p,h,f)}T=[]},I=function(e,t){M(e,t);for(var s=0;s<T.length;s++){var r=T[s].split("/"),a=parseInt(r[0])-1,n=parseInt(r[1])-1,i=parseInt(r[2])-1,p=o[a],h=u[n],f=l[i];L(a,i,p,h,f)}T=[]},A=function(t,s){M(t,s);for(var r=0;r<T.length;r++){var a=T[r].split("//"),n=parseInt(a[0])-1,i=parseInt(a[1])-1,u=o[n],p=new e.Vector2(0,0),h=l[i];L(n,i,u,p,h)}T=[]},C=a.split("\n"),k=0;k<C.length;k++){var B=C[k];B=B.trim();var N;if(0!==B.length&&"#"!==B.charAt(0))if(null!==(N=this.vertexPattern.exec(B))){var O=new e.Vector3(parseFloat(N[1]),parseFloat(N[2]),parseFloat(N[3]));o.push(O)}else if(null!==(N=this.normalPattern.exec(B))){var S=new e.Vector3(parseFloat(N[1]),parseFloat(N[2]),parseFloat(N[3]));l.push(S)}else if(null!==(N=this.uvPattern.exec(B))){var R=new e.Vector2(parseFloat(N[1]),parseFloat(N[2]));u.push(R)}else if(null!==(N=this.facePattern3.exec(B))){N=N[1].trim();var j=N.split(" ");I(j,1)}else if(null!==(N=this.facePattern4.exec(B))){N=N[1].trim();var j=N.split(" ");A(j,1)}else if(null!==(N=this.facePattern2.exec(B))){N=N[1].trim();var j=N.split(" ");_(j,1)}else if(null!==(N=this.facePattern1.exec(B))){N=N[1].trim();var j=N.split(" ");V(j,1)}else if(this.group.test(B)||this.obj.test(B)){var U={name:B.substring(2).trim(),indices:void 0,positions:void 0,normals:void 0,uvs:void 0,materialName:""};p.length>0&&(i=p[p.length-1],E(),h.reverse(),i.indices=h.slice(),i.positions=x.slice(),i.normals=g.slice(),i.uvs=w.slice(),h=[],x=[],g=[],w=[]),p.push(U),d=!0}else if(this.usemtl.test(B)){if(y=B.substring(7).trim(),d){var z=p.length;p[z-1].materialName=y}}else this.mtllib.test(B)?b=B.substring(7).trim():this.smooth.test(B)||console.log("Unhandled expression at line : "+B)}if(d&&(i=p[p.length-1],h.reverse(),E(),i.indices=h,i.positions=x,i.normals=g,i.uvs=w),!d){var D=e.Geometry.RandomId();h.reverse(),E(),p.push({name:D,indices:h,positions:x,normals:g,uvs:w,materialName:y})}for(var G=new e.VertexData,H=[],X=[],Y=0;Y<p.length;Y++){if(s&&p[Y].name)if(s instanceof Array){if(-1==s.indexOf(p[Y].name))continue}else if(p[Y].name!==s)continue;i=p[Y];var q=new e.Mesh(p[Y].name,r);X.push(p[Y].materialName),G.positions=i.positions,G.normals=i.normals,G.uvs=i.uvs,G.indices=i.indices,G.applyToMesh(q),H.push(q)}return""!==b&&this._loadMTL(b,n,function(e){P.parseMTL(r,e,n);for(var t=0;t<P.materials.length;t++){var s=X.indexOf(P.materials[t].name);-1==s?P.materials[t].dispose():H[s].material=P.materials[t]}}),H},s}();e.OBJFileLoader=s,e.SceneLoader.RegisterPlugin(new s)}(BABYLON||(BABYLON={}));