webgl_interactive_cubes.html 4.2 KB

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