export .obj from alternativa3d

As usual I’ve been playing with Alternativa3D and in my most recent project I wanted to be able to export an .obj 3d file of the 3d environment I had built. There is a ExporterA3D class which I’ve made use of a few times but in this case the export is for users so I wanted something more recognisable so I opted to create a quick object exporter. The environment I created was using the alternativa primitives and the export I wanted was purely the mesh. So this is what I created.

package exporters 
{
    	import flash.utils.ByteArray;
    	import flash.display.Sprite;
    	import flash.net.FileReference; 
    	import alternativa.engine3d.objects.Mesh;
    	import alternativa.engine3d.resources.Geometry;
    	import alternativa.engine3d.core.Object3D;
    	import alternativa.engine3d.core.VertexAttributes;
    	import alternativa.engine3d.alternativa3d;
    	use namespace alternativa3d;
    	
    	/**
    	 * ...
    	 * @author David E Jones
    	 */
    	public class ExportOBJ extends Sprite 
    	{
    		public var data:ByteArray;	
    		private var objstring:String = "";
    		
    		public function ExportOBJ(root:Object3D) 
    		{
    			data = new ByteArray();
    			
    			exportMeshes(root);
    
    			data.writeUTFBytes(objstring);
    			
    			var fileRef:FileReference = new FileReference();  
                fileRef.save(data,"export.obj");
    		}
    		
    		private function exportMeshes(source:Object3D):void 
    		{
    			if (source is Mesh) {
    				exportMesh(source as Mesh);
    			} else {
    				trace("Unsupported object type", source);
    			}
    
    			for (var child:Object3D = source.childrenList; child != null; child = child.next) {
    				exportMeshes(child);
    			}
    		}
    		
    		private function exportMesh(source:Mesh):void 
    		{
    			var g:Geometry = source.geometry;
    			var vertices:Vector. = g.getAttributeValues(VertexAttributes.POSITION);
    			var uvts:Vector. = g.getAttributeValues(VertexAttributes.TEXCOORDS[0]);
    			var normals:Vector. = g.getAttributeValues(VertexAttributes.NORMAL);
    			var tangent:Vector. = g.getAttributeValues(VertexAttributes.TANGENT4);
    			var ind:Vector. = g.indices;
    			
    			var faces:Array = new Array();
    			
    			//write verts
    			for (var i:int = 0; i < vertices.length; i = i + 3)
    			{
    				faces.push([i+1,i+2,i+3]);
    				objstring = objstring + "v " + vertices[i] + " " + vertices[i + 1] + " " + vertices[i + 2] + "\n";
    			}
    			
    			objstring = objstring + "\n";
    			
    			//write uvs
    			for (var j:int = 0; j < uvts.length; j = j + 2)
    			{
    				objstring = objstring + "vt " + uvts[j] + " " + uvts[j+1] + "\n";
    			}
    			
    			objstring = objstring + "\n";
    			
    			//write normals
    			for (var k:int = 0; k < normals.length; k = k + 3)
    			{
    				objstring = objstring + "vn " + normals[k] + " " + normals[k + 1] + " " + normals[k + 2] + "\n";
    			}
    			
    			objstring = objstring + "\n";
    			
    			//write faces
    			for (var l:int = 0; l < faces.length; l++)
    			{
    				objstring = objstring + "f " + faces[l][0] + " " + faces[l][1] + " " + faces[l][2] + "\n";
    			}
    			
    			objstring = objstring + "\n";
    		}
    		
    	}
    
}

and the usage as follows

import exports.*;
    
var exp:ExportOBJ = new ExportOBJ(scene);
    

Comments

  • avatar-uv-scrolling-material-for-alternativa3d
    # UV Scrolling Material for Alternativa3D |
    […] Export .obj from Alternativa3D […]

Comments are currently closed