webgl_collisions_box.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - intersection: ray with box</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. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #f0f0f0;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. #oldie { background-color: #ddd !important }
  15. #info {
  16. position: absolute;
  17. top: 30px; left: 150px; width: 800px;
  18. color: #000000;
  19. padding: 5px;
  20. font-family: Monospace;
  21. font-size: 13px;
  22. text-align: left;
  23. z-index:100;
  24. }
  25. #options {
  26. position: absolute;
  27. top: 10px; left: 10px; width: 800px;
  28. color: #000000;
  29. padding: 5px;
  30. font-family: Monospace;
  31. font-size: 13px;
  32. text-align: left;
  33. z-index:100;
  34. }
  35. </style>
  36. <script src="../build/Three.js"></script>
  37. <script src="js/RequestAnimationFrame.js"></script>
  38. <script src="js/Stats.js"></script>
  39. <script>
  40. var camera, scene, projector, renderer,
  41. info, mouse = { x: 0, y: 0 }, sun, cube;
  42. var bounce = 0;
  43. function init() {
  44. container = document.createElement( 'div' );
  45. document.body.appendChild( container );
  46. info = document.getElementById("info");
  47. camera = new THREE.Camera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
  48. camera.position.z = -500;
  49. scene = new THREE.Scene();
  50. projector = new THREE.Projector();
  51. renderer = new THREE.WebGLRenderer();
  52. renderer.setSize( window.innerWidth, window.innerHeight );
  53. container.appendChild(renderer.domElement);
  54. var ambientLight = new THREE.AmbientLight( 0x606060 );
  55. scene.add( ambientLight );
  56. sun = new THREE.DirectionalLight( 0xffffff );
  57. sun.position = camera.position.clone();
  58. scene.add( sun );
  59. createCube( 200, new THREE.Vector3( 0,0,0 ) );
  60. stats = new Stats();
  61. stats.domElement.style.position = 'absolute';
  62. stats.domElement.style.top = '0px';
  63. container.appendChild( stats.domElement );
  64. container.onmousemove = onDocumentMouseMove;
  65. animate();
  66. }
  67. function createCube( s, p ) {
  68. cube = new THREE.Mesh (
  69. new THREE.CubeGeometry( s, s, s ),
  70. new THREE.MeshLambertMaterial( { color: 0x003300 } )
  71. );
  72. cube.position = p;
  73. scene.add( cube );
  74. THREE.Collisions.colliders.push( THREE.CollisionUtils.MeshOBB( cube ) );
  75. };
  76. function onDocumentMouseMove( event ) {
  77. event.preventDefault();
  78. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  79. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  80. };
  81. function animate() {
  82. requestAnimationFrame( animate );
  83. info.innerHTML = "";
  84. var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 );
  85. projector.unprojectVector( vector, camera );
  86. var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
  87. var c = THREE.Collisions.rayCastNearest( ray );
  88. if ( c ) {
  89. info.innerHTML += "Found @ distance " + c.distance.toFixed(2);
  90. c.mesh.materials[ 0 ].color.setHex( 0xaa0000 );
  91. } else {
  92. info.innerHTML += "No intersection";
  93. cube.materials[0].color.setHex( 0x003300 );
  94. }
  95. cube.rotation.x += 0.01;
  96. cube.rotation.y += 0.02;
  97. cube.position.x = Math.sin(bounce) * 100;
  98. bounce += 0.01;
  99. renderer.render( scene, camera );
  100. stats.update();
  101. };
  102. function vts(v) {
  103. if(!v) return "undefined<br>";
  104. else return v.x + " , " + v.y + " , " + v.z + "<br>";
  105. };
  106. </script>
  107. </head>
  108. <body onload="init();">
  109. <div id="info"></div>
  110. <div id="options"></div>
  111. </body>
  112. </html>