webgl_collisions_primitives.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - intersection: ray with sphere/AABB/plane</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #f0f0f0;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. #oldie {
  15. background-color: #ddd !important
  16. }
  17. #info {
  18. position: absolute;
  19. top: 30px;
  20. left: 150px;
  21. width: 800px;
  22. color: #000000;
  23. padding: 5px;
  24. font-family: Monospace;
  25. font-size: 13px;
  26. text-align: left;
  27. z-index: 100;
  28. }
  29. #options {
  30. position: absolute;
  31. top: 10px;
  32. left: 150px;
  33. width: 800px;
  34. color: #000000;
  35. padding: 5px;
  36. font-family: Monospace;
  37. font-size: 13px;
  38. text-align: left;
  39. z-index: 100;
  40. }
  41. </style>
  42. <script src="../build/Three.js"></script>
  43. <script src="js/RequestAnimationFrame.js"></script>
  44. <script src="js/Stats.js"></script>
  45. <script>
  46. var camera, scene, projector, renderer,
  47. info, mouse = { x: 0, y: 0 }, sun;
  48. var theta = 0;
  49. var camdist = 1500;
  50. var geoms = [];
  51. function init () {
  52. container = document.createElement('div');
  53. document.body.appendChild(container);
  54. info = document.getElementById("info");
  55. camera = new THREE.Camera(40, window.innerWidth / window.innerHeight, 1, 10000);
  56. camera.useTarget = true;
  57. scene = new THREE.Scene();
  58. projector = new THREE.Projector();
  59. renderer = new THREE.WebGLRenderer();
  60. renderer.setSize(window.innerWidth, window.innerHeight);
  61. container.appendChild(renderer.domElement);
  62. var ambientLight = new THREE.AmbientLight(0x606060);
  63. scene.add(ambientLight);
  64. sun = new THREE.DirectionalLight(0xffffff);
  65. scene.add(sun);
  66. makeWall(240);
  67. makeWall(120);
  68. makeWall(0);
  69. makeWall(-120);
  70. makeWall(-240);
  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.add( 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.add(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.add(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. mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
  116. mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
  117. }
  118. function animate(){
  119. requestAnimationFrame( animate );
  120. var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 );
  121. projector.unprojectVector( vector, camera );
  122. var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
  123. for ( var i = 0; i < geoms.length; i++) {
  124. geoms[i].materials[0].color = new THREE.Color(0x007700);
  125. }
  126. if ( !document.getElementById( "nearest" ).checked ) {
  127. // Raycast all
  128. ts = new Date().getTime();
  129. var cs = THREE.Collisions.rayCastAll( ray );
  130. tt = new Date().getTime() - ts;
  131. if ( cs.length > 0 ) {
  132. info.innerHTML = cs.length + " colliders found in " + tt;
  133. for ( var i = 0; i < cs.length; i ++ ) {
  134. cs[ i ].mesh.materials[ 0 ].color.setHex( 0xaa0000 );
  135. }
  136. } else {
  137. info.innerHTML = "No intersection";
  138. }
  139. } else {
  140. // Raycast nearest
  141. ts = new Date().getTime();
  142. var c = THREE.Collisions.rayCastNearest( ray );
  143. tt = new Date().getTime() - ts;
  144. if ( c ) {
  145. info.innerHTML = "Found in " + tt + " @ distance " + c.distance;
  146. c.mesh.materials[ 0 ].color.setHex( 0xaa0000 );
  147. } else {
  148. info.innerHTML = "No intersection";
  149. }
  150. }
  151. camera.position.x = camdist * Math.cos(theta);
  152. camera.position.z = camdist * Math.sin(theta);
  153. camera.position.y = camdist / 2 * Math.sin(theta * 2);
  154. sun.position = camera.position.clone();
  155. sun.position.normalize();
  156. theta += 0.005;
  157. renderer.render( scene, camera );
  158. stats.update();
  159. }
  160. function vts(v){
  161. if (!v)
  162. return "undefined<br>";
  163. else
  164. return v.x + " , " + v.y + " , " + v.z + "<br>";
  165. }
  166. </script>
  167. </head>
  168. <body onload="init();">
  169. <div id="info">
  170. </div>
  171. <div id="options">
  172. <input type="checkbox" id="nearest" checked/> Nearest collider only
  173. <br/>
  174. </div>
  175. </body>
  176. </html>