webgl_lod.html 3.5 KB

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