webgl_interactive_cubes_ortho.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. var container, stats;
  26. var camera, scene, raycaster, renderer;
  27. var mouse = new THREE.Vector2(), INTERSECTED;
  28. var radius = 500, theta = 0;
  29. var frustumSize = 1000;
  30. init();
  31. animate();
  32. function init() {
  33. container = document.createElement( 'div' );
  34. document.body.appendChild( container );
  35. var aspect = window.innerWidth / window.innerHeight;
  36. camera = new THREE.OrthographicCamera( frustumSize * aspect / - 2, frustumSize * aspect / 2, frustumSize / 2, frustumSize / - 2, 1, 1000 );
  37. scene = new THREE.Scene();
  38. scene.background = new THREE.Color( 0xf0f0f0 );
  39. var light = new THREE.DirectionalLight( 0xffffff, 1 );
  40. light.position.set( 1, 1, 1 ).normalize();
  41. scene.add( light );
  42. var geometry = new THREE.BoxBufferGeometry( 20, 20, 20 );
  43. for ( var i = 0; i < 2000; i ++ ) {
  44. var 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, false );
  64. //
  65. window.addEventListener( 'resize', onWindowResize, false );
  66. }
  67. function onWindowResize() {
  68. var aspect = window.innerWidth / window.innerHeight;
  69. camera.left = - frustumSize * aspect / 2;
  70. camera.right = frustumSize * aspect / 2;
  71. camera.top = frustumSize / 2;
  72. camera.bottom = - frustumSize / 2;
  73. camera.updateProjectionMatrix();
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  75. }
  76. function onDocumentMouseMove( event ) {
  77. event.preventDefault();
  78. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  79. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  80. }
  81. //
  82. function animate() {
  83. requestAnimationFrame( 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( mouse, camera );
  96. var intersects = raycaster.intersectObjects( scene.children );
  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>