webgl_interactive_cubes.html 4.0 KB

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