webgl_interactive_draggablecubes.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - draggable cubes</title>
  5. <meta charset="utf-8">
  6. <style type="text/css">
  7. body {
  8. font-family: Monospace;
  9. background-color: #f0f0f0;
  10. margin: 0px;
  11. overflow: hidden;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <script type="text/javascript" src="../build/Three.js"></script>
  17. <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
  18. <script type="text/javascript" src="js/Stats.js"></script>
  19. <script type="text/javascript">
  20. var container, data, stats;
  21. var camera, scene, projector, renderer;
  22. var objects = [], plane;
  23. var mouse = { x: 0, y: 0 }, INTERSECTED, SELECTED;
  24. init();
  25. animate();
  26. function init() {
  27. container = document.createElement( 'div' );
  28. document.body.appendChild( container );
  29. var info = document.createElement( 'div' );
  30. info.style.position = 'absolute';
  31. info.style.top = '10px';
  32. info.style.width = '100%';
  33. info.style.textAlign = 'center';
  34. info.innerHTML = '<a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> webgl - draggable cubes';
  35. container.appendChild( info );
  36. camera = new THREE.Camera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
  37. camera.position.z = 1000;
  38. scene = new THREE.Scene();
  39. var light = new THREE.DirectionalLight( 0xffffff, 2 );
  40. light.position.x = 1;
  41. light.position.y = 1;
  42. light.position.z = 1;
  43. light.position.normalize();
  44. scene.addLight( light );
  45. var light = new THREE.DirectionalLight( 0xffffff );
  46. light.position.x = - 1;
  47. light.position.y = - 1;
  48. light.position.z = - 1;
  49. light.position.normalize();
  50. scene.addLight( light );
  51. var geometry = new THREE.CubeGeometry( 40, 40, 40 );
  52. for ( var i = 0; i < 200; i ++ ) {
  53. var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
  54. object.position.x = Math.random() * 800 - 400;
  55. object.position.y = Math.random() * 600 - 300;
  56. object.position.z = Math.random() * 800 - 400;
  57. object.rotation.x = ( Math.random() * 360 ) * Math.PI / 180;
  58. object.rotation.y = ( Math.random() * 360 ) * Math.PI / 180;
  59. object.rotation.z = ( Math.random() * 360 ) * Math.PI / 180;
  60. object.scale.x = Math.random() * 2 + 1;
  61. object.scale.y = Math.random() * 2 + 1;
  62. object.scale.z = Math.random() * 2 + 1;
  63. scene.addObject( object );
  64. objects.push( object );
  65. }
  66. plane = new THREE.Mesh( new THREE.PlaneGeometry( 2000, 2000, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0x000000, opacity: 0.5, wireframe: true } ) );
  67. plane.visible = false;
  68. scene.addObject( plane );
  69. projector = new THREE.Projector();
  70. renderer = new THREE.WebGLRenderer();
  71. renderer.sortObjects = false;
  72. renderer.setSize( window.innerWidth, window.innerHeight );
  73. container.appendChild(renderer.domElement);
  74. data = document.createElement( 'div' );
  75. data.style.position = 'absolute';
  76. data.style.whiteSpace = 'nowrap';
  77. data.style.color = '#404040';
  78. data.style.padding = '8px';
  79. data.style.backgroundColor = '#ffffff';
  80. container.appendChild( data );
  81. stats = new Stats();
  82. stats.domElement.style.position = 'absolute';
  83. stats.domElement.style.top = '0px';
  84. container.appendChild( stats.domElement );
  85. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  86. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  87. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  88. }
  89. function onDocumentMouseMove( event ) {
  90. event.preventDefault();
  91. data.style.left = ( - 20 + event.clientX) + 'px';
  92. data.style.top = ( 10 + event.clientY ) + 'px';
  93. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  94. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  95. //
  96. var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 );
  97. projector.unprojectVector( vector, camera );
  98. var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
  99. if ( SELECTED ) {
  100. var intersects = ray.intersectObject( plane );
  101. SELECTED.position.copy( intersects[ 0 ].point );
  102. return;
  103. }
  104. var intersects = ray.intersectObjects( objects );
  105. if ( intersects.length > 0 ) {
  106. if ( INTERSECTED != intersects[ 0 ].object ) {
  107. if ( INTERSECTED ) INTERSECTED.materials[ 0 ].color.setHex( INTERSECTED.currentHex );
  108. INTERSECTED = intersects[ 0 ].object;
  109. INTERSECTED.currentHex = INTERSECTED.materials[ 0 ].color.getHex();
  110. INTERSECTED.materials[ 0 ].color.setHex( 0xff0000 );
  111. plane.position.copy( INTERSECTED.position );
  112. }
  113. } else {
  114. if ( INTERSECTED ) INTERSECTED.materials[ 0 ].color.setHex( INTERSECTED.currentHex );
  115. INTERSECTED = null;
  116. }
  117. }
  118. function onDocumentMouseDown( event ) {
  119. event.preventDefault();
  120. var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 );
  121. projector.unprojectVector( vector, camera );
  122. var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
  123. var intersects = ray.intersectObjects( objects );
  124. if ( intersects.length > 0 ) {
  125. SELECTED = intersects[ 0 ].object;
  126. }
  127. }
  128. function onDocumentMouseUp( event ) {
  129. event.preventDefault();
  130. SELECTED = null;
  131. }
  132. //
  133. function animate() {
  134. requestAnimationFrame( animate );
  135. render();
  136. stats.update();
  137. }
  138. var radius = 100;
  139. var theta = 0;
  140. function render() {
  141. theta += 0.2;
  142. renderer.render( scene, camera );
  143. if ( INTERSECTED ) {
  144. data.style.visibility = 'visible';
  145. data.innerHTML = 'x: ' + INTERSECTED.position.x + '<br>y: ' + INTERSECTED.position.y + '<br>z: ' + INTERSECTED.position.z;
  146. } else {
  147. data.style.visibility = 'hidden';
  148. }
  149. }
  150. </script>
  151. </body>
  152. </html>
粤ICP备19079148号