webgl_collisions_trigger.html 4.0 KB

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