webgl_math_obb.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - math - OBB</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. body {
  10. color: #444;
  11. }
  12. a {
  13. color: #444;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <div id="container"></div>
  19. <div id="info">
  20. <a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> - OBB (Oriented Bounding Box)
  21. </div>
  22. <script type="importmap">
  23. {
  24. "imports": {
  25. "three": "../build/three.module.js",
  26. "three/addons/": "./jsm/"
  27. }
  28. }
  29. </script>
  30. <script type="module">
  31. import * as THREE from 'three';
  32. import { OBB } from 'three/addons/math/OBB.js';
  33. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  34. import Stats from 'three/addons/libs/stats.module.js';
  35. let camera, scene, renderer, clock, controls, stats, raycaster, hitbox;
  36. const objects = [], mouse = new THREE.Vector2();
  37. init();
  38. function init() {
  39. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  40. camera.position.set( 0, 0, 75 );
  41. scene = new THREE.Scene();
  42. scene.background = new THREE.Color( 0xffffff );
  43. clock = new THREE.Clock();
  44. raycaster = new THREE.Raycaster();
  45. const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x222222, 4 );
  46. hemiLight.position.set( 1, 1, 1 );
  47. scene.add( hemiLight );
  48. const size = new THREE.Vector3( 10, 5, 6 );
  49. const geometry = new THREE.BoxGeometry( size.x, size.y, size.z );
  50. // setup OBB on geometry level (doing this manually for now)
  51. geometry.userData.obb = new OBB();
  52. geometry.userData.obb.halfSize.copy( size ).multiplyScalar( 0.5 );
  53. for ( let i = 0; i < 100; i ++ ) {
  54. const object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: 0x00ff00 } ) );
  55. object.matrixAutoUpdate = false;
  56. object.position.x = Math.random() * 80 - 40;
  57. object.position.y = Math.random() * 80 - 40;
  58. object.position.z = Math.random() * 80 - 40;
  59. object.rotation.x = Math.random() * 2 * Math.PI;
  60. object.rotation.y = Math.random() * 2 * Math.PI;
  61. object.rotation.z = Math.random() * 2 * Math.PI;
  62. object.scale.x = Math.random() + 0.5;
  63. object.scale.y = Math.random() + 0.5;
  64. object.scale.z = Math.random() + 0.5;
  65. scene.add( object );
  66. // bounding volume on object level (this will reflect the current world transform)
  67. object.userData.obb = new OBB();
  68. objects.push( object );
  69. }
  70. //
  71. hitbox = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true } ) );
  72. //
  73. renderer = new THREE.WebGLRenderer( { antialias: true } );
  74. renderer.setPixelRatio( window.devicePixelRatio );
  75. renderer.setSize( window.innerWidth, window.innerHeight );
  76. renderer.setAnimationLoop( animate );
  77. document.body.appendChild( renderer.domElement );
  78. //
  79. controls = new OrbitControls( camera, renderer.domElement );
  80. controls.enableDamping = true;
  81. //
  82. stats = new Stats();
  83. document.body.appendChild( stats.dom );
  84. //
  85. window.addEventListener( 'resize', onWindowResize );
  86. document.addEventListener( 'click', onClick );
  87. }
  88. function onClick( event ) {
  89. event.preventDefault();
  90. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  91. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  92. raycaster.setFromCamera( mouse, camera );
  93. const intersectionPoint = new THREE.Vector3();
  94. const intersections = [];
  95. for ( let i = 0, il = objects.length; i < il; i ++ ) {
  96. const object = objects[ i ];
  97. const obb = object.userData.obb;
  98. const ray = raycaster.ray;
  99. if ( obb.intersectRay( ray, intersectionPoint ) !== null ) {
  100. const distance = ray.origin.distanceTo( intersectionPoint );
  101. intersections.push( { distance: distance, object: object } );
  102. }
  103. }
  104. if ( intersections.length > 0 ) {
  105. // determine closest intersection and highlight the respective 3D object
  106. intersections.sort( sortIntersections );
  107. intersections[ 0 ].object.add( hitbox );
  108. } else {
  109. const parent = hitbox.parent;
  110. if ( parent ) parent.remove( hitbox );
  111. }
  112. }
  113. function sortIntersections( a, b ) {
  114. return a.distance - b.distance;
  115. }
  116. function onWindowResize() {
  117. camera.aspect = window.innerWidth / window.innerHeight;
  118. camera.updateProjectionMatrix();
  119. renderer.setSize( window.innerWidth, window.innerHeight );
  120. }
  121. //
  122. function animate() {
  123. controls.update();
  124. // transform cubes
  125. const delta = clock.getDelta();
  126. for ( let i = 0, il = objects.length; i < il; i ++ ) {
  127. const object = objects[ i ];
  128. object.rotation.x += delta * Math.PI * 0.20;
  129. object.rotation.y += delta * Math.PI * 0.1;
  130. object.updateMatrix();
  131. object.updateMatrixWorld();
  132. // update OBB
  133. object.userData.obb.copy( object.geometry.userData.obb );
  134. object.userData.obb.applyMatrix4( object.matrixWorld );
  135. // reset
  136. object.material.color.setHex( 0x00ff00 );
  137. }
  138. // collision detection
  139. for ( let i = 0, il = objects.length; i < il; i ++ ) {
  140. const object = objects[ i ];
  141. const obb = object.userData.obb;
  142. for ( let j = i + 1, jl = objects.length; j < jl; j ++ ) {
  143. const objectToTest = objects[ j ];
  144. const obbToTest = objectToTest.userData.obb;
  145. // now perform intersection test
  146. if ( obb.intersectsOBB( obbToTest ) === true ) {
  147. object.material.color.setHex( 0xff0000 );
  148. objectToTest.material.color.setHex( 0xff0000 );
  149. }
  150. }
  151. }
  152. renderer.render( scene, camera );
  153. stats.update();
  154. }
  155. </script>
  156. </body>
  157. </html>