99
1010namespace ZO . ImportExport {
1111 public class ZOImportOBJ {
12- public static string WorkingDirectory {
13- get ; set ;
14- } = "." ;
1512
16- public static string FileName {
13+ public static bool SplitByMaterial {
1714 get ; set ;
18- } = "temp" ;
19-
15+ } = true ;
2016
2117 struct OBJFace {
2218 public string materialName ;
@@ -64,11 +60,11 @@ protected static Material[] ImportMTLFile(string mtlFilePath) {
6460 string texturePath = Path . Combine ( workingDirectory , lineComponents [ 1 ] ) ;
6561 string textureType = Path . GetExtension ( texturePath ) ;
6662 if ( textureType == ".png" || textureType == ".jpg" ) {
67- Texture2D texture = new Texture2D ( 1 , 1 ) ;
63+ Texture2D texture = new Texture2D ( 1 , 1 ) ;
6864 texture . LoadImage ( File . ReadAllBytes ( texturePath ) ) ;
6965 currentMaterial . SetTexture ( "_MainTex" , texture ) ;
7066 }
71-
67+
7268 } else if ( lineComponents [ 0 ] == "map_Bump" ) {
7369 // TODO
7470 } else if ( lineComponents [ 0 ] == "Ks" ) {
@@ -133,7 +129,7 @@ public static GameObject Import(StreamReader streamReader, string meshName, stri
133129 List < string > objectNames = new List < string > ( ) ;
134130 Dictionary < string , int > hashtable = new Dictionary < string , int > ( ) ;
135131 List < OBJFace > faceList = new List < OBJFace > ( ) ;
136- string cmaterial = "" ;
132+ string cmaterial = "default " ;
137133 string cmesh = "default" ;
138134 //CACHE
139135 Material [ ] materials = null ;
@@ -149,7 +145,7 @@ public static GameObject Import(StreamReader streamReader, string meshName, stri
149145 string mtlFilePath = Path . Combine ( workingDirectory , lineComponents [ 1 ] ) ;
150146 materials = ImportMTLFile ( mtlFilePath ) ;
151147
152- } else if ( ( lineComponents [ 0 ] == "g" || lineComponents [ 0 ] == "o" ) ) {
148+ } else if ( ( lineComponents [ 0 ] == "g" || lineComponents [ 0 ] == "o" ) && SplitByMaterial == false ) {
153149 cmesh = data ;
154150 if ( ! objectNames . Contains ( cmesh ) ) {
155151 objectNames . Add ( cmesh ) ;
@@ -160,6 +156,12 @@ public static GameObject Import(StreamReader streamReader, string meshName, stri
160156 materialNames . Add ( cmaterial ) ;
161157 }
162158
159+ if ( SplitByMaterial ) {
160+ if ( ! objectNames . Contains ( cmaterial ) ) {
161+ objectNames . Add ( cmaterial ) ;
162+ }
163+ }
164+
163165 } else if ( lineComponents [ 0 ] == "v" ) {
164166 //VERTEX
165167 vertices . Add ( ParseVector3 ( lineComponents ) ) ;
@@ -222,13 +224,13 @@ public static GameObject Import(StreamReader streamReader, string meshName, stri
222224 OBJFace f1 = new OBJFace ( ) ;
223225 f1 . materialName = cmaterial ;
224226 f1 . indexes = new int [ ] { indexes [ 0 ] , indexes [ 1 ] , indexes [ 2 ] } ;
225- f1 . meshName = cmesh ;
227+ f1 . meshName = ( SplitByMaterial ) ? cmaterial : cmesh ;
226228 faceList . Add ( f1 ) ;
227229 if ( indexes . Length > 3 ) {
228230
229231 OBJFace f2 = new OBJFace ( ) ;
230232 f2 . materialName = cmaterial ;
231- f2 . meshName = cmesh ;
233+ f2 . meshName = ( SplitByMaterial ) ? cmaterial : cmesh ; ;
232234 f2 . indexes = new int [ ] { indexes [ 2 ] , indexes [ 3 ] , indexes [ 0 ] } ;
233235 faceList . Add ( f2 ) ;
234236 }
@@ -237,12 +239,21 @@ public static GameObject Import(StreamReader streamReader, string meshName, stri
237239 }
238240 }
239241
240- if ( objectNames . Count == 0 )
242+ if ( objectNames . Count == 0 ) {
241243 objectNames . Add ( "default" ) ;
244+ }
242245
243- GameObject meshGameObject = new GameObject ( meshName ) ;
246+ if ( materialNames . Count == 0 ) {
247+ materialNames . Add ( "default" ) ;
248+ }
249+
250+ GameObject parentGameObject = new GameObject ( meshName ) ;
251+
252+ // GameObject meshGameObject = new GameObject(meshName);
244253 foreach ( string obj in objectNames ) {
245- meshGameObject . transform . localScale = new Vector3 ( - 1 , 1 , 1 ) ;
254+ GameObject childGameObject = new GameObject ( obj ) ;
255+ childGameObject . transform . parent = parentGameObject . transform ;
256+ childGameObject . transform . localScale = new Vector3 ( - 1 , 1 , 1 ) ;
246257 Mesh m = new Mesh ( ) ;
247258 m . name = obj ;
248259 List < Vector3 > processedVertices = new List < Vector3 > ( ) ;
@@ -253,6 +264,7 @@ public static GameObject Import(StreamReader streamReader, string meshName, stri
253264 List < string > meshMaterialNames = new List < string > ( ) ;
254265
255266 OBJFace [ ] objFaces = faceList . Where ( x => x . meshName == obj ) . ToArray ( ) ;
267+
256268 foreach ( string mn in materialNames ) {
257269 OBJFace [ ] faces = objFaces . Where ( x => x . materialName == mn ) . ToArray ( ) ;
258270 if ( faces . Length > 0 ) {
@@ -297,10 +309,17 @@ public static GameObject Import(StreamReader streamReader, string meshName, stri
297309 m . RecalculateNormals ( ) ;
298310 }
299311 m . RecalculateBounds ( ) ;
300- ;
301312
302- MeshFilter mf = meshGameObject . AddComponent < MeshFilter > ( ) ;
303- MeshRenderer mr = meshGameObject . AddComponent < MeshRenderer > ( ) ;
313+ MeshFilter mf = childGameObject . GetComponent < MeshFilter > ( ) ;
314+ if ( mf == null ) {
315+ mf = childGameObject . AddComponent < MeshFilter > ( ) ;
316+ }
317+ MeshRenderer mr = childGameObject . GetComponent < MeshRenderer > ( ) ;
318+ if ( mr == null ) {
319+ mr = childGameObject . AddComponent < MeshRenderer > ( ) ;
320+ }
321+
322+
304323
305324 Material [ ] processedMaterials = new Material [ meshMaterialNames . Count ] ;
306325 for ( int i = 0 ; i < meshMaterialNames . Count ; i ++ ) {
@@ -324,7 +343,7 @@ public static GameObject Import(StreamReader streamReader, string meshName, stri
324343
325344 }
326345
327- return meshGameObject ;
346+ return parentGameObject ;
328347 }
329348
330349 }
0 commit comments