PLYLoader.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /**
  2. * @author Wei Meng / http://about.me/menway
  3. *
  4. * Description: A THREE loader for PLY ASCII files (known as the Polygon File Format or the Stanford Triangle Format).
  5. *
  6. * Currently only supports ASCII encoded files.
  7. *
  8. * Limitations: ASCII decoding assumes file is UTF-8.
  9. *
  10. * Usage:
  11. * var loader = new THREE.PLYLoader();
  12. * loader.addEventListener( 'load', function ( event ) {
  13. *
  14. * var geometry = event.content;
  15. * scene.add( new THREE.Mesh( geometry ) );
  16. *
  17. * } );
  18. * loader.load( './models/ply/ascii/dolphins.ply' );
  19. */
  20. THREE.PLYLoader = function () {};
  21. THREE.PLYLoader.prototype = {
  22. constructor: THREE.PLYLoader,
  23. load: function ( url, callback ) {
  24. var scope = this;
  25. var request = new XMLHttpRequest();
  26. request.addEventListener( 'load', function ( event ) {
  27. var geometry = scope.parse( event.target.response );
  28. scope.dispatchEvent( { type: 'load', content: geometry } );
  29. if ( callback ) callback( geometry );
  30. }, false );
  31. request.addEventListener( 'progress', function ( event ) {
  32. scope.dispatchEvent( { type: 'progress', loaded: event.loaded, total: event.total } );
  33. }, false );
  34. request.addEventListener( 'error', function () {
  35. scope.dispatchEvent( { type: 'error', message: 'Couldn\'t load URL [' + url + ']' } );
  36. }, false );
  37. request.open( 'GET', url, true );
  38. request.responseType = "arraybuffer";
  39. request.send( null );
  40. },
  41. bin2str: function (buf) {
  42. var array_buffer = new Uint8Array(buf);
  43. var str = '';
  44. for(var i = 0; i < buf.byteLength; i++) {
  45. str += String.fromCharCode(array_buffer[i]); // implicitly assumes little-endian
  46. }
  47. return str;
  48. },
  49. isASCII: function(buf){
  50. var header = this.parseHeader( buf );
  51. // currently only supports ASCII encoded files.
  52. return header.format === "ascii";
  53. },
  54. parse: function ( data ) {
  55. if ( data instanceof ArrayBuffer ) {
  56. return this.isASCII( this.bin2str( data ) )
  57. ? this.parseASCII( this.bin2str( data ) )
  58. : this.parseBinary( data );
  59. } else {
  60. return this.parseASCII( data );
  61. }
  62. },
  63. parseHeader: function ( data ) {
  64. var patternHeader = /ply([\s\S]*)end_header/;
  65. var headerText = "";
  66. if ( ( result = patternHeader.exec( data ) ) != null ) {
  67. headerText = result [ 1 ];
  68. }
  69. var header = new Object();
  70. header.comments = [];
  71. header.elements = [];
  72. var lines = headerText.split( '\n' );
  73. var currentElement = undefined;
  74. var lineType, lineValues;
  75. function make_ply_element_property(propertValues) {
  76. var property = Object();
  77. property.type = propertValues[0]
  78. if ( property.type === "list" ) {
  79. property.name = propertValues[3]
  80. property.countType = propertValues[1]
  81. property.itemType = propertValues[2]
  82. } else {
  83. property.name = propertValues[1]
  84. }
  85. return property
  86. }
  87. for ( var i = 0; i < lines.length; i ++ ) {
  88. var line = lines[ i ];
  89. line = line.trim()
  90. if ( line === "" ) { continue; }
  91. lineValues = line.split( /\s+/ );
  92. lineType = lineValues.shift()
  93. line = lineValues.join(" ")
  94. switch( lineType ) {
  95. case "format":
  96. header.format = lineValues[0];
  97. header.version = lineValues[1];
  98. break;
  99. case "comment":
  100. header.comments.push(line);
  101. break;
  102. case "element":
  103. if ( !(currentElement === undefined) ) {
  104. header.elements.push(currentElement);
  105. }
  106. currentElement = Object();
  107. currentElement.name = lineValues[0];
  108. currentElement.count = parseInt( lineValues[1] );
  109. currentElement.properties = [];
  110. break;
  111. case "property":
  112. currentElement.properties.push( make_ply_element_property( lineValues ) );
  113. break;
  114. default:
  115. console.log("unhandled", lineType, lineValues);
  116. }
  117. }
  118. if ( !(currentElement === undefined) ) {
  119. header.elements.push(currentElement);
  120. }
  121. return header;
  122. },
  123. parseASCIINumber: function ( n, type ) {
  124. switch( type ) {
  125. case 'char': case 'uchar': case 'short': case 'ushort': case 'int': case 'uint':
  126. case 'int8': case 'uint8': case 'int16': case 'uint16': case 'int32': case 'uint32':
  127. return parseInt( n );
  128. case 'float': case 'double': case 'float32': case 'float64':
  129. return parseFloat( n );
  130. }
  131. },
  132. parseASCIIElement: function ( properties, line ) {
  133. values = line.split( /\s+/ );
  134. var element = Object();
  135. for ( var i = 0; i < properties.length; i ++ ) {
  136. if ( properties[i].type === "list" ) {
  137. var list = [];
  138. var n = this.parseASCIINumber( values.shift(), properties[i].countType );
  139. for ( j = 0; j < n; j ++ ) {
  140. list.push( this.parseASCIINumber( values.shift(), properties[i].itemType ) );
  141. }
  142. element[ properties[i].name ] = list;
  143. } else {
  144. element[ properties[i].name ] = this.parseASCIINumber( values.shift(), properties[i].type );
  145. }
  146. }
  147. return element;
  148. },
  149. parseASCII: function ( data ) {
  150. // PLY ascii format specification, as per http://en.wikipedia.org/wiki/PLY_(file_format)
  151. var geometry = new THREE.Geometry();
  152. var result;
  153. var header = this.parseHeader( data );
  154. var patternBody = /end_header\n([\s\S]*)$/;
  155. var body = "";
  156. if ( ( result = patternBody.exec( data ) ) != null ) {
  157. body = result [ 1 ];
  158. }
  159. var lines = body.split( '\n' );
  160. var currentElement = 0;
  161. var currentElementCount = 0;
  162. var useColor = false;
  163. for ( var i = 0; i < lines.length; i ++ ) {
  164. var line = lines[ i ];
  165. line = line.trim()
  166. if ( line === "" ) { continue; }
  167. if ( currentElementCount >= header.elements[currentElement].count ) {
  168. currentElement++;
  169. currentElementCount = 0;
  170. }
  171. var element = this.parseASCIIElement( header.elements[currentElement].properties, line );
  172. if ( header.elements[currentElement].name === "vertex" ) {
  173. geometry.vertices.push(
  174. new THREE.Vector3( element.x, element.y, element.z )
  175. );
  176. if ( 'red' in element && 'green' in element && 'blue' in element ) {
  177. useColor = true;
  178. color = new THREE.Color();
  179. color.setRGB( element.red / 255.0, element.green / 255.0, element.blue / 255.0 );
  180. geometry.colors.push( color );
  181. }
  182. } else if ( header.elements[currentElement].name === "face" ) {
  183. geometry.faces.push(
  184. new THREE.Face3( element.vertex_indices[0], element.vertex_indices[1], element.vertex_indices[2] )
  185. );
  186. }
  187. currentElementCount++;
  188. }
  189. if ( useColor ) {
  190. for ( var i = 0; i < geometry.faces.length; i ++ ) {
  191. geometry.faces[i].vertexColors = [
  192. geometry.colors[geometry.faces[i].a],
  193. geometry.colors[geometry.faces[i].b],
  194. geometry.colors[geometry.faces[i].c]
  195. ];
  196. }
  197. geometry.elementsNeedUpdate = true;
  198. }
  199. geometry.computeCentroids();
  200. geometry.computeBoundingSphere();
  201. return geometry;
  202. },
  203. parseBinary: function (buf) {
  204. // not supported yet
  205. console.error('Not supported yet.');
  206. }
  207. };
  208. THREE.EventDispatcher.prototype.apply( THREE.PLYLoader.prototype );