import { Nullable } from "babylonjs/types" ; import { Matrix } from "babylonjs/Maths/math.vector" ; import { Tools } from "babylonjs/Misc/tools" ; import { StandardMaterial } from "babylonjs/Materials/standardMaterial" ; import { Geometry } from "babylonjs/Meshes/geometry" ; import { Mesh } from "babylonjs/Meshes/mesh" ; /**
 * Class for generating OBJ data from a Babylon scene.
 */ export class OBJExport { param mesh param materials param matlibname param globalposition returns public static OBJ ( mesh , materials ? , matlibname ? , globalposition ? ) { const output = [ ] ; let v = 1 ; let textureV = 1 ; if ( materials ) { if ( ! matlibname ) { matlibname = 'mat' ; } output . push ( "mtllib " + matlibname + ".mtl" ) ; } for ( let j = 0 ; j < mesh . length ; j ++ ) { output . push ( "g object" + j ) ; output . push ( "o object_" + j ) ; let inverseTransform = null ; if ( globalposition ) { const transform = mesh [ j ] . computeWorldMatrix ( true ) ; inverseTransform = new Matrix ( ) ; transform . invertToRef ( inverseTransform ) ; mesh [ j ] . bakeTransformIntoVertices ( transform ) ; } if ( materials ) { let mat = mesh [ j ] . material ; if ( mat ) { output . push ( "usemtl " + mat . id ) ; } } const g = mesh [ j ] . geometry ; if ( ! g ) { Tools . Warn ( "No geometry is present on the mesh" ) ; continue ; } const trunkVerts = g . getVerticesData ( 'position' ) ; const trunkNormals = g . getVerticesData ( 'normal' ) ; const trunkUV = g . getVerticesData ( 'uv' ) ; const trunkFaces = g . getIndices ( ) ; let currentV = 0 ; let currentTextureV = 0 ; if ( ! trunkVerts || ! trunkFaces ) { Tools . Warn ( "There are no position vertices or indices on the mesh!" ) ; continue ; } for ( var i = 0 ; i < trunkVerts . length ; i += 3 ) { if ( mesh [ 0 ] . getScene ( ) . useRightHandedSystem ) { output . push ( "v " + trunkVerts [ i ] + " " + trunkVerts [ i + 1 ] + " " + trunkVerts [ i + 2 ] ) ; } else { output . push ( "v " + trunkVerts [ i ] + " " + trunkVerts [ i + 1 ] + " " + - trunkVerts [ i + 2 ] ) ; } currentV ++ ; } if ( trunkNormals != null ) { for ( i = 0 ; i < trunkNormals . length ; i += 3 ) { output . push ( "vn " + trunkNormals [ i ] + " " + trunkNormals [ i + 1 ] + " " + trunkNormals [ i + 2 ] ) ; } } if ( trunkUV != null ) { for ( i = 0 ; i < trunkUV . length ; i += 2 ) { output . push ( "vt " + trunkUV [ i ] + " " + trunkUV [ i + 1 ] ) ; currentTextureV ++ ; } } for ( i = 0 ; i < trunkFaces . length ; i += 3 ) { const indices = [ String ( trunkFaces [ i + 2 ] + v ) , String ( trunkFaces [ i + 1 ] + v ) , String ( trunkFaces [ i ] + v ) ] ; const textureIndices = [ String ( trunkFaces [ i + 2 ] + textureV ) , String ( trunkFaces [ i + 1 ] + textureV ) , String ( trunkFaces [ i ] + textureV ) ] ; const blanks = [ "" , "" , "" ] ; const facePositions = indices ; const faceUVs = trunkUV != null ? textureIndices : blanks ; const faceNormals = trunkNormals != null ? indices : blanks ; output . push ( "f " + facePositions [ 0 ] + "/" + faceUVs [ 0 ] + "/" + faceNormals [ 0 ] + " " + facePositions [ 1 ] + "/" + faceUVs [ 1 ] + "/" + faceNormals [ 1 ] + " " + facePositions [ 2 ] + "/" + faceUVs [ 2 ] + "/" + faceNormals [ 2 ] ) ; } if ( globalposition && inverseTransform ) { mesh [ j ] . bakeTransformIntoVertices ( inverseTransform ) ; } v += currentV ; textureV += currentTextureV ; } const text = output . join ( "\n" ) ; return ( text ) ; } param mesh returns public static MTL ( mesh ) { var output = [ ] ; var m = < StandardMaterial > 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 ) ) ; var uvscale = "" ; if ( m . ambientTexture ) { output . push ( "  map_Ka " + uvscale + m . ambientTexture . name ) ; } if ( m . diffuseTexture ) { output . push ( "  map_Kd " + uvscale + m . diffuseTexture . name ) ; } if ( m . specularTexture ) { output . push ( "  map_Ks " + uvscale + m . specularTexture . 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 ) ; } }