webgl_collisions_reaction.html 4.7 KB

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