XYZLoader.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. * @three_import import { XYZLoader } from 'three/addons/loaders/XYZLoader.js';
  29. */
  30. class XYZLoader extends Loader {
  31. /**
  32. * Starts loading from the given URL and passes the loaded XYZ asset
  33. * to the `onLoad()` callback.
  34. *
  35. * @param {string} url - The path/URL of the file to be loaded. This can also be a data URI.
  36. * @param {function(BufferGeometry)} onLoad - Executed when the loading process has been finished.
  37. * @param {onProgressCallback} onProgress - Executed while the loading is in progress.
  38. * @param {onErrorCallback} onError - Executed when errors occur.
  39. */
  40. load( url, onLoad, onProgress, onError ) {
  41. const scope = this;
  42. const loader = new FileLoader( this.manager );
  43. loader.setPath( this.path );
  44. loader.setRequestHeader( this.requestHeader );
  45. loader.setWithCredentials( this.withCredentials );
  46. loader.load( url, function ( text ) {
  47. try {
  48. onLoad( scope.parse( text ) );
  49. } catch ( e ) {
  50. if ( onError ) {
  51. onError( e );
  52. } else {
  53. console.error( e );
  54. }
  55. scope.manager.itemError( url );
  56. }
  57. }, onProgress, onError );
  58. }
  59. /**
  60. * Parses the given XYZ data and returns the resulting geometry.
  61. *
  62. * @param {string} text - The raw XYZ data as a string.
  63. * @return {BufferGeometry} The geometry representing the point cloud.
  64. */
  65. parse( text ) {
  66. const lines = text.split( '\n' );
  67. const vertices = [];
  68. const colors = [];
  69. const color = new Color();
  70. for ( let line of lines ) {
  71. line = line.trim();
  72. if ( line.charAt( 0 ) === '#' ) continue; // skip comments
  73. const lineValues = line.split( /\s+/ );
  74. if ( lineValues.length === 3 ) {
  75. // XYZ
  76. vertices.push( parseFloat( lineValues[ 0 ] ) );
  77. vertices.push( parseFloat( lineValues[ 1 ] ) );
  78. vertices.push( parseFloat( lineValues[ 2 ] ) );
  79. }
  80. if ( lineValues.length === 6 ) {
  81. // XYZRGB
  82. vertices.push( parseFloat( lineValues[ 0 ] ) );
  83. vertices.push( parseFloat( lineValues[ 1 ] ) );
  84. vertices.push( parseFloat( lineValues[ 2 ] ) );
  85. const r = parseFloat( lineValues[ 3 ] ) / 255;
  86. const g = parseFloat( lineValues[ 4 ] ) / 255;
  87. const b = parseFloat( lineValues[ 5 ] ) / 255;
  88. color.setRGB( r, g, b, SRGBColorSpace );
  89. colors.push( color.r, color.g, color.b );
  90. }
  91. }
  92. const geometry = new BufferGeometry();
  93. geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  94. if ( colors.length > 0 ) {
  95. geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
  96. }
  97. return geometry;
  98. }
  99. }
  100. export { XYZLoader };