babylon.objSerializer.min.js 2.2 KB

1
  1. var BABYLON;!(function(e){var o=(function(){function o(){}return o.OBJ=function(o,t,r,i){var s=[],n=1;t&&(r||(r="mat"),s.push("mtllib "+r+".mtl"));for(var a=0;a<o.length;a++){s.push("g object"+a),s.push("o object_"+a);var u=null;if(i){var p=e.Matrix.Translation(o[a].position.x,o[a].position.y,o[a].position.z);u=e.Matrix.Translation(-o[a].position.x,-o[a].position.y,-o[a].position.z),o[a].bakeTransformIntoVertices(p)}if(t){var l=o[a].material;l&&s.push("usemtl "+l.id)}var m=o[a].geometry;if(m){var h=m.getVerticesData("position"),f=m.getVerticesData("normal"),x=m.getVerticesData("uv"),d=m.getIndices(),c=0;if(h&&d){for(var v=0;v<h.length;v+=3)s.push("v "+h[v]+" "+h[v+1]+" "+h[v+2]),c++;if(null!=f)for(v=0;v<f.length;v+=3)s.push("vn "+f[v]+" "+f[v+1]+" "+f[v+2]);if(null!=x)for(v=0;v<x.length;v+=2)s.push("vt "+x[v]+" "+x[v+1]);for(v=0;v<d.length;v+=3){var T=[String(d[v+2]+n),String(d[v+1]+n),String(d[v]+n)],g=["","",""],b=T,F=null!=x?T:g,C=null!=f?T:g;s.push("f "+b[0]+"/"+F[0]+"/"+C[0]+" "+b[1]+"/"+F[1]+"/"+C[1]+" "+b[2]+"/"+F[2]+"/"+C[2])}i&&u&&o[a].bakeTransformIntoVertices(u),n+=c}else e.Tools.Warn("There are no position vertices or indices on the mesh!")}else e.Tools.Warn("No geometry is present on the mesh")}return s.join("\n")},o.MTL=function(e){var o=[],t=e.material;o.push("newmtl mat1"),o.push(" Ns "+t.specularPower.toFixed(4)),o.push(" Ni 1.5000"),o.push(" d "+t.alpha.toFixed(4)),o.push(" Tr 0.0000"),o.push(" Tf 1.0000 1.0000 1.0000"),o.push(" illum 2"),o.push(" Ka "+t.ambientColor.r.toFixed(4)+" "+t.ambientColor.g.toFixed(4)+" "+t.ambientColor.b.toFixed(4)),o.push(" Kd "+t.diffuseColor.r.toFixed(4)+" "+t.diffuseColor.g.toFixed(4)+" "+t.diffuseColor.b.toFixed(4)),o.push(" Ks "+t.specularColor.r.toFixed(4)+" "+t.specularColor.g.toFixed(4)+" "+t.specularColor.b.toFixed(4)),o.push(" Ke "+t.emissiveColor.r.toFixed(4)+" "+t.emissiveColor.g.toFixed(4)+" "+t.emissiveColor.b.toFixed(4));return t.ambientTexture&&o.push(" map_Ka "+t.ambientTexture.name),t.diffuseTexture&&o.push(" map_Kd "+t.diffuseTexture.name),t.specularTexture&&o.push(" map_Ks "+t.specularTexture.name),t.bumpTexture&&o.push(" map_bump -imfchan z "+t.bumpTexture.name),t.opacityTexture&&o.push(" map_d "+t.opacityTexture.name),o.join("\n")},o})();e.OBJExport=o})(BABYLON||(BABYLON={}));