webgl_collisions_primitives.html 7.7 KB

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