@@ -66,10 +66,9 @@ public final class OBJLoader implements AssetLoader {
6666 protected final ArrayList <Vector3f > verts = new ArrayList <Vector3f >();
6767 protected final ArrayList <Vector2f > texCoords = new ArrayList <Vector2f >();
6868 protected final ArrayList <Vector3f > norms = new ArrayList <Vector3f >();
69-
70- protected final ArrayList <Face > faces = new ArrayList <Face >();
71- protected final HashMap <String , ArrayList <Face >> matFaces = new HashMap <String , ArrayList <Face >>();
72-
69+
70+ private final ArrayList <Group > groups = new ArrayList <Group >();
71+
7372 protected String currentMatName ;
7473 protected String currentObjectName ;
7574
@@ -87,6 +86,16 @@ public final class OBJLoader implements AssetLoader {
8786 protected String objName ;
8887 protected Node objNode ;
8988
89+ private static class Group {
90+ private String name ;
91+ private final ArrayList <Face > faces = new ArrayList <Face >();
92+ private final HashMap <String , ArrayList <Face >> matFaces = new HashMap <String , ArrayList <Face >>();
93+
94+ public Group (final String name ) {
95+ this .name = name ;
96+ }
97+ }
98+
9099 protected static class Vertex {
91100
92101 Vector3f v ;
@@ -164,8 +173,7 @@ public void reset(){
164173 verts .clear ();
165174 texCoords .clear ();
166175 norms .clear ();
167- faces .clear ();
168- matFaces .clear ();
176+ groups .clear ();
169177
170178 vertIndexMap .clear ();
171179 indexVertMap .clear ();
@@ -289,10 +297,17 @@ protected void readFace(){
289297 f .verticies [i ] = vertList .get (i );
290298 }
291299
292- if (matList != null && matFaces .containsKey (currentMatName )){
293- matFaces .get (currentMatName ).add (f );
300+ Group group = groups .get (groups .size () - 1 );
301+
302+ if (currentMatName != null && matList != null && matList .containsKey (currentMatName )){
303+ ArrayList <Face > matFaces = group .matFaces .get (currentMatName );
304+ if (matFaces == null ) {
305+ matFaces = new ArrayList <Face >();
306+ group .matFaces .put (currentMatName , matFaces );
307+ }
308+ matFaces .add (f );
294309 }else {
295- faces .add (f ); // faces that belong to the default material
310+ group . faces .add (f ); // faces that belong to the default material
296311 }
297312 }
298313
@@ -337,13 +352,6 @@ protected void loadMtlLib(String name) throws IOException{
337352 } catch (AssetNotFoundException ex ){
338353 logger .log (Level .WARNING , "Cannot locate {0} for model {1}" , new Object []{name , key });
339354 }
340-
341- if (matList != null ){
342- // create face lists for every material
343- for (String matName : matList .keySet ()){
344- matFaces .put (matName , new ArrayList <Face >());
345- }
346- }
347355 }
348356
349357 protected boolean nextStatement (){
@@ -387,8 +395,10 @@ protected boolean readLine() throws IOException{
387395 // specify MTL lib to use for this OBJ file
388396 String mtllib = scan .nextLine ().trim ();
389397 loadMtlLib (mtllib );
390- }else if (cmd .equals ("s" ) || cmd . equals ( "g" )) {
398+ }else if (cmd .equals ("s" )) {
391399 return nextStatement ();
400+ }else if (cmd .equals ("g" )) {
401+ groups .add (new Group (scan .nextLine ().trim ()));
392402 }else {
393403 // skip entire command until next line
394404 logger .log (Level .WARNING , "Unknown statement in OBJ! {0}" , cmd );
@@ -566,10 +576,12 @@ public Object load(AssetInfo info) throws IOException{
566576
567577 objNode = new Node (objName + "-objnode" );
568578
579+ groups .add (new Group ("DefaultGroup" ));
580+
569581 if (!(info .getKey () instanceof ModelKey ))
570582 throw new IllegalArgumentException ("Model assets must be loaded using a ModelKey" );
571583
572- InputStream in = null ;
584+ InputStream in = null ;
573585 try {
574586 in = info .openStream ();
575587
@@ -583,23 +595,33 @@ public Object load(AssetInfo info) throws IOException{
583595 }
584596 }
585597
586- if (matFaces .size () > 0 ){
587- for (Entry <String , ArrayList <Face >> entry : matFaces .entrySet ()){
588- ArrayList <Face > materialFaces = entry .getValue ();
589- if (materialFaces .size () > 0 ){
590- Geometry geom = createGeometry (materialFaces , entry .getKey ());
591- objNode .attachChild (geom );
598+ for (final Group group : groups ) {
599+ final Node groupNode = new Node (group .name );
600+ if (group .matFaces .size () > 0 ) {
601+ for (Entry <String , ArrayList <Face >> entry : group .matFaces .entrySet ()){
602+ ArrayList <Face > materialFaces = entry .getValue ();
603+ if (materialFaces .size () > 0 ){
604+ Geometry geom = createGeometry (materialFaces , entry .getKey ());
605+ groupNode .attachChild (geom );
606+ }
592607 }
608+ } else if (group .faces .size () > 0 ) {
609+ // generate final geometry
610+ Geometry geom = createGeometry (group .faces , null );
611+ groupNode .attachChild (geom );
612+ }
613+ if (groupNode .getQuantity () == 1 ) {
614+ Spatial geom = groupNode .getChild (0 );
615+ geom .setName (groupNode .getName ());
616+ objNode .attachChild (geom );
617+ } else if (groupNode .getQuantity () > 1 ) {
618+ objNode .attachChild (groupNode );
593619 }
594- }else if (faces .size () > 0 ){
595- // generate final geometry
596- Geometry geom = createGeometry (faces , null );
597- objNode .attachChild (geom );
598620 }
599621
600622 if (objNode .getQuantity () == 1 )
601623 // only 1 geometry, so no need to send node
602- return objNode .getChild (0 );
624+ return objNode .getChild (0 );
603625 else
604626 return objNode ;
605627 }
0 commit comments