OctreeHelper.js 1.8 KB

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