提交 81bea678 编写于 作者: K Kai Salmen

Added n-gon support to OBJLoader2

上级 7c40f1c6
...@@ -15,7 +15,7 @@ if ( THREE.OBJLoader2 === undefined ) { THREE.OBJLoader2 = {} } ...@@ -15,7 +15,7 @@ if ( THREE.OBJLoader2 === undefined ) { THREE.OBJLoader2 = {} }
*/ */
THREE.OBJLoader2 = (function () { THREE.OBJLoader2 = (function () {
var OBJLOADER2_VERSION = '1.3.1'; var OBJLOADER2_VERSION = '1.4.0';
function OBJLoader2( manager ) { function OBJLoader2( manager ) {
console.log( "Using THREE.OBJLoader2 version: " + OBJLOADER2_VERSION ); console.log( "Using THREE.OBJLoader2 version: " + OBJLOADER2_VERSION );
...@@ -265,9 +265,9 @@ THREE.OBJLoader2 = (function () { ...@@ -265,9 +265,9 @@ THREE.OBJLoader2 = (function () {
Parser.prototype.parseArrayBuffer = function ( arrayBuffer ) { Parser.prototype.parseArrayBuffer = function ( arrayBuffer ) {
var arrayBufferView = new Uint8Array( arrayBuffer ); var arrayBufferView = new Uint8Array( arrayBuffer );
var length = arrayBufferView.byteLength; var length = arrayBufferView.byteLength;
var buffer = new Array( 32 ); var buffer = new Array( 128 );
var bufferPointer = 0; var bufferPointer = 0;
var slashes = new Array( 32 ); var slashes = new Array( 128 );
var slashesPointer = 0; var slashesPointer = 0;
var reachedFaces = false; var reachedFaces = false;
var code; var code;
...@@ -313,9 +313,9 @@ THREE.OBJLoader2 = (function () { ...@@ -313,9 +313,9 @@ THREE.OBJLoader2 = (function () {
*/ */
Parser.prototype.parseText = function ( text ) { Parser.prototype.parseText = function ( text ) {
var length = text.length; var length = text.length;
var buffer = new Array( 32 ); var buffer = new Array( 128 );
var bufferPointer = 0; var bufferPointer = 0;
var slashes = new Array( 32 ); var slashes = new Array( 128 );
var slashesPointer = 0; var slashesPointer = 0;
var reachedFaces = false; var reachedFaces = false;
var char; var char;
...@@ -399,40 +399,8 @@ THREE.OBJLoader2 = (function () { ...@@ -399,40 +399,8 @@ THREE.OBJLoader2 = (function () {
* 2: "f vertex//normal ..." * 2: "f vertex//normal ..."
* 3: "f vertex ..." * 3: "f vertex ..."
*/ */
var haveQuad = bufferLength % 4 === 0; var faceDescType = ( slashesPointer > 1 && ( slashes[ 1 ] - slashes[ 0 ] ) === 1 ) ? 2 : ( bufferLength === slashesPointer * 2 ) ? 1 : ( bufferLength * 2 === slashesPointer * 3 ) ? 0 : 3;
if ( slashesPointer > 1 && ( slashes[ 1 ] - slashes[ 0 ] ) === 1 ) { this.rawObject.processFaces( buffer, bufferPointer, faceDescType );
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 );
}
}
break; break;
case Consts.LINE_L: case Consts.LINE_L:
...@@ -558,6 +526,8 @@ THREE.OBJLoader2 = (function () { ...@@ -558,6 +526,8 @@ THREE.OBJLoader2 = (function () {
var index = this.buildIndex( this.activeMtlName, this.activeSmoothingGroup ); var index = this.buildIndex( this.activeMtlName, this.activeSmoothingGroup );
this.rawObjectDescriptionInUse = new RawObjectDescription( this.objectName, this.groupName, this.activeMtlName, this.activeSmoothingGroup ); this.rawObjectDescriptionInUse = new RawObjectDescription( this.objectName, this.groupName, this.activeMtlName, this.activeSmoothingGroup );
this.rawObjectDescriptions[ index ] = this.rawObjectDescriptionInUse; this.rawObjectDescriptions[ index ] = this.rawObjectDescriptionInUse;
this.facesBuffer = new Array( 128 );
} }
RawObject.prototype.buildIndex = function ( materialName, smoothingGroup) { RawObject.prototype.buildIndex = function ( materialName, smoothingGroup) {
...@@ -660,98 +630,96 @@ THREE.OBJLoader2 = (function () { ...@@ -660,98 +630,96 @@ THREE.OBJLoader2 = (function () {
} }
}; };
RawObject.prototype.buildQuadVVtVn = function ( indexArray ) { RawObject.prototype.processFaces = function ( buffer, bufferPointer, faceDescType ) {
for ( var i = 0; i < 6; i ++ ) { var bufferLength = bufferPointer - 1;
this.attachFaceV_( indexArray[ Consts.QUAD_INDICES_3[ i ] ] ); var facesBuffer = this.facesBuffer;
this.attachFaceVt( indexArray[ Consts.QUAD_INDICES_3[ i ] + 1 ] ); var i;
this.attachFaceVn( indexArray[ Consts.QUAD_INDICES_3[ i ] + 2 ] ); for ( i = 1; i < bufferLength + 1; i++) {
facesBuffer[ i ] = parseInt( buffer[ i ] );
} }
};
RawObject.prototype.buildQuadVVt = function ( indexArray ) { /*
for ( var i = 0; i < 6; i ++ ) { * 0: "f vertex/uv/normal ..."
this.attachFaceV_( indexArray[ Consts.QUAD_INDICES_2[ i ] ] ); * 1: "f vertex/uv ..."
this.attachFaceVt( indexArray[ Consts.QUAD_INDICES_2[ i ] + 1 ] ); * 2: "f vertex//normal ..."
} * 3: "f vertex ..."
}; */
if ( faceDescType === 0 ) {
RawObject.prototype.buildQuadVVn = function ( indexArray ) { for ( i = 4; i < bufferLength - 3; i += 3 ) {
for ( var i = 0; i < 6; i ++ ) {
this.attachFaceV_( indexArray[ Consts.QUAD_INDICES_2[ i ] ] );
this.attachFaceVn( indexArray[ Consts.QUAD_INDICES_2[ i ] + 1 ] );
}
};
RawObject.prototype.buildQuadV = function ( indexArray ) { this.attachFace( facesBuffer[ 1 ], facesBuffer[ 2 ], facesBuffer[ 3 ] );
for ( var i = 0; i < 6; i ++ ) { this.attachFace( facesBuffer[ i ], facesBuffer[ i + 1 ], facesBuffer[ i + 2 ] );
this.attachFaceV_( indexArray[ Consts.QUAD_INDICES_1[ i ] ] ); 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 ) { } else if ( faceDescType === 1 ) {
for ( var i = 1; i < 7; i += 2 ) {
this.attachFaceV_( indexArray[ i ] );
this.attachFaceVt( indexArray[ i + 1 ] );
}
};
RawObject.prototype.buildFaceVVn = function ( indexArray ) { for ( i = 3; i < bufferLength - 2; i += 2 ) {
for ( var i = 1; i < 7; i += 2 ) {
this.attachFaceV_( indexArray[ i ] ); this.attachFace( facesBuffer[ 1 ], facesBuffer[ 2 ], -1 );
this.attachFaceVn( indexArray[ i + 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 ) { RawObject.prototype.attachFace = function ( faceIndexV, faceIndexVt, faceIndexVn ) {
var faceIndexInt = parseInt( faceIndex ); var indexV = ( faceIndexV - this.globalVertexOffset ) * 3;
var index = ( faceIndexInt - this.globalVertexOffset ) * 3; var vertices = this.rawObjectDescriptionInUse.vertices;
vertices.push( this.vertices[ indexV++ ] );
var rodiu = this.rawObjectDescriptionInUse; vertices.push( this.vertices[ indexV++ ] );
rodiu.vertices.push( this.vertices[ index++ ] ); vertices.push( this.vertices[ indexV ] );
rodiu.vertices.push( this.vertices[ index++ ] );
rodiu.vertices.push( this.vertices[ index ] );
if ( this.colors.length > 0 ) { if ( this.colors.length > 0 ) {
index -= 2; indexV -= 2;
rodiu.colors.push( this.colors[ index++ ] ); var colors = this.rawObjectDescriptionInUse.colors;
rodiu.colors.push( this.colors[ index++ ] ); colors.push( this.colors[ indexV++ ] );
rodiu.colors.push( this.colors[ index ] ); colors.push( this.colors[ indexV++ ] );
colors.push( this.colors[ indexV ] );
} }
}; if ( faceIndexVt > -1 ) {
RawObject.prototype.attachFaceVt = function ( faceIndex ) { var indexVt = ( faceIndexVt - this.globalUvOffset ) * 2;
var faceIndexInt = parseInt( faceIndex ); var uvs = this.rawObjectDescriptionInUse.uvs;
var index = ( faceIndexInt - this.globalUvOffset ) * 2; uvs.push( this.uvs[ indexVt++ ] );
uvs.push( this.uvs[ indexVt ] );
var rodiu = this.rawObjectDescriptionInUse; }
rodiu.uvs.push( this.uvs[ index++ ] ); if ( faceIndexVn > -1 ) {
rodiu.uvs.push( this.uvs[ index ] );
};
RawObject.prototype.attachFaceVn = function ( faceIndex ) { var indexVn = ( faceIndexVn - this.globalNormalOffset ) * 3;
var faceIndexInt = parseInt( faceIndex ); var normals = this.rawObjectDescriptionInUse.normals;
var index = ( faceIndexInt - this.globalNormalOffset ) * 3; 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 ] );
}; };
/* /*
......
...@@ -15,7 +15,7 @@ if ( THREE.OBJLoader2 === undefined ) { THREE.OBJLoader2 = {} } ...@@ -15,7 +15,7 @@ if ( THREE.OBJLoader2 === undefined ) { THREE.OBJLoader2 = {} }
*/ */
THREE.OBJLoader2.WWOBJLoader2 = (function () { THREE.OBJLoader2.WWOBJLoader2 = (function () {
var WWOBJLOADER2_VERSION = '1.3.1'; var WWOBJLOADER2_VERSION = '1.4.0';
var Validator = THREE.OBJLoader2.prototype._getValidator(); var Validator = THREE.OBJLoader2.prototype._getValidator();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册