webgl_interactive_draggablecubes.html 5.9 KB

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