webgl_collisions_box.html 3.3 KB

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