Skip to content

Commit

Permalink
Add obj format export option
Browse files Browse the repository at this point in the history
  • Loading branch information
karimnaaji committed Nov 2, 2017
1 parent 95b0329 commit f430db1
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 11 deletions.
4 changes: 4 additions & 0 deletions Assets/Editor/EditorConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public class EditorConfig

public static Color DownloadButtonDisabledColor = new Color(0.45f, 0.45f, 0.45f);

public static Color ExportButtonEnabledColor = new Color(0.30f, 0.35f, 0.85f);

public static Color ExportButtonDisabledColor = new Color(0.45f, 0.45f, 0.45f);

public static Color RemoveButtonColor = new Color(0.5f, 0.5f, 0.5f);

public static GUILayoutOption SmallButtonWidth = GUILayout.Width(50.0f);
Expand Down
15 changes: 15 additions & 0 deletions Assets/Editor/MapzenMapEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using System.Collections.Generic;
using Mapzen;
using Mapzen.Unity;
using UnityEditor;

[CustomEditor(typeof(MapzenMap))]
Expand All @@ -11,6 +12,7 @@ public class MapzenMapEditor : Editor
private bool showTileDataFoldout = false;
private bool showRegionScaleRatioFoldout = false;


void OnEnable()
{
this.mapzenMap = (MapzenMap)target;
Expand Down Expand Up @@ -48,6 +50,19 @@ public override void OnInspectorGUI()

EditorConfig.ResetColor();

valid = mapzenMap.MeshFilters.Count > 0;

EditorConfig.SetColor(valid ?
EditorConfig.ExportButtonEnabledColor :
EditorConfig.ExportButtonDisabledColor);

if (GUILayout.Button("Export to OBJ"))
{
ModelExporter.OBJ(mapzenMap.MeshFilters, mapzenMap.RegionName);
}

EditorConfig.ResetColor();

SavePreferences();
}

Expand Down
151 changes: 151 additions & 0 deletions Assets/Editor/ModelExporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
using System;
using System.Text;
using System.IO;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class ModelExporter
{
private struct MaterialDesc
{
public string mainTexture;
public Color color;
}

public static void OBJ(List<MeshFilter> meshes, string filename)
{
int vertexOffset = 0;
int normalOffset = 0;
int uvOffset = 0;

StringBuilder builder = new StringBuilder();

builder.Append("# Exported with Tangram Unity\n");
builder.Append("# Visit https://github.com/tangrams/tangram-unity/ for more details\n\n");
builder.Append("mtllib " + filename + ".mtl\n");

foreach (var meshFilter in meshes)
{
Mesh mesh = meshFilter.sharedMesh;

Material[] materials = meshFilter.GetComponent<Renderer>().sharedMaterials;

builder.Append("g ").Append(meshFilter.name).Append("\n");

foreach (Vector3 lv in mesh.vertices)
{
Vector3 wv = meshFilter.transform.TransformPoint(lv);
builder.Append(string.Format("v {0} {1} {2}\n", -wv.x, wv.y, wv.z));
}

builder.Append("\n");

foreach (Vector3 ln in mesh.normals)
{
Vector3 wn = meshFilter.transform.TransformDirection(ln);
builder.Append(string.Format("vn {0} {1} {2}\n", -wn.x, wn.y, wn.z));
}

builder.Append("\n");

foreach (Vector2 uv in mesh.uv)
{
builder.Append(string.Format("vt {0} {1}\n", uv.x, uv.y));
}

for (int i = 0; i < mesh.subMeshCount; ++i)
{
var material = materials[i];

builder.Append("\n");
builder.Append("usemtl ").Append(material.name).Append("\n");
builder.Append("usemap ").Append(material.name).Append("\n");

int[] triangles = mesh.GetTriangles(i);
for (int j = 0; j < triangles.Length; j += 3)
{
if (mesh.uv.Length > 0)
{
builder.Append(string.Format("f {1}/{1}/{1} {0}/{0}/{0} {2}/{2}/{2}\n",
triangles[j + 0] + 1 + vertexOffset,
triangles[j + 1] + 1 + vertexOffset,
triangles[j + 2] + 1 + vertexOffset));
}
else
{
builder.Append(string.Format("f {1}//{1} {0}//{0} {2}//{2}\n",
triangles[j + 0] + 1 + vertexOffset,
triangles[j + 1] + 1 + vertexOffset,
triangles[j + 2] + 1 + vertexOffset));
}
}
}

vertexOffset += mesh.vertices.Length;
}

var materialAssetPerName = new Dictionary<string, MaterialDesc>();
foreach (var meshFilter in meshes)
{
Mesh mesh = meshFilter.sharedMesh;
Material[] materials = meshFilter.GetComponent<Renderer>().sharedMaterials;

for (int i = 0; i < mesh.subMeshCount; ++i)
{
var material = materials[i];
string mainTexture = null;
if (material.mainTexture != null)
{
mainTexture = AssetDatabase.GetAssetPath(material.mainTexture);
}
if (!materialAssetPerName.ContainsKey(material.name))
{
MaterialDesc materialDesc = new MaterialDesc();
materialDesc.mainTexture = mainTexture;
materialDesc.color = material.color;
materialAssetPerName.Add(material.name, materialDesc);
}
}
}

using (StreamWriter sw = new StreamWriter(filename + ".mtl"))
{
foreach (var materialAssetPair in materialAssetPerName)
{
MaterialDesc materialDesc = materialAssetPair.Value;

sw.Write("\n");
sw.Write("newmtl {0}\n", materialAssetPair.Key);
sw.Write("Ka 1.0 1.0 1.0\n");
sw.Write(string.Format("Kd {0} {1} {2}\n",
materialDesc.color.r,
materialDesc.color.g,
materialDesc.color.b));
sw.Write("Ks 0.0 0.0 0.0\n");
sw.Write("d 1.0\n");
sw.Write("Ns 0.0\n");
sw.Write("illum 2\n");

if (materialDesc.mainTexture != null)
{
string dest = materialDesc.mainTexture;
int stripIndex = dest.LastIndexOf("/");
if (stripIndex >= 0)
{
dest = dest.Substring(stripIndex + 1).Trim();
}
File.Copy(materialDesc.mainTexture, dest);
sw.Write("map_Kd {0}\n", dest);
}
}
}

using (StreamWriter sw = new StreamWriter(filename + ".obj"))
{
sw.Write(builder.ToString());
}

Debug.Log("Model exported as " + filename + ".obj");
}
}
12 changes: 12 additions & 0 deletions Assets/Editor/ModelExporter.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions Assets/Mapzen/Unity/SceneGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ public class SceneGraph
/// </summary>
/// <param name="group">The scene group to visit.</param>
/// <param name="parent">The parent transform of the generated game object for the current scene group.</param>
public static void Generate(SceneGroup group, Transform parent, GameObjectOptions options)
public static List<MeshFilter> Generate(SceneGroup group, Transform parent, GameObjectOptions options)
{
List<MeshFilter> meshFilters = new List<MeshFilter>();

if (group.meshData.Meshes.Count == 0 && group.childs.Count == 0)
{
return;
return meshFilters;
}

if (group.childs.Count > 0)
Expand All @@ -36,7 +38,7 @@ public static void Generate(SceneGroup group, Transform parent, GameObjectOption

foreach (var child in group.childs)
{
Generate(child.Value, gameObject.transform, options);
meshFilters.AddRange(Generate(child.Value, gameObject.transform, options));
}
}
else
Expand Down Expand Up @@ -107,8 +109,12 @@ public static void Generate(SceneGroup group, Transform parent, GameObjectOption
meshColliderComponent.material = options.PhysicMaterial;
meshColliderComponent.sharedMesh = mesh;
}

meshFilters.Add(meshFilterComponent);
}
}

return meshFilters;
}
}
}
12 changes: 4 additions & 8 deletions Assets/MapzenMap.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
using System.Collections;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Networking;
using Mapzen.VectorData;
using Mapzen.Unity;
using Mapzen.VectorData.Filters;
using Mapzen;

public class MapzenMap : MonoBehaviour
Expand All @@ -24,7 +20,7 @@ public class MapzenMap : MonoBehaviour

public string RegionName = "";

private List<GameObject> tiles = new List<GameObject>();
private List<MeshFilter> meshFilters = new List<MeshFilter>();

private IO tileIO = new IO();

Expand Down Expand Up @@ -123,13 +119,13 @@ void OnTaskReady(TileTask readyTask)
{
tasks.Clear();

SceneGraph.Generate(regionMap, null, gameObjectOptions);
meshFilters = SceneGraph.Generate(regionMap, null, gameObjectOptions);
}
}

public List<GameObject> Tiles
public List<MeshFilter> MeshFilters
{
get { return tiles; }
get { return meshFilters; }
}

public List<FeatureStyle> FeatureStyling
Expand Down

0 comments on commit f430db1

Please sign in to comment.