webgl_collisions_trigger.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <html>
  2. <head>
  3. <title>three.js webgl - collision detection</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, loader, sphere;
  46. var range = 400;
  47. var speed = 1;
  48. var sphereSize = 4;
  49. var cubes = [];
  50. function init(){
  51. container = document.createElement('div');
  52. document.body.appendChild(container);
  53. info = document.getElementById("info");
  54. camera = new THREE.Camera(40, window.innerWidth / window.innerHeight, 1, 10000);
  55. camera.position.y = 120;
  56. camera.position.x = 300;
  57. camera.position.z = 0;
  58. mouse2d = new THREE.Vector3(0, 0, 1);
  59. loader = new THREE.Loader(true);
  60. scene = new THREE.Scene();
  61. sphere = new THREE.Mesh(new THREE.Sphere(sphereSize, 10, 10), new THREE.MeshLambertMaterial({
  62. color: 0xff0000
  63. }));
  64. scene.addObject(sphere);
  65. renderer = new THREE.WebGLRenderer();
  66. renderer.setSize(window.innerWidth, window.innerHeight);
  67. container.appendChild(renderer.domElement);
  68. var ambientLight = new THREE.AmbientLight(0xdddddd);
  69. scene.addLight(ambientLight);
  70. sun = new THREE.DirectionalLight(0xffffff);
  71. sun.position = new THREE.Vector3(1, -1, 1).normalize();
  72. scene.addLight(sun);
  73. createObstacles();
  74. container.onmousemove = onDocumentMouseMove;
  75. animate();
  76. }
  77. function createObstacles(){
  78. createCube(100, 50, 10, new THREE.Vector3(0, 0, 100));
  79. camera.target = createCube(100, 50, 10, new THREE.Vector3(0, 0, 200));
  80. createCube(100, 50, 10, new THREE.Vector3(0, 0, 300));
  81. }
  82. function createCube(sx, sy, sz, p){
  83. var cube = new THREE.Mesh(new THREE.Cube(sx, sy, sz, 1, 1, 1), new THREE.MeshLambertMaterial({
  84. color: 0x003300//, wireframe: true
  85. }));
  86. cube.position = p;
  87. scene.addObject(cube);
  88. THREE.Collisions.colliders.push( THREE.CollisionUtils.MeshOBB(cube) );
  89. cubes.push(cube);
  90. return cube;
  91. }
  92. function onDocumentMouseMove(event){
  93. event.preventDefault();
  94. mouse2d.x = (event.clientX / window.innerWidth) * 2 - 1;
  95. mouse2d.y = -(event.clientY / window.innerHeight) * 2 + 1;
  96. mouse2d.z = 1;
  97. }
  98. function animate(){
  99. requestAnimationFrame(animate);
  100. sphere.position.z += speed;
  101. if(sphere.position.z > range) sphere.position.z = 0;
  102. for (var i = 0; i < cubes.length; i++) {
  103. cubes[i].materials[0].color = new THREE.Color(0x003300);
  104. }
  105. var ray = new THREE.Ray( sphere.position, new THREE.Vector3(0,0,1) );
  106. var c = THREE.Collisions.rayCastNearest(ray);
  107. if (c && c.distance == -1) {
  108. info.innerHTML = "Colliding!";
  109. c.mesh.materials[0].color = new THREE.Color(0xff0000);
  110. } else if(c && c.distance >= 0) {
  111. info.innerHTML = "Approaching @ " + c.distance;
  112. } else {
  113. info.innerHTML = "No collider in sight.";
  114. }
  115. camera.position.x = Math.cos(mouse2d.x * Math.PI) * 300;
  116. camera.position.z = 200 + Math.sin(mouse2d.x * Math.PI) * 300;
  117. renderer.render(scene, camera);
  118. }
  119. function vts(v){
  120. if (!v)
  121. return "undefined<br>";
  122. else
  123. return v.x + " , " + v.y + " , " + v.z + "<br>";
  124. }
  125. </script>
  126. </head>
  127. <body onload="init();">
  128. <div id="info">
  129. </div>
  130. <div id="options">
  131. </div>
  132. </body>
  133. </html>