webgl_collisions_terrain.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <html>
  2. <head>
  3. <title>three.js webgl - intersection: ray with terrain mesh (through box)</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 { background-color: #ddd !important }
  13. #info {
  14. position: absolute;
  15. top: 30px; left: 10px; width: 800px;
  16. color: #000000;
  17. padding: 5px;
  18. font-family: Monospace;
  19. font-size: 13px;
  20. text-align: left;
  21. z-index:100;
  22. }
  23. #options {
  24. position: absolute;
  25. top: 10px; left: 10px; width: 800px;
  26. color: #000000;
  27. padding: 5px;
  28. font-family: Monospace;
  29. font-size: 13px;
  30. text-align: left;
  31. z-index:100;
  32. }
  33. </style>
  34. <script type="text/javascript" src="../build/Three.js"></script>
  35. <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
  36. <script type="text/javascript">
  37. var camera, scene, renderer,
  38. info, mouse2d, sun, loader, sphere, ray;
  39. var theta = 0, radius = 250, speed = 0.002, sphereSize = 4;
  40. function init() {
  41. container = document.createElement( 'div' );
  42. document.body.appendChild( container );
  43. info = document.getElementById("info");
  44. camera = new THREE.Camera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
  45. camera.position.y = 120;
  46. mouse2d = new THREE.Vector3( 0, 0, 1 );
  47. loader = new THREE.JSONLoader();
  48. scene = new THREE.Scene();
  49. ray = new THREE.Ray();
  50. ray.origin.y = 10000;
  51. ray.direction = new THREE.Vector3(0, -1, 0);
  52. sphere = new THREE.Mesh( new THREE.SphereGeometry( sphereSize, 10, 10 ), new THREE.MeshLambertMaterial( { color: 0xff0000 } ) );
  53. scene.add(sphere);
  54. renderer = new THREE.WebGLRenderer();
  55. renderer.setSize( window.innerWidth, window.innerHeight );
  56. container.appendChild(renderer.domElement);
  57. var ambientLight = new THREE.AmbientLight( 0x444444 );
  58. scene.add( ambientLight );
  59. sun = new THREE.DirectionalLight( 0xaaaaaa );
  60. sun.position = new THREE.Vector3(-1,1, -1).normalize();
  61. scene.add( sun );
  62. loadCube();
  63. container.onmousemove = onDocumentMouseMove;
  64. animate();
  65. }
  66. function loadCube(p) {
  67. var onGeometry = function( geometry ) {
  68. var mesh = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: 0xf3e4b8 } ) );
  69. scene.add( mesh );
  70. THREE.Collisions.colliders.push( THREE.CollisionUtils.MeshColliderWBox(mesh) );
  71. };
  72. loader.load( { model: "obj/terrain.js", callback: onGeometry } );
  73. }
  74. function onDocumentMouseMove( event ) {
  75. event.preventDefault();
  76. mouse2d.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  77. mouse2d.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  78. mouse2d.z = 1;
  79. }
  80. function animate() {
  81. requestAnimationFrame( animate );
  82. ray.origin.x = radius * Math.cos(theta);
  83. ray.origin.z = radius * Math.sin(theta);
  84. var c = THREE.Collisions.rayCastNearest(ray);
  85. if(c) {
  86. //info.innerHTML = "Found @ distance " + c.distance;
  87. sphere.position = ray.origin.clone().subSelf( new THREE.Vector3(0, c.distance - sphereSize/2, 0) );
  88. } else {
  89. //info.innerHTML = "No intersection";
  90. }
  91. theta += speed;
  92. camera.target.position.copy( sphere.position );
  93. renderer.render( scene, camera );
  94. }
  95. function vts(v) {
  96. if(!v) return "undefined<br>";
  97. else return v.x + " , " + v.y + " , " + v.z + "<br>";
  98. }
  99. </script>
  100. </head>
  101. <body onload="init();">
  102. <div id="info"></div>
  103. <div id="options"></div>
  104. </body>
  105. </html>