webgl_interactive_draggablecubes.html 5.2 KB

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