webgl_lod.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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">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/Detector.js"></script>
  36. <script src="js/libs/stats.min.js"></script>
  37. <script>
  38. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  39. var container, stats;
  40. var camera, scene, renderer;
  41. var geometry, objects;
  42. var controls, clock = new THREE.Clock();
  43. init();
  44. animate();
  45. function init() {
  46. container = document.createElement( 'div' );
  47. document.body.appendChild( container );
  48. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 15000 );
  49. camera.position.z = 1000;
  50. controls = new THREE.FlyControls( camera );
  51. controls.movementSpeed = 1000;
  52. controls.rollSpeed = Math.PI / 10;
  53. scene = new THREE.Scene();
  54. scene.fog = new THREE.Fog( 0x000000, 1, 15000 );
  55. scene.autoUpdate = false;
  56. var light = new THREE.PointLight( 0xff2200 );
  57. light.position.set( 0, 0, 0 );
  58. scene.add( light );
  59. var light = new THREE.DirectionalLight( 0xffffff );
  60. light.position.set( 0, 0, 1 ).normalize();
  61. scene.add( light );
  62. var geometry = [
  63. [ new THREE.IcosahedronGeometry( 100, 4 ), 50 ],
  64. [ new THREE.IcosahedronGeometry( 100, 3 ), 300 ],
  65. [ new THREE.IcosahedronGeometry( 100, 2 ), 1000 ],
  66. [ new THREE.IcosahedronGeometry( 100, 1 ), 2000 ],
  67. [ new THREE.IcosahedronGeometry( 100, 0 ), 8000 ]
  68. ];
  69. var material = new THREE.MeshLambertMaterial( { color: 0xffffff, wireframe: true } );
  70. var i, j, mesh, lod;
  71. for ( j = 0; j < 1000; j ++ ) {
  72. lod = new THREE.LOD();
  73. for ( i = 0; i < geometry.length; i ++ ) {
  74. mesh = new THREE.Mesh( geometry[ i ][ 0 ], material );
  75. mesh.scale.set( 1.5, 1.5, 1.5 );
  76. mesh.updateMatrix();
  77. mesh.matrixAutoUpdate = false;
  78. lod.addLevel( mesh, geometry[ i ][ 1 ] );
  79. }
  80. lod.position.x = 10000 * ( 0.5 - Math.random() );
  81. lod.position.y = 7500 * ( 0.5 - Math.random() );
  82. lod.position.z = 10000 * ( 0.5 - Math.random() );
  83. lod.updateMatrix();
  84. lod.matrixAutoUpdate = false;
  85. scene.add( lod );
  86. }
  87. renderer = new THREE.WebGLRenderer();
  88. renderer.setPixelRatio( window.devicePixelRatio );
  89. renderer.setSize( window.innerWidth, window.innerHeight );
  90. renderer.sortObjects = false;
  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>