OBJLoader.js 3.2 KB
Newer Older
M
Mr.doob 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/**
 * @author mrdoob / http://mrdoob.com/
 */

THREE.OBJLoader = function () {};
THREE.OBJLoader.prototype.load = function ( url, callback ) {

	var xhr = new XMLHttpRequest();
	xhr.onreadystatechange = function () {

		if ( xhr.readyState == 4 ) {

			if ( xhr.status == 200 || xhr.status == 0 ) {

				THREE.OBJLoader.prototype.parse( xhr.responseText, callback );

			} else {

				console.error( 'THREE.OBJLoader: Couldn\'t load ' + url + ' (' + xhr.status + ')' );

			}

		}

	};

	xhr.open( "GET", url, true );
	xhr.send( null );

};

THREE.OBJLoader.prototype.parse = function ( data, callback ) {

	var geometry = new THREE.Geometry();

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
	function vertex( a, b, c ) {

		return new THREE.Vector3( parseFloat( a ), parseFloat( b ), parseFloat( c ) );

	}

	function face3( a, b, c ) {

		return new THREE.Face3( parseInt( a ) - 1, parseInt( b ) - 1, parseInt( c ) - 1 );

	}

	function face4( a, b, c, d ) {

		return new THREE.Face4( parseInt( a ) - 1, parseInt( b ) - 1, parseInt( c ) - 1, parseInt( d ) - 1 );

	}

M
Mr.doob 已提交
54
	var pattern, result;
M
Mr.doob 已提交
55

56 57 58 59 60 61 62 63 64 65 66 67 68
	// v float float float ...
	// ["v 1.0 2.0 3.0", "1.0", "2.0", "3.0", undefined]

	pattern = /v( [\-|\d|.]+)( [\-|\d|.]+)( [\-|\d|.]+)( [\-|\d|.]+)?/g;

	while ( ( result = pattern.exec( data ) ) != null ) {

		geometry.vertices.push( vertex( result[ 1 ], result[ 2 ], result[ 3 ] ) );

	}

	// f vertex vertex vertex ...
	// ["f 1 2 3", "1", "2", "3", undefined]
M
Mr.doob 已提交
69

70
	pattern = /f( [\d]+)( [\d]+)( [\d]+)( [\d]+)?/g;
M
Mr.doob 已提交
71

M
Mr.doob 已提交
72
	while ( ( result = pattern.exec( data ) ) != null ) {
M
Mr.doob 已提交
73

74 75 76 77 78
		geometry.faces.push(
			result[ 4 ] === undefined ?
			face3( result[ 1 ], result[ 2 ], result[ 3 ] ) :
			face4( result[ 1 ], result[ 2 ], result[ 3 ], result[ 4 ] )
		);
M
Mr.doob 已提交
79

M
Mr.doob 已提交
80
	}
M
Mr.doob 已提交
81

82 83
	// f vertex/uv vertex/uv vertex/uv ...
	// ["f 1/1 2/2 3/3", " 1/1", "1", "1", " 2/2", "2", "2", " 3/3", "3", "3", undefined, undefined, undefined]
84 85 86 87 88

	pattern = /f ([\d]+)\/([\d]+) ([\d]+)\/([\d]+) ([\d]+)\/([\d]+)/g;

	while ( ( result = pattern.exec( data ) ) != null ) {

89 90 91 92 93
		geometry.faces.push(
			result[ 10 ] === undefined ?
			face3( result[ 2 ], result[ 5 ], result[ 8 ] ) :
			face4( result[ 2 ], result[ 5 ], result[ 8 ], result[ 11 ] )
		);
94 95 96

	}

97 98
	// f vertex/uv/normal vertex/uv/normal vertex/uv/normal ...
	// ["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]
M
Mr.doob 已提交
99

100
	pattern = /f( ([\d]+)\/([\d]+)\/([\d]+))( ([\d]+)\/([\d]+)\/([\d]+))( ([\d]+)\/([\d]+)\/([\d]+))( ([\d]+)\/([\d]+)\/([\d]+))?/g;
M
Mr.doob 已提交
101

M
Mr.doob 已提交
102
	while ( ( result = pattern.exec( data ) ) != null ) {
M
Mr.doob 已提交
103

104 105 106 107 108
		geometry.faces.push(
			result[ 13 ] === undefined ?
			face3( result[ 2 ], result[ 6 ], result[ 10 ] ) :
			face4( result[ 2 ], result[ 6 ], result[ 10 ], result[ 14 ] )
		);
M
Mr.doob 已提交
109 110 111

	}

112 113
	// f vertex//normal vertex//normal vertex//normal ...
	// ["f 1//1 2//2 3//3", " 1//1", "1", "1", " 2//2", "2", "2", " 3//3", "3", "3", undefined, undefined, undefined]
114 115 116 117 118

	pattern = /f ([\d]+)\/\/([\d]+) ([\d]+)\/\/([\d]+) ([\d]+)\/\/([\d]+)/g;

	while ( ( result = pattern.exec( data ) ) != null ) {

119 120 121 122 123
		geometry.faces.push(
			result[ 10 ] === undefined ?
			face3( result[ 2 ], result[ 5 ], result[ 8 ] ) :
			face4( result[ 2 ], result[ 5 ], result[ 8 ], result[ 11 ] )
		);
124 125 126

	}

M
Mr.doob 已提交
127 128 129 130 131
	geometry.computeCentroids();

	callback( geometry );

}