OctreeHelper.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. ( function () {
  2. class OctreeHelper extends THREE.LineSegments {
  3. constructor( octree, color = 0xffff00 ) {
  4. super( new THREE.BufferGeometry(), new THREE.LineBasicMaterial( {
  5. color: color,
  6. toneMapped: false
  7. } ) );
  8. this.octree = octree;
  9. this.color = color;
  10. this.type = 'OctreeHelper';
  11. this.update();
  12. }
  13. update() {
  14. const vertices = [];
  15. function traverse( tree ) {
  16. for ( let i = 0; i < tree.length; i ++ ) {
  17. const min = tree[ i ].box.min;
  18. const max = tree[ i ].box.max;
  19. vertices.push( max.x, max.y, max.z );
  20. vertices.push( min.x, max.y, max.z ); // 0, 1
  21. vertices.push( min.x, max.y, max.z );
  22. vertices.push( min.x, min.y, max.z ); // 1, 2
  23. vertices.push( min.x, min.y, max.z );
  24. vertices.push( max.x, min.y, max.z ); // 2, 3
  25. vertices.push( max.x, min.y, max.z );
  26. vertices.push( max.x, max.y, max.z ); // 3, 0
  27. vertices.push( max.x, max.y, min.z );
  28. vertices.push( min.x, max.y, min.z ); // 4, 5
  29. vertices.push( min.x, max.y, min.z );
  30. vertices.push( min.x, min.y, min.z ); // 5, 6
  31. vertices.push( min.x, min.y, min.z );
  32. vertices.push( max.x, min.y, min.z ); // 6, 7
  33. vertices.push( max.x, min.y, min.z );
  34. vertices.push( max.x, max.y, min.z ); // 7, 4
  35. vertices.push( max.x, max.y, max.z );
  36. vertices.push( max.x, max.y, min.z ); // 0, 4
  37. vertices.push( min.x, max.y, max.z );
  38. vertices.push( min.x, max.y, min.z ); // 1, 5
  39. vertices.push( min.x, min.y, max.z );
  40. vertices.push( min.x, min.y, min.z ); // 2, 6
  41. vertices.push( max.x, min.y, max.z );
  42. vertices.push( max.x, min.y, min.z ); // 3, 7
  43. traverse( tree[ i ].subTrees );
  44. }
  45. }
  46. traverse( this.octree.subTrees );
  47. this.geometry.dispose();
  48. this.geometry = new THREE.BufferGeometry();
  49. this.geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  50. }
  51. dispose() {
  52. this.geometry.dispose();
  53. this.material.dispose();
  54. }
  55. }
  56. THREE.OctreeHelper = OctreeHelper;
  57. } )();