123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- var babylonDependency; try { babylonDependency = BABYLON || (typeof require !== 'undefined' && require("../babylon.max")); } catch (e) { babylonDependency = BABYLON || (typeof require !== 'undefined' && require("babylonjs")); }
- var BABYLON = babylonDependency;
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- var __extends = (this && this.__extends) || (function () {
- var extendStatics = Object.setPrototypeOf ||
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
- function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
- return function (d, b) {
- extendStatics(d, b);
- function __() { this.constructor = d; }
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
- };
- })();
-
- var BABYLON;
- (function (BABYLON) {
- var OBJExport = /** @class */ (function () {
- function OBJExport() {
- }
- //Exports the geometrys of a Mesh array in .OBJ file format (text)
- OBJExport.OBJ = function (mesh, materials, matlibname, globalposition) {
- var output = [];
- var v = 1;
- if (materials) {
- if (!matlibname) {
- matlibname = 'mat';
- }
- output.push("mtllib " + matlibname + ".mtl");
- }
- for (var j = 0; j < mesh.length; j++) {
- output.push("g object" + j);
- output.push("o object_" + j);
- //Uses the position of the item in the scene, to the file (this back to normal in the end)
- var lastMatrix = null;
- if (globalposition) {
- var newMatrix = BABYLON.Matrix.Translation(mesh[j].position.x, mesh[j].position.y, mesh[j].position.z);
- lastMatrix = BABYLON.Matrix.Translation(-(mesh[j].position.x), -(mesh[j].position.y), -(mesh[j].position.z));
- mesh[j].bakeTransformIntoVertices(newMatrix);
- }
- //TODO: submeshes (groups)
- //TODO: smoothing groups (s 1, s off);
- if (materials) {
- var mat = mesh[j].material;
- if (mat) {
- output.push("usemtl " + mat.id);
- }
- }
- var g = mesh[j].geometry;
- if (!g) {
- continue;
- }
- var trunkVerts = g.getVerticesData('position');
- var trunkNormals = g.getVerticesData('normal');
- var trunkUV = g.getVerticesData('uv');
- var trunkFaces = g.getIndices();
- var curV = 0;
- if (!trunkVerts || !trunkNormals || !trunkUV || !trunkFaces) {
- continue;
- }
- for (var i = 0; i < trunkVerts.length; i += 3) {
- output.push("v " + trunkVerts[i] + " " + trunkVerts[i + 1] + " " + trunkVerts[i + 2]);
- curV++;
- }
- for (i = 0; i < trunkNormals.length; i += 3) {
- output.push("vn " + trunkNormals[i] + " " + trunkNormals[i + 1] + " " + trunkNormals[i + 2]);
- }
- for (i = 0; i < trunkUV.length; i += 2) {
- output.push("vt " + trunkUV[i] + " " + trunkUV[i + 1]);
- }
- for (i = 0; i < trunkFaces.length; i += 3) {
- output.push("f " + (trunkFaces[i + 2] + v) + "/" + (trunkFaces[i + 2] + v) + "/" + (trunkFaces[i + 2] + v) +
- " " + (trunkFaces[i + 1] + v) + "/" + (trunkFaces[i + 1] + v) + "/" + (trunkFaces[i + 1] + v) +
- " " + (trunkFaces[i] + v) + "/" + (trunkFaces[i] + v) + "/" + (trunkFaces[i] + v));
- }
- //back de previous matrix, to not change the original mesh in the scene
- if (globalposition && lastMatrix) {
- mesh[j].bakeTransformIntoVertices(lastMatrix);
- }
- v += curV;
- }
- var text = output.join("\n");
- return (text);
- };
- //Exports the material(s) of a mesh in .MTL file format (text)
- //TODO: Export the materials of mesh array
- OBJExport.MTL = function (mesh) {
- var output = [];
- var m = mesh.material;
- output.push("newmtl mat1");
- output.push(" Ns " + m.specularPower.toFixed(4));
- output.push(" Ni 1.5000");
- output.push(" d " + m.alpha.toFixed(4));
- output.push(" Tr 0.0000");
- output.push(" Tf 1.0000 1.0000 1.0000");
- output.push(" illum 2");
- output.push(" Ka " + m.ambientColor.r.toFixed(4) + " " + m.ambientColor.g.toFixed(4) + " " + m.ambientColor.b.toFixed(4));
- output.push(" Kd " + m.diffuseColor.r.toFixed(4) + " " + m.diffuseColor.g.toFixed(4) + " " + m.diffuseColor.b.toFixed(4));
- output.push(" Ks " + m.specularColor.r.toFixed(4) + " " + m.specularColor.g.toFixed(4) + " " + m.specularColor.b.toFixed(4));
- output.push(" Ke " + m.emissiveColor.r.toFixed(4) + " " + m.emissiveColor.g.toFixed(4) + " " + m.emissiveColor.b.toFixed(4));
- //TODO: uv scale, offset, wrap
- //TODO: UV mirrored in Blender? second UV channel? lightMap? reflection textures?
- var uvscale = "";
- if (m.ambientTexture) {
- output.push(" map_Ka " + uvscale + m.ambientTexture.name);
- }
- if (m.diffuseTexture) {
- output.push(" map_Kd " + uvscale + m.diffuseTexture.name);
- //TODO: alpha testing, opacity in diffuse texture alpha channel (diffuseTexture.hasAlpha -> map_d)
- }
- if (m.specularTexture) {
- output.push(" map_Ks " + uvscale + m.specularTexture.name);
- /* TODO: glossiness = specular highlight component is in alpha channel of specularTexture. (???)
- if (m.useGlossinessFromSpecularMapAlpha) {
- output.push(" map_Ns "+uvscale + m.specularTexture.name);
- }
- */
- }
- /* TODO: emissive texture not in .MAT format (???)
- if (m.emissiveTexture) {
- output.push(" map_d "+uvscale+m.emissiveTexture.name);
- }
- */
- if (m.bumpTexture) {
- output.push(" map_bump -imfchan z " + uvscale + m.bumpTexture.name);
- }
- if (m.opacityTexture) {
- output.push(" map_d " + uvscale + m.opacityTexture.name);
- }
- var text = output.join("\n");
- return (text);
- };
- return OBJExport;
- }());
- BABYLON.OBJExport = OBJExport;
- })(BABYLON || (BABYLON = {}));
- //# sourceMappingURL=babylon.objSerializer.js.map
- (function universalModuleDefinition(root, factory) {
- if (root && root["BABYLON"]) {
- return;
- }
- if(typeof exports === 'object' && typeof module === 'object')
- module.exports = factory();
- else if(typeof define === 'function' && define.amd)
- define([], factory);
- else if(typeof exports === 'object')
- exports["BJSSerializers"] = factory();
- else {
- root["BABYLON"] = factory();
- }
- })(this, function() {
- return BABYLON;
- });
|