webgl_collisions_box.html 3.1 KB

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