webgl_interactive_cubes.html 4.1 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/libs/stats.min.js"></script>
  19. <script>
  20. var container, stats;
  21. var camera, scene, raycaster, renderer;
  22. var mouse = new THREE.Vector2(), INTERSECTED;
  23. var radius = 100, theta = 0;
  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://threejs.org" target="_blank">three.js</a> webgl - interactive cubes';
  35. container.appendChild( info );
  36. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
  37. scene = new THREE.Scene();
  38. var light = new THREE.DirectionalLight( 0xffffff, 1 );
  39. light.position.set( 1, 1, 1 ).normalize();
  40. scene.add( light );
  41. var geometry = new THREE.BoxBufferGeometry( 20, 20, 20 );
  42. for ( var i = 0; i < 2000; i ++ ) {
  43. var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
  44. object.position.x = Math.random() * 800 - 400;
  45. object.position.y = Math.random() * 800 - 400;
  46. object.position.z = Math.random() * 800 - 400;
  47. object.rotation.x = Math.random() * 2 * Math.PI;
  48. object.rotation.y = Math.random() * 2 * Math.PI;
  49. object.rotation.z = Math.random() * 2 * Math.PI;
  50. object.scale.x = Math.random() + 0.5;
  51. object.scale.y = Math.random() + 0.5;
  52. object.scale.z = Math.random() + 0.5;
  53. scene.add( object );
  54. }
  55. raycaster = new THREE.Raycaster();
  56. renderer = new THREE.WebGLRenderer();
  57. renderer.setClearColor( 0xf0f0f0 );
  58. renderer.setPixelRatio( window.devicePixelRatio );
  59. renderer.setSize( window.innerWidth, window.innerHeight );
  60. renderer.sortObjects = false;
  61. container.appendChild(renderer.domElement);
  62. stats = new Stats();
  63. container.appendChild( stats.dom );
  64. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  65. //
  66. window.addEventListener( 'resize', onWindowResize, false );
  67. }
  68. function onWindowResize() {
  69. camera.aspect = window.innerWidth / window.innerHeight;
  70. camera.updateProjectionMatrix();
  71. renderer.setSize( window.innerWidth, window.innerHeight );
  72. }
  73. function onDocumentMouseMove( event ) {
  74. event.preventDefault();
  75. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  76. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  77. }
  78. //
  79. function animate() {
  80. requestAnimationFrame( animate );
  81. render();
  82. stats.update();
  83. }
  84. function render() {
  85. theta += 0.1;
  86. camera.position.x = radius * Math.sin( THREE.Math.degToRad( theta ) );
  87. camera.position.y = radius * Math.sin( THREE.Math.degToRad( theta ) );
  88. camera.position.z = radius * Math.cos( THREE.Math.degToRad( theta ) );
  89. camera.lookAt( scene.position );
  90. camera.updateMatrixWorld();
  91. // find intersections
  92. raycaster.setFromCamera( mouse, camera );
  93. var intersects = raycaster.intersectObjects( scene.children );
  94. if ( intersects.length > 0 ) {
  95. if ( INTERSECTED != intersects[ 0 ].object ) {
  96. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  97. INTERSECTED = intersects[ 0 ].object;
  98. INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex();
  99. INTERSECTED.material.emissive.setHex( 0xff0000 );
  100. }
  101. } else {
  102. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  103. INTERSECTED = null;
  104. }
  105. renderer.render( scene, camera );
  106. }
  107. </script>
  108. </body>
  109. </html>