XYZLoader.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import {
  2. BufferGeometry,
  3. Color,
  4. FileLoader,
  5. Float32BufferAttribute,
  6. Loader,
  7. SRGBColorSpace
  8. } from 'three';
  9. /**
  10. * A loader for the XYZ format.
  11. *
  12. * XYZ is a very simple format for storing point clouds. The layouts
  13. * `XYZ` (points) and `XYZRGB` (points + colors) are supported.
  14. *
  15. * ```js
  16. * const loader = new XYZLoader();
  17. * const geometry = await loader.loadAsync( 'models/xyz/helix_201.xyz' );
  18. * geometry.center();
  19. *
  20. * const vertexColors = ( geometry.hasAttribute( 'color' ) === true );
  21. * const material = new THREE.PointsMaterial( { size: 0.1, vertexColors: vertexColors } );
  22. *
  23. * const points = new THREE.Points( geometry, material );
  24. * scene.add( points );
  25. * ```
  26. *
  27. * @augments Loader
  28. */
  29. class XYZLoader extends Loader {
  30. /**
  31. * Starts loading from the given URL and passes the loaded XYZ asset
  32. * to the `onLoad()` callback.
  33. *
  34. * @param {string} url - The path/URL of the file to be loaded. This can also be a data URI.
  35. * @param {function(BufferGeometry)} onLoad - Executed when the loading process has been finished.
  36. * @param {onProgressCallback} onProgress - Executed while the loading is in progress.
  37. * @param {onErrorCallback} onError - Executed when errors occur.
  38. */
  39. load( url, onLoad, onProgress, onError ) {
  40. const scope = this;
  41. const loader = new FileLoader( this.manager );
  42. loader.setPath( this.path );
  43. loader.setRequestHeader( this.requestHeader );
  44. loader.setWithCredentials( this.withCredentials );
  45. loader.load( url, function ( text ) {
  46. try {
  47. onLoad( scope.parse( text ) );
  48. } catch ( e ) {
  49. if ( onError ) {
  50. onError( e );
  51. } else {
  52. console.error( e );
  53. }
  54. scope.manager.itemError( url );
  55. }
  56. }, onProgress, onError );
  57. }
  58. /**
  59. * Parses the given XYZ data and returns the resulting geometry.
  60. *
  61. * @param {string} text - The raw XYZ data as a string.
  62. * @return {BufferGeometry} The geometry representing the point cloud.
  63. */
  64. parse( text ) {
  65. const lines = text.split( '\n' );
  66. const vertices = [];
  67. const colors = [];
  68. const color = new Color();
  69. for ( let line of lines ) {
  70. line = line.trim();
  71. if ( line.charAt( 0 ) === '#' ) continue; // skip comments
  72. const lineValues = line.split( /\s+/ );
  73. if ( lineValues.length === 3 ) {
  74. // XYZ
  75. vertices.push( parseFloat( lineValues[ 0 ] ) );
  76. vertices.push( parseFloat( lineValues[ 1 ] ) );
  77. vertices.push( parseFloat( lineValues[ 2 ] ) );
  78. }
  79. if ( lineValues.length === 6 ) {
  80. // XYZRGB
  81. vertices.push( parseFloat( lineValues[ 0 ] ) );
  82. vertices.push( parseFloat( lineValues[ 1 ] ) );
  83. vertices.push( parseFloat( lineValues[ 2 ] ) );
  84. const r = parseFloat( lineValues[ 3 ] ) / 255;
  85. const g = parseFloat( lineValues[ 4 ] ) / 255;
  86. const b = parseFloat( lineValues[ 5 ] ) / 255;
  87. color.setRGB( r, g, b, SRGBColorSpace );
  88. colors.push( color.r, color.g, color.b );
  89. }
  90. }
  91. const geometry = new BufferGeometry();
  92. geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  93. if ( colors.length > 0 ) {
  94. geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
  95. }
  96. return geometry;
  97. }
  98. }
  99. export { XYZLoader };
粤ICP备19079148号