webgl_collisions_primitives.html 7.9 KB

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