PLYLoader.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /**
  2. * @author Wei Meng / http://about.me/menway
  3. *
  4. * Description: A THREE loader for PLY ASCII files (known as the Polygon
  5. * File Format or the Stanford Triangle Format).
  6. *
  7. * Limitations: ASCII decoding assumes file is UTF-8.
  8. *
  9. * Usage:
  10. * var loader = new THREE.PLYLoader();
  11. * loader.load('./models/ply/ascii/dolphins.ply', function (geometry) {
  12. *
  13. * scene.add( new THREE.Mesh( geometry ) );
  14. *
  15. * } );
  16. *
  17. * If the PLY file uses non standard property names, they can be mapped while
  18. * loading. For example, the following maps the properties
  19. * “diffuse_(red|green|blue)” in the file to standard color names.
  20. *
  21. * loader.setPropertyNameMapping( {
  22. * diffuse_red: 'red',
  23. * diffuse_green: 'green',
  24. * diffuse_blue: 'blue'
  25. * } );
  26. *
  27. */
  28. THREE.PLYLoader = function ( manager ) {
  29. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  30. this.propertyNameMapping = {};
  31. };
  32. THREE.PLYLoader.prototype = {
  33. constructor: THREE.PLYLoader,
  34. load: function ( url, onLoad, onProgress, onError ) {
  35. var scope = this;
  36. var loader = new THREE.XHRLoader( this.manager );
  37. loader.setResponseType( 'arraybuffer' );
  38. loader.load( url, function ( text ) {
  39. onLoad( scope.parse( text ) );
  40. }, onProgress, onError );
  41. },
  42. setPropertyNameMapping: function ( mapping ) {
  43. this.propertyNameMapping = mapping;
  44. },
  45. parse: function ( data ) {
  46. function isASCII( data ) {
  47. var header = parseHeader( bin2str( data ) );
  48. return header.format === "ascii";
  49. }
  50. function bin2str( buf ) {
  51. var array_buffer = new Uint8Array( buf );
  52. var str = '';
  53. for ( var i = 0; i < buf.byteLength; i ++ ) {
  54. str += String.fromCharCode( array_buffer[ i ] ); // implicitly assumes little-endian
  55. }
  56. return str;
  57. }
  58. function parseHeader( data ) {
  59. var patternHeader = /ply([\s\S]*)end_header\s/;
  60. var headerText = "";
  61. var headerLength = 0;
  62. var result = patternHeader.exec( data );
  63. if ( result !== null ) {
  64. headerText = result [ 1 ];
  65. headerLength = result[ 0 ].length;
  66. }
  67. var header = {
  68. comments: [],
  69. elements: [],
  70. headerLength: headerLength
  71. };
  72. var lines = headerText.split( '\n' );
  73. var currentElement;
  74. var lineType, lineValues;
  75. function make_ply_element_property( propertValues, propertyNameMapping ) {
  76. var property = { type: propertValues[ 0 ] };
  77. if ( property.type === 'list' ) {
  78. property.name = propertValues[ 3 ];
  79. property.countType = propertValues[ 1 ];
  80. property.itemType = propertValues[ 2 ];
  81. } else {
  82. property.name = propertValues[ 1 ];
  83. }
  84. if ( property.name in propertyNameMapping ) {
  85. property.name = propertyNameMapping[ property.name ];
  86. }
  87. return property;
  88. }
  89. for ( var i = 0; i < lines.length; i ++ ) {
  90. var line = lines[ i ];
  91. line = line.trim();
  92. if ( line === "" ) continue;
  93. lineValues = line.split( /\s+/ );
  94. lineType = lineValues.shift();
  95. line = lineValues.join( " " );
  96. switch ( lineType ) {
  97. case "format":
  98. header.format = lineValues[ 0 ];
  99. header.version = lineValues[ 1 ];
  100. break;
  101. case "comment":
  102. header.comments.push( line );
  103. break;
  104. case "element":
  105. if ( currentElement !== undefined ) {
  106. header.elements.push( currentElement );
  107. }
  108. currentElement = {};
  109. currentElement.name = lineValues[ 0 ];
  110. currentElement.count = parseInt( lineValues[ 1 ] );
  111. currentElement.properties = [];
  112. break;
  113. case "property":
  114. currentElement.properties.push( make_ply_element_property( lineValues, scope.propertyNameMapping ) );
  115. break;
  116. default:
  117. console.log( "unhandled", lineType, lineValues );
  118. }
  119. }
  120. if ( currentElement !== undefined ) {
  121. header.elements.push( currentElement );
  122. }
  123. return header;
  124. }
  125. function parseASCIINumber( n, type ) {
  126. switch ( type ) {
  127. case 'char': case 'uchar': case 'short': case 'ushort': case 'int': case 'uint':
  128. case 'int8': case 'uint8': case 'int16': case 'uint16': case 'int32': case 'uint32':
  129. return parseInt( n );
  130. case 'float': case 'double': case 'float32': case 'float64':
  131. return parseFloat( n );
  132. }
  133. }
  134. function parseASCIIElement( properties, line ) {
  135. var values = line.split( /\s+/ );
  136. var element = {};
  137. for ( var i = 0; i < properties.length; i ++ ) {
  138. if ( properties[ i ].type === "list" ) {
  139. var list = [];
  140. var n = parseASCIINumber( values.shift(), properties[ i ].countType );
  141. for ( var j = 0; j < n; j ++ ) {
  142. list.push( parseASCIINumber( values.shift(), properties[ i ].itemType ) );
  143. }
  144. element[ properties[ i ].name ] = list;
  145. } else {
  146. element[ properties[ i ].name ] = parseASCIINumber( values.shift(), properties[ i ].type );
  147. }
  148. }
  149. return element;
  150. }
  151. function parseASCII( data ) {
  152. // PLY ascii format specification, as per http://en.wikipedia.org/wiki/PLY_(file_format)
  153. var geometry = new THREE.Geometry();
  154. var result;
  155. var header = parseHeader( data );
  156. var patternBody = /end_header\s([\s\S]*)$/;
  157. var body = "";
  158. if ( ( result = patternBody.exec( data ) ) !== null ) {
  159. body = result [ 1 ];
  160. }
  161. var lines = body.split( '\n' );
  162. var currentElement = 0;
  163. var currentElementCount = 0;
  164. geometry.useColor = false;
  165. for ( var i = 0; i < lines.length; i ++ ) {
  166. var line = lines[ i ];
  167. line = line.trim();
  168. if ( line === "" ) {
  169. continue;
  170. }
  171. if ( currentElementCount >= header.elements[ currentElement ].count ) {
  172. currentElement ++;
  173. currentElementCount = 0;
  174. }
  175. var element = parseASCIIElement( header.elements[ currentElement ].properties, line );
  176. handleElement( geometry, header.elements[ currentElement ].name, element );
  177. currentElementCount ++;
  178. }
  179. return postProcess( geometry );
  180. }
  181. function postProcess( geometry ) {
  182. if ( geometry.useColor ) {
  183. for ( var i = 0; i < geometry.faces.length; i ++ ) {
  184. geometry.faces[ i ].vertexColors = [
  185. geometry.colors[ geometry.faces[ i ].a ],
  186. geometry.colors[ geometry.faces[ i ].b ],
  187. geometry.colors[ geometry.faces[ i ].c ]
  188. ];
  189. }
  190. geometry.elementsNeedUpdate = true;
  191. }
  192. geometry.computeBoundingSphere();
  193. return geometry;
  194. }
  195. function handleElement( geometry, elementName, element ) {
  196. if ( elementName === "vertex" ) {
  197. geometry.vertices.push(
  198. new THREE.Vector3( element.x, element.y, element.z )
  199. );
  200. if ( 'red' in element && 'green' in element && 'blue' in element ) {
  201. geometry.useColor = true;
  202. var color = new THREE.Color();
  203. color.setRGB( element.red / 255.0, element.green / 255.0, element.blue / 255.0 );
  204. geometry.colors.push( color );
  205. }
  206. } else if ( elementName === "face" ) {
  207. var vertex_indices = element.vertex_indices;
  208. var texcoord = element.texcoord;
  209. if ( vertex_indices.length === 3 ) {
  210. geometry.faces.push(
  211. new THREE.Face3( vertex_indices[ 0 ], vertex_indices[ 1 ], vertex_indices[ 2 ] )
  212. );
  213. if ( texcoord ) {
  214. geometry.faceVertexUvs[ 0 ].push( [
  215. new THREE.Vector2( texcoord[ 0 ], texcoord[ 1 ]),
  216. new THREE.Vector2( texcoord[ 2 ], texcoord[ 3 ]),
  217. new THREE.Vector2( texcoord[ 4 ], texcoord[ 5 ])
  218. ] );
  219. }
  220. } else if ( vertex_indices.length === 4 ) {
  221. geometry.faces.push(
  222. new THREE.Face3( vertex_indices[ 0 ], vertex_indices[ 1 ], vertex_indices[ 3 ] ),
  223. new THREE.Face3( vertex_indices[ 1 ], vertex_indices[ 2 ], vertex_indices[ 3 ] )
  224. );
  225. if ( texcoord ) {
  226. geometry.faceVertexUvs[ 0 ].push( [
  227. new THREE.Vector2( texcoord[ 0 ], texcoord[ 1 ]),
  228. new THREE.Vector2( texcoord[ 2 ], texcoord[ 3 ]),
  229. new THREE.Vector2( texcoord[ 6 ], texcoord[ 7 ])
  230. ], [
  231. new THREE.Vector2( texcoord[ 2 ], texcoord[ 3 ]),
  232. new THREE.Vector2( texcoord[ 4 ], texcoord[ 5 ]),
  233. new THREE.Vector2( texcoord[ 6 ], texcoord[ 7 ])
  234. ] );
  235. }
  236. }
  237. }
  238. }
  239. function binaryRead( dataview, at, type, little_endian ) {
  240. switch ( type ) {
  241. // corespondences for non-specific length types here match rply:
  242. case 'int8': case 'char': return [ dataview.getInt8( at ), 1 ];
  243. case 'uint8': case 'uchar': return [ dataview.getUint8( at ), 1 ];
  244. case 'int16': case 'short': return [ dataview.getInt16( at, little_endian ), 2 ];
  245. case 'uint16': case 'ushort': return [ dataview.getUint16( at, little_endian ), 2 ];
  246. case 'int32': case 'int': return [ dataview.getInt32( at, little_endian ), 4 ];
  247. case 'uint32': case 'uint': return [ dataview.getUint32( at, little_endian ), 4 ];
  248. case 'float32': case 'float': return [ dataview.getFloat32( at, little_endian ), 4 ];
  249. case 'float64': case 'double': return [ dataview.getFloat64( at, little_endian ), 8 ];
  250. }
  251. }
  252. function binaryReadElement( dataview, at, properties, little_endian ) {
  253. var element = {};
  254. var result, read = 0;
  255. for ( var i = 0; i < properties.length; i ++ ) {
  256. if ( properties[ i ].type === "list" ) {
  257. var list = [];
  258. result = binaryRead( dataview, at + read, properties[ i ].countType, little_endian );
  259. var n = result[ 0 ];
  260. read += result[ 1 ];
  261. for ( var j = 0; j < n; j ++ ) {
  262. result = binaryRead( dataview, at + read, properties[ i ].itemType, little_endian );
  263. list.push( result[ 0 ] );
  264. read += result[ 1 ];
  265. }
  266. element[ properties[ i ].name ] = list;
  267. } else {
  268. result = binaryRead( dataview, at + read, properties[ i ].type, little_endian );
  269. element[ properties[ i ].name ] = result[ 0 ];
  270. read += result[ 1 ];
  271. }
  272. }
  273. return [ element, read ];
  274. }
  275. function parseBinary( data ) {
  276. var geometry = new THREE.Geometry();
  277. var header = parseHeader( bin2str( data ) );
  278. var little_endian = ( header.format === "binary_little_endian" );
  279. var body = new DataView( data, header.headerLength );
  280. var result, loc = 0;
  281. for ( var currentElement = 0; currentElement < header.elements.length; currentElement ++ ) {
  282. for ( var currentElementCount = 0; currentElementCount < header.elements[ currentElement ].count; currentElementCount ++ ) {
  283. result = binaryReadElement( body, loc, header.elements[ currentElement ].properties, little_endian );
  284. loc += result[ 1 ];
  285. var element = result[ 0 ];
  286. handleElement( geometry, header.elements[ currentElement ].name, element );
  287. }
  288. }
  289. return postProcess( geometry );
  290. }
  291. //
  292. console.time( 'PLYLoader' );
  293. var geometry;
  294. var scope = this;
  295. if ( data instanceof ArrayBuffer ) {
  296. geometry = isASCII( data ) ? parseASCII( bin2str( data ) ) : parseBinary( data );
  297. } else {
  298. geometry = parseASCII( data );
  299. }
  300. console.timeEnd( 'PLYLoader' );
  301. return geometry;
  302. }
  303. };