webgl_interactive_cubes.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - interactive 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/RequestAnimationFrame.js"></script>
  19. <script src="js/Stats.js"></script>
  20. <script>
  21. var container, stats;
  22. var camera, scene, projector, renderer;
  23. var mouse = { x: 0, y: 0 }, INTERSECTED;
  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 - interactive cubes';
  35. container.appendChild( info );
  36. camera = new THREE.Camera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
  37. camera.position.y = 300;
  38. camera.position.z = 500;
  39. scene = new THREE.Scene();
  40. var light = new THREE.DirectionalLight( 0xffffff, 2 );
  41. light.position.x = 1;
  42. light.position.y = 1;
  43. light.position.z = 1;
  44. light.position.normalize();
  45. scene.add( light );
  46. var light = new THREE.DirectionalLight( 0xffffff );
  47. light.position.x = - 1;
  48. light.position.y = - 1;
  49. light.position.z = - 1;
  50. light.position.normalize();
  51. scene.add( light );
  52. var geometry = new THREE.CubeGeometry( 20, 20, 20 );
  53. for ( var i = 0; i < 500; i ++ ) {
  54. var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
  55. object.position.x = Math.random() * 800 - 400;
  56. object.position.y = Math.random() * 800 - 400;
  57. object.position.z = Math.random() * 800 - 400;
  58. object.rotation.x = ( Math.random() * 360 ) * Math.PI / 180;
  59. object.rotation.y = ( Math.random() * 360 ) * Math.PI / 180;
  60. object.rotation.z = ( Math.random() * 360 ) * Math.PI / 180;
  61. object.scale.x = Math.random() * 2 + 1;
  62. object.scale.y = Math.random() * 2 + 1;
  63. object.scale.z = Math.random() * 2 + 1;
  64. scene.add( object );
  65. }
  66. projector = new THREE.Projector();
  67. renderer = new THREE.WebGLRenderer();
  68. renderer.sortObjects = false;
  69. renderer.setSize( window.innerWidth, window.innerHeight );
  70. container.appendChild(renderer.domElement);
  71. stats = new Stats();
  72. stats.domElement.style.position = 'absolute';
  73. stats.domElement.style.top = '0px';
  74. container.appendChild( stats.domElement );
  75. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  76. }
  77. function onDocumentMouseMove( event ) {
  78. event.preventDefault();
  79. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  80. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  81. }
  82. //
  83. function animate() {
  84. requestAnimationFrame( animate );
  85. render();
  86. stats.update();
  87. }
  88. var radius = 100;
  89. var theta = 0;
  90. function render() {
  91. theta += 0.2;
  92. camera.position.x = radius * Math.sin( theta * Math.PI / 360 );
  93. camera.position.y = radius * Math.sin( theta * Math.PI / 360 );
  94. camera.position.z = radius * Math.cos( theta * Math.PI / 360 );
  95. // find intersections
  96. camera.update();
  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. var intersects = ray.intersectScene( scene );
  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. INTERSECTED.materials[ 0 ].color.setHex( 0xff0000 );
  107. }
  108. } else {
  109. if ( INTERSECTED ) INTERSECTED.materials[ 0 ].color.setHex( INTERSECTED.currentHex );
  110. INTERSECTED = null;
  111. }
  112. renderer.render( scene, camera );
  113. }
  114. </script>
  115. </body>
  116. </html>