webgl_lod.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. animate();
  29. function init() {
  30. container = document.createElement( 'div' );
  31. document.body.appendChild( container );
  32. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 15000 );
  33. camera.position.z = 1000;
  34. scene = new THREE.Scene();
  35. scene.fog = new THREE.Fog( 0x000000, 1, 15000 );
  36. const pointLight = new THREE.PointLight( 0xff2200, 3, 0, 0 );
  37. pointLight.position.set( 0, 0, 0 );
  38. scene.add( pointLight );
  39. const dirLight = new THREE.DirectionalLight( 0xffffff, 3 );
  40. dirLight.position.set( 0, 0, 1 ).normalize();
  41. scene.add( dirLight );
  42. const geometry = [
  43. [ new THREE.IcosahedronGeometry( 100, 16 ), 50 ],
  44. [ new THREE.IcosahedronGeometry( 100, 8 ), 300 ],
  45. [ new THREE.IcosahedronGeometry( 100, 4 ), 1000 ],
  46. [ new THREE.IcosahedronGeometry( 100, 2 ), 2000 ],
  47. [ new THREE.IcosahedronGeometry( 100, 1 ), 8000 ]
  48. ];
  49. const material = new THREE.MeshLambertMaterial( { color: 0xffffff, wireframe: true } );
  50. for ( let j = 0; j < 1000; j ++ ) {
  51. const lod = new THREE.LOD();
  52. for ( let i = 0; i < geometry.length; i ++ ) {
  53. const mesh = new THREE.Mesh( geometry[ i ][ 0 ], material );
  54. mesh.scale.set( 1.5, 1.5, 1.5 );
  55. mesh.updateMatrix();
  56. mesh.matrixAutoUpdate = false;
  57. lod.addLevel( mesh, geometry[ i ][ 1 ] );
  58. }
  59. lod.position.x = 10000 * ( 0.5 - Math.random() );
  60. lod.position.y = 7500 * ( 0.5 - Math.random() );
  61. lod.position.z = 10000 * ( 0.5 - Math.random() );
  62. lod.updateMatrix();
  63. lod.matrixAutoUpdate = false;
  64. scene.add( lod );
  65. }
  66. renderer = new THREE.WebGLRenderer( { antialias: true } );
  67. renderer.setPixelRatio( window.devicePixelRatio );
  68. renderer.setSize( window.innerWidth, window.innerHeight );
  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. requestAnimationFrame( animate );
  84. render();
  85. }
  86. function render() {
  87. controls.update( clock.getDelta() );
  88. renderer.render( scene, camera );
  89. }
  90. </script>
  91. </body>
  92. </html>