From 81bea67888687a451e13f201365cbf2330156832 Mon Sep 17 00:00:00 2001 From: Kai Salmen Date: Sat, 12 Aug 2017 00:19:34 +0200 Subject: [PATCH] Added n-gon support to OBJLoader2 --- examples/js/loaders/OBJLoader2.js | 190 ++++++++++++---------------- examples/js/loaders/WWOBJLoader2.js | 2 +- 2 files changed, 80 insertions(+), 112 deletions(-) diff --git a/examples/js/loaders/OBJLoader2.js b/examples/js/loaders/OBJLoader2.js index 93793459b4..05c06a9d39 100644 --- a/examples/js/loaders/OBJLoader2.js +++ b/examples/js/loaders/OBJLoader2.js @@ -15,7 +15,7 @@ if ( THREE.OBJLoader2 === undefined ) { THREE.OBJLoader2 = {} } */ THREE.OBJLoader2 = (function () { - var OBJLOADER2_VERSION = '1.3.1'; + var OBJLOADER2_VERSION = '1.4.0'; function OBJLoader2( manager ) { console.log( "Using THREE.OBJLoader2 version: " + OBJLOADER2_VERSION ); @@ -265,9 +265,9 @@ THREE.OBJLoader2 = (function () { Parser.prototype.parseArrayBuffer = function ( arrayBuffer ) { var arrayBufferView = new Uint8Array( arrayBuffer ); var length = arrayBufferView.byteLength; - var buffer = new Array( 32 ); + var buffer = new Array( 128 ); var bufferPointer = 0; - var slashes = new Array( 32 ); + var slashes = new Array( 128 ); var slashesPointer = 0; var reachedFaces = false; var code; @@ -313,9 +313,9 @@ THREE.OBJLoader2 = (function () { */ Parser.prototype.parseText = function ( text ) { var length = text.length; - var buffer = new Array( 32 ); + var buffer = new Array( 128 ); var bufferPointer = 0; - var slashes = new Array( 32 ); + var slashes = new Array( 128 ); var slashesPointer = 0; var reachedFaces = false; var char; @@ -399,40 +399,8 @@ THREE.OBJLoader2 = (function () { * 2: "f vertex//normal ..." * 3: "f vertex ..." */ - var haveQuad = bufferLength % 4 === 0; - if ( slashesPointer > 1 && ( slashes[ 1 ] - slashes[ 0 ] ) === 1 ) { - - if ( haveQuad ) { - this.rawObject.buildQuadVVn( buffer ); - } else { - this.rawObject.buildFaceVVn( buffer ); - } - - } else if ( bufferLength === slashesPointer * 2 ) { - - if ( haveQuad ) { - this.rawObject.buildQuadVVt( buffer ); - } else { - this.rawObject.buildFaceVVt( buffer ); - } - - } else if ( bufferLength * 2 === slashesPointer * 3 ) { - - if ( haveQuad ) { - this.rawObject.buildQuadVVtVn( buffer ); - } else { - this.rawObject.buildFaceVVtVn( buffer ); - } - - } else { - - if ( haveQuad ) { - this.rawObject.buildQuadV( buffer ); - } else { - this.rawObject.buildFaceV( buffer ); - } - - } + var faceDescType = ( slashesPointer > 1 && ( slashes[ 1 ] - slashes[ 0 ] ) === 1 ) ? 2 : ( bufferLength === slashesPointer * 2 ) ? 1 : ( bufferLength * 2 === slashesPointer * 3 ) ? 0 : 3; + this.rawObject.processFaces( buffer, bufferPointer, faceDescType ); break; case Consts.LINE_L: @@ -558,6 +526,8 @@ THREE.OBJLoader2 = (function () { var index = this.buildIndex( this.activeMtlName, this.activeSmoothingGroup ); this.rawObjectDescriptionInUse = new RawObjectDescription( this.objectName, this.groupName, this.activeMtlName, this.activeSmoothingGroup ); this.rawObjectDescriptions[ index ] = this.rawObjectDescriptionInUse; + + this.facesBuffer = new Array( 128 ); } RawObject.prototype.buildIndex = function ( materialName, smoothingGroup) { @@ -660,98 +630,96 @@ THREE.OBJLoader2 = (function () { } }; - RawObject.prototype.buildQuadVVtVn = function ( indexArray ) { - for ( var i = 0; i < 6; i ++ ) { - this.attachFaceV_( indexArray[ Consts.QUAD_INDICES_3[ i ] ] ); - this.attachFaceVt( indexArray[ Consts.QUAD_INDICES_3[ i ] + 1 ] ); - this.attachFaceVn( indexArray[ Consts.QUAD_INDICES_3[ i ] + 2 ] ); + RawObject.prototype.processFaces = function ( buffer, bufferPointer, faceDescType ) { + var bufferLength = bufferPointer - 1; + var facesBuffer = this.facesBuffer; + var i; + for ( i = 1; i < bufferLength + 1; i++) { + facesBuffer[ i ] = parseInt( buffer[ i ] ); } - }; - RawObject.prototype.buildQuadVVt = function ( indexArray ) { - for ( var i = 0; i < 6; i ++ ) { - this.attachFaceV_( indexArray[ Consts.QUAD_INDICES_2[ i ] ] ); - this.attachFaceVt( indexArray[ Consts.QUAD_INDICES_2[ i ] + 1 ] ); - } - }; + /* + * 0: "f vertex/uv/normal ..." + * 1: "f vertex/uv ..." + * 2: "f vertex//normal ..." + * 3: "f vertex ..." + */ + if ( faceDescType === 0 ) { - RawObject.prototype.buildQuadVVn = function ( indexArray ) { - for ( var i = 0; i < 6; i ++ ) { - this.attachFaceV_( indexArray[ Consts.QUAD_INDICES_2[ i ] ] ); - this.attachFaceVn( indexArray[ Consts.QUAD_INDICES_2[ i ] + 1 ] ); - } - }; + for ( i = 4; i < bufferLength - 3; i += 3 ) { - RawObject.prototype.buildQuadV = function ( indexArray ) { - for ( var i = 0; i < 6; i ++ ) { - this.attachFaceV_( indexArray[ Consts.QUAD_INDICES_1[ i ] ] ); - } - }; + this.attachFace( facesBuffer[ 1 ], facesBuffer[ 2 ], facesBuffer[ 3 ] ); + this.attachFace( facesBuffer[ i ], facesBuffer[ i + 1 ], facesBuffer[ i + 2 ] ); + this.attachFace( facesBuffer[ i + 3 ], facesBuffer[ i + 4 ], facesBuffer[ i + 5 ] ); - RawObject.prototype.buildFaceVVtVn = function ( indexArray ) { - for ( var i = 1; i < 10; i += 3 ) { - this.attachFaceV_( indexArray[ i ] ); - this.attachFaceVt( indexArray[ i + 1 ] ); - this.attachFaceVn( indexArray[ i + 2 ] ); - } - }; + } - RawObject.prototype.buildFaceVVt = function ( indexArray ) { - for ( var i = 1; i < 7; i += 2 ) { - this.attachFaceV_( indexArray[ i ] ); - this.attachFaceVt( indexArray[ i + 1 ] ); - } - }; + } else if ( faceDescType === 1 ) { - RawObject.prototype.buildFaceVVn = function ( indexArray ) { - for ( var i = 1; i < 7; i += 2 ) { - this.attachFaceV_( indexArray[ i ] ); - this.attachFaceVn( indexArray[ i + 1 ] ); - } - }; + for ( i = 3; i < bufferLength - 2; i += 2 ) { + + this.attachFace( facesBuffer[ 1 ], facesBuffer[ 2 ], -1 ); + this.attachFace( facesBuffer[ i ], facesBuffer[ i + 1 ], -1 ); + this.attachFace( facesBuffer[ i + 2 ], facesBuffer[ i + 3 ], -1 ); + + } + + } else if ( faceDescType === 2 ) { + + for ( i = 3; i < bufferLength - 2; i += 2 ) { + + this.attachFace( facesBuffer[ 1 ], -1, facesBuffer[ 2 ] ); + this.attachFace( facesBuffer[ i ], -1, facesBuffer[ i + 1 ] ); + this.attachFace( facesBuffer[ i + 2 ], -1, facesBuffer[ i + 3 ] ); + + } + + } else { + + for ( i = 2; i < bufferLength - 1; i ++ ) { + + this.attachFace( facesBuffer[ 1 ], -1, -1 ); + this.attachFace( facesBuffer[ i ], -1, -1 ); + this.attachFace( facesBuffer[ i + 1 ], -1, -1 ); + + } - RawObject.prototype.buildFaceV = function ( indexArray ) { - for ( var i = 1; i < 4; i ++ ) { - this.attachFaceV_( indexArray[ i ] ); } }; - RawObject.prototype.attachFaceV_ = function ( faceIndex ) { - var faceIndexInt = parseInt( faceIndex ); - var index = ( faceIndexInt - this.globalVertexOffset ) * 3; - - var rodiu = this.rawObjectDescriptionInUse; - rodiu.vertices.push( this.vertices[ index++ ] ); - rodiu.vertices.push( this.vertices[ index++ ] ); - rodiu.vertices.push( this.vertices[ index ] ); + RawObject.prototype.attachFace = function ( faceIndexV, faceIndexVt, faceIndexVn ) { + var indexV = ( faceIndexV - this.globalVertexOffset ) * 3; + var vertices = this.rawObjectDescriptionInUse.vertices; + vertices.push( this.vertices[ indexV++ ] ); + vertices.push( this.vertices[ indexV++ ] ); + vertices.push( this.vertices[ indexV ] ); if ( this.colors.length > 0 ) { - index -= 2; - rodiu.colors.push( this.colors[ index++ ] ); - rodiu.colors.push( this.colors[ index++ ] ); - rodiu.colors.push( this.colors[ index ] ); + indexV -= 2; + var colors = this.rawObjectDescriptionInUse.colors; + colors.push( this.colors[ indexV++ ] ); + colors.push( this.colors[ indexV++ ] ); + colors.push( this.colors[ indexV ] ); } - }; + if ( faceIndexVt > -1 ) { - RawObject.prototype.attachFaceVt = function ( faceIndex ) { - var faceIndexInt = parseInt( faceIndex ); - var index = ( faceIndexInt - this.globalUvOffset ) * 2; + var indexVt = ( faceIndexVt - this.globalUvOffset ) * 2; + var uvs = this.rawObjectDescriptionInUse.uvs; + uvs.push( this.uvs[ indexVt++ ] ); + uvs.push( this.uvs[ indexVt ] ); - var rodiu = this.rawObjectDescriptionInUse; - rodiu.uvs.push( this.uvs[ index++ ] ); - rodiu.uvs.push( this.uvs[ index ] ); - }; + } + if ( faceIndexVn > -1 ) { - RawObject.prototype.attachFaceVn = function ( faceIndex ) { - var faceIndexInt = parseInt( faceIndex ); - var index = ( faceIndexInt - this.globalNormalOffset ) * 3; + var indexVn = ( faceIndexVn - this.globalNormalOffset ) * 3; + var normals = this.rawObjectDescriptionInUse.normals; + normals.push( this.normals[ indexVn ++ ] ); + normals.push( this.normals[ indexVn ++ ] ); + normals.push( this.normals[ indexVn ] ); - var rodiu = this.rawObjectDescriptionInUse; - rodiu.normals.push( this.normals[ index++ ] ); - rodiu.normals.push( this.normals[ index++ ] ); - rodiu.normals.push( this.normals[ index ] ); + } }; /* diff --git a/examples/js/loaders/WWOBJLoader2.js b/examples/js/loaders/WWOBJLoader2.js index 4c9b379080..c74eb2110b 100644 --- a/examples/js/loaders/WWOBJLoader2.js +++ b/examples/js/loaders/WWOBJLoader2.js @@ -15,7 +15,7 @@ if ( THREE.OBJLoader2 === undefined ) { THREE.OBJLoader2 = {} } */ THREE.OBJLoader2.WWOBJLoader2 = (function () { - var WWOBJLOADER2_VERSION = '1.3.1'; + var WWOBJLOADER2_VERSION = '1.4.0'; var Validator = THREE.OBJLoader2.prototype._getValidator(); -- GitLab