webgl_lod.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.module.js",
  17. "three/addons/": "./jsm/"
  18. }
  19. }
  20. </script>
  21. <script type="module">
  22. import * as THREE from 'three';
  23. import { FlyControls } from 'three/addons/controls/FlyControls.js';
  24. let container;
  25. let camera, scene, renderer, controls;
  26. const clock = new THREE.Clock();
  27. init();
  28. function init() {
  29. container = document.createElement( 'div' );
  30. document.body.appendChild( container );
  31. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 15000 );
  32. camera.position.z = 1000;
  33. scene = new THREE.Scene();
  34. scene.fog = new THREE.Fog( 0x000000, 1, 15000 );
  35. const pointLight = new THREE.PointLight( 0xff2200, 3, 0, 0 );
  36. pointLight.position.set( 0, 0, 0 );
  37. scene.add( pointLight );
  38. const dirLight = new THREE.DirectionalLight( 0xffffff, 3 );
  39. dirLight.position.set( 0, 0, 1 ).normalize();
  40. scene.add( dirLight );
  41. const geometry = [
  42. [ new THREE.IcosahedronGeometry( 100, 16 ), 50 ],
  43. [ new THREE.IcosahedronGeometry( 100, 8 ), 300 ],
  44. [ new THREE.IcosahedronGeometry( 100, 4 ), 1000 ],
  45. [ new THREE.IcosahedronGeometry( 100, 2 ), 2000 ],
  46. [ new THREE.IcosahedronGeometry( 100, 1 ), 8000 ]
  47. ];
  48. const material = new THREE.MeshLambertMaterial( { color: 0xffffff, wireframe: true } );
  49. for ( let j = 0; j < 1000; j ++ ) {
  50. const lod = new THREE.LOD();
  51. for ( let i = 0; i < geometry.length; i ++ ) {
  52. const mesh = new THREE.Mesh( geometry[ i ][ 0 ], material );
  53. mesh.scale.set( 1.5, 1.5, 1.5 );
  54. mesh.updateMatrix();
  55. mesh.matrixAutoUpdate = false;
  56. lod.addLevel( mesh, geometry[ i ][ 1 ] );
  57. }
  58. lod.position.x = 10000 * ( 0.5 - Math.random() );
  59. lod.position.y = 7500 * ( 0.5 - Math.random() );
  60. lod.position.z = 10000 * ( 0.5 - Math.random() );
  61. lod.updateMatrix();
  62. lod.matrixAutoUpdate = false;
  63. scene.add( lod );
  64. }
  65. renderer = new THREE.WebGLRenderer( { antialias: true } );
  66. renderer.setPixelRatio( window.devicePixelRatio );
  67. renderer.setSize( window.innerWidth, window.innerHeight );
  68. renderer.setAnimationLoop( animate );
  69. container.appendChild( renderer.domElement );
  70. //
  71. controls = new FlyControls( camera, renderer.domElement );
  72. controls.movementSpeed = 1000;
  73. controls.rollSpeed = Math.PI / 10;
  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 animate() {
  83. controls.update( clock.getDelta() );
  84. renderer.render( scene, camera );
  85. }
  86. </script>
  87. </body>
  88. </html>