123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - math - OBB</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- <style>
- body {
- color: #444;
- }
- a {
- color: #444;
- }
- </style>
- </head>
- <body>
- <div id="container"></div>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> - OBB (Oriented Bounding Box)
- </div>
- <script type="importmap">
- {
- "imports": {
- "three": "../build/three.module.js",
- "three/addons/": "./jsm/"
- }
- }
- </script>
- <script type="module">
- import * as THREE from 'three';
- import { OBB } from 'three/addons/math/OBB.js';
- import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
- import Stats from 'three/addons/libs/stats.module.js';
- let camera, scene, renderer, clock, controls, stats, raycaster, hitbox;
- const objects = [], mouse = new THREE.Vector2();
- init();
- function init() {
- camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
- camera.position.set( 0, 0, 75 );
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0xffffff );
- clock = new THREE.Clock();
- raycaster = new THREE.Raycaster();
- const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x222222, 4 );
- hemiLight.position.set( 1, 1, 1 );
- scene.add( hemiLight );
- const size = new THREE.Vector3( 10, 5, 6 );
- const geometry = new THREE.BoxGeometry( size.x, size.y, size.z );
- // setup OBB on geometry level (doing this manually for now)
- geometry.userData.obb = new OBB();
- geometry.userData.obb.halfSize.copy( size ).multiplyScalar( 0.5 );
- for ( let i = 0; i < 100; i ++ ) {
- const object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: 0x00ff00 } ) );
- object.matrixAutoUpdate = false;
- object.position.x = Math.random() * 80 - 40;
- object.position.y = Math.random() * 80 - 40;
- object.position.z = Math.random() * 80 - 40;
- object.rotation.x = Math.random() * 2 * Math.PI;
- object.rotation.y = Math.random() * 2 * Math.PI;
- object.rotation.z = Math.random() * 2 * Math.PI;
- object.scale.x = Math.random() + 0.5;
- object.scale.y = Math.random() + 0.5;
- object.scale.z = Math.random() + 0.5;
- scene.add( object );
- // bounding volume on object level (this will reflect the current world transform)
- object.userData.obb = new OBB();
- objects.push( object );
- }
- //
- hitbox = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true } ) );
- //
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.setAnimationLoop( animate );
- document.body.appendChild( renderer.domElement );
- //
- controls = new OrbitControls( camera, renderer.domElement );
- controls.enableDamping = true;
- //
- stats = new Stats();
- document.body.appendChild( stats.dom );
- //
- window.addEventListener( 'resize', onWindowResize );
- document.addEventListener( 'click', onClick );
- }
- function onClick( event ) {
- event.preventDefault();
- mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
- mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
- raycaster.setFromCamera( mouse, camera );
- const intersectionPoint = new THREE.Vector3();
- const intersections = [];
- for ( let i = 0, il = objects.length; i < il; i ++ ) {
- const object = objects[ i ];
- const obb = object.userData.obb;
- const ray = raycaster.ray;
- if ( obb.intersectRay( ray, intersectionPoint ) !== null ) {
- const distance = ray.origin.distanceTo( intersectionPoint );
- intersections.push( { distance: distance, object: object } );
- }
- }
- if ( intersections.length > 0 ) {
- // determine closest intersection and highlight the respective 3D object
- intersections.sort( sortIntersections );
- intersections[ 0 ].object.add( hitbox );
- } else {
- const parent = hitbox.parent;
- if ( parent ) parent.remove( hitbox );
- }
- }
- function sortIntersections( a, b ) {
- return a.distance - b.distance;
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- //
- function animate() {
- controls.update();
- // transform cubes
- const delta = clock.getDelta();
- for ( let i = 0, il = objects.length; i < il; i ++ ) {
- const object = objects[ i ];
- object.rotation.x += delta * Math.PI * 0.20;
- object.rotation.y += delta * Math.PI * 0.1;
- object.updateMatrix();
- object.updateMatrixWorld();
- // update OBB
- object.userData.obb.copy( object.geometry.userData.obb );
- object.userData.obb.applyMatrix4( object.matrixWorld );
- // reset
- object.material.color.setHex( 0x00ff00 );
- }
- // collision detection
- for ( let i = 0, il = objects.length; i < il; i ++ ) {
- const object = objects[ i ];
- const obb = object.userData.obb;
- for ( let j = i + 1, jl = objects.length; j < jl; j ++ ) {
- const objectToTest = objects[ j ];
- const obbToTest = objectToTest.userData.obb;
- // now perform intersection test
- if ( obb.intersectsOBB( obbToTest ) === true ) {
- object.material.color.setHex( 0xff0000 );
- objectToTest.material.color.setHex( 0xff0000 );
- }
- }
- }
- renderer.render( scene, camera );
- stats.update();
- }
- </script>
- </body>
- </html>
|