@@ -620,12 +620,10 @@ function parseObj(model, lines, materials= {}) {
620620 const vertString = tokens [ vertexTokens [ tokenInd ] ] ;
621621 let vertParts = vertString . split ( '/' ) ;
622622
623- // TODO: Faces can technically use negative numbers to refer to the
624- // previous nth vertex. I haven't seen this used in practice, but
625- // it might be good to implement this in the future.
626-
627623 for ( let i = 0 ; i < vertParts . length ; i ++ ) {
628- vertParts [ i ] = parseInt ( vertParts [ i ] ) - 1 ;
624+ let index = parseInt ( vertParts [ i ] ) ;
625+ if ( index > 0 ) index -= 1 ; // OBJ uses 1-based indexing
626+ vertParts [ i ] = index ;
629627 }
630628
631629 if ( ! usedVerts [ vertString ] ) {
@@ -634,19 +632,19 @@ function parseObj(model, lines, materials= {}) {
634632
635633 if ( usedVerts [ vertString ] [ currentMaterial ] === undefined ) {
636634 const vertIndex = model . vertices . length ;
637- model . vertices . push ( loadedVerts . v [ vertParts [ 0 ] ] . copy ( ) ) ;
638- model . uvs . push ( loadedVerts . vt [ vertParts [ 1 ] ] ?
639- loadedVerts . vt [ vertParts [ 1 ] ] . slice ( ) : [ 0 , 0 ] ) ;
640- model . vertexNormals . push ( loadedVerts . vn [ vertParts [ 2 ] ] ?
641- loadedVerts . vn [ vertParts [ 2 ] ] . copy ( ) : new p5 . Vector ( ) ) ;
635+ model . vertices . push ( loadedVerts . v . at ( vertParts [ 0 ] ) . copy ( ) ) ;
636+ model . uvs . push ( loadedVerts . vt . at ( vertParts [ 1 ] ) ?
637+ loadedVerts . vt . at ( vertParts [ 1 ] ) . slice ( ) : [ 0 , 0 ] ) ;
638+ model . vertexNormals . push ( loadedVerts . vn . at ( vertParts [ 2 ] ) ?
639+ loadedVerts . vn . at ( vertParts [ 2 ] ) . copy ( ) : new p5 . Vector ( ) ) ;
642640
643641 usedVerts [ vertString ] [ currentMaterial ] = vertIndex ;
644642 face . push ( vertIndex ) ;
645643 if ( currentMaterial
646644 && materials [ currentMaterial ]
647645 && materials [ currentMaterial ] . diffuseColor ) {
648646 // Mark this vertex as colored
649- coloredVerts . add ( loadedVerts . v [ vertParts [ 0 ] ] ) ; //since a set would only push unique values
647+ coloredVerts . add ( loadedVerts . v . at ( vertParts [ 0 ] ) ) ; //since a set would only push unique values
650648 }
651649 } else {
652650 face . push ( usedVerts [ vertString ] [ currentMaterial ] ) ;
0 commit comments