RapierHelper.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { LineSegments, LineBasicMaterial, BufferAttribute } from 'three';
  2. /**
  3. * This class displays all Rapier Colliders in outline.
  4. *
  5. * @augments LineSegments
  6. * @three_import import { RapierHelper } from 'three/addons/helpers/RapierHelper.js';
  7. */
  8. class RapierHelper extends LineSegments {
  9. /**
  10. * Constructs a new Rapier debug helper.
  11. *
  12. * @param {RAPIER.world} world - The Rapier world to visualize.
  13. */
  14. constructor( world ) {
  15. super();
  16. /**
  17. * The Rapier world to visualize.
  18. *
  19. * @type {RAPIER.world}
  20. */
  21. this.world = world;
  22. this.material = new LineBasicMaterial( { vertexColors: true } );
  23. this.frustumCulled = false;
  24. }
  25. /**
  26. * Call this in the render loop to update the outlines.
  27. */
  28. update() {
  29. const { vertices, colors } = this.world.debugRender();
  30. this.geometry.deleteAttribute( 'position' );
  31. this.geometry.deleteAttribute( 'color' );
  32. this.geometry.setAttribute( 'position', new BufferAttribute( vertices, 3 ) );
  33. this.geometry.setAttribute( 'color', new BufferAttribute( colors, 4 ) );
  34. }
  35. /**
  36. * Frees the GPU-related resources allocated by this instance. Call this
  37. * method whenever this instance is no longer used in your app.
  38. */
  39. dispose() {
  40. this.geometry.dispose();
  41. this.material.dispose();
  42. }
  43. }
  44. export { RapierHelper };