webgl_collisions_normal.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <html>
  2. <head>
  3. <title>three.js webgl - intersection: ray/mesh readinf normal</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" src="js/Stats.js"></script>
  37. <script type="text/javascript">
  38. var scene, camera, renderer, info, mouse2d, sun, loader, stats, line;
  39. var meshes = [];
  40. var theta = 0;
  41. var camdist = 500;
  42. var totalFaces = 0;
  43. var totalColliders = 0;
  44. var ray = new THREE.Ray();
  45. var matrix = new THREE.Matrix4(),
  46. matrix2 = new THREE.Matrix4();
  47. function init() {
  48. container = document.createElement( 'div' );
  49. document.body.appendChild( container );
  50. info = document.getElementById("info");
  51. camera = new THREE.Camera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
  52. camera.position.z = camdist;
  53. mouse2d = new THREE.Vector3( 0, 0, 1 );
  54. loader = new THREE.JSONLoader( );
  55. scene = new THREE.Scene();
  56. renderer = new THREE.WebGLRenderer();
  57. renderer.setSize( window.innerWidth, window.innerHeight );
  58. container.appendChild( renderer.domElement );
  59. var ambientLight = new THREE.AmbientLight( 0x606060 );
  60. scene.addLight( ambientLight );
  61. sun = new THREE.DirectionalLight( 0xffffff );
  62. sun.position = camera.position.clone();
  63. scene.addLight( sun );
  64. loadCube();
  65. var lineMat = new THREE.LineBasicMaterial( { color: 0xff0000, opacity: 1, linewidth: 3 } );
  66. var geom = new THREE.Geometry();
  67. geom.vertices.push( new THREE.Vertex( new THREE.Vector3(-100, 0, 0) ) );
  68. geom.vertices.push( new THREE.Vertex( new THREE.Vector3( 100, 0, 0) ) );
  69. line = new THREE.Line(geom, lineMat);
  70. scene.addObject( line );
  71. stats = new Stats();
  72. stats.domElement.style.position = 'absolute';
  73. stats.domElement.style.top = '0px';
  74. container.appendChild( stats.domElement );
  75. container.onmousemove = onDocumentMouseMove;
  76. animate();
  77. }
  78. function loadCube(p) {
  79. var onGeometry = function( geometry ) {
  80. var sx = 300;
  81. var sy = 240;
  82. var sz = 300;
  83. addCube( new THREE.Vector3( 0, 0, 0), geometry );
  84. };
  85. loader.load( { model: "obj/suzanne/suzanneHi.js", callback: onGeometry } );
  86. }
  87. function addCube( p, g) {
  88. totalFaces += g.faces.length;
  89. totalColliders++;
  90. var mesh = new THREE.Mesh( g, new THREE.MeshLambertMaterial( { color: 0x003300 } ) );
  91. mesh.position = p;
  92. scene.addObject( mesh );
  93. var mc = THREE.CollisionUtils.MeshColliderWBox(mesh);
  94. THREE.Collisions.colliders.push( mc );
  95. meshes.push( mesh );
  96. };
  97. function onDocumentMouseMove( event ) {
  98. event.preventDefault();
  99. mouse2d.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  100. mouse2d.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  101. mouse2d.z = 1;
  102. };
  103. function animate() {
  104. requestAnimationFrame( animate );
  105. ray.origin.copy( mouse2d );
  106. matrix.copy( camera.matrixWorld );
  107. matrix.multiplySelf( THREE.Matrix4.makeInvert( camera.projectionMatrix, matrix2 ) );
  108. matrix.multiplyVector3( ray.origin );
  109. ray.direction.copy( ray.origin );
  110. ray.direction.subSelf( camera.position );
  111. //var ray2 = new THREE.Ray();
  112. //ray2.origin = ray.origin.clone();
  113. if( meshes.length == 0 ) return;
  114. var i, l = meshes.length;
  115. for ( i = 0; i < l; i++ ) {
  116. meshes[ i ].materials[ 0 ].color.setHex( 0x003300 );
  117. }
  118. info.innerHTML = "";
  119. var c = THREE.Collisions.rayCastNearest( ray );
  120. if( c ) {
  121. info.innerHTML += "Found @ normal " + vts(c.normal);
  122. var poi = ray.origin.clone().addSelf( ray.direction.clone().multiplyScalar(c.distance) );
  123. line.geometry.vertices[0].position = poi;
  124. line.geometry.vertices[1].position = poi.clone().addSelf(c.normal.multiplyScalar(100));
  125. line.geometry.__dirtyVertices = true;
  126. line.geometry.__dirtyElements = true;
  127. c.mesh.materials[ 0 ].color.setHex( 0xbb0000 );
  128. } else {
  129. info.innerHTML += "No intersection";
  130. }
  131. camera.position.x = camdist * Math.cos( theta );
  132. camera.position.z = camdist * Math.sin( theta );
  133. camera.position.y = camdist/2 * Math.sin( theta * 2) ;
  134. sun.position.copy( camera.position );
  135. sun.position.normalize();
  136. theta += 0.005;
  137. renderer.render( scene, camera );
  138. stats.update();
  139. };
  140. function vts(v) {
  141. if(!v) return "undefined<br>";
  142. else return v.x.toFixed(2) + " , " + v.y.toFixed(2) + " , " + v.z.toFixed(2) + "<br>";
  143. };
  144. </script>
  145. </head>
  146. <body onload="init();">
  147. <div id="info"></div>
  148. <div id="options"></div>
  149. </body>
  150. </html>