webgl_collisions_terrain.html 3.3 KB

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