webgl_collisions_primitives.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 scene, camera, renderer, info, mouse2d, sun;
  45. var theta = 0;
  46. var camdist = 1500;
  47. var geoms = [];
  48. function init(){
  49. container = document.createElement('div');
  50. document.body.appendChild(container);
  51. info = document.getElementById("info");
  52. camera = new THREE.Camera(40, window.innerWidth / window.innerHeight, 1, 10000);
  53. mouse2d = new THREE.Vector3(0, 0, 1);
  54. scene = new THREE.Scene();
  55. renderer = new THREE.WebGLRenderer();
  56. renderer.setSize(window.innerWidth, window.innerHeight);
  57. container.appendChild(renderer.domElement);
  58. var ambientLight = new THREE.AmbientLight(0x606060);
  59. scene.addLight(ambientLight);
  60. sun = new THREE.DirectionalLight(0xffffff);
  61. scene.addLight(sun);
  62. //makeWall(480);
  63. //makeWall(360);
  64. makeWall(240);
  65. makeWall(120);
  66. makeWall(0);
  67. makeWall(-120);
  68. makeWall(-240);
  69. //makeWall(-360);
  70. //makeWall(-480);
  71. plane = new THREE.Mesh( new THREE.PlaneGeometry( 30000, 30000, 10, 10 ), new THREE.MeshLambertMaterial( { color: 0x003300 } ) );
  72. plane.position.y = - 480;
  73. plane.rotation.x = Math.PI / - 2;
  74. scene.addObject( plane );
  75. geoms.push( plane );
  76. var cplane = new THREE.PlaneCollider( plane.position, new THREE.Vector3( 0, 1, 0 ) );
  77. cplane.mesh = plane;
  78. THREE.Collisions.colliders.push( cplane );
  79. stats = new Stats();
  80. stats.domElement.style.position = 'absolute';
  81. stats.domElement.style.top = '0px';
  82. container.appendChild( stats.domElement );
  83. container.onmousemove = onDocumentMouseMove;
  84. animate();
  85. }
  86. function makeWall(z){
  87. var mx = 120 * 2;
  88. for (var i = -mx; i <= mx; i += 120) {
  89. for (var j = -mx; j <= mx; j += 120) {
  90. if (Math.random() > 0.5)
  91. createCube(100, new THREE.Vector3(j, i, z));
  92. else
  93. createSphere(50, new THREE.Vector3(j, i, z));
  94. }
  95. }
  96. }
  97. function createCube(s, p){
  98. var cube = new THREE.Mesh(new THREE.CubeGeometry( s, s, s ), new THREE.MeshLambertMaterial( { color: 0x003300 } ) );
  99. cube.position = p;
  100. scene.addObject(cube);
  101. geoms.push(cube);
  102. THREE.Collisions.colliders.push(THREE.CollisionUtils.MeshAABB(cube, p));
  103. }
  104. function createSphere(rad, p){
  105. var sphere = new THREE.Mesh( new THREE.SphereGeometry( rad, 10, 10 ), new THREE.MeshLambertMaterial( { color: 0x003300 } ) );
  106. sphere.position = p;
  107. scene.addObject(sphere);
  108. geoms.push(sphere);
  109. var sc = new THREE.SphereCollider(p, rad);
  110. sc.mesh = sphere;
  111. THREE.Collisions.colliders.push(sc);
  112. }
  113. function onDocumentMouseMove(event){
  114. event.preventDefault();
  115. mouse2d.x = (event.clientX / window.innerWidth) * 2 - 1;
  116. mouse2d.y = -(event.clientY / window.innerHeight) * 2 + 1;
  117. mouse2d.z = 1;
  118. }
  119. function animate(){
  120. requestAnimationFrame(animate);
  121. var r = new THREE.Ray();
  122. r.origin = mouse2d.clone();
  123. var matrix = camera.matrixWorld.clone();
  124. matrix.multiplySelf(THREE.Matrix4.makeInvert(camera.projectionMatrix));
  125. matrix.multiplyVector3(r.origin);
  126. r.direction = r.origin.clone().subSelf(camera.position);
  127. for (var i = 0; i < geoms.length; i++) {
  128. geoms[i].materials[0].color = new THREE.Color(0x007700);
  129. }
  130. if ( !document.getElementById( "nearest" ).checked ) {
  131. // Raycast all
  132. ts = new Date().getTime();
  133. var cs = THREE.Collisions.rayCastAll( r );
  134. tt = new Date().getTime() - ts;
  135. if ( cs.length > 0 ) {
  136. info.innerHTML = cs.length + " colliders found in " + tt;
  137. for ( var i = 0; i < cs.length; i++ ) {
  138. cs[ i ].mesh.materials[ 0 ].color.setHex( 0xaa0000 );
  139. }
  140. } else {
  141. info.innerHTML = "No intersection";
  142. }
  143. } else {
  144. // Raycast nearest
  145. ts = new Date().getTime();
  146. var c = THREE.Collisions.rayCastNearest( r );
  147. tt = new Date().getTime() - ts;
  148. if ( c ) {
  149. info.innerHTML = "Found in " + tt + " @ distance " + c.distance;
  150. c.mesh.materials[ 0 ].color.setHex( 0xaa0000 );
  151. } else {
  152. info.innerHTML = "No intersection";
  153. }
  154. }
  155. camera.position.x = camdist * Math.cos(theta);
  156. camera.position.z = camdist * Math.sin(theta);
  157. camera.position.y = camdist / 2 * Math.sin(theta * 2);
  158. sun.position = camera.position.clone();
  159. sun.position.normalize();
  160. theta += 0.005;
  161. renderer.render( scene, camera );
  162. stats.update();
  163. }
  164. function vts(v){
  165. if (!v)
  166. return "undefined<br>";
  167. else
  168. return v.x + " , " + v.y + " , " + v.z + "<br>";
  169. }
  170. </script>
  171. </head>
  172. <body onload="init();">
  173. <div id="info">
  174. </div>
  175. <div id="options">
  176. <input type="checkbox" id="nearest" checked/> Nearest collider only
  177. <br/>
  178. </div>
  179. </body>
  180. </html>