123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505 |
- /**
- * @author Wei Meng / http://about.me/menway
- *
- * Description: A THREE loader for PLY ASCII files (known as the Polygon
- * File Format or the Stanford Triangle Format).
- *
- * Limitations: ASCII decoding assumes file is UTF-8.
- *
- * Usage:
- * var loader = new THREE.PLYLoader();
- * loader.load('./models/ply/ascii/dolphins.ply', function (geometry) {
- *
- * scene.add( new THREE.Mesh( geometry ) );
- *
- * } );
- *
- * If the PLY file uses non standard property names, they can be mapped while
- * loading. For example, the following maps the properties
- * “diffuse_(red|green|blue)” in the file to standard color names.
- *
- * loader.setPropertyNameMapping( {
- * diffuse_red: 'red',
- * diffuse_green: 'green',
- * diffuse_blue: 'blue'
- * } );
- *
- */
- THREE.PLYLoader = function ( manager ) {
- this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
- this.propertyNameMapping = {};
- };
- THREE.PLYLoader.prototype = {
- constructor: THREE.PLYLoader,
- load: function ( url, onLoad, onProgress, onError ) {
- var scope = this;
- var loader = new THREE.FileLoader( this.manager );
- loader.setPath( this.path );
- loader.setResponseType( 'arraybuffer' );
- loader.load( url, function ( text ) {
- onLoad( scope.parse( text ) );
- }, onProgress, onError );
- },
- setPath: function ( value ) {
- this.path = value;
- return this;
- },
- setPropertyNameMapping: function ( mapping ) {
- this.propertyNameMapping = mapping;
- },
- parse: function ( data ) {
- function parseHeader( data ) {
- var patternHeader = /ply([\s\S]*)end_header\r?\n/;
- var headerText = '';
- var headerLength = 0;
- var result = patternHeader.exec( data );
- if ( result !== null ) {
- headerText = result[ 1 ];
- headerLength = result[ 0 ].length;
- }
- var header = {
- comments: [],
- elements: [],
- headerLength: headerLength
- };
- var lines = headerText.split( '\n' );
- var currentElement;
- var lineType, lineValues;
- function make_ply_element_property( propertValues, propertyNameMapping ) {
- var property = { type: propertValues[ 0 ] };
- if ( property.type === 'list' ) {
- property.name = propertValues[ 3 ];
- property.countType = propertValues[ 1 ];
- property.itemType = propertValues[ 2 ];
- } else {
- property.name = propertValues[ 1 ];
- }
- if ( property.name in propertyNameMapping ) {
- property.name = propertyNameMapping[ property.name ];
- }
- return property;
- }
- for ( var i = 0; i < lines.length; i ++ ) {
- var line = lines[ i ];
- line = line.trim();
- if ( line === '' ) continue;
- lineValues = line.split( /\s+/ );
- lineType = lineValues.shift();
- line = lineValues.join( ' ' );
- switch ( lineType ) {
- case 'format':
- header.format = lineValues[ 0 ];
- header.version = lineValues[ 1 ];
- break;
- case 'comment':
- header.comments.push( line );
- break;
- case 'element':
- if ( currentElement !== undefined ) {
- header.elements.push( currentElement );
- }
- currentElement = {};
- currentElement.name = lineValues[ 0 ];
- currentElement.count = parseInt( lineValues[ 1 ] );
- currentElement.properties = [];
- break;
- case 'property':
- currentElement.properties.push( make_ply_element_property( lineValues, scope.propertyNameMapping ) );
- break;
- default:
- console.log( 'unhandled', lineType, lineValues );
- }
- }
- if ( currentElement !== undefined ) {
- header.elements.push( currentElement );
- }
- return header;
- }
- function parseASCIINumber( n, type ) {
- switch ( type ) {
- case 'char': case 'uchar': case 'short': case 'ushort': case 'int': case 'uint':
- case 'int8': case 'uint8': case 'int16': case 'uint16': case 'int32': case 'uint32':
- return parseInt( n );
- case 'float': case 'double': case 'float32': case 'float64':
- return parseFloat( n );
- }
- }
- function parseASCIIElement( properties, line ) {
- var values = line.split( /\s+/ );
- var element = {};
- for ( var i = 0; i < properties.length; i ++ ) {
- if ( properties[ i ].type === 'list' ) {
- var list = [];
- var n = parseASCIINumber( values.shift(), properties[ i ].countType );
- for ( var j = 0; j < n; j ++ ) {
- list.push( parseASCIINumber( values.shift(), properties[ i ].itemType ) );
- }
- element[ properties[ i ].name ] = list;
- } else {
- element[ properties[ i ].name ] = parseASCIINumber( values.shift(), properties[ i ].type );
- }
- }
- return element;
- }
- function parseASCII( data, header ) {
- // PLY ascii format specification, as per http://en.wikipedia.org/wiki/PLY_(file_format)
- var buffer = {
- indices: [],
- vertices: [],
- normals: [],
- uvs: [],
- faceVertexUvs: [],
- colors: []
- };
- var result;
- var patternBody = /end_header\s([\s\S]*)$/;
- var body = '';
- if ( ( result = patternBody.exec( data ) ) !== null ) {
- body = result[ 1 ];
- }
- var lines = body.split( '\n' );
- var currentElement = 0;
- var currentElementCount = 0;
- for ( var i = 0; i < lines.length; i ++ ) {
- var line = lines[ i ];
- line = line.trim();
- if ( line === '' ) {
- continue;
- }
- if ( currentElementCount >= header.elements[ currentElement ].count ) {
- currentElement ++;
- currentElementCount = 0;
- }
- var element = parseASCIIElement( header.elements[ currentElement ].properties, line );
- handleElement( buffer, header.elements[ currentElement ].name, element );
- currentElementCount ++;
- }
- return postProcess( buffer );
- }
- function postProcess( buffer ) {
- var geometry = new THREE.BufferGeometry();
- // mandatory buffer data
- if ( buffer.indices.length > 0 ) {
- geometry.setIndex( buffer.indices );
- }
- geometry.addAttribute( 'position', new THREE.Float32BufferAttribute( buffer.vertices, 3 ) );
- // optional buffer data
- if ( buffer.normals.length > 0 ) {
- geometry.addAttribute( 'normal', new THREE.Float32BufferAttribute( buffer.normals, 3 ) );
- }
- if ( buffer.uvs.length > 0 ) {
- geometry.addAttribute( 'uv', new THREE.Float32BufferAttribute( buffer.uvs, 2 ) );
- }
- if ( buffer.colors.length > 0 ) {
- geometry.addAttribute( 'color', new THREE.Float32BufferAttribute( buffer.colors, 3 ) );
- }
- if ( buffer.faceVertexUvs.length > 0 ) {
- geometry = geometry.toNonIndexed();
- geometry.addAttribute( 'uv', new THREE.Float32BufferAttribute( buffer.faceVertexUvs, 2 ) );
- }
- geometry.computeBoundingSphere();
- return geometry;
- }
- function handleElement( buffer, elementName, element ) {
- if ( elementName === 'vertex' ) {
- buffer.vertices.push( element.x, element.y, element.z );
- if ( 'nx' in element && 'ny' in element && 'nz' in element ) {
- buffer.normals.push( element.nx, element.ny, element.nz );
- }
- if ( 's' in element && 't' in element ) {
- buffer.uvs.push( element.s, element.t );
- }
- if ( 'red' in element && 'green' in element && 'blue' in element ) {
- buffer.colors.push( element.red / 255.0, element.green / 255.0, element.blue / 255.0 );
- }
- } else if ( elementName === 'face' ) {
- var vertex_indices = element.vertex_indices || element.vertex_index; // issue #9338
- var texcoord = element.texcoord;
- if ( vertex_indices.length === 3 ) {
- buffer.indices.push( vertex_indices[ 0 ], vertex_indices[ 1 ], vertex_indices[ 2 ] );
- if ( texcoord && texcoord.length === 6 ) {
- buffer.faceVertexUvs.push( texcoord[ 0 ], texcoord[ 1 ] );
- buffer.faceVertexUvs.push( texcoord[ 2 ], texcoord[ 3 ] );
- buffer.faceVertexUvs.push( texcoord[ 4 ], texcoord[ 5 ] );
- }
- } else if ( vertex_indices.length === 4 ) {
- buffer.indices.push( vertex_indices[ 0 ], vertex_indices[ 1 ], vertex_indices[ 3 ] );
- buffer.indices.push( vertex_indices[ 1 ], vertex_indices[ 2 ], vertex_indices[ 3 ] );
- }
- }
- }
- function binaryRead( dataview, at, type, little_endian ) {
- switch ( type ) {
- // corespondences for non-specific length types here match rply:
- case 'int8': case 'char': return [ dataview.getInt8( at ), 1 ];
- case 'uint8': case 'uchar': return [ dataview.getUint8( at ), 1 ];
- case 'int16': case 'short': return [ dataview.getInt16( at, little_endian ), 2 ];
- case 'uint16': case 'ushort': return [ dataview.getUint16( at, little_endian ), 2 ];
- case 'int32': case 'int': return [ dataview.getInt32( at, little_endian ), 4 ];
- case 'uint32': case 'uint': return [ dataview.getUint32( at, little_endian ), 4 ];
- case 'float32': case 'float': return [ dataview.getFloat32( at, little_endian ), 4 ];
- case 'float64': case 'double': return [ dataview.getFloat64( at, little_endian ), 8 ];
- }
- }
- function binaryReadElement( dataview, at, properties, little_endian ) {
- var element = {};
- var result, read = 0;
- for ( var i = 0; i < properties.length; i ++ ) {
- if ( properties[ i ].type === 'list' ) {
- var list = [];
- result = binaryRead( dataview, at + read, properties[ i ].countType, little_endian );
- var n = result[ 0 ];
- read += result[ 1 ];
- for ( var j = 0; j < n; j ++ ) {
- result = binaryRead( dataview, at + read, properties[ i ].itemType, little_endian );
- list.push( result[ 0 ] );
- read += result[ 1 ];
- }
- element[ properties[ i ].name ] = list;
- } else {
- result = binaryRead( dataview, at + read, properties[ i ].type, little_endian );
- element[ properties[ i ].name ] = result[ 0 ];
- read += result[ 1 ];
- }
- }
- return [ element, read ];
- }
- function parseBinary( data, header ) {
- var buffer = {
- indices: [],
- vertices: [],
- normals: [],
- uvs: [],
- faceVertexUvs: [],
- colors: []
- };
- var little_endian = ( header.format === 'binary_little_endian' );
- var body = new DataView( data, header.headerLength );
- var result, loc = 0;
- for ( var currentElement = 0; currentElement < header.elements.length; currentElement ++ ) {
- for ( var currentElementCount = 0; currentElementCount < header.elements[ currentElement ].count; currentElementCount ++ ) {
- result = binaryReadElement( body, loc, header.elements[ currentElement ].properties, little_endian );
- loc += result[ 1 ];
- var element = result[ 0 ];
- handleElement( buffer, header.elements[ currentElement ].name, element );
- }
- }
- return postProcess( buffer );
- }
- //
- var geometry;
- var scope = this;
- if ( data instanceof ArrayBuffer ) {
- var text = THREE.LoaderUtils.decodeText( new Uint8Array( data ) );
- var header = parseHeader( text );
- geometry = header.format === 'ascii' ? parseASCII( text, header ) : parseBinary( data, header );
- } else {
- geometry = parseASCII( data, parseHeader( data ) );
- }
- return geometry;
- }
- };
|