webgl_collisions_box.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 scene, camera, renderer, info, mouse2d, sun, cube;
  39. var bounce = 0;
  40. function init() {
  41. container = document.createElement( 'div' );
  42. document.body.appendChild( container );
  43. info = document.getElementById("info");
  44. camera = new THREE.Camera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
  45. camera.position.z = -500;
  46. mouse2d = new THREE.Vector3( 0, 0, 1 );
  47. scene = new THREE.Scene();
  48. renderer = new THREE.WebGLRenderer();
  49. renderer.setSize( window.innerWidth, window.innerHeight );
  50. container.appendChild(renderer.domElement);
  51. var ambientLight = new THREE.AmbientLight( 0x606060 );
  52. scene.addLight( ambientLight );
  53. sun = new THREE.DirectionalLight( 0xffffff );
  54. sun.position = camera.position.clone();
  55. scene.addLight( sun );
  56. createCube( 200, new THREE.Vector3( 0,0,0 ) );
  57. stats = new Stats();
  58. stats.domElement.style.position = 'absolute';
  59. stats.domElement.style.top = '0px';
  60. container.appendChild( stats.domElement );
  61. container.onmousemove = onDocumentMouseMove;
  62. animate();
  63. }
  64. function createCube( s, p ) {
  65. cube = new THREE.Mesh (
  66. new THREE.Cube( s,s,s, 1,1,1 ),
  67. new THREE.MeshLambertMaterial( { color: 0x003300 })
  68. );
  69. cube.position = p;
  70. scene.addObject( cube );
  71. THREE.Collisions.colliders.push( THREE.CollisionUtils.MeshOBB( cube ) );
  72. };
  73. function onDocumentMouseMove( event ) {
  74. event.preventDefault();
  75. mouse2d.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  76. mouse2d.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  77. mouse2d.z = 1;
  78. };
  79. function animate() {
  80. requestAnimationFrame( animate );
  81. var r = new THREE.Ray();
  82. r.origin.copy( mouse2d );
  83. var matrix = camera.matrixWorld.clone();
  84. matrix.multiplySelf( THREE.Matrix4.makeInvert( camera.projectionMatrix ) );
  85. matrix.multiplyVector3( r.origin );
  86. r.direction = r.origin.clone().subSelf( camera.position );
  87. info.innerHTML = "";
  88. var c = THREE.Collisions.rayCastNearest( r );
  89. if( c ) {
  90. info.innerHTML += "Found @ distance " + c.distance.toFixed(2);
  91. c.mesh.materials[ 0 ].color.setHex( 0xaa0000 );
  92. } else {
  93. info.innerHTML += "No intersection";
  94. cube.materials[0].color.setHex( 0x003300 );
  95. }
  96. cube.rotation.x += 0.01;
  97. cube.rotation.y += 0.02;
  98. cube.position.x = Math.sin(bounce) * 100;
  99. bounce += 0.01;
  100. renderer.render( scene, camera );
  101. stats.update();
  102. };
  103. function vts(v) {
  104. if(!v) return "undefined<br>";
  105. else return v.x + " , " + v.y + " , " + v.z + "<br>";
  106. };
  107. </script>
  108. </head>
  109. <body onload="init();">
  110. <div id="info"></div>
  111. <div id="options"></div>
  112. </body>
  113. </html>