OctreeHelper.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import {
  2. LineSegments,
  3. BufferGeometry,
  4. Float32BufferAttribute,
  5. LineBasicMaterial
  6. } from 'three';
  7. /**
  8. * A helper for visualizing an Octree.
  9. *
  10. * ```js
  11. * const helper = new OctreeHelper( octree );
  12. * scene.add( helper );
  13. * ```
  14. *
  15. * @augments LineSegments
  16. * @three_import import { OctreeHelper } from 'three/addons/helpers/OctreeHelper.js';
  17. */
  18. class OctreeHelper extends LineSegments {
  19. /**
  20. * Constructs a new Octree helper.
  21. *
  22. * @param {Octree} octree - The octree to visualize.
  23. * @param {number|Color|string} [color=0xffff00] - The helper's color.
  24. */
  25. constructor( octree, color = 0xffff00 ) {
  26. super( new BufferGeometry(), new LineBasicMaterial( { color: color, toneMapped: false } ) );
  27. /**
  28. * The octree to visualize.
  29. *
  30. * @type {Octree}
  31. */
  32. this.octree = octree;
  33. /**
  34. * The helper's color.
  35. *
  36. * @type {number|Color|string}
  37. */
  38. this.color = color;
  39. this.type = 'OctreeHelper';
  40. this.update();
  41. }
  42. /**
  43. * Updates the helper. This method must be called whenever the Octree's
  44. * structure is changed.
  45. */
  46. update() {
  47. const vertices = [];
  48. function traverse( tree ) {
  49. for ( let i = 0; i < tree.length; i ++ ) {
  50. const min = tree[ i ].box.min;
  51. const max = tree[ i ].box.max;
  52. vertices.push( max.x, max.y, max.z ); vertices.push( min.x, max.y, max.z ); // 0, 1
  53. vertices.push( min.x, max.y, max.z ); vertices.push( min.x, min.y, max.z ); // 1, 2
  54. vertices.push( min.x, min.y, max.z ); vertices.push( max.x, min.y, max.z ); // 2, 3
  55. vertices.push( max.x, min.y, max.z ); vertices.push( max.x, max.y, max.z ); // 3, 0
  56. vertices.push( max.x, max.y, min.z ); vertices.push( min.x, max.y, min.z ); // 4, 5
  57. vertices.push( min.x, max.y, min.z ); vertices.push( min.x, min.y, min.z ); // 5, 6
  58. vertices.push( min.x, min.y, min.z ); vertices.push( max.x, min.y, min.z ); // 6, 7
  59. vertices.push( max.x, min.y, min.z ); vertices.push( max.x, max.y, min.z ); // 7, 4
  60. vertices.push( max.x, max.y, max.z ); vertices.push( max.x, max.y, min.z ); // 0, 4
  61. vertices.push( min.x, max.y, max.z ); vertices.push( min.x, max.y, min.z ); // 1, 5
  62. vertices.push( min.x, min.y, max.z ); vertices.push( min.x, min.y, min.z ); // 2, 6
  63. vertices.push( max.x, min.y, max.z ); vertices.push( max.x, min.y, min.z ); // 3, 7
  64. traverse( tree[ i ].subTrees );
  65. }
  66. }
  67. traverse( this.octree.subTrees );
  68. this.geometry.dispose();
  69. this.geometry = new BufferGeometry();
  70. this.geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  71. }
  72. /**
  73. * Frees the GPU-related resources allocated by this instance. Call this
  74. * method whenever this instance is no longer used in your app.
  75. */
  76. dispose() {
  77. this.geometry.dispose();
  78. this.material.dispose();
  79. }
  80. }
  81. export { OctreeHelper };