webgl_collisions_terrain.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 scene, camera, renderer, info, mouse2d, sun, loader, sphere, ray;
  38. var theta = 0;
  39. var radius = 250;
  40. var speed = 0.0015;
  41. var sphereSize = 4;
  42. function init() {
  43. container = document.createElement( 'div' );
  44. document.body.appendChild( container );
  45. info = document.getElementById("info");
  46. camera = new THREE.Camera( 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.addObject(sphere);
  56. camera.target = sphere;
  57. renderer = new THREE.WebGLRenderer();
  58. renderer.setSize( window.innerWidth, window.innerHeight );
  59. container.appendChild(renderer.domElement);
  60. var ambientLight = new THREE.AmbientLight( 0x444444 );
  61. scene.addLight( ambientLight );
  62. sun = new THREE.DirectionalLight( 0xaaaaaa );
  63. sun.position = new THREE.Vector3(-1,1, -1).normalize();
  64. scene.addLight( sun );
  65. loadCube();
  66. container.onmousemove = onDocumentMouseMove;
  67. animate();
  68. }
  69. function loadCube(p) {
  70. var onGeometry = function( geometry ) {
  71. var mesh = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: 0xf3e4b8 } ) );
  72. scene.addObject( mesh );
  73. THREE.Collisions.colliders.push( THREE.CollisionUtils.MeshColliderWBox(mesh) );
  74. };
  75. loader.load( { model: "obj/terrain.js", callback: onGeometry } );
  76. }
  77. function onDocumentMouseMove( event ) {
  78. event.preventDefault();
  79. mouse2d.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  80. mouse2d.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  81. mouse2d.z = 1;
  82. }
  83. function animate() {
  84. requestAnimationFrame( animate );
  85. ray.origin.x = radius * Math.cos(theta);
  86. ray.origin.z = radius * Math.sin(theta);
  87. var c = THREE.Collisions.rayCastNearest(ray);
  88. if(c) {
  89. //info.innerHTML = "Found @ distance " + c.distance;
  90. sphere.position = ray.origin.clone().subSelf( new THREE.Vector3(0, c.distance - sphereSize/2, 0) );
  91. } else {
  92. //info.innerHTML = "No intersection";
  93. }
  94. theta += speed;
  95. renderer.render( scene, camera );
  96. }
  97. function vts(v) {
  98. if(!v) return "undefined<br>";
  99. else return v.x + " , " + v.y + " , " + v.z + "<br>";
  100. }
  101. </script>
  102. </head>
  103. <body onload="init();">
  104. <div id="info"></div>
  105. <div id="options"></div>
  106. </body>
  107. </html>