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="http://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. controls = new FlyControls( camera );
  27. controls.movementSpeed = 1000;
  28. controls.rollSpeed = Math.PI / 10;
  29. scene = new THREE.Scene();
  30. scene.fog = new THREE.Fog( 0x000000, 1, 15000 );
  31. var light = new THREE.PointLight( 0xff2200 );
  32. light.position.set( 0, 0, 0 );
  33. scene.add( light );
  34. var light = new THREE.DirectionalLight( 0xffffff );
  35. light.position.set( 0, 0, 1 ).normalize();
  36. scene.add( light );
  37. var geometry = [
  38. [ new THREE.IcosahedronBufferGeometry( 100, 4 ), 50 ],
  39. [ new THREE.IcosahedronBufferGeometry( 100, 3 ), 300 ],
  40. [ new THREE.IcosahedronBufferGeometry( 100, 2 ), 1000 ],
  41. [ new THREE.IcosahedronBufferGeometry( 100, 1 ), 2000 ],
  42. [ new THREE.IcosahedronBufferGeometry( 100, 0 ), 8000 ]
  43. ];
  44. var material = new THREE.MeshLambertMaterial( { color: 0xffffff, wireframe: true } );
  45. var i, j, mesh, lod;
  46. for ( j = 0; j < 1000; j ++ ) {
  47. lod = new THREE.LOD();
  48. for ( i = 0; i < geometry.length; i ++ ) {
  49. mesh = new THREE.Mesh( geometry[ i ][ 0 ], material );
  50. mesh.scale.set( 1.5, 1.5, 1.5 );
  51. mesh.updateMatrix();
  52. mesh.matrixAutoUpdate = false;
  53. lod.addLevel( mesh, geometry[ i ][ 1 ] );
  54. }
  55. lod.position.x = 10000 * ( 0.5 - Math.random() );
  56. lod.position.y = 7500 * ( 0.5 - Math.random() );
  57. lod.position.z = 10000 * ( 0.5 - Math.random() );
  58. lod.updateMatrix();
  59. lod.matrixAutoUpdate = false;
  60. scene.add( lod );
  61. }
  62. renderer = new THREE.WebGLRenderer( { antialias: true } );
  63. renderer.setPixelRatio( window.devicePixelRatio );
  64. renderer.setSize( window.innerWidth, window.innerHeight );
  65. container.appendChild( renderer.domElement );
  66. //
  67. window.addEventListener( 'resize', onWindowResize, false );
  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>