webgl_collisions_primitives.html 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <html>
  2. <head>
  3. <title>three.js webgl - intersection: ray with sphere/AABB/plane</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 {
  13. background-color: #ddd !important
  14. }
  15. #info {
  16. position: absolute;
  17. top: 30px;
  18. left: 150px;
  19. width: 800px;
  20. color: #000000;
  21. padding: 5px;
  22. font-family: Monospace;
  23. font-size: 13px;
  24. text-align: left;
  25. z-index: 100;
  26. }
  27. #options {
  28. position: absolute;
  29. top: 10px;
  30. left: 150px;
  31. width: 800px;
  32. color: #000000;
  33. padding: 5px;
  34. font-family: Monospace;
  35. font-size: 13px;
  36. text-align: left;
  37. z-index: 100;
  38. }
  39. </style>
  40. <script type="text/javascript" src="../build/Three.js"></script>
  41. <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
  42. <script type="text/javascript" src="js/Stats.js"></script>
  43. <script type="text/javascript">
  44. var camera, scene, projector, renderer,
  45. info, mouse = { x: 0, y: 0 }, sun;
  46. var theta = 0;
  47. var camdist = 1500;
  48. var geoms = [];
  49. function init () {
  50. container = document.createElement('div');
  51. document.body.appendChild(container);
  52. info = document.getElementById("info");
  53. camera = new THREE.Camera(40, window.innerWidth / window.innerHeight, 1, 10000);
  54. scene = new THREE.Scene();
  55. projector = new THREE.Projector();
  56. renderer = new THREE.WebGLRenderer();
  57. renderer.setSize(window.innerWidth, window.innerHeight);
  58. container.appendChild(renderer.domElement);
  59. var ambientLight = new THREE.AmbientLight(0x606060);
  60. scene.add(ambientLight);
  61. sun = new THREE.DirectionalLight(0xffffff);
  62. scene.add(sun);
  63. //makeWall(480);
  64. //makeWall(360);
  65. makeWall(240);
  66. makeWall(120);
  67. makeWall(0);
  68. makeWall(-120);
  69. makeWall(-240);
  70. //makeWall(-360);
  71. //makeWall(-480);
  72. plane = new THREE.Mesh( new THREE.PlaneGeometry( 30000, 30000, 10, 10 ), new THREE.MeshLambertMaterial( { color: 0x003300 } ) );
  73. plane.position.y = - 480;
  74. plane.rotation.x = Math.PI / - 2;
  75. scene.add( plane );
  76. geoms.push( plane );
  77. var cplane = new THREE.PlaneCollider( plane.position, new THREE.Vector3( 0, 1, 0 ) );
  78. cplane.mesh = plane;
  79. THREE.Collisions.colliders.push( cplane );
  80. stats = new Stats();
  81. stats.domElement.style.position = 'absolute';
  82. stats.domElement.style.top = '0px';
  83. container.appendChild( stats.domElement );
  84. container.onmousemove = onDocumentMouseMove;
  85. animate();
  86. }
  87. function makeWall(z){
  88. var mx = 120 * 2;
  89. for (var i = -mx; i <= mx; i += 120) {
  90. for (var j = -mx; j <= mx; j += 120) {
  91. if (Math.random() > 0.5)
  92. createCube(100, new THREE.Vector3(j, i, z));
  93. else
  94. createSphere(50, new THREE.Vector3(j, i, z));
  95. }
  96. }
  97. }
  98. function createCube(s, p){
  99. var cube = new THREE.Mesh(new THREE.CubeGeometry( s, s, s ), new THREE.MeshLambertMaterial( { color: 0x003300 } ) );
  100. cube.position = p;
  101. scene.add(cube);
  102. geoms.push(cube);
  103. THREE.Collisions.colliders.push(THREE.CollisionUtils.MeshAABB(cube, p));
  104. }
  105. function createSphere(rad, p){
  106. var sphere = new THREE.Mesh( new THREE.SphereGeometry( rad, 10, 10 ), new THREE.MeshLambertMaterial( { color: 0x003300 } ) );
  107. sphere.position = p;
  108. scene.add(sphere);
  109. geoms.push(sphere);
  110. var sc = new THREE.SphereCollider(p, rad);
  111. sc.mesh = sphere;
  112. THREE.Collisions.colliders.push(sc);
  113. }
  114. function onDocumentMouseMove(event){
  115. event.preventDefault();
  116. mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
  117. mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
  118. }
  119. function animate(){
  120. requestAnimationFrame( animate );
  121. var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 );
  122. projector.unprojectVector( vector, camera );
  123. var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
  124. for ( var i = 0; i < geoms.length; i++) {
  125. geoms[i].materials[0].color = new THREE.Color(0x007700);
  126. }
  127. if ( !document.getElementById( "nearest" ).checked ) {
  128. // Raycast all
  129. ts = new Date().getTime();
  130. var cs = THREE.Collisions.rayCastAll( ray );
  131. tt = new Date().getTime() - ts;
  132. if ( cs.length > 0 ) {
  133. info.innerHTML = cs.length + " colliders found in " + tt;
  134. for ( var i = 0; i < cs.length; i ++ ) {
  135. cs[ i ].mesh.materials[ 0 ].color.setHex( 0xaa0000 );
  136. }
  137. } else {
  138. info.innerHTML = "No intersection";
  139. }
  140. } else {
  141. // Raycast nearest
  142. ts = new Date().getTime();
  143. var c = THREE.Collisions.rayCastNearest( ray );
  144. tt = new Date().getTime() - ts;
  145. if ( c ) {
  146. info.innerHTML = "Found in " + tt + " @ distance " + c.distance;
  147. c.mesh.materials[ 0 ].color.setHex( 0xaa0000 );
  148. } else {
  149. info.innerHTML = "No intersection";
  150. }
  151. }
  152. camera.position.x = camdist * Math.cos(theta);
  153. camera.position.z = camdist * Math.sin(theta);
  154. camera.position.y = camdist / 2 * Math.sin(theta * 2);
  155. sun.position = camera.position.clone();
  156. sun.position.normalize();
  157. theta += 0.005;
  158. renderer.render( scene, camera );
  159. stats.update();
  160. }
  161. function vts(v){
  162. if (!v)
  163. return "undefined<br>";
  164. else
  165. return v.x + " , " + v.y + " , " + v.z + "<br>";
  166. }
  167. </script>
  168. </head>
  169. <body onload="init();">
  170. <div id="info">
  171. </div>
  172. <div id="options">
  173. <input type="checkbox" id="nearest" checked/> Nearest collider only
  174. <br/>
  175. </div>
  176. </body>
  177. </html>