webgl_lod.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 {
  15. Clock,
  16. DirectionalLight,
  17. Fog,
  18. IcosahedronBufferGeometry,
  19. LOD,
  20. Mesh,
  21. MeshLambertMaterial,
  22. PerspectiveCamera,
  23. PointLight,
  24. Scene,
  25. WebGLRenderer,
  26. } from "../build/three.module.js";
  27. import { FlyControls } from './jsm/controls/FlyControls.js';
  28. var container;
  29. var camera, scene, renderer;
  30. var controls, clock = new Clock();
  31. init();
  32. animate();
  33. function init() {
  34. container = document.createElement( 'div' );
  35. document.body.appendChild( container );
  36. camera = new PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 15000 );
  37. camera.position.z = 1000;
  38. controls = new FlyControls( camera );
  39. controls.movementSpeed = 1000;
  40. controls.rollSpeed = Math.PI / 10;
  41. scene = new Scene();
  42. scene.fog = new Fog( 0x000000, 1, 15000 );
  43. scene.autoUpdate = false;
  44. var light = new PointLight( 0xff2200 );
  45. light.position.set( 0, 0, 0 );
  46. scene.add( light );
  47. var light = new DirectionalLight( 0xffffff );
  48. light.position.set( 0, 0, 1 ).normalize();
  49. scene.add( light );
  50. var geometry = [
  51. [ new IcosahedronBufferGeometry( 100, 4 ), 50 ],
  52. [ new IcosahedronBufferGeometry( 100, 3 ), 300 ],
  53. [ new IcosahedronBufferGeometry( 100, 2 ), 1000 ],
  54. [ new IcosahedronBufferGeometry( 100, 1 ), 2000 ],
  55. [ new IcosahedronBufferGeometry( 100, 0 ), 8000 ]
  56. ];
  57. var material = new MeshLambertMaterial( { color: 0xffffff, wireframe: true } );
  58. var i, j, mesh, lod;
  59. for ( j = 0; j < 1000; j ++ ) {
  60. lod = new LOD();
  61. for ( i = 0; i < geometry.length; i ++ ) {
  62. mesh = new Mesh( geometry[ i ][ 0 ], material );
  63. mesh.scale.set( 1.5, 1.5, 1.5 );
  64. mesh.updateMatrix();
  65. mesh.matrixAutoUpdate = false;
  66. lod.addLevel( mesh, geometry[ i ][ 1 ] );
  67. }
  68. lod.position.x = 10000 * ( 0.5 - Math.random() );
  69. lod.position.y = 7500 * ( 0.5 - Math.random() );
  70. lod.position.z = 10000 * ( 0.5 - Math.random() );
  71. lod.updateMatrix();
  72. lod.matrixAutoUpdate = false;
  73. scene.add( lod );
  74. }
  75. renderer = new WebGLRenderer( { antialias: true } );
  76. renderer.setPixelRatio( window.devicePixelRatio );
  77. renderer.setSize( window.innerWidth, window.innerHeight );
  78. container.appendChild( renderer.domElement );
  79. //
  80. window.addEventListener( 'resize', onWindowResize, false );
  81. }
  82. function onWindowResize() {
  83. camera.aspect = window.innerWidth / window.innerHeight;
  84. camera.updateProjectionMatrix();
  85. renderer.setSize( window.innerWidth, window.innerHeight );
  86. }
  87. function animate() {
  88. requestAnimationFrame( animate );
  89. render();
  90. }
  91. function render() {
  92. controls.update( clock.getDelta() );
  93. scene.updateMatrixWorld();
  94. scene.traverse( function ( object ) {
  95. if ( object instanceof LOD ) {
  96. object.update( camera );
  97. }
  98. } );
  99. renderer.render( scene, camera );
  100. }
  101. </script>
  102. </body>
  103. </html>