OctreeHelper.js 1.8 KB

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