webgl_math_obb.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. <!-- Import maps polyfill -->
  23. <!-- Remove this when import maps will be widely supported -->
  24. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  25. <script type="importmap">
  26. {
  27. "imports": {
  28. "three": "../build/three.module.js",
  29. "three/addons/": "./jsm/"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three';
  35. import { OBB } from 'three/addons/math/OBB.js';
  36. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  37. import Stats from 'three/addons/libs/stats.module.js';
  38. let camera, scene, renderer, clock, controls, stats, raycaster, hitbox;
  39. const objects = [], mouse = new THREE.Vector2();
  40. init();
  41. animate();
  42. function init() {
  43. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  44. camera.position.set( 0, 0, 75 );
  45. scene = new THREE.Scene();
  46. scene.background = new THREE.Color( 0xffffff );
  47. clock = new THREE.Clock();
  48. raycaster = new THREE.Raycaster();
  49. const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x222222, 1.5 );
  50. hemiLight.position.set( 1, 1, 1 );
  51. scene.add( hemiLight );
  52. const size = new THREE.Vector3( 10, 5, 6 );
  53. const geometry = new THREE.BoxGeometry( size.x, size.y, size.z );
  54. // setup OBB on geometry level (doing this manually for now)
  55. geometry.userData.obb = new OBB();
  56. geometry.userData.obb.halfSize.copy( size ).multiplyScalar( 0.5 );
  57. for ( let i = 0; i < 100; i ++ ) {
  58. const object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: 0x00ff00 } ) );
  59. object.matrixAutoUpdate = false;
  60. object.position.x = Math.random() * 80 - 40;
  61. object.position.y = Math.random() * 80 - 40;
  62. object.position.z = Math.random() * 80 - 40;
  63. object.rotation.x = Math.random() * 2 * Math.PI;
  64. object.rotation.y = Math.random() * 2 * Math.PI;
  65. object.rotation.z = Math.random() * 2 * Math.PI;
  66. object.scale.x = Math.random() + 0.5;
  67. object.scale.y = Math.random() + 0.5;
  68. object.scale.z = Math.random() + 0.5;
  69. scene.add( object );
  70. // bounding volume on object level (this will reflect the current world transform)
  71. object.userData.obb = new OBB();
  72. objects.push( object );
  73. }
  74. //
  75. hitbox = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true } ) );
  76. //
  77. renderer = new THREE.WebGLRenderer( { antialias: true } );
  78. renderer.setPixelRatio( window.devicePixelRatio );
  79. renderer.setSize( window.innerWidth, window.innerHeight );
  80. document.body.appendChild( renderer.domElement );
  81. //
  82. controls = new OrbitControls( camera, renderer.domElement );
  83. controls.enableDamping = true;
  84. //
  85. stats = new Stats();
  86. document.body.appendChild( stats.dom );
  87. //
  88. window.addEventListener( 'resize', onWindowResize );
  89. document.addEventListener( 'click', onClick );
  90. }
  91. function onClick( event ) {
  92. event.preventDefault();
  93. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  94. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  95. raycaster.setFromCamera( mouse, camera );
  96. const intersectionPoint = new THREE.Vector3();
  97. const intersections = [];
  98. for ( let i = 0, il = objects.length; i < il; i ++ ) {
  99. const object = objects[ i ];
  100. const obb = object.userData.obb;
  101. const ray = raycaster.ray;
  102. if ( obb.intersectRay( ray, intersectionPoint ) !== null ) {
  103. const distance = ray.origin.distanceTo( intersectionPoint );
  104. intersections.push( { distance: distance, object: object } );
  105. }
  106. }
  107. if ( intersections.length > 0 ) {
  108. // determine closest intersection and highlight the respective 3D object
  109. intersections.sort( sortIntersections );
  110. intersections[ 0 ].object.add( hitbox );
  111. } else {
  112. const parent = hitbox.parent;
  113. if ( parent ) parent.remove( hitbox );
  114. }
  115. }
  116. function sortIntersections( a, b ) {
  117. return a.distance - b.distance;
  118. }
  119. function onWindowResize() {
  120. camera.aspect = window.innerWidth / window.innerHeight;
  121. camera.updateProjectionMatrix();
  122. renderer.setSize( window.innerWidth, window.innerHeight );
  123. }
  124. //
  125. function animate() {
  126. requestAnimationFrame( animate );
  127. controls.update();
  128. // transform cubes
  129. const delta = clock.getDelta();
  130. for ( let i = 0, il = objects.length; i < il; i ++ ) {
  131. const object = objects[ i ];
  132. object.rotation.x += delta * Math.PI * 0.20;
  133. object.rotation.y += delta * Math.PI * 0.1;
  134. object.updateMatrix();
  135. object.updateMatrixWorld();
  136. // update OBB
  137. object.userData.obb.copy( object.geometry.userData.obb );
  138. object.userData.obb.applyMatrix4( object.matrixWorld );
  139. // reset
  140. object.material.color.setHex( 0x00ff00 );
  141. }
  142. // collision detection
  143. for ( let i = 0, il = objects.length; i < il; i ++ ) {
  144. const object = objects[ i ];
  145. const obb = object.userData.obb;
  146. for ( let j = i + 1, jl = objects.length; j < jl; j ++ ) {
  147. const objectToTest = objects[ j ];
  148. const obbToTest = objectToTest.userData.obb;
  149. // now perform intersection test
  150. if ( obb.intersectsOBB( obbToTest ) === true ) {
  151. object.material.color.setHex( 0xff0000 );
  152. objectToTest.material.color.setHex( 0xff0000 );
  153. }
  154. }
  155. }
  156. renderer.render( scene, camera );
  157. stats.update();
  158. }
  159. </script>
  160. </body>
  161. </html>