88namespace ZO . ImportExport {
99 public class ZOExportOBJ {
1010
11+ public enum Orientation {
12+ URDF ,
13+ Unity // Unity Default direction
14+ }
15+
1116 protected string _objString = null ;
1217 public string OBJString {
1318 get { return _objString ; }
@@ -31,19 +36,25 @@ public List<string> TextureAssetPaths {
3136
3237 private int _startIndex = 0 ;
3338
34- public void Start ( ) {
39+ protected void Start ( ) {
3540 _startIndex = 0 ;
3641 }
37- public void End ( ) {
42+ protected void End ( ) {
3843 _startIndex = 0 ;
3944 }
4045
4146
42- protected string MeshToString ( MeshFilter meshFilter , Transform transform ) {
47+ protected string MeshToString ( MeshFilter meshFilter , Transform transform , bool applyLocalTransform , ZOExportOBJ . Orientation orientation ) {
4348 Vector3 s = transform . localScale ;
4449 Vector3 p = transform . localPosition ;
4550 Quaternion r = transform . localRotation ;
4651
52+ if ( applyLocalTransform == false ) {
53+ s = Vector3 . one ;
54+ p = Vector3 . zero ;
55+ r = Quaternion . identity ;
56+ }
57+
4758
4859 int numVertices = 0 ;
4960 Mesh mesh = meshFilter . sharedMesh ;
@@ -54,15 +65,26 @@ protected string MeshToString(MeshFilter meshFilter, Transform transform) {
5465
5566 StringBuilder sb = new StringBuilder ( ) ;
5667
57- foreach ( Vector3 vv in mesh . vertices ) {
58- Vector3 v = transform . TransformPoint ( vv ) ;
68+ foreach ( Vector3 vv in mesh . vertices ) {
69+ Vector3 v = vv ;
70+ if ( applyLocalTransform == true ) {
71+ v = transform . TransformPoint ( vv ) ;
72+ }
5973 numVertices ++ ;
60- sb . AppendLine ( $ "v { v . x } { v . y } { v . z } ") ;
74+ if ( orientation == Orientation . Unity ) {
75+ sb . AppendLine ( $ "v { v . x } { v . y } { v . z } ") ;
76+ } else if ( orientation == Orientation . URDF ) {
77+ sb . AppendLine ( $ "v { v . z } { v . x } { v . y } ") ;
78+ }
6179 }
6280 sb . AppendLine ( ) ;
6381 foreach ( Vector3 nn in mesh . normals ) {
6482 Vector3 v = r * nn ;
65- sb . AppendLine ( $ "vn { v . x } { v . y } { v . z } ") ;
83+ if ( orientation == Orientation . Unity ) {
84+ sb . AppendLine ( $ "vn { v . x } { v . y } { v . z } ") ;
85+ } else if ( orientation == Orientation . URDF ) {
86+ sb . AppendLine ( $ "vn { v . z } { v . x } { v . y } ") ;
87+ }
6688 }
6789 sb . AppendLine ( ) ;
6890 foreach ( Vector3 v in mesh . uv ) {
@@ -106,7 +128,7 @@ protected string MaterialToString(Material material) {
106128 return sb . ToString ( ) ;
107129 }
108130
109- protected string ProcessTransform ( Transform transform , bool makeSubmeshes ) {
131+ protected string ProcessTransform ( Transform transform , bool makeSubmeshes , bool applyLocalTransform , ZOExportOBJ . Orientation orientation ) {
110132 StringBuilder meshString = new StringBuilder ( ) ;
111133
112134 meshString . Append ( "#" + transform . name
@@ -119,18 +141,18 @@ protected string ProcessTransform(Transform transform, bool makeSubmeshes) {
119141
120142 MeshFilter meshFilter = transform . GetComponent < MeshFilter > ( ) ;
121143 if ( meshFilter != null ) {
122- meshString . Append ( MeshToString ( meshFilter , transform ) ) ;
144+ meshString . Append ( MeshToString ( meshFilter , transform , applyLocalTransform , orientation ) ) ;
123145 }
124146
125147 for ( int i = 0 ; i < transform . childCount ; i ++ ) {
126- meshString . Append ( ProcessTransform ( transform . GetChild ( i ) , makeSubmeshes ) ) ;
148+ meshString . Append ( ProcessTransform ( transform . GetChild ( i ) , makeSubmeshes , applyLocalTransform , orientation ) ) ;
127149 }
128150
129151 return meshString . ToString ( ) ;
130152 }
131153
132154
133- public void BuildExportData ( GameObject gameObject , bool makeSubmeshes , bool zeroPosition = true ) {
155+ public void BuildExportData ( GameObject gameObject , bool makeSubmeshes , bool applyLocalTransform , ZOExportOBJ . Orientation orientation ) {
134156 Debug . Assert ( gameObject != null , "ERROR: invalid GameObject in BuildExport" ) ;
135157
136158 string meshName = gameObject . name ;
@@ -149,19 +171,19 @@ public void BuildExportData(GameObject gameObject, bool makeSubmeshes, bool zero
149171
150172 Transform transform = gameObject . transform ;
151173
152- Vector3 originalPosition = transform . position ;
153- if ( zeroPosition == true ) {
154- transform . position = Vector3 . zero ;
155- }
174+ // Vector3 originalPosition = transform.position;
175+ // if (zeroPosition == true) {
176+ // transform.position = Vector3.zero;
177+ // }
156178
157179 if ( ! makeSubmeshes ) {
158180 meshString . Append ( "g " ) . Append ( transform . name ) . Append ( "\n " ) ;
159181 }
160- meshString . Append ( ProcessTransform ( transform , makeSubmeshes ) ) ;
182+ meshString . Append ( ProcessTransform ( transform , makeSubmeshes , applyLocalTransform , orientation ) ) ;
161183
162184 OBJString = meshString . ToString ( ) ;
163185
164- transform . position = originalPosition ;
186+ // transform.position = originalPosition;
165187
166188 // export materials and textures
167189 StringBuilder mtlFileString = new StringBuilder ( ) ;
@@ -222,8 +244,8 @@ public void BuildExportData(GameObject gameObject, bool makeSubmeshes, bool zero
222244
223245 }
224246
225- public void ExportToDirectory ( GameObject gameObject , string directoryPath , bool makeSubmeshes , bool zeroPosition = true ) {
226- BuildExportData ( gameObject , makeSubmeshes , zeroPosition ) ;
247+ public void ExportToDirectory ( GameObject gameObject , string directoryPath , bool makeSubmeshes , bool applyLocalTransform , ZOExportOBJ . Orientation orientation ) {
248+ BuildExportData ( gameObject , makeSubmeshes , applyLocalTransform , orientation ) ;
227249
228250 // write out obj file
229251 string objFilePath = Path . Combine ( directoryPath , $ "{ gameObject . name } .obj") ;
0 commit comments