webgl_interactive_cubes.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. body {
  10. background-color: #f0f0f0;
  11. color: #444;
  12. }
  13. a {
  14. color: #08f;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="info">
  20. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - interactive cubes
  21. </div>
  22. <script type="module">
  23. import * as THREE from '../build/three.module.js';
  24. import Stats from './jsm/libs/stats.module.js';
  25. var container, stats;
  26. var camera, scene, raycaster, renderer;
  27. var mouse = new THREE.Vector2(), INTERSECTED;
  28. var radius = 100, theta = 0;
  29. init();
  30. animate();
  31. function init() {
  32. container = document.createElement( 'div' );
  33. document.body.appendChild( container );
  34. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
  35. scene = new THREE.Scene();
  36. scene.background = new THREE.Color( 0xf0f0f0 );
  37. var light = new THREE.DirectionalLight( 0xffffff, 1 );
  38. light.position.set( 1, 1, 1 ).normalize();
  39. scene.add( light );
  40. var geometry = new THREE.BoxBufferGeometry( 20, 20, 20 );
  41. for ( var i = 0; i < 2000; i ++ ) {
  42. var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
  43. object.position.x = Math.random() * 800 - 400;
  44. object.position.y = Math.random() * 800 - 400;
  45. object.position.z = Math.random() * 800 - 400;
  46. object.rotation.x = Math.random() * 2 * Math.PI;
  47. object.rotation.y = Math.random() * 2 * Math.PI;
  48. object.rotation.z = Math.random() * 2 * Math.PI;
  49. object.scale.x = Math.random() + 0.5;
  50. object.scale.y = Math.random() + 0.5;
  51. object.scale.z = Math.random() + 0.5;
  52. scene.add( object );
  53. }
  54. raycaster = new THREE.Raycaster();
  55. renderer = new THREE.WebGLRenderer();
  56. renderer.setPixelRatio( window.devicePixelRatio );
  57. renderer.setSize( window.innerWidth, window.innerHeight );
  58. container.appendChild( renderer.domElement );
  59. stats = new Stats();
  60. container.appendChild( stats.dom );
  61. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  62. //
  63. window.addEventListener( 'resize', onWindowResize, false );
  64. }
  65. function onWindowResize() {
  66. camera.aspect = window.innerWidth / window.innerHeight;
  67. camera.updateProjectionMatrix();
  68. renderer.setSize( window.innerWidth, window.innerHeight );
  69. }
  70. function onDocumentMouseMove( event ) {
  71. event.preventDefault();
  72. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  73. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  74. }
  75. //
  76. function animate() {
  77. requestAnimationFrame( animate );
  78. render();
  79. stats.update();
  80. }
  81. function render() {
  82. theta += 0.1;
  83. camera.position.x = radius * Math.sin( THREE.MathUtils.degToRad( theta ) );
  84. camera.position.y = radius * Math.sin( THREE.MathUtils.degToRad( theta ) );
  85. camera.position.z = radius * Math.cos( THREE.MathUtils.degToRad( theta ) );
  86. camera.lookAt( scene.position );
  87. camera.updateMatrixWorld();
  88. // find intersections
  89. raycaster.setFromCamera( mouse, camera );
  90. var intersects = raycaster.intersectObjects( scene.children );
  91. if ( intersects.length > 0 ) {
  92. if ( INTERSECTED != intersects[ 0 ].object ) {
  93. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  94. INTERSECTED = intersects[ 0 ].object;
  95. INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex();
  96. INTERSECTED.material.emissive.setHex( 0xff0000 );
  97. }
  98. } else {
  99. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  100. INTERSECTED = null;
  101. }
  102. renderer.render( scene, camera );
  103. }
  104. </script>
  105. </body>
  106. </html>