webgl_interactive_cubes.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. <meta property="og:title" content="three.js webgl - interactive cubes">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_interactive_cubes.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_interactive_cubes.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. <style>
  13. body {
  14. background-color: #f0f0f0;
  15. color: #444;
  16. }
  17. a {
  18. color: #08f;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div id="info">
  24. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - interactive cubes
  25. </div>
  26. <script type="importmap">
  27. {
  28. "imports": {
  29. "three": "../build/three.module.js",
  30. "three/addons/": "./jsm/"
  31. }
  32. }
  33. </script>
  34. <script type="module">
  35. import * as THREE from 'three';
  36. import Stats from 'three/addons/libs/stats.module.js';
  37. let stats;
  38. let camera, scene, raycaster, renderer;
  39. let INTERSECTED;
  40. let theta = 0;
  41. const pointer = new THREE.Vector2();
  42. const radius = 5;
  43. init();
  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. renderer.setAnimationLoop( animate );
  70. document.body.appendChild( renderer.domElement );
  71. stats = new Stats();
  72. document.body.appendChild( stats.dom );
  73. document.addEventListener( 'mousemove', onPointerMove );
  74. //
  75. window.addEventListener( 'resize', onWindowResize );
  76. }
  77. function onWindowResize() {
  78. camera.aspect = window.innerWidth / window.innerHeight;
  79. camera.updateProjectionMatrix();
  80. renderer.setSize( window.innerWidth, window.innerHeight );
  81. }
  82. function onPointerMove( event ) {
  83. pointer.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  84. pointer.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  85. }
  86. //
  87. function 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>
粤ICP备19079148号