webgl_collisions_reaction.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 scene, camera, 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.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. var geometry = new THREE.Geometry();
  75. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3(0, 0, 0) ) );
  76. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3(10, 0, 0) ) );
  77. debugNormal = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0xff0000 } ) );
  78. scene.addObject( debugNormal );
  79. container.onmousemove = onDocumentMouseMove;
  80. animate();
  81. }
  82. function createObstacles(){
  83. createCube(100, 50, 10, new THREE.Vector3( 20, 0, 100), Math.PI / 4);
  84. camera.target = createCube(100, 50, 10, new THREE.Vector3(-20, 0, 200), -Math.PI / 4);
  85. createCube(100, 50, 10, new THREE.Vector3( 20, 0, 300), Math.PI / 4);
  86. }
  87. function createCube(sx, sy, sz, p, ry){
  88. var cube = new THREE.Mesh(new THREE.Cube(sx, sy, sz, 1, 1, 1), new THREE.MeshLambertMaterial({
  89. color: 0x003300//, wireframe: true
  90. }));
  91. cube.position = p;
  92. cube.rotation.y = ry;
  93. scene.addObject(cube);
  94. THREE.Collisions.colliders.push( THREE.CollisionUtils.MeshOBB(cube) );
  95. cubes.push(cube);
  96. return cube;
  97. }
  98. function onDocumentMouseMove(event){
  99. event.preventDefault();
  100. mouse2d.x = (event.clientX / window.innerWidth) * 2 - 1;
  101. mouse2d.y = -(event.clientY / window.innerHeight) * 2 + 1;
  102. mouse2d.z = 1;
  103. }
  104. function animate(){
  105. requestAnimationFrame(animate);
  106. for (var i = 0; i < cubes.length; i++) {
  107. cubes[i].materials[0].color = new THREE.Color(0x003300);
  108. }
  109. var ray = new THREE.Ray( sphere.position, new THREE.Vector3(0,0,1) );
  110. //debugNormal.position = sphere.position;
  111. var c = THREE.Collisions.rayCastNearest(ray);
  112. if (!c || c.distance > speed) {
  113. sphere.position.z += speed;
  114. }
  115. if(c && c.normal) {
  116. info.innerHTML = vts(c.normal);
  117. var poi = ray.origin.clone().addSelf( ray.direction.clone().multiplyScalar( c.distance ) );
  118. debugNormal.geometry.vertices[0].position = poi.clone().addSelf( new THREE.Vector3(2,2,2) );
  119. debugNormal.geometry.vertices[1].position = poi.clone().addSelf( c.normal.clone() );
  120. }
  121. if(sphere.position.z > range) sphere.position = new THREE.Vector3(0,0,0);
  122. camera.position.x = Math.cos(mouse2d.x * Math.PI) * 300;
  123. camera.position.y = Math.cos(mouse2d.y * Math.PI) * 300;
  124. camera.position.z = 200 + Math.sin(mouse2d.x * Math.PI) * 300 + Math.sin(mouse2d.y * Math.PI) * 300;
  125. renderer.render(scene, camera);
  126. }
  127. function vts(v){
  128. if (!v)
  129. return "undefined<br>";
  130. else
  131. return v.x + " , " + v.y + " , " + v.z + "<br>";
  132. }
  133. </script>
  134. </head>
  135. <body onload="init();">
  136. <div id="info">
  137. </div>
  138. <div id="options">
  139. </div>
  140. </body>
  141. </html>