OctreeHelper.js 2.7 KB

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