webgl_collisions_reaction.html 5.7 KB

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