webgl_lod.html 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - level of detail</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. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - level of detail
  12. </div>
  13. <script type="module">
  14. import * as THREE from '../build/three.module.js';
  15. import { FlyControls } from './jsm/controls/FlyControls.js';
  16. let container;
  17. let camera, scene, renderer, controls;
  18. const clock = new THREE.Clock();
  19. init();
  20. animate();
  21. function init() {
  22. container = document.createElement( 'div' );
  23. document.body.appendChild( container );
  24. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 15000 );
  25. camera.position.z = 1000;
  26. scene = new THREE.Scene();
  27. scene.fog = new THREE.Fog( 0x000000, 1, 15000 );
  28. const pointLight = new THREE.PointLight( 0xff2200 );
  29. pointLight.position.set( 0, 0, 0 );
  30. scene.add( pointLight );
  31. const dirLight = new THREE.DirectionalLight( 0xffffff );
  32. dirLight.position.set( 0, 0, 1 ).normalize();
  33. scene.add( dirLight );
  34. const geometry = [
  35. [ new THREE.IcosahedronGeometry( 100, 16 ), 50 ],
  36. [ new THREE.IcosahedronGeometry( 100, 8 ), 300 ],
  37. [ new THREE.IcosahedronGeometry( 100, 4 ), 1000 ],
  38. [ new THREE.IcosahedronGeometry( 100, 2 ), 2000 ],
  39. [ new THREE.IcosahedronGeometry( 100, 1 ), 8000 ]
  40. ];
  41. const material = new THREE.MeshLambertMaterial( { color: 0xffffff, wireframe: true } );
  42. for ( let j = 0; j < 1000; j ++ ) {
  43. const lod = new THREE.LOD();
  44. for ( let i = 0; i < geometry.length; i ++ ) {
  45. const mesh = new THREE.Mesh( geometry[ i ][ 0 ], material );
  46. mesh.scale.set( 1.5, 1.5, 1.5 );
  47. mesh.updateMatrix();
  48. mesh.matrixAutoUpdate = false;
  49. lod.addLevel( mesh, geometry[ i ][ 1 ] );
  50. }
  51. lod.position.x = 10000 * ( 0.5 - Math.random() );
  52. lod.position.y = 7500 * ( 0.5 - Math.random() );
  53. lod.position.z = 10000 * ( 0.5 - Math.random() );
  54. lod.updateMatrix();
  55. lod.matrixAutoUpdate = false;
  56. scene.add( lod );
  57. }
  58. renderer = new THREE.WebGLRenderer( { antialias: true } );
  59. renderer.setPixelRatio( window.devicePixelRatio );
  60. renderer.setSize( window.innerWidth, window.innerHeight );
  61. container.appendChild( renderer.domElement );
  62. //
  63. controls = new FlyControls( camera, renderer.domElement );
  64. controls.movementSpeed = 1000;
  65. controls.rollSpeed = Math.PI / 10;
  66. //
  67. window.addEventListener( 'resize', onWindowResize );
  68. }
  69. function onWindowResize() {
  70. camera.aspect = window.innerWidth / window.innerHeight;
  71. camera.updateProjectionMatrix();
  72. renderer.setSize( window.innerWidth, window.innerHeight );
  73. }
  74. function animate() {
  75. requestAnimationFrame( animate );
  76. render();
  77. }
  78. function render() {
  79. controls.update( clock.getDelta() );
  80. renderer.render( scene, camera );
  81. }
  82. </script>
  83. </body>
  84. </html>