webgl_interactive_draggablecubes.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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.js"></script>
  18. <script src="js/Stats.js"></script>
  19. <script>
  20. var container, stats;
  21. var camera, controls, 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. scene = new THREE.Scene();
  32. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
  33. camera.position.z = 1000;
  34. scene.add( camera );
  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.add( new THREE.AmbientLight( 0x505050 ) );
  44. var light = new THREE.SpotLight( 0xffffff, 1.5 );
  45. light.position.set( 0, 500, 2000 );
  46. light.castShadow = true;
  47. light.shadowCameraNear = 200;
  48. light.shadowCameraFar = camera.far;
  49. light.shadowCameraFov = 50;
  50. light.shadowBias = -0.00022;
  51. light.shadowDarkness = 0.5;
  52. light.shadowMapWidth = 1024;
  53. light.shadowMapHeight = 1024;
  54. scene.add( light );
  55. var geometry = new THREE.CubeGeometry( 40, 40, 40 );
  56. for ( var i = 0; i < 200; i ++ ) {
  57. var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
  58. object.material.ambient = object.material.color;
  59. object.position.x = Math.random() * 1000 - 500;
  60. object.position.y = Math.random() * 600 - 300;
  61. object.position.z = Math.random() * 800 - 400;
  62. object.rotation.x = ( Math.random() * 360 ) * Math.PI / 180;
  63. object.rotation.y = ( Math.random() * 360 ) * Math.PI / 180;
  64. object.rotation.z = ( Math.random() * 360 ) * Math.PI / 180;
  65. object.scale.x = Math.random() * 2 + 1;
  66. object.scale.y = Math.random() * 2 + 1;
  67. object.scale.z = Math.random() * 2 + 1;
  68. object.castShadow = true;
  69. object.receiveShadow = true;
  70. scene.add( object );
  71. objects.push( object );
  72. }
  73. plane = new THREE.Mesh( new THREE.PlaneGeometry( 2000, 2000, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0x000000, opacity: 0.25, transparent: true, wireframe: true } ) );
  74. plane.geometry.applyMatrix( new THREE.Matrix4().makeRotationX( Math.PI / 2 ) );
  75. plane.visible = false;
  76. scene.add( plane );
  77. projector = new THREE.Projector();
  78. renderer = new THREE.WebGLRenderer( { antialias: true } );
  79. renderer.sortObjects = false;
  80. renderer.setSize( window.innerWidth, window.innerHeight );
  81. renderer.shadowMapEnabled = true;
  82. renderer.shadowMapSoft = true;
  83. container.appendChild( renderer.domElement );
  84. var info = document.createElement( 'div' );
  85. info.style.position = 'absolute';
  86. info.style.top = '10px';
  87. info.style.width = '100%';
  88. info.style.textAlign = 'center';
  89. info.innerHTML = '<a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> webgl - draggable cubes';
  90. container.appendChild( info );
  91. stats = new Stats();
  92. stats.domElement.style.position = 'absolute';
  93. stats.domElement.style.top = '0px';
  94. container.appendChild( stats.domElement );
  95. renderer.domElement.addEventListener( 'mousemove', onDocumentMouseMove, false );
  96. renderer.domElement.addEventListener( 'mousedown', onDocumentMouseDown, false );
  97. renderer.domElement.addEventListener( 'mouseup', onDocumentMouseUp, false );
  98. }
  99. function onDocumentMouseMove( event ) {
  100. event.preventDefault();
  101. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  102. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  103. //
  104. var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 );
  105. projector.unprojectVector( vector, camera );
  106. var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
  107. if ( SELECTED ) {
  108. var intersects = ray.intersectObject( plane );
  109. SELECTED.position.copy( intersects[ 0 ].point.subSelf( offset ) );
  110. return;
  111. }
  112. var intersects = ray.intersectObjects( objects );
  113. if ( intersects.length > 0 ) {
  114. if ( INTERSECTED != intersects[ 0 ].object ) {
  115. if ( INTERSECTED ) INTERSECTED.material.color.setHex( INTERSECTED.currentHex );
  116. INTERSECTED = intersects[ 0 ].object;
  117. INTERSECTED.currentHex = INTERSECTED.material.color.getHex();
  118. plane.position.copy( INTERSECTED.position );
  119. plane.lookAt( camera.position );
  120. }
  121. container.style.cursor = 'pointer';
  122. } else {
  123. if ( INTERSECTED ) INTERSECTED.material.color.setHex( INTERSECTED.currentHex );
  124. INTERSECTED = null;
  125. container.style.cursor = 'auto';
  126. }
  127. }
  128. function onDocumentMouseDown( event ) {
  129. event.preventDefault();
  130. var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 );
  131. projector.unprojectVector( vector, camera );
  132. var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
  133. var intersects = ray.intersectObjects( objects );
  134. if ( intersects.length > 0 ) {
  135. controls.enabled = false;
  136. SELECTED = intersects[ 0 ].object;
  137. var intersects = ray.intersectObject( plane );
  138. offset.copy( intersects[ 0 ].point ).subSelf( plane.position );
  139. container.style.cursor = 'move';
  140. }
  141. }
  142. function onDocumentMouseUp( event ) {
  143. event.preventDefault();
  144. controls.enabled = true;
  145. if ( INTERSECTED ) {
  146. plane.position.copy( INTERSECTED.position );
  147. SELECTED = null;
  148. }
  149. container.style.cursor = 'auto';
  150. }
  151. //
  152. function animate() {
  153. requestAnimationFrame( animate );
  154. render();
  155. stats.update();
  156. }
  157. function render() {
  158. controls.update();
  159. renderer.render( scene, camera );
  160. }
  161. </script>
  162. </body>
  163. </html>