webgl_math_obb.html 6.1 KB

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