webgl_scene_changes.html 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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="../build/Three.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, mesh;
  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. setInterval( change, 100 );
  48. function init() {
  49. container = document.createElement( 'div' );
  50. document.body.appendChild( container );
  51. camera = new THREE.Camera( 45, window.innerWidth / window.innerHeight, 1, 15000 );
  52. camera.position.z = 1000;
  53. scene = new THREE.Scene();
  54. scene.fog = new THREE.Fog( 0x000000, 1, 15000 );
  55. var light = new THREE.PointLight( 0xff2200 );
  56. light.position.set( 0, 0, 0 );
  57. scene.addLight( light );
  58. var light = new THREE.DirectionalLight( 0xffffff );
  59. light.position.set( 0, 0, 1 );
  60. light.position.normalize();
  61. scene.addLight( light );
  62. var geometry = new Sphere( 100, 128, 64 );
  63. var material = new THREE.MeshLambertMaterial( { color: 0xffffff } );
  64. mesh = new THREE.Mesh( geometry, material );
  65. scene.addObject( mesh );
  66. renderer = new THREE.WebGLRenderer( { clearColor:0x000000, clearAlpha: 1 } );
  67. renderer.setSize( window.innerWidth, window.innerHeight );
  68. renderer.sortObjects = false;
  69. container.appendChild( renderer.domElement );
  70. }
  71. function onDocumentMouseMove(event) {
  72. mouseX = ( event.clientX - windowHalfX ) * 10;
  73. mouseY = ( event.clientY - windowHalfY ) * 10;
  74. }
  75. function change() {
  76. mesh.position.x = Math.random() * 800 - 400;
  77. mesh.position.y = Math.random() * 800 - 400;
  78. mesh.position.z = Math.random() * 800 - 400;
  79. scene.removeObject( mesh );
  80. scene.addObject( mesh );
  81. }
  82. function animate() {
  83. requestAnimationFrame( animate );
  84. render();
  85. }
  86. function render() {
  87. /*
  88. camera.position.x += ( mouseX - camera.position.x ) * .005;
  89. camera.position.y += ( - mouseY - camera.position.y ) * .01;
  90. */
  91. renderer.render( scene, camera );
  92. }
  93. </script>
  94. </body>
  95. </html>
粤ICP备19079148号