1
0

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