webgl_lod.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - level-of-details</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. <style>
  8. body {
  9. background:#000;
  10. color:#fff;
  11. padding:0;
  12. margin:0;
  13. font-weight: bold;
  14. overflow:hidden;
  15. }
  16. #info {
  17. position: absolute;
  18. top: 0px; width: 100%;
  19. color: #ffffff;
  20. padding: 5px;
  21. font-family: Monospace;
  22. font-size: 13px;
  23. text-align: center;
  24. z-index:100;
  25. }
  26. a { color:red }
  27. </style>
  28. </head>
  29. <body>
  30. <div id="info">
  31. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - level-of-details WebGL example
  32. </div>
  33. <script src="../build/three.js"></script>
  34. <script src="js/controls/FlyControls.js"></script>
  35. <script src="js/WebGL.js"></script>
  36. <script src="js/libs/stats.min.js"></script>
  37. <script>
  38. if ( WEBGL.isWebGLAvailable() === false ) {
  39. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  40. }
  41. var container;
  42. var camera, scene, renderer;
  43. var controls, clock = new THREE.Clock();
  44. init();
  45. animate();
  46. function init() {
  47. container = document.createElement( 'div' );
  48. document.body.appendChild( container );
  49. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 15000 );
  50. camera.position.z = 1000;
  51. controls = new THREE.FlyControls( camera );
  52. controls.movementSpeed = 1000;
  53. controls.rollSpeed = Math.PI / 10;
  54. scene = new THREE.Scene();
  55. scene.fog = new THREE.Fog( 0x000000, 1, 15000 );
  56. scene.autoUpdate = false;
  57. var light = new THREE.PointLight( 0xff2200 );
  58. light.position.set( 0, 0, 0 );
  59. scene.add( light );
  60. var light = new THREE.DirectionalLight( 0xffffff );
  61. light.position.set( 0, 0, 1 ).normalize();
  62. scene.add( light );
  63. var geometry = [
  64. [ new THREE.IcosahedronBufferGeometry( 100, 4 ), 50 ],
  65. [ new THREE.IcosahedronBufferGeometry( 100, 3 ), 300 ],
  66. [ new THREE.IcosahedronBufferGeometry( 100, 2 ), 1000 ],
  67. [ new THREE.IcosahedronBufferGeometry( 100, 1 ), 2000 ],
  68. [ new THREE.IcosahedronBufferGeometry( 100, 0 ), 8000 ]
  69. ];
  70. var material = new THREE.MeshLambertMaterial( { color: 0xffffff, wireframe: true } );
  71. var i, j, mesh, lod;
  72. for ( j = 0; j < 1000; j ++ ) {
  73. lod = new THREE.LOD();
  74. for ( i = 0; i < geometry.length; i ++ ) {
  75. mesh = new THREE.Mesh( geometry[ i ][ 0 ], material );
  76. mesh.scale.set( 1.5, 1.5, 1.5 );
  77. mesh.updateMatrix();
  78. mesh.matrixAutoUpdate = false;
  79. lod.addLevel( mesh, geometry[ i ][ 1 ] );
  80. }
  81. lod.position.x = 10000 * ( 0.5 - Math.random() );
  82. lod.position.y = 7500 * ( 0.5 - Math.random() );
  83. lod.position.z = 10000 * ( 0.5 - Math.random() );
  84. lod.updateMatrix();
  85. lod.matrixAutoUpdate = false;
  86. scene.add( lod );
  87. }
  88. renderer = new THREE.WebGLRenderer( { antialias: true } );
  89. renderer.setPixelRatio( window.devicePixelRatio );
  90. renderer.setSize( window.innerWidth, window.innerHeight );
  91. container.appendChild( renderer.domElement );
  92. //
  93. window.addEventListener( 'resize', onWindowResize, false );
  94. }
  95. function onWindowResize() {
  96. camera.aspect = window.innerWidth / window.innerHeight;
  97. camera.updateProjectionMatrix();
  98. renderer.setSize( window.innerWidth, window.innerHeight );
  99. }
  100. function animate() {
  101. requestAnimationFrame( animate );
  102. render();
  103. }
  104. function render() {
  105. controls.update( clock.getDelta() );
  106. scene.updateMatrixWorld();
  107. scene.traverse( function ( object ) {
  108. if ( object instanceof THREE.LOD ) {
  109. object.update( camera );
  110. }
  111. } );
  112. renderer.render( scene, camera );
  113. }
  114. </script>
  115. </body>
  116. </html>