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

2 lines
5.3 KiB
JavaScript
Raw Permalink Normal View History

2016-12-01 23:32:19 +01:00
!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,