webgl_interactive_cubes.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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.min.js"></script>
  18. <script src="js/Stats.js"></script>
  19. <script>
  20. var container, stats;
  21. var camera, scene, projector, renderer;
  22. var mouse = { x: 0, y: 0 }, INTERSECTED;
  23. init();
  24. animate();
  25. function init() {
  26. container = document.createElement( 'div' );
  27. document.body.appendChild( container );
  28. var info = document.createElement( 'div' );
  29. info.style.position = 'absolute';
  30. info.style.top = '10px';
  31. info.style.width = '100%';
  32. info.style.textAlign = 'center';
  33. info.innerHTML = '<a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> webgl - interactive cubes';
  34. container.appendChild( info );
  35. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
  36. camera.position.set( 0, 300, 500 );
  37. scene = new THREE.Scene();
  38. var light = new THREE.DirectionalLight( 0xffffff, 2 );
  39. light.position.set( 1, 1, 1 ).normalize();
  40. scene.add( light );
  41. var light = new THREE.DirectionalLight( 0xffffff );
  42. light.position.set( -1, -1, -1 ).normalize();
  43. scene.add( light );
  44. var geometry = new THREE.CubeGeometry( 20, 20, 20 );
  45. for ( var i = 0; i < 500; i ++ ) {
  46. var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
  47. object.position.x = Math.random() * 800 - 400;
  48. object.position.y = Math.random() * 800 - 400;
  49. object.position.z = Math.random() * 800 - 400;
  50. object.rotation.x = ( Math.random() * 360 ) * Math.PI / 180;
  51. object.rotation.y = ( Math.random() * 360 ) * Math.PI / 180;
  52. object.rotation.z = ( Math.random() * 360 ) * Math.PI / 180;
  53. object.scale.x = Math.random() * 2 + 1;
  54. object.scale.y = Math.random() * 2 + 1;
  55. object.scale.z = Math.random() * 2 + 1;
  56. scene.add( object );
  57. }
  58. projector = new THREE.Projector();
  59. renderer = new THREE.WebGLRenderer();
  60. renderer.sortObjects = false;
  61. renderer.setSize( window.innerWidth, window.innerHeight );
  62. container.appendChild(renderer.domElement);
  63. stats = new Stats();
  64. stats.domElement.style.position = 'absolute';
  65. stats.domElement.style.top = '0px';
  66. container.appendChild( stats.domElement );
  67. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  68. //
  69. window.addEventListener( 'resize', onWindowResize, false );
  70. }
  71. function onWindowResize() {
  72. camera.aspect = window.innerWidth / window.innerHeight;
  73. camera.updateProjectionMatrix();
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  75. }
  76. function onDocumentMouseMove( event ) {
  77. event.preventDefault();
  78. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  79. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  80. }
  81. //
  82. function animate() {
  83. requestAnimationFrame( animate );
  84. render();
  85. stats.update();
  86. }
  87. var radius = 100;
  88. var theta = 0;
  89. function render() {
  90. theta += 0.2;
  91. camera.position.x = radius * Math.sin( theta * Math.PI / 360 );
  92. camera.position.y = radius * Math.sin( theta * Math.PI / 360 );
  93. camera.position.z = radius * Math.cos( theta * Math.PI / 360 );
  94. camera.lookAt( scene.position );
  95. // find intersections
  96. var vector = new THREE.Vector3( mouse.x, mouse.y, 1 );
  97. projector.unprojectVector( vector, camera );
  98. var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
  99. var intersects = ray.intersectObjects( scene.children );
  100. if ( intersects.length > 0 ) {
  101. if ( INTERSECTED != intersects[ 0 ].object ) {
  102. if ( INTERSECTED ) INTERSECTED.material.color.setHex( INTERSECTED.currentHex );
  103. INTERSECTED = intersects[ 0 ].object;
  104. INTERSECTED.currentHex = INTERSECTED.material.color.getHex();
  105. INTERSECTED.material.color.setHex( 0xff0000 );
  106. }
  107. } else {
  108. if ( INTERSECTED ) INTERSECTED.material.color.setHex( INTERSECTED.currentHex );
  109. INTERSECTED = null;
  110. }
  111. renderer.render( scene, camera );
  112. }
  113. </script>
  114. </body>
  115. </html>