webgl_collisions_normal.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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;
  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. stats = new Stats();
  66. stats.domElement.style.position = 'absolute';
  67. stats.domElement.style.top = '0px';
  68. container.appendChild( stats.domElement );
  69. container.onmousemove = onDocumentMouseMove;
  70. animate();
  71. }
  72. function loadCube(p) {
  73. var onGeometry = function( geometry ) {
  74. var sx = 300;
  75. var sy = 240;
  76. var sz = 300;
  77. addCube( new THREE.Vector3( 0, 0, 0), geometry );
  78. };
  79. loader.load( { model: "obj/suzanne/suzanne.js", callback: onGeometry } );
  80. }
  81. function addCube( p, g) {
  82. totalFaces += g.faces.length;
  83. totalColliders++;
  84. var mesh = new THREE.Mesh( g, new THREE.MeshLambertMaterial( { color: 0x003300 } ) );
  85. mesh.position = p;
  86. scene.addObject( mesh );
  87. var mc = THREE.CollisionUtils.MeshColliderWBox(mesh);
  88. THREE.Collisions.colliders.push( mc );
  89. meshes.push( mesh );
  90. };
  91. function onDocumentMouseMove( event ) {
  92. event.preventDefault();
  93. mouse2d.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  94. mouse2d.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  95. mouse2d.z = 1;
  96. };
  97. function animate() {
  98. requestAnimationFrame( animate );
  99. ray.origin.copy( mouse2d );
  100. matrix.copy( camera.matrixWorld );
  101. matrix.multiplySelf( THREE.Matrix4.makeInvert( camera.projectionMatrix, matrix2 ) );
  102. matrix.multiplyVector3( ray.origin );
  103. ray.direction.copy( ray.origin );
  104. ray.direction.subSelf( camera.position );
  105. if( meshes.length == 0 ) return;
  106. var i, l = meshes.length;
  107. for ( i = 0; i < l; i++ ) {
  108. meshes[ i ].materials[ 0 ].color.setHex( 0x003300 );
  109. }
  110. info.innerHTML = "";
  111. var c = THREE.Collisions.rayCastNearest( ray );
  112. if( c ) {
  113. info.innerHTML += "Found @ normal " + vts(c.normal);
  114. c.mesh.materials[ 0 ].color.setHex( 0xbb0000 );
  115. } else {
  116. info.innerHTML += "No intersection";
  117. }
  118. camera.position.x = camdist * Math.cos( theta );
  119. camera.position.z = camdist * Math.sin( theta );
  120. camera.position.y = camdist/2 * Math.sin( theta * 2) ;
  121. sun.position.copy( camera.position );
  122. sun.position.normalize();
  123. theta += 0.005;
  124. renderer.render( scene, camera );
  125. stats.update();
  126. };
  127. function vts(v) {
  128. if(!v) return "undefined<br>";
  129. else return v.x.toFixed(2) + " , " + v.y.toFixed(2) + " , " + v.z.toFixed(2) + "<br>";
  130. };
  131. </script>
  132. </head>
  133. <body onload="init();">
  134. <div id="info"></div>
  135. <div id="options"></div>
  136. </body>
  137. </html>
粤ICP备19079148号