OctreeHelper.js 1.9 KB

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