webgl_lod.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. var light = new PointLight( 0xff2200 );
  44. light.position.set( 0, 0, 0 );
  45. scene.add( light );
  46. var light = new DirectionalLight( 0xffffff );
  47. light.position.set( 0, 0, 1 ).normalize();
  48. scene.add( light );
  49. var geometry = [
  50. [ new IcosahedronBufferGeometry( 100, 4 ), 50 ],
  51. [ new IcosahedronBufferGeometry( 100, 3 ), 300 ],
  52. [ new IcosahedronBufferGeometry( 100, 2 ), 1000 ],
  53. [ new IcosahedronBufferGeometry( 100, 1 ), 2000 ],
  54. [ new IcosahedronBufferGeometry( 100, 0 ), 8000 ]
  55. ];
  56. var material = new MeshLambertMaterial( { color: 0xffffff, wireframe: true } );
  57. var i, j, mesh, lod;
  58. for ( j = 0; j < 1000; j ++ ) {
  59. lod = new LOD();
  60. for ( i = 0; i < geometry.length; i ++ ) {
  61. mesh = new Mesh( geometry[ i ][ 0 ], material );
  62. mesh.scale.set( 1.5, 1.5, 1.5 );
  63. mesh.updateMatrix();
  64. mesh.matrixAutoUpdate = false;
  65. lod.addLevel( mesh, geometry[ i ][ 1 ] );
  66. }
  67. lod.position.x = 10000 * ( 0.5 - Math.random() );
  68. lod.position.y = 7500 * ( 0.5 - Math.random() );
  69. lod.position.z = 10000 * ( 0.5 - Math.random() );
  70. lod.updateMatrix();
  71. lod.matrixAutoUpdate = false;
  72. scene.add( lod );
  73. }
  74. renderer = new WebGLRenderer( { antialias: true } );
  75. renderer.setPixelRatio( window.devicePixelRatio );
  76. renderer.setSize( window.innerWidth, window.innerHeight );
  77. container.appendChild( renderer.domElement );
  78. //
  79. window.addEventListener( 'resize', onWindowResize, false );
  80. }
  81. function onWindowResize() {
  82. camera.aspect = window.innerWidth / window.innerHeight;
  83. camera.updateProjectionMatrix();
  84. renderer.setSize( window.innerWidth, window.innerHeight );
  85. }
  86. function animate() {
  87. requestAnimationFrame( animate );
  88. render();
  89. }
  90. function render() {
  91. controls.update( clock.getDelta() );
  92. renderer.render( scene, camera );
  93. }
  94. </script>
  95. </body>
  96. </html>