webgl_interactive_draggablecubes.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - draggable cubes</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #f0f0f0;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <script src="../build/three.min.js"></script>
  18. <script src="js/controls/TrackballControls.js"></script>
  19. <script src="js/libs/stats.min.js"></script>
  20. <script>
  21. var container, stats;
  22. var camera, controls, scene, renderer;
  23. var objects = [], plane;
  24. var raycaster = new THREE.Raycaster();
  25. var mouse = new THREE.Vector2(),
  26. offset = new THREE.Vector3(),
  27. INTERSECTED, SELECTED;
  28. init();
  29. animate();
  30. function init() {
  31. container = document.createElement( 'div' );
  32. document.body.appendChild( container );
  33. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
  34. camera.position.z = 1000;
  35. controls = new THREE.TrackballControls( camera );
  36. controls.rotateSpeed = 1.0;
  37. controls.zoomSpeed = 1.2;
  38. controls.panSpeed = 0.8;
  39. controls.noZoom = false;
  40. controls.noPan = false;
  41. controls.staticMoving = true;
  42. controls.dynamicDampingFactor = 0.3;
  43. scene = new THREE.Scene();
  44. scene.add( new THREE.AmbientLight( 0x505050 ) );
  45. var light = new THREE.SpotLight( 0xffffff, 1.5 );
  46. light.position.set( 0, 500, 2000 );
  47. light.castShadow = true;
  48. light.shadowCameraNear = 200;
  49. light.shadowCameraFar = camera.far;
  50. light.shadowCameraFov = 50;
  51. light.shadowBias = -0.00022;
  52. light.shadowDarkness = 0.5;
  53. light.shadowMapWidth = 2048;
  54. light.shadowMapHeight = 2048;
  55. scene.add( light );
  56. var geometry = new THREE.BoxGeometry( 40, 40, 40 );
  57. for ( var i = 0; i < 200; i ++ ) {
  58. var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
  59. object.material.ambient = object.material.color;
  60. object.position.x = Math.random() * 1000 - 500;
  61. object.position.y = Math.random() * 600 - 300;
  62. object.position.z = Math.random() * 800 - 400;
  63. object.rotation.x = Math.random() * 2 * Math.PI;
  64. object.rotation.y = Math.random() * 2 * Math.PI;
  65. object.rotation.z = Math.random() * 2 * Math.PI;
  66. object.scale.x = Math.random() * 2 + 1;
  67. object.scale.y = Math.random() * 2 + 1;
  68. object.scale.z = Math.random() * 2 + 1;
  69. object.castShadow = true;
  70. object.receiveShadow = true;
  71. scene.add( object );
  72. objects.push( object );
  73. }
  74. plane = new THREE.Mesh(
  75. new THREE.PlaneBufferGeometry( 2000, 2000, 8, 8 ),
  76. new THREE.MeshBasicMaterial( { color: 0x000000, opacity: 0.25, transparent: true } )
  77. );
  78. plane.visible = false;
  79. scene.add( plane );
  80. renderer = new THREE.WebGLRenderer( { antialias: true } );
  81. renderer.setClearColor( 0xf0f0f0 );
  82. renderer.setPixelRatio( window.devicePixelRatio );
  83. renderer.setSize( window.innerWidth, window.innerHeight );
  84. renderer.sortObjects = false;
  85. renderer.shadowMapEnabled = true;
  86. renderer.shadowMapType = THREE.PCFShadowMap;
  87. container.appendChild( renderer.domElement );
  88. var info = document.createElement( 'div' );
  89. info.style.position = 'absolute';
  90. info.style.top = '10px';
  91. info.style.width = '100%';
  92. info.style.textAlign = 'center';
  93. info.innerHTML = '<a href="http://threejs.org" target="_blank">three.js</a> webgl - draggable cubes';
  94. container.appendChild( info );
  95. stats = new Stats();
  96. stats.domElement.style.position = 'absolute';
  97. stats.domElement.style.top = '0px';
  98. container.appendChild( stats.domElement );
  99. renderer.domElement.addEventListener( 'mousemove', onDocumentMouseMove, false );
  100. renderer.domElement.addEventListener( 'mousedown', onDocumentMouseDown, false );
  101. renderer.domElement.addEventListener( 'mouseup', onDocumentMouseUp, false );
  102. //
  103. window.addEventListener( 'resize', onWindowResize, false );
  104. }
  105. function onWindowResize() {
  106. camera.aspect = window.innerWidth / window.innerHeight;
  107. camera.updateProjectionMatrix();
  108. renderer.setSize( window.innerWidth, window.innerHeight );
  109. }
  110. function onDocumentMouseMove( event ) {
  111. event.preventDefault();
  112. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  113. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  114. //
  115. raycaster.setFromCamera( mouse, camera );
  116. if ( SELECTED ) {
  117. var intersects = raycaster.intersectObject( plane );
  118. SELECTED.position.copy( intersects[ 0 ].point.sub( offset ) );
  119. return;
  120. }
  121. var intersects = raycaster.intersectObjects( objects );
  122. if ( intersects.length > 0 ) {
  123. if ( INTERSECTED != intersects[ 0 ].object ) {
  124. if ( INTERSECTED ) INTERSECTED.material.color.setHex( INTERSECTED.currentHex );
  125. INTERSECTED = intersects[ 0 ].object;
  126. INTERSECTED.currentHex = INTERSECTED.material.color.getHex();
  127. plane.position.copy( INTERSECTED.position );
  128. plane.lookAt( camera.position );
  129. }
  130. container.style.cursor = 'pointer';
  131. } else {
  132. if ( INTERSECTED ) INTERSECTED.material.color.setHex( INTERSECTED.currentHex );
  133. INTERSECTED = null;
  134. container.style.cursor = 'auto';
  135. }
  136. }
  137. function onDocumentMouseDown( event ) {
  138. event.preventDefault();
  139. var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 ).unproject( camera );
  140. var raycaster = new THREE.Raycaster( camera.position, vector.sub( camera.position ).normalize() );
  141. var intersects = raycaster.intersectObjects( objects );
  142. if ( intersects.length > 0 ) {
  143. controls.enabled = false;
  144. SELECTED = intersects[ 0 ].object;
  145. var intersects = raycaster.intersectObject( plane );
  146. offset.copy( intersects[ 0 ].point ).sub( plane.position );
  147. container.style.cursor = 'move';
  148. }
  149. }
  150. function onDocumentMouseUp( event ) {
  151. event.preventDefault();
  152. controls.enabled = true;
  153. if ( INTERSECTED ) {
  154. plane.position.copy( INTERSECTED.position );
  155. SELECTED = null;
  156. }
  157. container.style.cursor = 'auto';
  158. }
  159. //
  160. function animate() {
  161. requestAnimationFrame( animate );
  162. render();
  163. stats.update();
  164. }
  165. function render() {
  166. controls.update();
  167. renderer.render( scene, camera );
  168. }
  169. </script>
  170. </body>
  171. </html>