webgl_interactive_draggablecubes.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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, 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, 5000 );
  39. camera.position.z = 1000;
  40. scene = new THREE.Scene();
  41. var light = new THREE.SpotLight( 0xffffff, 2 );
  42. light.position.set( 0, 500, 2000 );
  43. light.castShadow = true;
  44. scene.addLight( light );
  45. var geometry = new THREE.CubeGeometry( 40, 40, 40 );
  46. for ( var i = 0; i < 200; i ++ ) {
  47. var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
  48. object.position.x = Math.random() * 1000 - 500;
  49. object.position.y = Math.random() * 600 - 300;
  50. object.position.z = Math.random() * 800 - 400;
  51. object.rotation.x = ( Math.random() * 360 ) * Math.PI / 180;
  52. object.rotation.y = ( Math.random() * 360 ) * Math.PI / 180;
  53. object.rotation.z = ( Math.random() * 360 ) * Math.PI / 180;
  54. object.scale.x = Math.random() * 2 + 1;
  55. object.scale.y = Math.random() * 2 + 1;
  56. object.scale.z = Math.random() * 2 + 1;
  57. object.castShadow = true;
  58. object.receiveShadow = true;
  59. scene.addObject( object );
  60. objects.push( object );
  61. }
  62. plane = new THREE.Mesh( new THREE.PlaneGeometry( 2000, 2000, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0x000000, opacity: 0.5, wireframe: true } ) );
  63. plane.visible = false;
  64. scene.addObject( plane );
  65. projector = new THREE.Projector();
  66. renderer = new THREE.WebGLRenderer( { antialias: true } );
  67. renderer.sortObjects = false;
  68. renderer.setSize( window.innerWidth, window.innerHeight );
  69. renderer.shadowCameraNear = 3;
  70. renderer.shadowCameraFar = camera.far;
  71. renderer.shadowCameraFov = 30;
  72. renderer.shadowMapBias = 0.00389;
  73. renderer.shadowMapDarkness = 0.5;
  74. renderer.shadowMapWidth = 1024;
  75. renderer.shadowMapHeight = 1024;
  76. renderer.shadowMapEnabled = true;
  77. renderer.shadowMapSoft = true;
  78. container.appendChild(renderer.domElement);
  79. stats = new Stats();
  80. stats.domElement.style.position = 'absolute';
  81. stats.domElement.style.top = '0px';
  82. container.appendChild( stats.domElement );
  83. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  84. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  85. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  86. }
  87. function onDocumentMouseMove( event ) {
  88. event.preventDefault();
  89. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  90. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  91. //
  92. var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 );
  93. projector.unprojectVector( vector, camera );
  94. var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
  95. if ( SELECTED ) {
  96. var intersects = ray.intersectObject( plane );
  97. SELECTED.position.copy( intersects[ 0 ].point.subSelf( offset ) );
  98. return;
  99. }
  100. var intersects = ray.intersectObjects( objects );
  101. if ( intersects.length > 0 ) {
  102. if ( INTERSECTED != intersects[ 0 ].object ) {
  103. if ( INTERSECTED ) INTERSECTED.materials[ 0 ].color.setHex( INTERSECTED.currentHex );
  104. INTERSECTED = intersects[ 0 ].object;
  105. INTERSECTED.currentHex = INTERSECTED.materials[ 0 ].color.getHex();
  106. plane.position.copy( INTERSECTED.position );
  107. }
  108. container.style.cursor = 'pointer';
  109. } else {
  110. if ( INTERSECTED ) INTERSECTED.materials[ 0 ].color.setHex( INTERSECTED.currentHex );
  111. INTERSECTED = null;
  112. container.style.cursor = 'auto';
  113. }
  114. }
  115. function onDocumentMouseDown( event ) {
  116. event.preventDefault();
  117. var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 );
  118. projector.unprojectVector( vector, camera );
  119. var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
  120. var intersects = ray.intersectObjects( objects );
  121. if ( intersects.length > 0 ) {
  122. SELECTED = intersects[ 0 ].object;
  123. var intersects = ray.intersectObject( plane );
  124. offset.copy( intersects[ 0 ].point ).subSelf( plane.position );
  125. container.style.cursor = 'move';
  126. }
  127. }
  128. function onDocumentMouseUp( event ) {
  129. event.preventDefault();
  130. plane.position.copy( INTERSECTED.position );
  131. SELECTED = null;
  132. container.style.cursor = 'auto';
  133. }
  134. //
  135. function animate() {
  136. requestAnimationFrame( animate );
  137. render();
  138. stats.update();
  139. }
  140. function render() {
  141. renderer.render( scene, camera );
  142. }
  143. </script>
  144. </body>
  145. </html>