webgl_lod.html 3.1 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="module">
  14. import * as THREE from '../build/three.module.js';
  15. import { FlyControls } from './jsm/controls/FlyControls.js';
  16. var container;
  17. var camera, scene, renderer;
  18. var controls, 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. var light = new THREE.PointLight( 0xff2200 );
  29. light.position.set( 0, 0, 0 );
  30. scene.add( light );
  31. var light = new THREE.DirectionalLight( 0xffffff );
  32. light.position.set( 0, 0, 1 ).normalize();
  33. scene.add( light );
  34. var geometry = [
  35. [ new THREE.IcosahedronBufferGeometry( 100, 4 ), 50 ],
  36. [ new THREE.IcosahedronBufferGeometry( 100, 3 ), 300 ],
  37. [ new THREE.IcosahedronBufferGeometry( 100, 2 ), 1000 ],
  38. [ new THREE.IcosahedronBufferGeometry( 100, 1 ), 2000 ],
  39. [ new THREE.IcosahedronBufferGeometry( 100, 0 ), 8000 ]
  40. ];
  41. var material = new THREE.MeshLambertMaterial( { color: 0xffffff, wireframe: true } );
  42. var i, j, mesh, lod;
  43. for ( j = 0; j < 1000; j ++ ) {
  44. lod = new THREE.LOD();
  45. for ( i = 0; i < geometry.length; i ++ ) {
  46. mesh = new THREE.Mesh( geometry[ i ][ 0 ], material );
  47. mesh.scale.set( 1.5, 1.5, 1.5 );
  48. mesh.updateMatrix();
  49. mesh.matrixAutoUpdate = false;
  50. lod.addLevel( mesh, geometry[ i ][ 1 ] );
  51. }
  52. lod.position.x = 10000 * ( 0.5 - Math.random() );
  53. lod.position.y = 7500 * ( 0.5 - Math.random() );
  54. lod.position.z = 10000 * ( 0.5 - Math.random() );
  55. lod.updateMatrix();
  56. lod.matrixAutoUpdate = false;
  57. scene.add( lod );
  58. }
  59. renderer = new THREE.WebGLRenderer( { antialias: true } );
  60. renderer.setPixelRatio( window.devicePixelRatio );
  61. renderer.setSize( window.innerWidth, window.innerHeight );
  62. container.appendChild( renderer.domElement );
  63. //
  64. controls = new FlyControls( camera, renderer.domElement );
  65. controls.movementSpeed = 1000;
  66. controls.rollSpeed = Math.PI / 10;
  67. //
  68. window.addEventListener( 'resize', onWindowResize, false );
  69. }
  70. function onWindowResize() {
  71. camera.aspect = window.innerWidth / window.innerHeight;
  72. camera.updateProjectionMatrix();
  73. renderer.setSize( window.innerWidth, window.innerHeight );
  74. }
  75. function animate() {
  76. requestAnimationFrame( animate );
  77. render();
  78. }
  79. function render() {
  80. controls.update( clock.getDelta() );
  81. renderer.render( scene, camera );
  82. }
  83. </script>
  84. </body>
  85. </html>