PLYLoader.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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.FileLoader( this.manager );
  37. loader.setPath( this.path );
  38. loader.setResponseType( 'arraybuffer' );
  39. loader.load( url, function ( text ) {
  40. onLoad( scope.parse( text ) );
  41. }, onProgress, onError );
  42. },
  43. setPath: function ( value ) {
  44. this.path = value;
  45. return this;
  46. },
  47. setPropertyNameMapping: function ( mapping ) {
  48. this.propertyNameMapping = mapping;
  49. },
  50. parse: function ( data ) {
  51. function parseHeader( data ) {
  52. var patternHeader = /ply([\s\S]*)end_header\r?\n/;
  53. var headerText = '';
  54. var headerLength = 0;
  55. var result = patternHeader.exec( data );
  56. if ( result !== null ) {
  57. headerText = result[ 1 ];
  58. headerLength = result[ 0 ].length;
  59. }
  60. var header = {
  61. comments: [],
  62. elements: [],
  63. headerLength: headerLength
  64. };
  65. var lines = headerText.split( '\n' );
  66. var currentElement;
  67. var lineType, lineValues;
  68. function make_ply_element_property( propertValues, propertyNameMapping ) {
  69. var property = { type: propertValues[ 0 ] };
  70. if ( property.type === 'list' ) {
  71. property.name = propertValues[ 3 ];
  72. property.countType = propertValues[ 1 ];
  73. property.itemType = propertValues[ 2 ];
  74. } else {
  75. property.name = propertValues[ 1 ];
  76. }
  77. if ( property.name in propertyNameMapping ) {
  78. property.name = propertyNameMapping[ property.name ];
  79. }
  80. return property;
  81. }
  82. for ( var i = 0; i < lines.length; i ++ ) {
  83. var line = lines[ i ];
  84. line = line.trim();
  85. if ( line === '' ) continue;
  86. lineValues = line.split( /\s+/ );
  87. lineType = lineValues.shift();
  88. line = lineValues.join( ' ' );
  89. switch ( lineType ) {
  90. case 'format':
  91. header.format = lineValues[ 0 ];
  92. header.version = lineValues[ 1 ];
  93. break;
  94. case 'comment':
  95. header.comments.push( line );
  96. break;
  97. case 'element':
  98. if ( currentElement !== undefined ) {
  99. header.elements.push( currentElement );
  100. }
  101. currentElement = {};
  102. currentElement.name = lineValues[ 0 ];
  103. currentElement.count = parseInt( lineValues[ 1 ] );
  104. currentElement.properties = [];
  105. break;
  106. case 'property':
  107. currentElement.properties.push( make_ply_element_property( lineValues, scope.propertyNameMapping ) );
  108. break;
  109. default:
  110. console.log( 'unhandled', lineType, lineValues );
  111. }
  112. }
  113. if ( currentElement !== undefined ) {
  114. header.elements.push( currentElement );
  115. }
  116. return header;
  117. }
  118. function parseASCIINumber( n, type ) {
  119. switch ( type ) {
  120. case 'char': case 'uchar': case 'short': case 'ushort': case 'int': case 'uint':
  121. case 'int8': case 'uint8': case 'int16': case 'uint16': case 'int32': case 'uint32':
  122. return parseInt( n );
  123. case 'float': case 'double': case 'float32': case 'float64':
  124. return parseFloat( n );
  125. }
  126. }
  127. function parseASCIIElement( properties, line ) {
  128. var values = line.split( /\s+/ );
  129. var element = {};
  130. for ( var i = 0; i < properties.length; i ++ ) {
  131. if ( properties[ i ].type === 'list' ) {
  132. var list = [];
  133. var n = parseASCIINumber( values.shift(), properties[ i ].countType );
  134. for ( var j = 0; j < n; j ++ ) {
  135. list.push( parseASCIINumber( values.shift(), properties[ i ].itemType ) );
  136. }
  137. element[ properties[ i ].name ] = list;
  138. } else {
  139. element[ properties[ i ].name ] = parseASCIINumber( values.shift(), properties[ i ].type );
  140. }
  141. }
  142. return element;
  143. }
  144. function parseASCII( data, header ) {
  145. // PLY ascii format specification, as per http://en.wikipedia.org/wiki/PLY_(file_format)
  146. var buffer = {
  147. indices: [],
  148. vertices: [],
  149. normals: [],
  150. uvs: [],
  151. faceVertexUvs: [],
  152. colors: []
  153. };
  154. var result;
  155. var patternBody = /end_header\s([\s\S]*)$/;
  156. var body = '';
  157. if ( ( result = patternBody.exec( data ) ) !== null ) {
  158. body = result[ 1 ];
  159. }
  160. var lines = body.split( '\n' );
  161. var currentElement = 0;
  162. var currentElementCount = 0;
  163. for ( var i = 0; i < lines.length; i ++ ) {
  164. var line = lines[ i ];
  165. line = line.trim();
  166. if ( line === '' ) {
  167. continue;
  168. }
  169. if ( currentElementCount >= header.elements[ currentElement ].count ) {
  170. currentElement ++;
  171. currentElementCount = 0;
  172. }
  173. var element = parseASCIIElement( header.elements[ currentElement ].properties, line );
  174. handleElement( buffer, header.elements[ currentElement ].name, element );
  175. currentElementCount ++;
  176. }
  177. return postProcess( buffer );
  178. }
  179. function postProcess( buffer ) {
  180. var geometry = new THREE.BufferGeometry();
  181. // mandatory buffer data
  182. if ( buffer.indices.length > 0 ) {
  183. geometry.setIndex( buffer.indices );
  184. }
  185. geometry.addAttribute( 'position', new THREE.Float32BufferAttribute( buffer.vertices, 3 ) );
  186. // optional buffer data
  187. if ( buffer.normals.length > 0 ) {
  188. geometry.addAttribute( 'normal', new THREE.Float32BufferAttribute( buffer.normals, 3 ) );
  189. }
  190. if ( buffer.uvs.length > 0 ) {
  191. geometry.addAttribute( 'uv', new THREE.Float32BufferAttribute( buffer.uvs, 2 ) );
  192. }
  193. if ( buffer.colors.length > 0 ) {
  194. geometry.addAttribute( 'color', new THREE.Float32BufferAttribute( buffer.colors, 3 ) );
  195. }
  196. if ( buffer.faceVertexUvs.length > 0 ) {
  197. geometry = geometry.toNonIndexed();
  198. geometry.addAttribute( 'uv', new THREE.Float32BufferAttribute( buffer.faceVertexUvs, 2 ) );
  199. }
  200. geometry.computeBoundingSphere();
  201. return geometry;
  202. }
  203. function handleElement( buffer, elementName, element ) {
  204. if ( elementName === 'vertex' ) {
  205. buffer.vertices.push( element.x, element.y, element.z );
  206. if ( 'nx' in element && 'ny' in element && 'nz' in element ) {
  207. buffer.normals.push( element.nx, element.ny, element.nz );
  208. }
  209. if ( 's' in element && 't' in element ) {
  210. buffer.uvs.push( element.s, element.t );
  211. }
  212. if ( 'red' in element && 'green' in element && 'blue' in element ) {
  213. buffer.colors.push( element.red / 255.0, element.green / 255.0, element.blue / 255.0 );
  214. }
  215. } else if ( elementName === 'face' ) {
  216. var vertex_indices = element.vertex_indices || element.vertex_index; // issue #9338
  217. var texcoord = element.texcoord;
  218. if ( vertex_indices.length === 3 ) {
  219. buffer.indices.push( vertex_indices[ 0 ], vertex_indices[ 1 ], vertex_indices[ 2 ] );
  220. if ( texcoord && texcoord.length === 6 ) {
  221. buffer.faceVertexUvs.push( texcoord[ 0 ], texcoord[ 1 ] );
  222. buffer.faceVertexUvs.push( texcoord[ 2 ], texcoord[ 3 ] );
  223. buffer.faceVertexUvs.push( texcoord[ 4 ], texcoord[ 5 ] );
  224. }
  225. } else if ( vertex_indices.length === 4 ) {
  226. buffer.indices.push( vertex_indices[ 0 ], vertex_indices[ 1 ], vertex_indices[ 3 ] );
  227. buffer.indices.push( vertex_indices[ 1 ], vertex_indices[ 2 ], vertex_indices[ 3 ] );
  228. }
  229. }
  230. }
  231. function binaryRead( dataview, at, type, little_endian ) {
  232. switch ( type ) {
  233. // corespondences for non-specific length types here match rply:
  234. case 'int8': case 'char': return [ dataview.getInt8( at ), 1 ];
  235. case 'uint8': case 'uchar': return [ dataview.getUint8( at ), 1 ];
  236. case 'int16': case 'short': return [ dataview.getInt16( at, little_endian ), 2 ];
  237. case 'uint16': case 'ushort': return [ dataview.getUint16( at, little_endian ), 2 ];
  238. case 'int32': case 'int': return [ dataview.getInt32( at, little_endian ), 4 ];
  239. case 'uint32': case 'uint': return [ dataview.getUint32( at, little_endian ), 4 ];
  240. case 'float32': case 'float': return [ dataview.getFloat32( at, little_endian ), 4 ];
  241. case 'float64': case 'double': return [ dataview.getFloat64( at, little_endian ), 8 ];
  242. }
  243. }
  244. function binaryReadElement( dataview, at, properties, little_endian ) {
  245. var element = {};
  246. var result, read = 0;
  247. for ( var i = 0; i < properties.length; i ++ ) {
  248. if ( properties[ i ].type === 'list' ) {
  249. var list = [];
  250. result = binaryRead( dataview, at + read, properties[ i ].countType, little_endian );
  251. var n = result[ 0 ];
  252. read += result[ 1 ];
  253. for ( var j = 0; j < n; j ++ ) {
  254. result = binaryRead( dataview, at + read, properties[ i ].itemType, little_endian );
  255. list.push( result[ 0 ] );
  256. read += result[ 1 ];
  257. }
  258. element[ properties[ i ].name ] = list;
  259. } else {
  260. result = binaryRead( dataview, at + read, properties[ i ].type, little_endian );
  261. element[ properties[ i ].name ] = result[ 0 ];
  262. read += result[ 1 ];
  263. }
  264. }
  265. return [ element, read ];
  266. }
  267. function parseBinary( data, header ) {
  268. var buffer = {
  269. indices: [],
  270. vertices: [],
  271. normals: [],
  272. uvs: [],
  273. faceVertexUvs: [],
  274. colors: []
  275. };
  276. var little_endian = ( header.format === 'binary_little_endian' );
  277. var body = new DataView( data, header.headerLength );
  278. var result, loc = 0;
  279. for ( var currentElement = 0; currentElement < header.elements.length; currentElement ++ ) {
  280. for ( var currentElementCount = 0; currentElementCount < header.elements[ currentElement ].count; currentElementCount ++ ) {
  281. result = binaryReadElement( body, loc, header.elements[ currentElement ].properties, little_endian );
  282. loc += result[ 1 ];
  283. var element = result[ 0 ];
  284. handleElement( buffer, header.elements[ currentElement ].name, element );
  285. }
  286. }
  287. return postProcess( buffer );
  288. }
  289. //
  290. var geometry;
  291. var scope = this;
  292. if ( data instanceof ArrayBuffer ) {
  293. var text = THREE.LoaderUtils.decodeText( new Uint8Array( data ) );
  294. var header = parseHeader( text );
  295. geometry = header.format === 'ascii' ? parseASCII( text, header ) : parseBinary( data, header );
  296. } else {
  297. geometry = parseASCII( data, parseHeader( data ) );
  298. }
  299. return geometry;
  300. }
  301. };