webgl_interactive_cubes_ortho.html 4.3 KB

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