webgl_lod.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - level-of-details</title>
  5. <meta charset="utf-8">
  6. <style type="text/css">
  7. body {
  8. background:#000;
  9. color:#fff;
  10. padding:0;
  11. margin:0;
  12. font-weight: bold;
  13. overflow:hidden;
  14. }
  15. #info {
  16. position: absolute;
  17. top: 0px; width: 100%;
  18. color: #ffffff;
  19. padding: 5px;
  20. font-family: Monospace;
  21. font-size: 13px;
  22. text-align: center;
  23. z-index:100;
  24. }
  25. a { color:red }
  26. </style>
  27. </head>
  28. <body>
  29. <div id="info">
  30. <a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - level-of-details WebGL example
  31. </div>
  32. <script type="text/javascript" src="js/ThreeLod.js"></script>
  33. <script type="text/javascript" src="js/Detector.js"></script>
  34. <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
  35. <script type="text/javascript" src="js/Stats.js"></script>
  36. <script type="text/javascript">
  37. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  38. var container, stats;
  39. var camera, scene, renderer;
  40. var geometry, objects;
  41. var mouseX = 0, mouseY = 0;
  42. var windowHalfX = window.innerWidth / 2;
  43. var windowHalfY = window.innerHeight / 2;
  44. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  45. init();
  46. animate();
  47. function init() {
  48. container = document.createElement( 'div' );
  49. document.body.appendChild( container );
  50. camera = new THREE.Camera( 45, window.innerWidth / window.innerHeight, 1, 15000 );
  51. camera.position.z = 1000;
  52. scene = new THREE.Scene();
  53. scene.fog = new THREE.Fog( 0x000000, 1, 15000 );
  54. var light = new THREE.PointLight( 0xff2200 );
  55. light.position.set( 0, 0, 0 );
  56. scene.addLight( light );
  57. var light = new THREE.DirectionalLight( 0xffffff );
  58. light.position.set( 0, 0, 1 );
  59. light.position.normalize();
  60. scene.addLight( light );
  61. var material = new THREE.MeshLambertMaterial( { color: 0xffffff, shading: THREE.FlatShading } );
  62. var geometry = [ [ new Sphere( 100, 128, 64 ), 0 ],
  63. [ new Sphere( 100, 64, 32 ), 200 ],
  64. [ new Sphere( 100, 32, 16 ), 400 ],
  65. [ new Sphere( 100, 16, 8 ), 6000 ],
  66. [ new Sphere( 100, 8, 4 ), 12000 ]
  67. ];
  68. var i, j, mesh, lod;
  69. for ( j = 0; j < 2000; j++ ) {
  70. lod = new THREE.LOD();
  71. for ( i = 0; i < geometry.length; i++ ) {
  72. mesh = new THREE.Mesh( geometry[ i ][ 0 ], material );
  73. mesh.scale.set( 1.5, 1.5, 1.5 );
  74. lod.add( mesh, geometry[ i ][ 1 ] );
  75. }
  76. lod.position.x = 10000 * ( 0.5 - Math.random() );
  77. lod.position.y = 7500 * ( 0.5 - Math.random() );
  78. lod.position.z = 10000 * ( 0.5 - Math.random() );
  79. scene.addObject( lod );
  80. }
  81. renderer = new THREE.WebGLRenderer( { clearColor:0x000000, clearAlpha: 1 } );
  82. renderer.setSize( window.innerWidth, window.innerHeight );
  83. renderer.sortObjects = false;
  84. container.appendChild( renderer.domElement );
  85. }
  86. function onDocumentMouseMove(event) {
  87. mouseX = ( event.clientX - windowHalfX ) * 10;
  88. mouseY = ( event.clientY - windowHalfY ) * 10;
  89. }
  90. function animate() {
  91. requestAnimationFrame( animate );
  92. render();
  93. }
  94. function render() {
  95. camera.position.x += ( mouseX - camera.position.x ) * .005;
  96. camera.position.y += ( - mouseY - camera.position.y ) * .01;
  97. renderer.render( scene, camera );
  98. }
  99. function log( text ) {
  100. var e = document.getElementById("log");
  101. e.innerHTML = text + "<br/>" + e.innerHTML;
  102. }
  103. </script>
  104. </body>
  105. </html>