webgl_interactive_draggablecubes.html 6.0 KB

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