123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- /**
- * @author mrdoob / http://mrdoob.com/
- */
- THREE.OBJLoader = function () {
- THREE.EventDispatcher.call( this );
- };
- THREE.OBJLoader.prototype = {
- constructor: THREE.OBJLoader,
- load: function ( url, callback ) {
- var scope = this;
- var request = new XMLHttpRequest();
- request.addEventListener( 'load', function ( event ) {
- var hierarchy = scope.parse( request.responseText );
- scope.dispatchEvent( { type: 'load', content: hierarchy } );
- if ( callback ) callback( hierarchy );
- }, false );
- request.addEventListener( 'progress', function ( event ) {
- //scope.dispatchEvent( { type: 'progress', loaded: event.loaded, total: event.total } );
- }, false );
- request.addEventListener( 'error', function () {
- scope.dispatchEvent( { type: 'error', message: 'Couldn\'t load URL [' + url + ']' } );
- }, false );
- request.open( 'GET', url, true );
- request.send( null );
- },
- parse: function ( data ) {
- function vector( x, y, z ) {
- return new THREE.Vector3( x, y, z );
- }
- function uv( u, v ) {
- return new THREE.Vector2( u, v );
- }
- function face3( a, b, c, normals ) {
- return new THREE.Face3( a, b, c, normals );
- }
- function face4( a, b, c, d, normals ) {
- return new THREE.Face4( a, b, c, d, normals );
- }
- var group = new THREE.Object3D();
- var vertices = [];
- var normals = [];
- var uvs = [];
- var pattern, result;
- // v float float float
- pattern = /v( +[\d|\.|\+|\-|e]+)( [\d|\.|\+|\-|e]+)( [\d|\.|\+|\-|e]+)/g;
- while ( ( result = pattern.exec( data ) ) != null ) {
- // ["v 1.0 2.0 3.0", "1.0", "2.0", "3.0"]
- vertices.push( vector(
- parseFloat( result[ 1 ] ),
- parseFloat( result[ 2 ] ),
- parseFloat( result[ 3 ] )
- ) );
- }
- // vn float float float
- pattern = /vn( +[\d|\.|\+|\-|e]+)( [\d|\.|\+|\-|e]+)( [\d|\.|\+|\-|e]+)/g;
- while ( ( result = pattern.exec( data ) ) != null ) {
- // ["vn 1.0 2.0 3.0", "1.0", "2.0", "3.0"]
- normals.push( vector(
- parseFloat( result[ 1 ] ),
- parseFloat( result[ 2 ] ),
- parseFloat( result[ 3 ] )
- ) );
- }
- // vt float float
- pattern = /vt( +[\d|\.|\+|\-|e]+)( [\d|\.|\+|\-|e]+)/g;
- while ( ( result = pattern.exec( data ) ) != null ) {
- // ["vt 0.1 0.2", "0.1", "0.2"]
- uvs.push( uv(
- parseFloat( result[ 1 ] ),
- parseFloat( result[ 2 ] )
- ) );
- }
- var splitData = data.split( '\no ' );
- for ( var i = 0, l = splitData.length; i < l; i ++ ) {
- var object = splitData[ i ];
- var geometry = new THREE.Geometry();
- geometry.vertices = vertices;
- // f vertex vertex vertex ...
- pattern = /f( +[\d]+)( [\d]+)( [\d]+)( [\d]+)?/g;
- while ( ( result = pattern.exec( object ) ) != null ) {
- // ["f 1 2 3", "1", "2", "3", undefined]
- if ( result[ 4 ] === undefined ) {
- geometry.faces.push( face3(
- parseInt( result[ 1 ] ) - 1,
- parseInt( result[ 2 ] ) - 1,
- parseInt( result[ 3 ] ) - 1
- ) );
- } else {
- geometry.faces.push( face4(
- parseInt( result[ 1 ] ) - 1,
- parseInt( result[ 2 ] ) - 1,
- parseInt( result[ 3 ] ) - 1,
- parseInt( result[ 4 ] ) - 1
- ) );
- }
- }
- // f vertex/uv vertex/uv vertex/uv ...
- pattern = /f( +([\d]+)\/([\d]+))( ([\d]+)\/([\d]+))( ([\d]+)\/([\d]+))( ([\d]+)\/([\d]+))?/g;
- while ( ( result = pattern.exec( object ) ) != null ) {
- // ["f 1/1 2/2 3/3", " 1/1", "1", "1", " 2/2", "2", "2", " 3/3", "3", "3", undefined, undefined, undefined]
- if ( result[ 10 ] === undefined ) {
- geometry.faces.push( face3(
- parseInt( result[ 2 ] ) - 1,
- parseInt( result[ 5 ] ) - 1,
- parseInt( result[ 8 ] ) - 1
- ) );
- geometry.faceVertexUvs[ 0 ].push( [
- uvs[ parseInt( result[ 3 ] ) - 1 ],
- uvs[ parseInt( result[ 6 ] ) - 1 ],
- uvs[ parseInt( result[ 9 ] ) - 1 ]
- ] );
- } else {
- geometry.faces.push( face4(
- parseInt( result[ 2 ] ) - 1,
- parseInt( result[ 5 ] ) - 1,
- parseInt( result[ 8 ] ) - 1,
- parseInt( result[ 11 ] ) - 1
- ) );
- geometry.faceVertexUvs[ 0 ].push( [
- uvs[ parseInt( result[ 3 ] ) - 1 ],
- uvs[ parseInt( result[ 6 ] ) - 1 ],
- uvs[ parseInt( result[ 9 ] ) - 1 ],
- uvs[ parseInt( result[ 12 ] ) - 1 ]
- ] );
- }
- }
- // f vertex/uv/normal vertex/uv/normal vertex/uv/normal ...
- pattern = /f( +([\d]+)\/([\d]+)\/([\d]+))( ([\d]+)\/([\d]+)\/([\d]+))( ([\d]+)\/([\d]+)\/([\d]+))( ([\d]+)\/([\d]+)\/([\d]+))?/g;
- while ( ( result = pattern.exec( object ) ) != null ) {
- // ["f 1/1/1 2/2/2 3/3/3", " 1/1/1", "1", "1", "1", " 2/2/2", "2", "2", "2", " 3/3/3", "3", "3", "3", undefined, undefined, undefined, undefined]
- if ( result[ 13 ] === undefined ) {
- geometry.faces.push( face3(
- parseInt( result[ 2 ] ) - 1,
- parseInt( result[ 6 ] ) - 1,
- parseInt( result[ 10 ] ) - 1,
- [
- normals[ parseInt( result[ 4 ] ) - 1 ],
- normals[ parseInt( result[ 8 ] ) - 1 ],
- normals[ parseInt( result[ 12 ] ) - 1 ]
- ]
- ) );
- geometry.faceVertexUvs[ 0 ].push( [
- uvs[ parseInt( result[ 3 ] ) - 1 ],
- uvs[ parseInt( result[ 7 ] ) - 1 ],
- uvs[ parseInt( result[ 11 ] ) - 1 ]
- ] );
- } else {
- geometry.faces.push( face4(
- parseInt( result[ 2 ] ) - 1,
- parseInt( result[ 6 ] ) - 1,
- parseInt( result[ 10 ] ) - 1,
- parseInt( result[ 14 ] ) - 1,
- [
- normals[ parseInt( result[ 4 ] ) - 1 ],
- normals[ parseInt( result[ 8 ] ) - 1 ],
- normals[ parseInt( result[ 12 ] ) - 1 ],
- normals[ parseInt( result[ 16 ] ) - 1 ]
- ]
- ) );
- geometry.faceVertexUvs[ 0 ].push( [
- uvs[ parseInt( result[ 3 ] ) - 1 ],
- uvs[ parseInt( result[ 7 ] ) - 1 ],
- uvs[ parseInt( result[ 11 ] ) - 1 ],
- uvs[ parseInt( result[ 15 ] ) - 1 ]
- ] );
- }
- }
- // f vertex//normal vertex//normal vertex//normal ...
- pattern = /f( +([\d]+)\/\/([\d]+))( ([\d]+)\/\/([\d]+))( ([\d]+)\/\/([\d]+))( ([\d]+)\/\/([\d]+))?/g;
- while ( ( result = pattern.exec( object ) ) != null ) {
- // ["f 1//1 2//2 3//3", " 1//1", "1", "1", " 2//2", "2", "2", " 3//3", "3", "3", undefined, undefined, undefined]
- if ( result[ 10 ] === undefined ) {
- geometry.faces.push( face3(
- parseInt( result[ 2 ] ) - 1,
- parseInt( result[ 5 ] ) - 1,
- parseInt( result[ 8 ] ) - 1,
- [
- normals[ parseInt( result[ 3 ] ) - 1 ],
- normals[ parseInt( result[ 6 ] ) - 1 ],
- normals[ parseInt( result[ 9 ] ) - 1 ]
- ]
- ) );
- } else {
- geometry.faces.push( face4(
- parseInt( result[ 2 ] ) - 1,
- parseInt( result[ 5 ] ) - 1,
- parseInt( result[ 8 ] ) - 1,
- parseInt( result[ 11 ] ) - 1,
- [
- normals[ parseInt( result[ 3 ] ) - 1 ],
- normals[ parseInt( result[ 6 ] ) - 1 ],
- normals[ parseInt( result[ 9 ] ) - 1 ],
- normals[ parseInt( result[ 12 ] ) - 1 ]
- ]
- ) );
- }
- }
- geometry.computeCentroids();
- geometry.computeFaceNormals();
- geometry.computeBoundingSphere();
- group.add( new THREE.Mesh( geometry, new THREE.MeshLambertMaterial() ) );
- }
- return group;
- }
- }
|