webgl_lod.html 3.4 KB

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