webgl_collisions_terrain.html 3.5 KB

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